Skip to content

Commit c7df7bd

Browse files
1829: Maximum XOR for Each Query
1 parent 83daa1b commit c7df7bd

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ My solutions to LeetCode problems in Kotlin.
1717
| [206](https://leetcode.com/problems/reverse-linked-list/) | [Reverse Linked List](src/main/kotlin/com/schmoczer/leetcode/_0206/ReverseLinkedList.kt) | Easy |
1818
| [344](https://leetcode.com/problems/reverse-string/) | [Reverse String](src/main/kotlin/com/schmoczer/leetcode/_0344/ReverseString.kt) | Easy |
1919
| [796](https://leetcode.com/problems/rotate-string/) | [Rotate String](src/main/kotlin/com/schmoczer/leetcode/_0796/RotateString.kt) | Easy |
20+
| [1829](https://leetcode.com/problems/maximum-xor-for-each-query/) | [Maximum XOR for Each Query](src/main/kotlin/com/schmoczer/leetcode/_1829/MaximumXorForEachQuery.kt) | Medium |
2021
| [1957](https://leetcode.com/problems/delete-characters-to-make-fancy-string/) | [Delete Characters to Make Fancy String](src/main/kotlin/com/schmoczer/leetcode/_1957/DeleteCharactersToMakeFancyString.kt) | Easy |
2122
| [2275](https://leetcode.com/problems/largest-combination-with-bitwise-and-greater-than-zero/) | [Largest Combination With Bitwise AND Greater Than Zero](src/main/kotlin/com/schmoczer/leetcode/_2275/LargestCombination.kt) | Medium |
2223
| [2490](https://leetcode.com/problems/circular-sentence/) | [Circular Sentence](src/main/kotlin/com/schmoczer/leetcode/_2490/CircularSentence.kt) | Easy |
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.schmoczer.leetcode._1829
2+
3+
import kotlin.math.pow
4+
5+
class MaximumXorForEachQuery {
6+
fun getMaximumXor(nums: IntArray, maximumBit: Int): IntArray {
7+
val result = IntArray(nums.size) { 0 }
8+
var queries = mutableMapOf<Int, Int>()
9+
val maxBitsValue = 2.0.pow(maximumBit).toInt() - 1
10+
11+
for (query in (0 until nums.size).reversed()) {
12+
val previous = queries[query + 1] ?: 0
13+
val value = nums[nums.size - 1 - query] xor previous
14+
queries[query] = value
15+
val k = value xor maxBitsValue
16+
result[query] = k
17+
}
18+
return result
19+
}
20+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Maximum XOR for Each Query
2+
3+
You are given a sorted array `nums` of `n` non-negative integers and an integer `maximumBit`. You want to perform the
4+
following query `n` times:
5+
6+
1. Find a non-negative integer `k < 2^maximumBit` such that `nums[0] XOR nums[1] XOR ... XOR nums[nums.length-1] XOR k`
7+
is maximized. `k` is the answer to the query number `i`.
8+
2. Remove the last element from the current array `nums`.
9+
10+
Return an array `answer`, where `answer[i]` is the answer to the `i`-th query.
11+
12+
Example 1:
13+
14+
> Input: nums = [0,1,1,3], maximumBit = 2
15+
>
16+
> Output: [0,3,2,3]
17+
18+
Example 2:
19+
20+
> Input: nums = [2,3,4,7], maximumBit = 3
21+
>
22+
> Output: [5,2,6,5]
23+
24+
Example 3:
25+
26+
> Input: nums = [0,1,2,2,5,7], maximumBit = 3
27+
>
28+
> Output: [4,3,6,4,6,7]
29+
30+
Constraints:
31+
32+
- `nums.length == n`
33+
- `1 <= n <= 105`
34+
- `1 <= maximumBit <= 20`
35+
- `0 <= nums[i] < 2^maximumBit`
36+
- `nums` is sorted in ascending order.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.schmoczer.leetcode._1829
2+
3+
import org.junit.jupiter.api.BeforeEach
4+
import kotlin.test.Test
5+
import kotlin.test.assertContentEquals
6+
7+
class MaximumXorForEachQueryTest {
8+
private lateinit var sut: MaximumXorForEachQuery
9+
10+
@BeforeEach
11+
fun setUp() {
12+
sut = MaximumXorForEachQuery()
13+
}
14+
15+
@Test
16+
fun `maximum xor for each query of 0,1,1,3 is 0,3,2,3`() {
17+
val input = intArrayOf(0, 1, 1, 3)
18+
val expected = intArrayOf(0, 3, 2, 3)
19+
20+
val result = sut.getMaximumXor(input, 2)
21+
22+
assertContentEquals(expected, result)
23+
}
24+
25+
@Test
26+
fun `maximum xor for each query of 2,3,4,7 is 5,2,6,5`() {
27+
val input = intArrayOf(2, 3, 4, 7)
28+
val expected = intArrayOf(5, 2, 6, 5)
29+
30+
val result = sut.getMaximumXor(input, 3)
31+
32+
assertContentEquals(expected, result)
33+
}
34+
}

0 commit comments

Comments
 (0)