Data Structures & AlgorithmsHardGATE CS · TCS NQT
How many contiguous subarrays of [1, 2, 1, 3, 4, 2, 3] contain exactly 3 distinct values?
- A. 7
- B. 6
- C. 20
- D. 14
Correct answer: B. 6
Exactly-3 = atMost(3) − atMost(2) = 20 − 14 = 6. Stopping at atMost(3) gives 20, atMost(2) gives 14, and 7 is the count for exactly 2 distinct values.
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, 8, 3, 2, 6, 1] have a product that is a perfect square?
- What is the minimum number of characters that must be deleted from "abcbdab" to make it a palindrome?
- How many pairs of non-overlapping equal substrings of length 2 exist in "abababab" (positions i < j with j >= i + 2)?
- In the text "aaabaaaabaaa", count the occurrences of "aaa" whose starting index is a prime number (0-indexed).
- What is the sum over all adjacent pairs of [4, 2, 5, 1, 3] of the smaller of the two values?
- A shop has a 'buy 2, get the cheapest of the three free' deal. Items priced [3, 1, 4, 2, 5, 6] are bought in groups of three, with the…