Data Structures & AlgorithmsModerate
What is the sum, over all non-empty contiguous subarrays of 1 3 5, of the bitwise OR of each subarray?
- A. 30
- B. 26
- C. 24
- D. 12
Correct answer: B. 26
ORs: 1, 3, 5, 1|3 = 3, 3|5 = 7, 1|3|5 = 7, total 26. Adding the plain sums gives 30 because OR does not add overlapping bits, XOR gives 24, and AND gives 12.
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 DISTINCT values arise as the bitwise OR of a non-empty contiguous subarray of 1 2 4 2?
- How many index pairs i < j in 3 5 6 1 4 2 satisfy A[i] OR A[j] = 6, the maximum value of the array?
- Consider the windows of length 3 of 1 2 3 1 2 4 1 2 starting at positions i and i+1. How many i satisfy sum(window i) = sum(window i+1)?
- How many contiguous subarrays of 1 3 2 5 4 6 1 2 of length at least 3 have exactly one interior strict local maximum (a position strictly…
- How many index pairs i < j in 2 5 3 9 4 7 6 satisfy A[i] < A[j] AND (A[j] − A[i]) divides (j − i)?
- How many contiguous subarrays of length exactly 2 in 2 4 7 9 1 3 6 8 consist of two values with the SAME parity?