Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
      • Data Structures Solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Divide the digits problem solution

YASH PAL, 31 July 202415 February 2026
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 HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

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