Data Structures & AlgorithmsHardGATE CS · TCS NQT
How many contiguous subarrays of [1, 2, 2, 3, 1, 1] contain no two adjacent equal elements?
- A. 11
- B. 10
- C. 8
- D. 13
Correct answer: B. 10
A subarray is valid exactly when it lies inside a maximal stretch with no equal neighbours, so count per stretch with L(L+1)/2.
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 length of the shortest contiguous subarray of [2, -1, 2, 4, -3, 5] whose sum is at least 7?
- What is the minimum number of characters that must be deleted from aaaabbbbcccdd so that no two letters occur the same number of times?
- How many distinct values appear among the sums of all non-empty contiguous subarrays of [1, 2, 3]?
- How many subarrays of [2, 5, 1, 5, 3] have a maximum value that occurs exactly once inside the subarray?
- How many elements of the array [3, 1, 4, 1, 5, 9, 2, 6] have no strictly greater element anywhere to their right?
- How many index pairs i < j in the array [6, 9, 2, 7, 4, 1] satisfy a[i] + i = a[j] + j (0-based indices)?