Data Structures & AlgorithmsVery hard
How many contiguous subarrays of 4 2 7 1 5 3 6 of length at least 2 have their FIRST element equal to their minimum?
- A. 12
- B. 6
- C. 4
- D. 5
Correct answer: D. 5
The first element must not be exceeded by any element that follows in the block, so the qualifying blocks end before the next smaller value. Including singletons, testing the last element, or testing the maximum changes 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
- How many contiguous subarrays of 1 2 4 3 5 2 have XOR equal to their SUM?
- What is the length of the shortest contiguous subarray of 1 4 2 7 3 5 6 1 whose sum is at least 14?
- How many contiguous subarrays of 2 3 4 5 6 7 8 contain EXACTLY two even values and have an ODD length?
- How many index pairs i < j in 1 3 6 5 4 10 2 have A[i] + A[j] equal to a perfect square?
- Choose three DIFFERENT positions of -10 -3 1 2 7 -1 to maximise the product of their values. What is the maximum product?
- How many ordered pairs (i, j) with i ≠ j in 3 1 4 1 5 satisfy A[i] > A[j]?