max oprations
import java.util.Arrays;
class Solution {
public int maxOperations(int[] nums, int k) {
// Sort the array to apply two-pointer technique
Arrays.sort(nums);
int start = 0; // Pointer from the beginning
int end = nums.length - 1; // Pointer from the end
int totalPairs = 0; // Counter to store the number of valid pairs
while (start < end) {
int currentSum = nums[start] + nums[end];
if (currentSum == k) {
// Found a valid pair
totalPairs++;
start++;
end--;
} else if (currentSum < k) {
// Need a bigger sum, so move the start pointer forward
start++;
} else {
// Need a smaller sum, so move the end pointer backward
end--;
}
}
return totalPairs;
}
}