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 Beautiful numbers problem solution

YASH PAL, 31 July 2024
In this HackerEarth Beautiful numbers problem solution, Any number is called beautiful if it consists of 2N digits and the sum of the first N digits is equal to the sum of the last N digits. Your task is to find the count of beautiful numbers in the interval from L to R (including L and R).
Beautiful numbers do not have leading zeroes.
HackerEarth Beautiful numbers problem solution

HackerEarth Beautiful numbers problem solution.

#include<bits/stdc++.h>
using namespace std;

#define F first
#define S second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define vi vector<int>
#define all(x) x.begin(),x.end()
#define fix fixed<<setprecision(10)
#define rep(i,a,b) for(int i=int(a);i<=int(b);i++)
#define repb(i,b,a) for(int i=int(b);i>=int(a);i--)
#define FastIO ios_base::sync_with_stdio(0),cin.tie(0)

typedef double db;
typedef long long ll;

const int N=2e5+5;
const int mod=1e9+7;

vi sum[50][10],v;

void pre(){
rep(i,1,9999){
int t=i,s=0,d=0;
while(t){
s+=(t%10);
t/=10;
d++;
}
rep(j,d,4) sum[s][j].pb(i);
}
rep(i,1,9999){
int t=i,s=0,d=0;
while(t){
s+=(t%10);
t/=10;
d++;
}
for(int u:sum[s][d]){
string s=to_string(i);
string t=to_string(u);
while(t.size()<d) t="0"+t;
s+=t;
v.pb(stoi(s));
}
}
sort(all(v));
}

void solve(){
int l,r;
cin>>l>>r;
cout<<ub(all(v),r)-lb(all(v),l)<<'n';
}

signed main(){
FastIO;
pre();
int t;
cin>>t;
while(t--) solve();
return 0;
}

Second solution

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int D = 5, MXS = 1000, N = 1e4;
vector<int> v[D][MXS], ans;

int main() {
ios::sync_with_stdio(0), cin.tie(0);
for (int i = 1; i < N; ++i) {
string t = to_string(i);
v[t.size()][accumulate(t.begin(), t.end(), 0) - t.size() * '0'].push_back(i);
}
for (int d = 1; d < D; ++d)
for (int s = 0; s < MXS; ++s)
for (auto l : v[d][s])
for (int dd = 0; dd <= d; ++dd)
for (auto r : v[dd][s])
ans.push_back(l * (int) pow(10, d) + r);
sort(ans.begin(), ans.end());
int t;
cin >> t;
while (t--) {
int l, r;
cin >> l >> r;
++r;
cout << lower_bound(ans.begin(), ans.end(), r) - lower_bound(ans.begin(), ans.end(), l) << 'n';
}
}
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