Skip to content
Programming101
Programmingoneonone
  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • 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
Programming101
Programmingoneonone

HackerEarth The furious five problem solution

YASH PAL, 31 July 2024
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

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes