1679.cpp (430B)
1 class Solution { 2 public: 3 int maxOperations(vector<int> &nums, const int k) { 4 sort(nums.begin(), nums.end()); 5 int i = 0, j = nums.size() - 1, res = 0; 6 while (i < j) { 7 const int sum = nums[i] + nums[j]; 8 if (sum == k) 9 i++, j--, res++; 10 else if (sum < k) 11 i++; 12 else 13 j--; 14 } 15 return res; 16 } 17 };