From 437712377a1bc6117be2564352e9e9815d434e65 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Fri, 8 Nov 2024 14:35:00 +0100 Subject: [PATCH] =?UTF-8?q?kt:=20add=20=C2=AB1829.=20Maximum=20XOR=20for?= =?UTF-8?q?=20Each=20Query=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit URL: https://leetcode.com/problems/maximum-xor-for-each-query/ Signed-off-by: Matej Focko --- kt/maximum-xor-for-each-query.kt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 kt/maximum-xor-for-each-query.kt diff --git a/kt/maximum-xor-for-each-query.kt b/kt/maximum-xor-for-each-query.kt new file mode 100644 index 0000000..26b490a --- /dev/null +++ b/kt/maximum-xor-for-each-query.kt @@ -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 + } + } +}