Max Number of K-Sum Pairs

MediumTwo Pointershashing

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given an integer array nums and an integer k. In one operation you pick two elements (at different positions) whose sum is exactly k and remove both from the array. Return the maximum number of operations you can perform.

Examples

Input: nums = [1,2,3,4], k = 5
Output: 2
Remove (1, 4), then (2, 3).
Input: nums = [3,1,3,4,3], k = 6
Output: 1
Only one pair of 3s can be removed; the third 3 has no partner left.

Constraints

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

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More hashing problems