-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMax No. of Balls in Box.cpp
40 lines (39 loc) · 1.09 KB
/
Max No. of Balls in Box.cpp
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
class Solution {
public:
int countBalls(int lowLimit, int highLimit) {
vector<int> ans;
int max=0;
for(int i=lowLimit; i<=highLimit; i++){
int n=i;
int sum=0;
while(n != 0){
sum+=int(n%10);
n= int(n/10);
}
ans.push_back(sum);
}
sort(ans.begin(), ans.end());
for(int i=0;i<ans.size(); ){
int countn = count(ans.begin(),ans.end(),ans[i]);
(max < countn)? max=countn : max=max;
i+=countn;
}
return max;
}
};
--------------------------------------------------------------------------------------------------------------------------------------------
class Solution {
public:
int countBalls(int lowLimit, int highLimit) {
int ans[45]={0};
for(int i=lowLimit; i<=highLimit; i++){
int n=i;
int sum=0;
while(n != 0){
sum+=int(n%10);
n= int(n/10);
}ans[sum-1]++;
}
return *max_element(ans, ans+45);
}
};