Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 <= 1050 <= nums[i] <= 108Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math · Bit Manipulation
[3,2,4,6]
[1,2,3,9,2]
maximum-xor-of-two-numbers-in-an-array)maximum-xor-product)minimize-or-of-remaining-elements-using-operations)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2317: Maximum XOR After Operations
class Solution {
public int maximumXOR(int[] nums) {
int ans = 0;
for (int x : nums) {
ans |= x;
}
return ans;
}
}
// Accepted solution for LeetCode #2317: Maximum XOR After Operations
func maximumXOR(nums []int) (ans int) {
for _, x := range nums {
ans |= x
}
return
}
# Accepted solution for LeetCode #2317: Maximum XOR After Operations
class Solution:
def maximumXOR(self, nums: List[int]) -> int:
return reduce(or_, nums)
// Accepted solution for LeetCode #2317: Maximum XOR After Operations
/**
* [2317] 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 <= 10^5
* 0 <= nums[i] <= 10^8
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/maximum-xor-after-operations/
// discuss: https://leetcode.com/problems/maximum-xor-after-operations/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn maximum_xor(nums: Vec<i32>) -> i32 {
nums.into_iter().fold(0, |acc, x| acc | x)
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_2317_example_1() {
let nums = vec![3, 2, 4, 6];
let result = 7;
assert_eq!(Solution::maximum_xor(nums), result);
}
#[test]
fn test_2317_example_2() {
let nums = vec![1, 2, 3, 9, 2];
let result = 11;
assert_eq!(Solution::maximum_xor(nums), result);
}
}
// Accepted solution for LeetCode #2317: Maximum XOR After Operations
function maximumXOR(nums: number[]): number {
let ans = 0;
for (const x of nums) {
ans |= x;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.