Data Structures & AlgorithmsHard
How many index pairs i < j in 1 2 3 4 5 6 7 8 have A[i] XOR A[j] equal to a POWER OF TWO (1, 2, 4, 8, …)?
- A. 12
- B. 4
- C. 9
- D. 6
Correct answer: C. 9
A value is a power of two exactly when it has a single set bit, which is tested by v & (v−1) = 0 for v > 0. Using AND or sums, or only checking the smallest powers, gives other counts.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- How many NON-EMPTY subsets of {1, 2, ..., 10} have a sum divisible by 10?
- How many NON-EMPTY subsets of 2 3 5 7 11 13 have a product that is at most 60?
- Among all arrays of length 4 with values in {1, 2, 3, 4}, the absolute differences between ADJACENT elements (three of them) must all be…
- Among all permutations of 1, 2, 3, 4, 5 with NO fixed point (no position i holds the value i), which permutation is lexicographically…
- Among binary strings of length 6 that contain exactly three '1's and no two '1's adjacent, which one is lexicographically smallest?
- How many contiguous windows of length exactly 4 in 2 5 1 4 7 3 6 2 9 have DISTINCT elements AND a sum divisible by 3?