Data Structures & AlgorithmsVery hardLeetCode-style · Codeforces Div2 D
For the multiset [1, 6, 1, 9, 4, 12, 3], consider all unordered pairs of distinct positions and their absolute differences. What is the 6-th smallest difference (counted with multiplicity)?
- A. 3
- B. 2
- C. 4
- D. 5
Correct answer: A. 3
Sort the array and binary search the difference d: count pairs with difference ≤ d with a two-pointer sweep, and find the smallest d whose count reaches k. Pairs with equal values have distance 0 and are counted with multiplicity, which shifts the index.
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 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…
- How many indices i in [1, 7, 3, 6, 5, 6] have the sum of elements strictly left of i equal to the sum of elements strictly right of i?