Skip to content
Decoder

Max Number of K-Sum Pairs

Medium

Problem

You are given a list of integers nums and an integer k. In one operation you pick two numbers whose sum equals `k` and remove them from the list. Return the maximum number of operations you can perform. Each element can be used at most once.

Example 1

Input
nums = [1, 2, 3, 4], k = 5
Output
2
Why
remove 1 and 4, then 2 and 3

Example 2

Input
nums = [3, 1, 3, 4, 3], k = 6
Output
1
Why
remove two 3s; the last 3 has no partner

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^9
  • 1 <= k <= 10^9

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.