Data Structures & AlgorithmsHardCodeforces Div 2 C · ICPC regional
Array a = [9, 4, 7, 1, 8, 2, 6, 3]. How many index pairs i < j have |a[i] - a[j]| >= 5?
- A. 12
- B. 7
- C. 10
- D. 6
Correct answer: C. 10
Sorting the values lets you count pairs whose gap is at least 5 with two pointers: for each smaller value, every larger value beyond the threshold pairs with it. A strict bound on the gap drops pairs with gap exactly 5.
More Coding Interview Problems questions
- Among [5, 3, 6, 12, 9, 10, 1], choose two different elements. What is the largest possible value of x OR y?
- How many pairs of distinct elements of [1, 4, 6, 7, 10, 13, 15] have a sum that is a perfect square (1, 4, 9, 16, ...)?
- Closed intervals [2,9], [4,7], [6,12], [10,14], [11,13], [15,18]. What is the total length of the set of points covered by at least one…
- With coins of values 1, 3 and 4 (each usable any number of times), in how many ways (order does not matter) can you make the amount 6?
- What is the length of the longest run of equal consecutive values in [4, 4, 1, 2, 2, 2, 5, 1]?
- Problem: Two sorted arrays of sizes m and n are merged in O(m + n) time with two pointers. What is the complexity of finding the median of…