Skip to content
Programmingoneonone
Programmingoneonone
  • 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

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

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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