forked from Ashish-kumar7/geeks-for-geeks-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRulling Pair.cpp
More file actions
41 lines (31 loc) · 839 Bytes
/
Rulling Pair.cpp
File metadata and controls
41 lines (31 loc) · 839 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
34
35
36
37
38
39
40
41
class Solution{
int val(int x){
int sum=0;
while(x>0){
sum+=(x%10);
x=x/10;
}
return sum;
}
public:
int RulingPair(vector<int> arr, int n)
{
// Your code goes here
unordered_map<int,vector<int>>mp;
for(auto it : arr){
int temp=val(it);
mp[temp].push_back(it);
}
int maxi=-1;
for(auto it=mp.begin();it!=mp.end();it++){
vector<int>val=it->second;
if(val.size()>1){
sort(val.begin(),val.end());
int sum=0;
sum+= ( val[val.size()-1] + val[val.size()-2] );
maxi=max(maxi,sum);
}
}
return maxi;
}
};