Data Structures & AlgorithmsHard
How many contiguous subarrays of [1, 2, 3, 1, 2, 1, 3, 3] contain the values 1, 2 and 3 each at least once?
- A. 4
- B. 19
- C. 27
- D. 17
Correct answer: B. 19
A two-pointer window that counts how many of each of 1, 2, 3 it contains gives, for each right end, the number of left starts that keep all three present. Restricting to length 3, counting any two values, or taking the complement each give different totals.
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 minimum number of elements you must DELETE from [5, 1, 9, 4, 8, 3, 7] (the remaining elements keep their order but need not be…
- How many index pairs i < j in [2, 8, 3, 18, 4, 12, 5] have a product A[i]·A[j] that is a perfect square?
- How many contiguous subarrays of [1, 2, 3, 4, 5, 6, 7, 8] have a sum that is a prime number?
- For each contiguous window of length 3 in [1, 2, 1, 3, 2, 1], count the number of distinct values inside it, then add these counts over…
- The numbers 1, 2, 3, 4, 5 are pushed onto an initially empty stack in this order, and a pop may be performed whenever the stack is…
- What is the minimum number of positive integers (each value at most 10, repetitions allowed in the array) needed so that EVERY integer…