1679. Max Number of K-Sum Pairs || 100 % Faster






class Solution {
public:
    int maxOperations(vector<int>& nums, int k) {
        sort(nums.begin(), nums.end());
        int start = 0, end = nums.size() - 1, ct_pairs = 0;
        while(start < end) {
            if(nums[start] + nums[end] == k){
                ct_pairs++;
                start++;
                end--;
            }
            else if(nums[start] + nums[end]< k){
                start++;
            }
            else{
                end--;
            }
        }
        return ct_pairs;
    }
};

Comments (0)