kt: add «1829. Maximum XOR for Each Query»

URL:	https://leetcode.com/problems/maximum-xor-for-each-query/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-11-08 14:35:00 +01:00
parent f29fb46aa0
commit 437712377a
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,15 @@
class Solution {
fun getMaximumXor(
nums: IntArray,
maximumBit: Int,
): IntArray {
val mask = 1.shl(maximumBit) - 1
var runningXor = nums.reduce { x, y -> x.xor(y) }
return IntArray(nums.size) { i ->
val current = runningXor.xor(mask)
runningXor = runningXor.xor(nums[nums.size - 1 - i])
current
}
}
}