Skip to content

Latest commit

 

History

History

2317

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

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

Companies: American Express

Related Topics:
Array, Math, Bit Manipulation

Similar Questions:

Solution 1.

A[i] x   xor and
0    0   0   0
0    1   1   0
1    0   1   1
1    1   0   0

So, we can remove bit 1s but can't make bit 0s to 1s.

As long as any A[i] as 1 at the i-th bit, the i-th bit of the answer must be one.

// OJ: https://leetcode.com/problems/maximum-xor-after-operations
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(1)
class Solution {
public:
    int maximumXOR(vector<int>& A) {
        int ans = 0;
        for (int n : A) ans |= n;
        return ans;
    }
};