Data Structures & AlgorithmsHard
How many contiguous subarrays of 2 9 4 7 1 8 3 have their MAXIMUM value located at an EVEN absolute index of the original array (index 0 counts as even)? If the maximum value appears more than once, use its first occurrence in the subarray.
- A. 24
- B. 16
- C. 17
- D. 4
Correct answer: D. 4
Locate the maximum of each subarray, converting its relative position into an absolute index before testing parity. Testing only the relative position, or flipping the parity, is a common slip.
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 index pairs i < j in 4 1 7 2 10 5 8 satisfy A[i] ≡ A[j] (mod 3) AND A[i] < A[j]?
- How many contiguous substrings of "abaabbaab" contain EXACTLY one 'b' and at least two 'a's?
- What is the maximum sum of a contiguous subarray of 3 5 2 7 9 1 4 11 in which every element is ODD?
- How many contiguous subarrays of 1 3 4 7 9 6 11 of length at least 2 contain EXACTLY one even value?
- How many contiguous subarrays of 2 5 1 2 4 7 have length at least 2, contain pairwise DISTINCT values, and have an ODD sum?
- How many index pairs i < j in 1 2 3 4 6 8 satisfy A[i]·A[j] ≥ 2·(A[i] + A[j])?