-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathMinimum addition.cpp
65 lines (53 loc) · 1.23 KB
/
Minimum addition.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
Solution by Rahul Surana
***********************************************************
Given N make 2 numbers from all digits of N either in number 1 or number 2 and there sum should be minimum
***********************************************************
*/
#include<bits/stdc++.h>
using namespace std;
int nums[10];
long long solve (long long N) {
long long k = N;
int count = 0;
while(k>0){
int x = k%10;
nums[x]++;
k/=10;
count++;
}
long long x=0,y=0;
bool xf = true;
for(int i = 0;i<10;i++){
if(nums[i] == 0) continue;
while(nums[i]>0){
if(xf){
x=((x*10)+i);
nums[i]--;
xf = false;
}
if(nums[i] == 0) break;
if(!xf){
y = ((y*10)+i);
nums[i]--;
xf = true;
}
}
}
return x+y;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
for(int t_i = 0; t_i < T; t_i++)
{
long long N;
cin >> N;
long long out_;
out_ = solve(N);
cout << out_;
cout << "\n";
}
}