Closest Numbers
Updated
Problem statement
arr holds distinct integers. For every two numbers in arr, take the absolute value of their difference, and let d be the smallest of these values. Return all pairs of numbers from arr whose difference is exactly d.
Write each pair as a list of two numbers with the smaller number first. Then sort the pairs in ascending order. Because the numbers are distinct, no two pairs start with the same number, so this simply sorts the pairs by their first number.
Function
minimumAbsDifference(arr: int[]) → List<List<Integer>>Examples
Example 1
arr = [12,3,7,5,10]return = [[3,5],[5,7],[10,12]]In sorted order the numbers are 3, 5, 7, 10, 12, and the differences between neighbours are 2, 2, 3 and 2. Two numbers that are not neighbours have at least one number between them, so their difference is the sum of two or more of these gaps, at least 4. The minimum difference is therefore 2, reached by [3,5], [5,7] and [10,12]. 5 is in two pairs, and the pairs are listed in ascending order.
Example 2
arr = [9,-9]return = [[-9,9]]There is only one pair, so its difference, 18, is the minimum. The smaller number, -9, is written first, even though it comes second in arr.
Example 3
arr = [20,-3,17,0,-6,41]return = [[-6,-3],[-3,0],[17,20]]In sorted order the numbers are -6, -3, 0, 17, 20, 41, and the differences between neighbours are 3, 3, 17, 3 and 21. Numbers that are not neighbours are further apart than any single gap between them, so the minimum difference is 3, reached by [-6,-3], [-3,0] and [17,20]. Negative numbers come first because the pairs are ordered by value, not by their position in arr.
Constraints
2 <= arr.length <= 10^5-10^6 <= arr[i] <= 10^6- All numbers in
arrare distinct.
More Headlands Technologies interview questions
- Minimum Changes for Distinct Adjacent CharactersOA · Seen Dec 2026
- Sliding-Window Means with IEEE Special ValuesOA · Seen Dec 2026
- Disaster RecoveryOA · Seen Nov 2026
- Array Interval SumsOA · Seen Oct 2026
- Job Runner Chain ReportsOA · Seen Oct 2026
- As-Of JoinOA · ONSITE INTERVIEW · Seen Aug 2026
- Exclusive Function Execution TimesOA · Seen Jan 2024
- Earliest Dependency ScheduleOA · Seen Oct 2023
- Integer Compression OffsetPHONE SCREEN · Seen Oct 2026
- Market Order BookPHONE SCREEN · Seen Jun 2026
Similar questions at other companies
- Macaron Order with Set DiscountsShopify · OAShopify interview questions
- Build the Task HierarchyPendo · OA
- Count User Mentions in Chat MessagesRamp · OARamp interview questions
- Catch Fish with Reusable BaitsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Competition Winner and Runner-Up (Forward Deployed Engineer)Boston Consulting Group · OABoston Consulting Group interview questions
- Word Frequency Counts in Alphabetical OrderAbridge · OAAbridge interview questions
- Rank Football TeamsCapital One · OACapital One interview questions
- Maximum Apples That Fit in a BoxVirtu Financial · OAVirtu Financial interview questions
- Rank Football TeamsPersona · OAPersona interview questions
- Meandering ArrayVerisk · OA