Data Structures & AlgorithmsModerate
How many contiguous subarrays of [5, 2, 7, 1, 4, 6] have bitwise XOR exactly 3? Use prefix XOR values p[0]=0, p[i]=B[0]^…^B[i−1].
- A. 1
- B. 2
- C. 4
- D. 3
Correct answer: B. 2
A subarray (i, j] has XOR p[i] XOR p[j], so the count is the number of index pairs i < j with that XOR. Leaving out p[0] = 0 loses every subarray that starts at the first element, and counting ordered pairs doubles the answer.
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
- In the array [1, 2, 1, 3, 2, 1, 4, 5], how many contiguous subarrays contain exactly 2 odd numbers?
- How many contiguous subarrays of [10, 5, 2, 6] have a product strictly less than 100? Every value is a positive integer.
- What is the sum of the sums of all contiguous subarrays of [999999999, 888888888, 777777777], taken modulo 1000000007?
- What is the sum, over all contiguous subarrays of [3, 1, 3, 2], of the maximum element of each subarray?
- In [4, 7, 5, 9, 6, 3], how many contiguous subarrays have (max − min) ≤ 3?
- How many contiguous subarrays of [1, 2, 1, 3, 3, 2, 2, 1] contain at most 2 distinct values?