Data Structures & AlgorithmsVery hard
How many contiguous subarrays of 1 2 3 1 2 4 1 contain EXACTLY three distinct values?
- A. 15
- B. 6
- C. 5
- D. 9
Correct answer: D. 9
Exactly three distinct values is the difference between at-most-three and at-most-two counts. Using at-least-three, exactly-two, or restricting the length to three each changes 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 2 4 3 6 1 5 have a sum that is divisible by the number of DISTINCT values in them?
- For every window of exactly 3 consecutive elements of 7 3 9 4 8 2 6, compute (maximum − minimum) inside that window. What is the SMALLEST…
- A sorted linked list holds 1 2 2 3 3 3 4 4 4 4. Delete nodes so that each value keeps AT MOST TWO nodes, preserving order. How many nodes…
- How many different valid sequences of PUSH and POP operations are there for a stack that performs exactly 6 pushes and 6 pops in total…
- How many contiguous subarrays of 3 1 2 4 2 3 1 2 have a sum EXACTLY equal to twice their length?
- What is the maximum sum of a contiguous subarray of 4 2 4 5 6 2 8 that contains NO repeated value?