Data Structures & AlgorithmsModerateCodeforces Div2 C · TCS NQT
How many unordered pairs of positions (i < j) in [9, 1, 6, 14, 3, 11, 7] have an absolute difference of at most 4?
- A. 7
- B. 12
- C. 10
- D. 9
Correct answer: D. 9
Sort the array, then for each i use a two-pointer or binary search to count the j with a[j] − a[i] ≤ d; the sorted order makes each count contiguous. Off-by-one at the boundary (strict vs non-strict) changes the total.
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
- For the multiset [1, 6, 1, 9, 4, 12, 3], consider all unordered pairs of distinct positions and their absolute differences. What is the…
- What is the smallest non-negative integer x for which x³ + x is at least 1000?
- A binary search for the FIRST index where a predicate becomes true uses: lo = 0, hi = n, while lo < hi: mid = (lo + hi) // 2; if…
- Parcels with weights [4, 9, 2, 7, 5, 8, 3, 6] must be shipped in this order, and a ship carries a contiguous block each day. What is the…
- What is the largest integer k such that 1 + 2 + ... + k is at most 2023?
- A train makes three legs of lengths 1, 3 and 2 km. Each leg except the last must be finished in whole hours (a partial hour is rounded…