-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxSumofpairwithequaldigits.java
More file actions
33 lines (33 loc) · 993 Bytes
/
Copy pathmaxSumofpairwithequaldigits.java
File metadata and controls
33 lines (33 loc) · 993 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
29
30
31
32
33
class Solution {
// Using Heaps and Map Striver Sheet Solutions
public static int getSum(int element){
int sum=0;
while(element>0){
sum+=element%10;
element=element/10;
}
return sum;
}
public int maximumSum(int[] nums) {
HashMap<Integer,PriorityQueue<Integer>> map=new HashMap<>();
for(int ele:nums){
int digitSum=getSum(ele);
if(map.containsKey(digitSum)){
map.get(digitSum).add(ele);
}
else{
PriorityQueue<Integer> pq=new PriorityQueue<>(Collections.reverseOrder());
pq.add(ele);
map.put(digitSum,pq);
}
}
int Ans = -1;
for (PriorityQueue<Integer> pq : map.values()) {
if (pq.size() >= 2) {
int maxSum = pq.poll() + pq.poll();
Ans = Math.max(Ans, maxSum);
}
}
return Ans;
}
}