2Brute2Best
🔥0

Max Number of K-Sum Pairs

Medium·LeetCode #1679 Pro OnlySlight Variation
LeetCode
01

Problem Statement

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

In one operation you pick two numbers from the array whose sum is exactly k and remove both of them. A removed number can't be used again.

Return the largest number of operations you can do.

Input

An integer array nums and an integer k.

Output

The maximum number of operations, i.e. the most disjoint pairs that each sum to k.

Example 1
Input: nums = [1, 2, 3, 4], k = 5
Output: 2

Remove 1 and 4, then remove 2 and 3. Nothing is left.

Example 2
Input: nums = [3, 1, 3, 4, 3], k = 6
Output: 1

Remove two of the 3s. The third 3 has no partner left, and 1 and 4 have no partners at all.

Constraints
  • • 1 ≤ nums.length ≤ 10^5
  • • 1 ≤ nums[i] ≤ 10^9
  • • 1 ≤ k ≤ 10^9
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“two numbers whose sum equals k”
→ The partner of `x` is fixed: `k − x`. Complement lookup again.
“remove them from the array”
→ Each number is used once. The map must track how many copies are **left**, not just whether a value was seen.
“maximum number of operations”
→ Count disjoint pairs. Pairing greedily works because copies of the same value are interchangeable.
03

Approach Complexity Ladder

Brute Force — find a partner for each numberO(N²)O(N)

For each unused number, scan for an unused partner; mark both used

Better — sort + two pointersO(N log N)O(1)

Sort, then pair the ends: equal → take both, too small → left++, too big → right--

Optimal — count map of waiting numbersO(N)O(N)

If cnt[k − x] > 0, pair and decrement; else cnt[x]++

Brute Force Intuition

Keep a used flag for every position. For each unused nums[i], scan the rest of the array for an unused nums[j] with nums[i] + nums[j] == k. If found, mark both used and count one operation.

Recognition rule for this sub-pattern:

For each element, ask "what partner do I need?" and check if that partner is already in the map.
Remember this · Arrays & Hashing
Trigger
"Have I seen X before?", pairs that add up to a target, grouping things that look alike, counting occurrences.
Think
Instead of scanning again for each element, can I remember earlier elements in a map?
Key question
What exactly do I store as the key, and what as the value?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Arrays & Hashing translate into production architectures and low-level memory mechanics.