-
Notifications
You must be signed in to change notification settings - Fork 5
/
MaxNumberOfKSumPairs.java
44 lines (38 loc) · 1.08 KB
/
MaxNumberOfKSumPairs.java
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*https://leetcode.com/problems/max-number-of-k-sum-pairs/*/
class Solution {
public int maxOperations(int[] nums, int k) {
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
for (int num : nums)
map.put(num,map.getOrDefault(num,0)+1);
// System.out.println(map);
int pairsCount = 0;
for (Map.Entry elem : map.entrySet())
{
int key = (Integer)elem.getKey();
int value = (Integer)elem.getValue();
pairsCount += Math.min(value,map.getOrDefault(k-key,0));
}
return pairsCount/2;
}
}
class Solution {
public int maxOperations(int[] nums, int k)
{
Arrays.sort(nums);
int p1 = 0, p2 = nums.length - 1;
int count = 0;
while(p1 < p2)
{
int sum = nums[p1] + nums[p2];
if(sum == k)
{
++count; ++p1; --p2;
}
else if(sum > k)
--p2;
else
++p1;
}
return count;
}
}