Skip to content

Commit c78c971

Browse files
author
Aarzoo
committed
add solutions explanation and update README file of the day 1829
1 parent f60b4f1 commit c78c971

File tree

8 files changed

+245
-0
lines changed

8 files changed

+245
-0
lines changed

Algorithms/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
| 1701 | Average Waiting Time | [C++](./Algorithms/src/1701.%20Average%20Waiting%20Time/Code/solution.cpp), [Java](./Algorithms/src/1701.%20Average%20Waiting%20Time/Code/solution.java), [JavaScript](./Algorithms/src/1701.%20Average%20Waiting%20Time/Code/solution.js), [Python](./Algorithms/src/1701.%20Average%20Waiting%20Time/Code/solution.py), [Go](./Algorithms/src/1701.%20Average%20Waiting%20Time/Code/solution.go) | [Explanation](./Algorithms/src/1701.%20Average%20Waiting%20Time/Explanation/explanation.md) | Medium |
4646
| 1813 | Sentence Similarity III | [C++](./Algorithms/src/1813.%20Sentence%20Similarity%20III/Code/solution.cpp), [Java](./Algorithms/src/1813.%20Sentence%20Similarity%20III/Code/solution.java), [JavaScript](./Algorithms/src/1813.%20Sentence%20Similarity%20III/Code/solution.js), [Python](./Algorithms/src/1813.%20Sentence%20Similarity%20III/Code/solution.py), [Go](./Algorithms/src/1813.%20Sentence%20Similarity%20III/Code/solution.go) | [Explanation](./Algorithms/src/1813.%20Sentence%20Similarity%20III/Explanation/explanation.md) | Medium |
4747
| 1823 | Find the Winner of the Circular Game | [C++](./Algorithms/src/1823.%20Find%20the%20Winner%20of%20the%20Circular%20Game/Code/solution.cpp), [Java](./Algorithms/src/1823.%20Find%20the%20Winner%20of%20the%20Circular%20Game/Code/solution.java), [JavaScript](./Algorithms/src/1823.%20Find%20the%20Winner%20of%20the%20Circular%20Game/Code/solution.js), [Python](./Algorithms/src/1823.%20Find%20the%20Winner%20of%20the%20Circular%20Game/Code/solution.py), [Go](./Algorithms/src/1823.%20Find%20the%20Winner%20of%20the%20Circular%20Game/Code/solution.go) | [Explanation](./Algorithms/src/1823.%20Find%20the%20Winner%20of%20the%20Circular%20Game/Explanation/explanation.md) | Medium |
48+
| 1829 | Maximum XOR for Each Query | [C++](./Algorithms/src/1829.%20Maximum%20XOR%20for%20Each%20Query/Code/solution.cpp), [Java](./Algorithms/src/1829.%20Maximum%20XOR%20for%20Each%20Query/Code/solution.java), [JavaScript](./Algorithms/src/1829.%20Maximum%20XOR%20for%20Each%20Query/Code/solution.js), [Python](./Algorithms/src/1829.%20Maximum%20XOR%20for%20Each%20Query/Code/solution.py), [Go](./Algorithms/src/1829.%20Maximum%20XOR%20for%20Each%20Query/Code/solution.go) | [Explanation](./Algorithms/src/1829.%20Maximum%20XOR%20for%20Each%20Query/Explanation/explanation.md) | Medium |
4849
| 1894 | Find the Winner of the Circular Game | [C++](./Algorithms/src/1823.%20Find%20the%20Winner%20of%20the%20Circular%20Game/Code/solution.cpp), [Java](./Algorithms/src/1823.%20Find%20the%20Winner%20of%20the%20Circular%20Game/Code/solution.java), [JavaScript](./Algorithms/src/1823.%20Find%20the%20Winner%20of%20the%20Circular%20Game/Code/solution.js), [Python](./Algorithms/src/1823.%20Find%20the%20Winner%20of%20the%20Circular%20Game/Code/solution.py), [Go](./Algorithms/src/1823.%20Find%20the%20Winner%20of%20the%20Circular%20Game/Code/solution.go) | [Explanation](./Algorithms/src/1823.%20Find%20the%20Winner%20of%20the%20Circular%20Game/Explanation/explanation.md) | Medium |
4950
| 1937 | Find the Student that Will Replace the Chalk | [C++](./Algorithms/src/1894.%20Find%20the%20Student%20that%20Will%20Replace%20the%20Chalk/Code/solution.cpp), [Java](./Algorithms/src/1894.%20Find%20the%20Student%20that%20Will%20Replace%20the%20Chalk/Code/solution.java), [JavaScript](./Algorithms/src/1894.%20Find%20the%20Student%20that%20Will%20Replace%20the%20Chalk/Code/solution.js), [Python](./Algorithms/src/1894.%20Find%20the%20Student%20that%20Will%20Replace%20the%20Chalk/Code/solution.py), [Go](./Algorithms/src/1894.%20Find%20the%20Student%20that%20Will%20Replace%20the%20Chalk/Code/solution.go) | [Explanation](./Algorithms/src/1894.%20Find%20the%20Student%20that%20Will%20Replace%20the%20Chalk/Explanation/explanation.md) | Medium |
5051
| 1942 | The Number of the Smallest Unoccupied Chair | [C++](./Algorithms/src/1942.%20The%20Number%20of%20the%20Smallest%20Unoccupied%20Chair/Code/solution.cpp), [Java](./Algorithms/src/1942.%20The%20Number%20of%20the%20Smallest%20Unoccupied%20Chair/Code/solution.java), [JavaScript](./Algorithms/src/1942.%20The%20Number%20of%20the%20Smallest%20Unoccupied%20Chair/Code/solution.js), [Python](./Algorithms/src/1942.%20The%20Number%20of%20the%20Smallest%20Unoccupied%20Chair/Code/solution.py), [Go](./Algorithms/src/1942.%20The%20Number%20of%20the%20Smallest%20Unoccupied%20Chair/Code/solution.go) | [Explanation](./Algorithms/src/1942.%20The%20Number%20of%20the%20Smallest%20Unoccupied%20Chair/Explanation/explanation.md) | Medium |
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution
2+
{
3+
public:
4+
vector<int> getMaximumXor(vector<int> &nums, int maximumBit)
5+
{
6+
int n = nums.size();
7+
vector<int> answer(n);
8+
int XORed = 0;
9+
10+
// Calculate the cumulative XOR of the entire nums array
11+
for (int num : nums)
12+
{
13+
XORed ^= num;
14+
}
15+
16+
// max_k is 2^maximumBit - 1
17+
int max_k = (1 << maximumBit) - 1;
18+
19+
// Process each query in reverse
20+
for (int i = 0; i < n; ++i)
21+
{
22+
// Calculate the k that maximizes XOR
23+
answer[i] = XORed ^ max_k;
24+
25+
// Update XORed by removing the effect of the last element
26+
XORed ^= nums[n - 1 - i];
27+
}
28+
29+
return answer;
30+
}
31+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
func getMaximumXor(nums []int, maximumBit int) []int {
2+
n := len(nums)
3+
answer := make([]int, n)
4+
XORed := 0
5+
6+
// Calculate the cumulative XOR of the entire nums array
7+
for _, num := range nums {
8+
XORed ^= num
9+
}
10+
11+
// max_k is 2^maximumBit - 1
12+
max_k := (1 << maximumBit) - 1
13+
14+
// Process each query in reverse
15+
for i := 0; i < n; i++ {
16+
// Calculate the k that maximizes XOR
17+
answer[i] = XORed ^ max_k
18+
19+
// Update XORed by removing the effect of the last element
20+
XORed ^= nums[n - 1 - i]
21+
}
22+
23+
return answer
24+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public int[] getMaximumXor(int[] nums, int maximumBit) {
3+
int n = nums.length;
4+
int[] answer = new int[n];
5+
int XORed = 0;
6+
7+
// Calculate the cumulative XOR of the entire nums array
8+
for (int num : nums) {
9+
XORed ^= num;
10+
}
11+
12+
// max_k is 2^maximumBit - 1
13+
int max_k = (1 << maximumBit) - 1;
14+
15+
// Process each query in reverse
16+
for (int i = 0; i < n; i++) {
17+
// Calculate the k that maximizes XOR
18+
answer[i] = XORed ^ max_k;
19+
20+
// Update XORed by removing the effect of the last element
21+
XORed ^= nums[n - 1 - i];
22+
}
23+
24+
return answer;
25+
}
26+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} maximumBit
4+
* @return {number[]}
5+
*/
6+
var getMaximumXor = function (nums, maximumBit) {
7+
let n = nums.length;
8+
let answer = new Array(n);
9+
let XORed = 0;
10+
11+
// Calculate the cumulative XOR of the entire nums array
12+
for (let num of nums) {
13+
XORed ^= num;
14+
}
15+
16+
// max_k is 2^maximumBit - 1
17+
let max_k = (1 << maximumBit) - 1;
18+
19+
// Process each query in reverse
20+
for (let i = 0; i < n; i++) {
21+
// Calculate the k that maximizes XOR
22+
answer[i] = XORed ^ max_k;
23+
24+
// Update XORed by removing the effect of the last element
25+
XORed ^= nums[n - 1 - i];
26+
}
27+
28+
return answer;
29+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def getMaximumXor(self, nums: List[int], maximumBit: int) -> List[int]:
3+
n = len(nums)
4+
answer = [0] * n
5+
XORed = 0
6+
7+
# Calculate the cumulative XOR of the entire nums array
8+
for num in nums:
9+
XORed ^= num
10+
11+
# max_k is 2^maximumBit - 1
12+
max_k = (1 << maximumBit) - 1
13+
14+
# Process each query in reverse
15+
for i in range(n):
16+
# Calculate the k that maximizes XOR
17+
answer[i] = XORed ^ max_k
18+
19+
# Update XORed by removing the effect of the last element
20+
XORed ^= nums[n - 1 - i]
21+
22+
return answer
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Solution Explanation: Maximum XOR Queries
2+
3+
This repository contains a solution for solving the **Maximum XOR Queries** problem in multiple programming languages: C++, Java, JavaScript, Python, and Go. Each solution is broken down step-by-step for ease of understanding.
4+
5+
## Problem Summary
6+
7+
Given an array of numbers (`nums`) and a parameter `maximumBit`, we are to find the maximum XOR result for each query by choosing an integer `k` that maximizes the XOR between `k` and the cumulative XOR of all elements in `nums`. The `k` must be within a range defined by `maximumBit`.
8+
9+
## Steps to Solution
10+
11+
The approach is the same across all languages but adapted to the syntax of each language. Here’s a language-by-language breakdown of how each solution works.
12+
13+
---
14+
15+
### C++ Code Explanation
16+
17+
1. **Initialize Variables**:
18+
- We start by defining `XORed`, an integer to store the cumulative XOR of all elements in `nums`.
19+
- We also define `max_k`, which is the maximum value achievable with `maximumBit` bits. This is calculated as `2^maximumBit - 1`.
20+
21+
2. **Compute Initial XOR**:
22+
- Using a loop, XOR all elements in `nums` together to get an initial cumulative XOR (`XORed`) of the entire array.
23+
24+
3. **Process Each Query in Reverse**:
25+
- We loop from the last element of `nums` to the first.
26+
- For each query, calculate `k` as `XORed ^ max_k`. This gives the integer `k` that maximizes the XOR.
27+
- Append `k` to our `answer` array.
28+
- Update `XORed` by removing the effect of the last element in `nums`.
29+
30+
4. **Return the Result**:
31+
- The final `answer` array contains the results for all queries, and we return it.
32+
33+
---
34+
35+
### Java Code Explanation
36+
37+
1. **Initialize Variables**:
38+
- Define `XORed` to store the cumulative XOR of all elements in `nums`.
39+
- Calculate `max_k` as `(1 << maximumBit) - 1` to get the maximum value achievable within `maximumBit` bits.
40+
41+
2. **Compute Initial XOR**:
42+
- Use a loop to XOR all elements in `nums`, updating `XORed` with each element.
43+
44+
3. **Reverse Loop for Queries**:
45+
- Loop backward from the last element of `nums`.
46+
- For each query, calculate `k` as `XORed ^ max_k`.
47+
- Store `k` in the `answer` array.
48+
- Update `XORed` by XORing it with the current element to "remove" its effect.
49+
50+
4. **Return the Final Answer**:
51+
- The `answer` array now contains all query results, which we return as our solution.
52+
53+
---
54+
55+
### JavaScript Code Explanation
56+
57+
1. **Initialize Variables**:
58+
- Define `XORed` to store the cumulative XOR of all numbers in `nums`.
59+
- Calculate `max_k` as `(1 << maximumBit) - 1` to get the highest possible value within `maximumBit` bits.
60+
61+
2. **Compute Initial XOR**:
62+
- Loop over `nums` and calculate the cumulative XOR (`XORed`).
63+
64+
3. **Process Each Query in Reverse**:
65+
- Start from the last element of `nums` and work backwards.
66+
- For each query, calculate `k` as `XORed ^ max_k`.
67+
- Store `k` in the `answer` array.
68+
- Update `XORed` by removing the effect of the last element.
69+
70+
4. **Return the Final Array**:
71+
- Return `answer` as the array containing all query results.
72+
73+
---
74+
75+
### Python Code Explanation
76+
77+
1. **Initialize Variables**:
78+
- Define `XORed` to hold the cumulative XOR of all elements in `nums`.
79+
- Calculate `max_k` as `(1 << maximumBit) - 1` to get the largest value allowed by `maximumBit`.
80+
81+
2. **Compute Cumulative XOR**:
82+
- Loop through each number in `nums` to calculate the cumulative XOR.
83+
84+
3. **Process Each Query in Reverse**:
85+
- Start from the last element of `nums` and process each in reverse.
86+
- For each query, calculate `k` as `XORed ^ max_k` to get the maximum XOR.
87+
- Append `k` to the `answer` list.
88+
- Update `XORed` by XORing it with the last element in `nums`.
89+
90+
4. **Return Results**:
91+
- `answer` now holds the maximum XOR result for each query, which we return as the solution.
92+
93+
---
94+
95+
### Go Code Explanation
96+
97+
1. **Initialize Variables**:
98+
- Define `XORed` to keep the cumulative XOR of all elements in `nums`.
99+
- Calculate `max_k` as `(1 << maximumBit) - 1` to get the maximum possible integer with `maximumBit` bits.
100+
101+
2. **Compute Initial XOR**:
102+
- XOR all elements in `nums` to get the cumulative XOR.
103+
104+
3. **Process Each Query in Reverse**:
105+
- Start from the end of `nums` and move backwards.
106+
- For each query, calculate `k` as `XORed ^ max_k`.
107+
- Add `k` to the `answer` slice.
108+
- Update `XORed` by removing the effect of the last element.
109+
110+
4. **Return the Result Slice**:
111+
- Return `answer` as the final result, containing the XOR values for each query.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Welcome to the LeetCode Solutions repository! Here, you'll find daily solutions
4747
| 1701 | Average Waiting Time | [C++](./Algorithms/src/1701.%20Average%20Waiting%20Time/Code/solution.cpp), [Java](./Algorithms/src/1701.%20Average%20Waiting%20Time/Code/solution.java), [JavaScript](./Algorithms/src/1701.%20Average%20Waiting%20Time/Code/solution.js), [Python](./Algorithms/src/1701.%20Average%20Waiting%20Time/Code/solution.py), [Go](./Algorithms/src/1701.%20Average%20Waiting%20Time/Code/solution.go) | [Explanation](./Algorithms/src/1701.%20Average%20Waiting%20Time/Explanation/explanation.md) | Medium |
4848
| 1813 | Sentence Similarity III | [C++](./Algorithms/src/1813.%20Sentence%20Similarity%20III/Code/solution.cpp), [Java](./Algorithms/src/1813.%20Sentence%20Similarity%20III/Code/solution.java), [JavaScript](./Algorithms/src/1813.%20Sentence%20Similarity%20III/Code/solution.js), [Python](./Algorithms/src/1813.%20Sentence%20Similarity%20III/Code/solution.py), [Go](./Algorithms/src/1813.%20Sentence%20Similarity%20III/Code/solution.go) | [Explanation](./Algorithms/src/1813.%20Sentence%20Similarity%20III/Explanation/explanation.md) | Medium |
4949
| 1823 | Find the Winner of the Circular Game | [C++](./Algorithms/src/1823.%20Find%20the%20Winner%20of%20the%20Circular%20Game/Code/solution.cpp), [Java](./Algorithms/src/1823.%20Find%20the%20Winner%20of%20the%20Circular%20Game/Code/solution.java), [JavaScript](./Algorithms/src/1823.%20Find%20the%20Winner%20of%20the%20Circular%20Game/Code/solution.js), [Python](./Algorithms/src/1823.%20Find%20the%20Winner%20of%20the%20Circular%20Game/Code/solution.py), [Go](./Algorithms/src/1823.%20Find%20the%20Winner%20of%20the%20Circular%20Game/Code/solution.go) | [Explanation](./Algorithms/src/1823.%20Find%20the%20Winner%20of%20the%20Circular%20Game/Explanation/explanation.md) | Medium |
50+
| 1829 | Maximum XOR for Each Query | [C++](./Algorithms/src/1829.%20Maximum%20XOR%20for%20Each%20Query/Code/solution.cpp), [Java](./Algorithms/src/1829.%20Maximum%20XOR%20for%20Each%20Query/Code/solution.java), [JavaScript](./Algorithms/src/1829.%20Maximum%20XOR%20for%20Each%20Query/Code/solution.js), [Python](./Algorithms/src/1829.%20Maximum%20XOR%20for%20Each%20Query/Code/solution.py), [Go](./Algorithms/src/1829.%20Maximum%20XOR%20for%20Each%20Query/Code/solution.go) | [Explanation](./Algorithms/src/1829.%20Maximum%20XOR%20for%20Each%20Query/Explanation/explanation.md) | Medium |
5051
| 1894 | Find the Winner of the Circular Game | [C++](./Algorithms/src/1823.%20Find%20the%20Winner%20of%20the%20Circular%20Game/Code/solution.cpp), [Java](./Algorithms/src/1823.%20Find%20the%20Winner%20of%20the%20Circular%20Game/Code/solution.java), [JavaScript](./Algorithms/src/1823.%20Find%20the%20Winner%20of%20the%20Circular%20Game/Code/solution.js), [Python](./Algorithms/src/1823.%20Find%20the%20Winner%20of%20the%20Circular%20Game/Code/solution.py), [Go](./Algorithms/src/1823.%20Find%20the%20Winner%20of%20the%20Circular%20Game/Code/solution.go) | [Explanation](./Algorithms/src/1823.%20Find%20the%20Winner%20of%20the%20Circular%20Game/Explanation/explanation.md) | Medium |
5152
| 1937 | Maximum Number of Points with Cost | [C++](./Algorithms/src/1937.%20Maximum%20Number%20of%20Points%20with%20Cost/Code/solution.cpp), [Java](./Algorithms/src/1937.%20Maximum%20Number%20of%20Points%20with%20Cost/Code/solution.java), [JavaScript](./Algorithms/src/1937.%20Maximum%20Number%20of%20Points%20with%20Cost/Code/solution.js), [Python](./Algorithms/src/1937.%20Maximum%20Number%20of%20Points%20with%20Cost/Code/solution.py), [Go](./Algorithms/src/1937.%20Maximum%20Number%20of%20Points%20with%20Cost/Code/solution.go) | [Explanation](./Algorithms/src/1937.%20Maximum%20Number%20of%20Points%20with%20Cost/Explanation/explanation.md) | Medium |
5253
| 1942 | The Number of the Smallest Unoccupied Chair | [C++](./Algorithms/src/1942.%20The%20Number%20of%20the%20Smallest%20Unoccupied%20Chair/Code/solution.cpp), [Java](./Algorithms/src/1942.%20The%20Number%20of%20the%20Smallest%20Unoccupied%20Chair/Code/solution.java), [JavaScript](./Algorithms/src/1942.%20The%20Number%20of%20the%20Smallest%20Unoccupied%20Chair/Code/solution.js), [Python](./Algorithms/src/1942.%20The%20Number%20of%20the%20Smallest%20Unoccupied%20Chair/Code/solution.py), [Go](./Algorithms/src/1942.%20The%20Number%20of%20the%20Smallest%20Unoccupied%20Chair/Code/solution.go) | [Explanation](./Algorithms/src/1942.%20The%20Number%20of%20the%20Smallest%20Unoccupied%20Chair/Explanation/explanation.md) | Medium |

0 commit comments

Comments
 (0)