Remove as Many Target-Sum Pairs as Possible. One operation removes two distinct occurrences from nums whose values sum to k. Each occurrence can be removed only once. Return the maximum number of operations.
### Problem
One operation removes two distinct occurrences from `nums` whose values sum to `k`. Each occurrence can be removed only once. Return the maximum number of operations.
```cpp
class Solution {
public:
int maxOperations(vector<int>& nums, int k);
};
```
### Examples
**Example 1:**
```text
nums = [2,4,2,4,4], k = 6
Output: 2
Explanation: Match each of the two 2s with one 4.
```
**Example 2:**
```text
nums = [5,5,5,1], k = 10
Output: 1
Explanation: Two of the three 5 occurrences form one pair.
```
### Constraints
- `1 <= nums.length <= 10^5`.
- `1 <= nums[i] <= 10^9`.
- `1 <= k <= 10^9`.
### Clarifying Questions
Pairs need not be adjacent. Equal-valued occurrences may pair when their doubled value equals k, but one occurrence cannot pair with itself.
```hint Inspect the structure
Can two equal values represent different removable occurrences, and when would an occurrence be reused illegally?
```
### Reasoning checkpoints
- Use two distinct occurrences per operation and never reuse an occurrence.
- Prove the maximum count, including the case when both required pair values are equal.
### Optional extensions
How would sorting and two pointers achieve the same count without hashing?
Overview: Remove as Many Target-Sum Pairs as Possible. One operation removes two distinct occurrences from nums whose values sum to k. Each occurrence can be removed only once. Return the maximum number of operations.
One operation removes two distinct occurrences from nums whose values sum to k. Each occurrence can be removed only once. Return the maximum number of operations.
class Solution {
public:
int maxOperations(vector<int>& nums, int k);
};
Examples
Example 1:
nums = [2,4,2,4,4], k = 6
Output: 2
Explanation: Match each of the two 2s with one 4.
Example 2:
nums = [5,5,5,1], k = 10
Output: 1
Explanation: Two of the three 5 occurrences form one pair.
Constraints
1 <= nums.length <= 10^5
.
1 <= nums[i] <= 10^9
.
1 <= k <= 10^9
.
Clarifying Questions Guidance
Pairs need not be adjacent. Equal-valued occurrences may pair when their doubled value equals k, but one occurrence cannot pair with itself.
Reasoning checkpoints
Use two distinct occurrences per operation and never reuse an occurrence.
Prove the maximum count, including the case when both required pair values are equal.
Optional extensions
How would sorting and two pointers achieve the same count without hashing?