FastPrepTop K Pair Sums From One Array
Problems/Top K Pair Sums From One Array

Top K Pair Sums From One Array

Google logoGoogle● MediumNEW GRADONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The source directly provides all two-number combinations from one integer array, preservation of repeats, and a Top K largest pair-sum follow-up using sorting and a max heap with duplicate-state avoidance. Distinct-index semantics, output ordering, 64-bit sums, examples, and bounds are practice scaffolding. The judged core matches the visible source at about 89%.

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 problems

  • Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
  • Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
  • Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public long[] topKPairSums(int[] nums, int k) {
        // write your code here
    }
}
Checking account…