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 Litte Jhool and World Tour problem solution

YASH PAL, 31 July 2024
In this HackerEarth Litte Jhool and World Tour problem solution Little Jhool is just not a smart algorithmic coder, he regularly contributes to open source, too. This year, he’s managed to be selected for a project in the prestigious Google Summer of Code, too. More than the project, he’s happy about the money he’s going to receive. He’s already started day dreaming about what he will do with the money, – he’s settled on for going to a tour of couple of countries. Now, he’s a bit confused about which countries he wants to consider, so he buys a globe, and starts rotating it like some boss.
The globe has n different countries on it, and let’s say they are labeled as 0, 1, 2, 3, 4… n-1, but since it is a globe, after n-1, country order follows again. That is to say, if there are n countries – 0, 1, 2, 3, 4, . . . , n-1, then, their arrangement would be: first 0, then 1, then 2, …, then n-1, and then again 0…
Let us take m = 6, then {4, 5, 0} can be a valid range of countries, {2} can be a valid range, too, {3, 4, 5, 0, 1, 2} is a valid range, too. Since it is a globe, the range can wrap around itself.
Now, his girlfriend decides a range of countries. Since, his girlfriend is confusing him a lot, he just wants to pick up ONLY ONE country from a range. He does NOT want to any country to be chosen more than once for different ranges.
Given number of countries, and number of ranges (And their range!) – help Little Jhool figure out if he’ll be able to do this.
HackerEarth Litte Jhool and World Tour problem solution

HackerEarth Litte Jhool and World Tour problem solution.

#include <bits/stdc++.h>

using namespace std;

typedef pair <int,int> PII;
typedef vector <int> VI;
typedef vector < PII > VPII;

int main()
{
int tc;
scanf("%d",&tc);
while (tc--)
{
int m, n;
scanf("%d %d",&m, &n);

VPII segm;
for(int i=0;i<n;i++)
{
int st, en;
scanf("%d %d",&st, &en);

if(st<=en)
{
segm.push_back(make_pair(st,en));
segm.push_back(make_pair(st+m,en+m));
}

else
segm.push_back(make_pair(st,en+m));
}

if(n>m)
{
puts("NO");
continue;
}

sort(segm.begin(),segm.end());

int T=0;
int i=0;

set < PII > que;

bool ok = true;

while(true)
{
if(que.empty())
{
if(i==segm.size())
break;
else
T = segm[i].first;
}

while(i<segm.size() && segm[i].first==T)
{
que.insert(make_pair(segm[i].second,i));
i++;
}

int ind = que.begin()->second;
que.erase(que.begin());

if(!(T>=segm[ind].first && T<=segm[ind].second))
{
ok = false;
break;
}
T++;
}
puts(ok?"YES":"NO");
}
}
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