Skip to content

Latest commit

 

History

History
32 lines (25 loc) · 665 Bytes

1679.Max-Number-of-K-Sum-Pairs.md

File metadata and controls

32 lines (25 loc) · 665 Bytes

1679. Max Number of K-Sum Pairs

Difficulty: Medium

URL

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

Solution

Approach 1: Sliding Window

The code is shown below:

class Solution:
    def maxOperations(self, nums: List[int], k: int) -> int:
        nums.sort()
        left = 0
        right = len(nums) - 1
        count = 0
        while left < right:
            if nums[left] + nums[right] == k:
                count += 1
                left += 1
                right -= 1
            elif nums[left] + nums[right] < k:
                left += 1
            else:
                right -= 1
        return count