2317. Maximum XOR After Operations LeetCode Solution
In this guide, you will get 2317. Maximum XOR After Operations LeetCode Solution with the best time and space complexity. The solution to Maximum XOR After Operations problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Maximum XOR After Operations solution in C++
- Maximum XOR After Operations solution in Java
- Maximum XOR After Operations solution in Python
- Additional Resources

Problem Statement of Maximum XOR After Operations
You are given a 0-indexed integer array nums. In one operation, select any non-negative integer x and an index i, then update nums[i] to be equal to nums[i] AND (nums[i] XOR x).
Note that AND is the bitwise AND operation and XOR is the bitwise XOR operation.
Return the maximum possible bitwise XOR of all elements of nums after applying the operation any number of times.
Example 1:
Input: nums = [3,2,4,6]
Output: 7
Explanation: Apply the operation with x = 4 and i = 3, num[3] = 6 AND (6 XOR 4) = 6 AND 2 = 2.
Now, nums = [3, 2, 4, 2] and the bitwise XOR of all the elements = 3 XOR 2 XOR 4 XOR 2 = 7.
It can be shown that 7 is the maximum possible bitwise XOR.
Note that other operations may be used to achieve a bitwise XOR of 7.
Example 2:
Input: nums = [1,2,3,9,2]
Output: 11
Explanation: Apply the operation zero times.
The bitwise XOR of all the elements = 1 XOR 2 XOR 3 XOR 9 XOR 2 = 11.
It can be shown that 11 is the maximum possible bitwise XOR.
Constraints:
1 <= nums.length <= 105
0 <= nums[i] <= 108
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
2317. Maximum XOR After Operations LeetCode Solution in C++
class Solution {
public:
int maximumXOR(vector<int>& nums) {
// 1. nums[i] & (nums[i] ^ x) enables you to turn 1-bit to 0-bit from
// nums[i] since x is arbitrary.
// 2. The i-th bit of the XOR of all the elements is 1 if the i-th bit is 1
// for an odd number of elements.
// 3. Therefore, the question is equivalent to: if you can convert any digit
// from 1 to 0 for any number, what is the maximum for XOR(nums[i]).
// 4. The maximum we can get is of course to make every digit of the answer
// to be 1 if possible
// 5. Therefore, OR(nums[i]) is an approach.
return reduce(nums.begin(), nums.end(), 0, bit_or());
}
};
/* code provided by PROGIEZ */
2317. Maximum XOR After Operations LeetCode Solution in Java
class Solution {
public int maximumXOR(int[] nums) {
// 1. nums[i] & (nums[i] ^ x) enables you to turn 1-bit to 0-bit from
// nums[i] since x is arbitrary.
// 2. The i-th bit of the XOR of all the elements is 1 if the i-th bit is 1
// for an odd number of elements.
// 3. Therefore, the question is equivalent to: if you can convert any digit
// from 1 to 0 for any number, what is the maximum for XOR(nums[i]).
// 4. The maximum we can get is of course to make every digit of the answer
// to be 1 if possible
// 5. Therefore, OR(nums[i]) is an approach.
return Arrays.stream(nums).reduce(0, (a, b) -> a | b);
}
}
// code provided by PROGIEZ
2317. Maximum XOR After Operations LeetCode Solution in Python
class Solution:
def maximumXOR(self, nums: list[int]) -> int:
# 1. nums[i] & (nums[i] ^ x) enables you to turn 1-bit to 0-bit from
# nums[i] since x is arbitrary.
# 2. The i-th bit of the XOR of all the elements is 1 if the i-th bit is 1
# for an odd number of elements.
# 3. Therefore, the question is equivalent to: if you can convert any digit
# from 1 to 0 for any number, what is the maximum for XOR(nums[i]).
# 4. The maximum we can get is of course to make every digit of the answer
# to be 1 if possible
# 5. Therefore, OR(nums[i]) is an approach.
return functools.reduce(operator.ior, nums)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.