-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdigitSum.cpp
More file actions
47 lines (34 loc) · 760 Bytes
/
Copy pathdigitSum.cpp
File metadata and controls
47 lines (34 loc) · 760 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
42
43
44
45
46
47
class Solution {
public:
int findMax(int n) {
// code Here
int temp = n;
int sum = 0;
vector<int>v;
while(temp){
v.push_back(temp%10);
sum+=(temp%10);
temp/=10;
}
reverse(v.begin(),v.end());
int pre = 0;
for(int i=1;i<v.size();i++){
if(v[i]==9)continue;
else if(pre)v[i]=9;
else{
v[i-1]--;
v[i] = 9;
pre=1;
}
}
int n2 = 0;
int sum2 = 0;
for(int i=0;i<v.size();i++){
sum2+=v[i];
n2*=10;
n2+=v[i];
}
if(sum2>sum)return n2;
else return n;
}
};