-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1679.cpp
More file actions
28 lines (28 loc) · 766 Bytes
/
1679.cpp
File metadata and controls
28 lines (28 loc) · 766 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
public:
Solution() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
}
int maxOperations(vector<int>& nums, int k) {
sort(nums.begin(), nums.end());
int left = 0;
int right = nums.size() - 1;
int result = 0;
while (left < right) {
if (nums[left] + nums[right] == k) {
result++;
left++;
right--;
}
else if (nums[left] + nums[right] > k) {
while(nums[left] + nums[right] > k && left < right && nums[right] > k - nums[left]) {
right--;
}
}
else {
left++;
}
}
return result;
}
};