Top K Pair Sums From One Array
Updated
Problem statement
Given an integer array nums, every pair of distinct indices i < j produces the sum nums[i] + nums[j].
Return the k largest pair sums, from largest to smallest. Count each different index pair separately, even when values or sums are equal.
Function
topKPairSums(nums: int[], k: int) → long[]Examples
Example 1
nums = [1,2,3]k = 2return = [5,4]The pair sums are 5 from 2+3, 4 from 1+3, and 3 from 1+2.
Example 2
nums = [4,4,1]k = 3return = [8,5,5]The two different index pairs containing 1 both contribute a sum of 5.
Example 3
nums = [-5,-1,2,3]k = 4return = [5,2,1,-2]The four largest sums are 2+3, -1+3, -1+2, and -5+3.
Constraints
2 <= nums.length <= 100000.-1000000000 <= nums[i] <= 1000000000.1 <= k <= min(100000, nums.length * (nums.length - 1) / 2).
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Top K Frequent ElementsAmazon · OA · PHONE SCREEN · ONSITE INTERVIEW
- Mutual-Friend RecommendationsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Tennis Court SchedulingAtlassian · PHONE SCREEN · ONSITE INTERVIEWAtlassian interview questions
- Retrieve Vectors by Cosine SimilarityHarvey · ONSITE INTERVIEWHarvey interview questions
- Meeting Rooms IISalesforce · ONSITE INTERVIEWSalesforce interview questions
- Top K Largest Values from a StreamThe D.E. Shaw Group · ONSITE INTERVIEWThe D.E. Shaw Group interview questions
- Generate Complete Inputs for a Supplied ClassifierGeneral Motors · ONSITE INTERVIEW
- Minimum Meeting Rooms Without a Sweep LineGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Top K Frequent ElementsCapgemini · ONSITE INTERVIEWCapgemini interview questions
- Kth Largest Element in an ArrayOneTrust · ONSITE INTERVIEW