# Max Number K Sum Pairs

````cpp
// https://leetcode.com/problems/max-number-of-k-sum-pairs

/*
You are given an integer array nums and an integer k.
In one operation, you can pick two numbers from the array whose sum equals k and remove them from the array.
Return the maximum number of operations you can perform on the array.

Ex1:
Input: nums = [1,2,3,4], k = 5
Output: 2
Explanation: Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.

Ex2:
Input: nums = [3,1,3,4,3], k = 6
Output: 1
Explanation: Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.
*/

#include <vector>
#include <unordered_map>
#include <cassert>
#include <iostream>

using namespace std;

// Time complexity: O(n), Space complexity: O(n)
int maxOperations(vector<int>& nums, int k) {
    if (nums.empty()) {
        return 0;
    }

    unordered_map<int, int> m;
    int n = nums.size();
    for (int i = 0; i < n; i++) {
        m[nums[i]]++;
    }

    int res = 0;
    for (auto i : m) {
        int val = i.first;
        int cnt = i.second;
        if (cnt <= 0) continue;

        if (m.count(k-val) && m[k-val] > 0) {
            if (k-val == val) {
                res += cnt / 2;
                m[k-val] = 0;
            } else {
                res += min(cnt, m[k-val]);
                // Don't forget to clear all values to 0.
                m[k-val] = 0;
                m[val] = 0;
            }
        }
    }

    return res;
}

// Two pointers
// Time complexity: O(nlogn), Space complexity: O(1)
int maxOperationsTwoPointer(vector<int>& nums, int k) {
    if (nums.empty()) {
        return 0;
    }

    sort(nums.begin(), nums.end());
    int res = 0;
    int n = nums.size();

    int l = 0, r = n-1;

    while (l < r) {
        int sum = nums[l] + nums[r];
        if (sum > k) {
            r--;
        } else if (sum < k) {
            l++;
        } else {
            res++;
            l++;
            r--;
        }
    }

    return res;
}

int main() {
    vector<int> nums {1,2,3,4};
    int k = 5;
    assert(maxOperations(nums, k) == 2);
    assert(maxOperationsTwoPointer(nums, k) == 2);

    nums = {3,1,3,4,3};
    k = 6;
    assert(maxOperations(nums, k) == 1);
    assert(maxOperationsTwoPointer(nums, k) == 1);

    nums = {2, 2, 2, 3, 1, 1, 4, 1};
    k = 4;
    assert(maxOperations(nums, k) == 2);
    assert(maxOperationsTwoPointer(nums, k) == 2);

    nums = {2,5,4,4,1,3,4,4,1,4,4,1,2,1,2,2,3,2,4,2};
    k = 3;
    assert(maxOperations(nums, k) == 4);
    assert(maxOperationsTwoPointer(nums, k) == 4);

    return 0;
}```
````


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dongzeli95s-organization.gitbook.io/swe-interview-handbook/algorithm/two_pointers/max_number_k_sum_pairs.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
