Skip to main content
Back to problems
Leetcode
Medium
Arrays
Hash Maps
Sorting
Amazon
Microsoft
Max Number of K-Sum Pairs

Count the maximum number of disjoint pairs whose sum equals a target value k.

Acceptance 0%
Problem Statement

Max Number of K-Sum Pairs

You are given an integer array nums and an integer k.

In one operation, you may choose two different elements from nums whose sum is exactly k, then remove both elements from the array.

Return the maximum number of operations you can perform.

The same element cannot be used in more than one operation.

Input Format

  • An integer array nums
  • An integer k

Output Format

  • Return an integer: the maximum number of disjoint pairs whose elements sum to k.

Constraints

  • Pairs must use two different elements.
  • Each element can be used at most once.
  • Find the maximum number of valid removals, not just whether one exists.
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [1,2,3,4], k = 5

Output

2

Explanation

The pairs (1,4) and (2,3) both sum to 5, so we can perform 2 operations.

Example 2

Input

nums = [3,1,3,4,3], k = 6

Output

1

Explanation

Only one pair can be formed: (3,3). After using those two 3s, no other valid pair remains.

Show 1 more example

Example 3

Input

nums = [2,5,4,1,3,3], k = 6

Output

2

Explanation

One valid pairing is (2,4) and (3,3), giving 2 operations.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.