Data Structures & AlgorithmsModerateCodeforces Div 2 C · ICPC regional
For the array a = [1, 4, 7, 2, 5, 6, 3], how many index pairs i < j satisfy a[i] XOR a[j] = 5?
- A. 3
- B. 4
- C. 6
- D. 1
Correct answer: A. 3
Store each value in a hash map and for each x look up x XOR 5, adding the earlier matches; each unordered pair is counted once. Using AND instead of XOR counts a different relation, which is the trap.
More Coding Interview Problems questions
- Array a = [2, -1, 3, 1, -2, 4]. How many contiguous subarrays have a sum equal to their length? Subtract 1 from each element first and…
- Sorted arrays A = [1, 5, 9, 12] and B = [3, 4, 10, 15, 20]. How many pairs (x in A, y in B) satisfy |x - y| <= 2?
- Array a = [1, 2, 1, 3, 2, 2, 4, 1]. What is the length of the longest contiguous subarray that contains at most two distinct values?
- Sorted array a = [2, 3, 5, 6, 8, 10]. How many DISTINCT values appear among the sums a[i] + a[j] over pairs i < j?
- Sorted array a = [1, 2, 4, 5, 6, 9, 10, 11]. How many elements does the largest contiguous block of this array have if its largest element…
- How many permutations (p1, ..., p5) of the numbers 1 to 5 satisfy pi <= i + 1 for every position i? Use a bitmask DP over the set of…