Problem · Array
Top K Closest Pairs in a Sorted Array
Learn this problemProblem statement
Given a nondecreasing integer array nums and an integer k, return the selected value pairs in priority order. Duplicate values and duplicate value pairs are retained when they come from different index pairs.
Function
topKClosestPairs(nums: int[], k: int) → int[][]Examples
Example 1
nums = [1,2,4]k = 2return = [[1,2],[2,4]]The differences are 1, 2, and 3.
Example 2
nums = [1,1,2]k = 3return = [[1,1],[1,2],[1,2]]Index-distinct duplicate pairs are retained.
Constraints
1 <= nums.length <= 5000 <= k <= 10000numsis sorted in nondecreasing order.