Data Structures & AlgorithmsHardGATE CS · Amazon SDE round
How many contiguous subarrays of [1, 2, 1, 2, 3, 3, 4] contain at most 2 distinct values?
- A. 16
- B. 19
- C. 18
- D. 21
Correct answer: C. 18
Count with a sliding window: for each right end, the number of valid left ends is added in one step. Forgetting that a shorter subarray is counted in every longer valid one is the usual slip.
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 non-empty contiguous subarrays of [10, 5, 2, 6] have a product strictly less than 100?
- An LRU cache holds 3 keys. Requests arrive in the order 1, 2, 3, 1, 4, 2, 5, 1, 2, 3, 4, 5, and a request for an absent key is a miss that…
- Given the linked list 1 -> 2 -> ... -> 8, the list is reversed in consecutive groups of 3 (a final group with fewer than 3 nodes is left…
- A linked list has a tail of 3 nodes leading into a cycle of 5 nodes. A slow pointer advances 1 node per step and a fast pointer advances 2…
- How many index pairs i < j satisfy (a[i] + a[j]) mod 7 = 0 for the array [3, 4, 7, 10, 11, 14, 21, 5, 9]?
- How many index pairs i < j satisfy a[i] XOR a[j] = 6 for the array [1, 4, 7, 2, 5, 3, 6]?