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:
parent
f29fb46aa0
commit
437712377a
1 changed files with 15 additions and 0 deletions
15
kt/maximum-xor-for-each-query.kt
Normal file
15
kt/maximum-xor-for-each-query.kt
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue