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
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth The furious five problem solution

YASH PAL, 31 July 202415 February 2026
In this HackerEarth The furious five problem-solution You are given an integer N.
 
Write a program to find a minimum number P such that 1 <= X <= P, Sigma F(X) >= N ( where F(X) represents the number of times X can be divided by 5 ).
 
 
HackerEarth The furious five problem solution

 

 

HackerEarth The furious five problem solution.

#include <bits/stdc++.h>
#define ll long long

ll minm(ll a, ll b)
{
return (a < b) ? a : b;
}

ll get(ll num)
{
ll ret = 0;
while(num != 0) {
ret += num/5;
num /= 5;
}
return ret;
}

int main()
{
int t;
scanf("%d", &t);
while(t--) {
int n;
ll low, mid, high;
scanf("%d", &n);
low = 0, high = 1e18;
ll ans = high;
while(low <= high) {
mid = (low+high)/2;
ll val = get(mid);
if(val >= n) {
high = mid-1;
ans = minm(ans, mid);
} else {
low = mid+1;
}
}
printf("%lldn", ans);
}
return 0;
}
 

Second solution

#include<bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(i= a ; i < b ; ++i)
#define rep(i,n) FOR(i,0,n)
#define INF INT_MAX
#define pb push_back
#define si(n) scanf("%d",&n)
#define pln(n) printf("%lldn",n)
#define pl(n) printf("%lld ",n)
#define sl(n) scanf("%lld",&n)
#define mod (int)(1e9 + 7)
#define ll long long int

inline ll helper(ll n)
{
ll ans=0,calc;
ans+=n;
calc=5;
while(n/calc!=0)
{
ans+=(n/calc);
calc*=5;
}
return ans;
}
int main()
{
int t,i;
ll calc,s=0,n,cnt,low,high,mid,c1,c2;
si(t);
assert(t>=1 && t<=100000);
while(t--)
{
sl(n);
assert(n>=1 && n<=1000000000);
low=0;
high=mod;
while(low<=high)
{
mid=(low+high)/2;
c1=helper(mid);
c2=helper(mid+1);
if(c1<n && c2>=n)
{
printf("%lldn",5*(mid+1));
break;
}
else if(n>c1)
low=mid+1;
else
high=mid-1;
}
}
return 0;
}
 
 
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