Maximum XOR After Operations - Problem
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.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [3,2,8]
›
Output:
11
💡 Note:
We can achieve maximum XOR by applying operations strategically. The optimal value is the OR of all numbers: 3 | 2 | 8 = 11. This works because we can turn off any bits we don't want.
Example 2 — Single Element
$
Input:
nums = [5]
›
Output:
5
💡 Note:
With only one element, the maximum XOR is the element itself. No operations can increase the XOR value since we can only turn bits OFF.
Example 3 — Powers of Two
$
Input:
nums = [1,2,4,8]
›
Output:
15
💡 Note:
Each number has a different bit set. The OR of all gives 1|2|4|8 = 15 (binary 1111), which is also the maximum possible XOR we can achieve.
Constraints
- 1 ≤ nums.length ≤ 105
- 0 ≤ nums[i] ≤ 108
Visualization
Tap to expand
Understanding the Visualization
1
Input Array
Array of integers with their binary representations
2
Operation Analysis
nums[i] & (nums[i] ^ x) can turn any bit OFF
3
Maximum XOR
OR of all numbers gives maximum achievable XOR
Key Takeaway
🎯 Key Insight: Since the operation can only turn bits OFF, the maximum XOR equals the OR of all numbers
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code