Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerEarth Divide the digits problem solution

YASH PAL, 31 July 2024
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:
  1. The sum of frequency of each digit in X and Y is equal to frequency of that digit in N.
  2. 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

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])))
coding problems solutions

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes