Data Structures & AlgorithmsHard
How many contiguous subarrays of 1 4 2 6 3 5 0 have their FIRST element equal to their minimum AND their LAST element equal to their maximum (length 1 subarrays count)?
- A. 16
- B. 4
- C. 11
- D. 12
Correct answer: C. 11
The condition needs the left end to be the minimum and the right end to be the maximum of the same subarray, which holds for singletons trivially. Reversing the roles, testing one end only, or dropping singletons each change the total.
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 3 1 6 2 9 4 5 12 contain EXACTLY 2 multiples of 3?
- What is the length of the longest contiguous subarray of 4 6 3 8 10 12 5 2 4 in which EVERY element is even and whose length is odd?
- How many contiguous subarrays of 1 3 2 4 5 3 1 6 have a sum divisible by 4 AND an odd length?
- How many index pairs i < j in 2 3 4 6 8 12 9 satisfy A[i]·A[j] = 24?
- A min-stack stores a pair (value, current minimum) per entry. Why does popping never require recomputing the minimum?
- In the 'remove nodes with a greater value to their right' problem, which list order is processed to decide survivors in O(n)?