In this HackerEarth Divide, the digits problem solution You are given a number N. You are required to form two numbers X and Y such that:
- The sum of frequency of each digit in X and Y is equal to frequency of that digit in N.
- The sum of numbers X and Y must be minimum.
Your task is to determine the minimum possible sum of X and Y.
HackerEarth Divide the digits problem solution.
#include<bits/stdc++.h>
#define int long long int
using namespace std;
void solve(){
int n;
cin >> n;
assert(10 <= n and n <= 2000000000000000000);
vector<int>dig;
while(n)
{
dig.push_back(n % 10);
n /= 10;
}
sort(dig.begin(), dig.end());
int sz = dig.size();
int first = 0;
int second = 0;
for(int i = 0 ; i < sz ; i++)
{
if(i % 2)
{
first = (first*10 + dig[i]);
}
else{
second = (second*10 + dig[i]);
}
}
cout << (first + second) << endl;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
assert(1 <= t and t <= 100000);
while(t--){
solve();
}
}
Second solution
t = int(input())
while t > 0:
t -= 1
n = list(input())
n.sort()
print(int(''.join(n[::2])) + int(''.join(n[1::2])))