Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
Let’s say we want to count the number of pairs <code>(l, r)</code> such that <code>nums[l] & nums[l + 1] & … & nums[r] == k</code>.
Fix the left index <code>l</code>.
Note that if you increase <code>r</code> for a fixed <code>l</code>, then the AND value of the subarray either decreases or remains unchanged.
Therefore, consider using binary search.
To calculate the AND value of a subarray, use sparse tables.