All Minimum-Difference Pairs
Updated
Problem statement
Given an array of integers nums, find the minimum absolute difference between any two distinct input elements. Return every pair of values that attains that minimum.
For this exercise, all input values are distinct. Each output pair must be written as [smaller, larger], and the pairs must be sorted by their first value in ascending order. Return an empty array when there are fewer than two values.
Include every minimizing pair, even when several disjoint pairs or several adjacent pairs share the same minimum difference.
Function
minimumDifferencePairs(nums: int[]) → int[][]Examples
Example 1
nums = [-1,-2,-4,-5,7,10]return = [[-5,-4],[-2,-1]]The minimum absolute difference is 1. Both [-5,-4] and [-2,-1] attain it, in ascending output order.
Example 2
nums = [3,1,7,5]return = [[1,3],[3,5],[5,7]]After ordering the values, every neighboring gap is 2, so all three pairs must be returned.
Example 3
nums = []return = []Fewer than two values form no pair, so the result is empty.
Constraints
0 <= nums.length <= 2000.- All values in
numsare distinct. -10^9 <= nums[i] <= 10^9.- Return each value pair once, in the stated order.
More Microsoft Onsite Interview questions
- Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Group Transactions by HourAffirm · ONSITE INTERVIEWAffirm interview questions
- Merge Sorted ArrayDP World · ONSITE INTERVIEW
- Map and Accumulate Learning QueriesPalantir · ONSITE INTERVIEWPalantir interview questions
- Meeting RoomsGoldman Sachs · PHONE SCREEN · ONSITE INTERVIEWGoldman Sachs interview questions
- Sort Products by Aisle With Frozen Items LastInstacart · ONSITE INTERVIEWInstacart interview questions
- Merge Two Arrays Into Sorted OrderNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
- Compress Consecutive Integer RangesDocuSign · ONSITE INTERVIEWDocuSign interview questions
- Rank Transform an ArrayAgoda · ONSITE INTERVIEWAgoda interview questions
- Sort Documents Left to RightFigma · ONSITE INTERVIEWFigma interview questions
- Filter and Sort Scheduled TasksRippling · ONSITE INTERVIEWRippling interview questions