Skip to main content
Back to problems
#2317
Medium Algorithms

Maximum xor after operations

Array Math Bit Manipulation
79.9% acceptance
Feb 25, 2026
641
170
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). The operation can only clear bits in nums[i]. Return the maximum possible bitwise XOR of all elements of nums after applying the operation any number of times.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn maximum_xor(nums: Vec<i32>) -> i32 {
    // The operation can clear any bit in any element.
    // For each bit, if at least one element has that bit set, we can make the XOR
    // of all elements have that bit set (by clearing it from others as needed).
    // So the answer is the OR of all elements.
    nums.iter().fold(0, |acc, &x| acc | x)
  }
}