values and an integer goal. For any contiguous non-empty stretch of the array you can compute the bitwise AND (&) of all its elements.
Return the smallest possible value of |AND(stretch) - goal| over all stretches.
There are O(n²) stretches, but for a fixed right end only a few distinct AND values appear: think about why.values=[12, 10, 6, 7], goal=5 → 1
The stretches give 12, 10, 6, 7, 12 & 10 = 8, 10 & 6 = 2, 6 & 7 = 6, plus 0 and 2 for the longer ones. The closest to 5 is 6, a difference of 1.
values=[9, 9, 9], goal=20 → 11
Every stretch gives 9, a difference of 11.
values) ≤ 10⁵values[i] ≤ 10⁶goal ≤ 10⁷values=[12, 10, 6, 7], goal=5
1
Run checks the first 3 tests in your browser. Submit checks all 107 tests on the server, hidden ones included.