Ctrl K

Clear Bits for the Biggest XOR

Medium
You are given an array of non-negative integers values. In one move you may pick any element and switch off any set of its 1-bits (turn them into zeros). You can never switch a bit on. You may make as many moves as you like, including none. Return the largest value the XOR of all elements can reach after your moves.
Example 1
values=[5, 6, 3] → 7

As is, 5 XOR 6 XOR 3 = 0. Turn off bit 2 of 6 (it becomes 2) and bit 1 of 3 (it becomes 1): 5 XOR 2 XOR 1 = 7.

Example 2
values=[8, 8, 8, 8] → 8

Zero out three of the four 8s; one is left and the XOR is 8.

Example 3
values=[0] → 0
Constraints
  • 1 ≤ len(values) ≤ 10⁵
  • 0 ≤ values[i] ≤ 10⁸
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSum of Bit Differences MediumEight Lamps After Many Nights MediumSplit Into Equal-XOR Halves MediumTake From Any Pile MediumSwitch Lines to Darken the Board Medium
loading Python…
Input
values=[5, 6, 3]
Expected
7

Run checks the first 3 tests in your browser. Submit checks all 86 tests on the server, hidden ones included.