Data Structures & AlgorithmsHard
How many contiguous subarrays of 3 1 6 2 9 4 5 12 contain EXACTLY 2 multiples of 3?
- A. 30
- B. 16
- C. 15
- D. 10
Correct answer: D. 10
Count the multiples of 3 inside each subarray, then keep the ones with exactly two. Using at-most or at-least bounds, or testing for one multiple, changes which blocks are counted.
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
- 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)?
- In the asteroid collision problem, why does a stack of right-moving asteroids suffice?