Data Structures & AlgorithmsHard
How many contiguous subarrays of 1 2 3 1 4 2 5 contain EXACTLY two distinct values that occur exactly once inside the subarray?
- A. 21
- B. 6
- C. 7
- D. 8
Correct answer: D. 8
Inside each block count how many values occur exactly once, then keep blocks with exactly two. At-least-two, exactly-two distinct values, or exactly-one counts answer different questions.
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
- Prices over days are 3 3 5 0 0 3 1 4. You may complete at most TWO buy-then-sell transactions, and you must sell before buying again. What…
- Consider all cyclic rotations of 3 1 2 5 4 (including the identity). For each rotation count its inversions (pairs i < j with a[i] >…
- Using only swaps of ADJACENT elements, what is the minimum number of swaps needed to make the MINIMUM value of 3 1 5 2 4 the FIRST element…
- What is the maximum number of pairwise DISJOINT contiguous blocks of 1 -1 2 3 -3 4 -4 5 whose sums are all zero? (Blocks must not overlap…
- How many contiguous subarrays of 2 1 3 2 1 3 3 2 contain EVERY distinct value that appears in the whole array?
- How many contiguous windows of length exactly 4 in 1 3 4 5 2 6 7 1 4 have a sum that is a perfect square?