Data Structures & AlgorithmsHardGATE CS · TCS NQT
How many index pairs i < j in [1, 3, 4, 7, 8, 10] satisfy |a[j] - a[i]| <= 3?
- A. 15
- B. 5
- C. 7
- D. 4
Correct answer: C. 7
Two pointers on the sorted array: pairs within 3 are (1,3), (1,4), (3,4), (4,7), (7,8), (7,10), (8,10) = 7. Checking only consecutive pairs gives 5, using < 3 drops the gap-3 pairs (4), and 15 counts all pairs.
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 index triples i < j < k satisfy a[i] + a[j] + a[k] <= 4 for a = [-4, -1, 0, 2, 3, 5]?
- How many windows of length 3 in [1, 2, 3, 1, 2, 2, 4, 1, 3, 4] contain three pairwise distinct values?
- In the string "bbaacbbaaac", what is the length of the shortest contiguous substring that contains at least three letters 'a'?
- Piles of bananas are [3, 6, 7, 11] and you must finish all piles within 8 hours, eating at speed k bananas per hour (taking ceil(pile / k)…
- The array [7, 2, 5, 10, 8] is split into exactly 2 non-empty contiguous parts so that the largest part sum is as small as possible. What…
- How many non-empty contiguous subarrays of [4, 5, 0, -2, -3, 1] have a sum divisible by 5 (including negative sums, which are divisible…