Leetcode in Java && Oracle
424 subscribers
8 photos
397 files
400 links
Second channel: @codeforces_java

Let's Develop Together!
Download Telegram
image_2025-12-16_00-53-41.png
21.3 KB
#N1679. Max Number of K-Sum Pairs
problem link
#solution
TC - O(n)
SC - O(n)
class Solution {
public int maxOperations(int[] nums, int k) {
int count = 0;
Map<Integer, Integer> map = new HashMap<>();
for(int n: nums){
if(map.containsKey(n)){
count++;
map.put(n, map.get(n) -1);
if(map.get(n)==0) map.remove(n);
}else if(n<k){
map.put(k-n, map.getOrDefault(k-n, 0) +1);
}
}
return count;
}
}