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 New World problem solution

YASH PAL, 31 July 2024
In this HackerEarth New World problem solution, The time has arrived when the world is going to end. But don’t worry, because the new world yuga will start soon. Manu (carrier of mankind) has been assigned the job to carry all the necessary elements of the current yuga to the upcoming yuga.
There are N stones arranged in a straight line. In order to fulfill the task, Manu has to travel from the first stone to the last one in a very specific way. First of all, he has to do it using exactly K jumps. In a single jump, he can jump from a stone with coordinate xi to a stone with coordinate xj if and only if xi < xj. We denote the value xj – xi as the length of such a jump.
Your task is to help Manu minimize the maximum length of a jump he makes in his journey, in order to reach the last stone in exactly K jumps and save mankind. This is the answer you have to provide.
HackerEarth New World problem solution

HackerEarth New World problem solution.

#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(false);cin.tie(0);
using namespace std;
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define mp make_pair
#define all(a) a.begin(),a.end()
#define bitcnt(x) __builtin_popcountll(x)
#define MOD 1000000007
#define BASE 31
typedef unsigned long long int uint64;
typedef long long int int64;

int stone[100005];

bool chck(int dist,int n,int k){
int req=0,cur=0,pre=0;

for(int i=0;i<n;i++){
while((cur!=n)&&(stone[cur]-stone[pre]<=dist))
cur++;
req++;
if(cur==n)
break;
pre=cur-1;
}
if(cur!=n)
return false;
if(req<=k)
return true;
return false;
}

int main(){
int t,n,k,i;
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
cin>>t;
while(t--){
cin>>n>>k;
for(i=0;i<n;i++)
cin>>stone[i];

int lo=1,hgh=1e9,ans=-1;

while(lo<=hgh){
int mid=(lo+hgh);
mid/=2;
if(chck(mid,n,k)){
ans=mid;
hgh=mid-1;
}
else{
lo=mid+1;
}
}
cout<<ans<<endl;
}
fclose(stdout);
return 0;
}
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