Data Structures & AlgorithmsHard
In the sorted array 1 3 4 7 9 12 14, how many index pairs i < j satisfy A[j] − A[i] ≥ 5?
- A. 11
- B. 10
- C. 14
- D. 13
Correct answer: D. 13
In a sorted array, once A[j] − A[i] ≥ d for some i, every larger j also qualifies, so a two-pointer sweep counts the qualifying j in O(1) per i. Reversing the inequality or using a strict bound counts a different set.
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
- Count the CIRCULAR descents of 3 4 5 1 2: positions i (0 ≤ i < n) where A[i] > A[(i+1) mod n], with the last element compared to the…
- How many contiguous subarrays of 1 2 1 1 3 1 2 have a STRICT majority element, meaning some value occurs more than half the time in the…
- How many index pairs i < j in 0 1 2 2 3 4 0 satisfy A[i]·A[j] = A[i] + A[j]?
- How many contiguous subarrays of 3 1 2 2 4 3 5 consist of consecutive integers (their values, in some order, are k, k+1, …, k+L−1 with no…
- Each operation increases one element of 3 3 1 6 2 by 1. What is the minimum total number of operations to make the array STRICTLY…
- Using AT MOST ONE swap of two elements, what is the lexicographically smallest array obtainable from 3 1 2 5 4?