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 Little Jhool and his punishment problem solution

YASH PAL, 31 July 2024
In this HackerEarth Little Jhool and his punishment problem solution Little Jhool was was the most intelligent kid of his school. But he did NOT like his class teacher, his school, and the way he was taught things in school. Well, he was a rebel, to say the least. Like most of us, he hated being punished by his teacher – specially, when the punishment was to sit beside someone of the opposite gender. (Oh, he was a kid, come on!)
There are n number of students in the class, b being the number of boys, and g being the number of girls. Little Jhool decided to challenge his class teacher when she started punishing the entire class using the aforesaid mentioned punishment – making them sit in a straight line outside the class, beside someone of the opposite gender.
The challenge is as follows: Little Jhool selects a student from the class and make him or her sit outside the class. Then, his teacher picks another student from the remaining students, and makes him or her sit next in the line. Little Jhool and teacher then take alternate turns, until all the students of the class are seated.
If the number of pairs of neighboring students of the same gender is GREATER than the number of pairs of neighboring students of the opposite gender, output “Little Jhool wins!” , otherwise, “The teacher wins!”
HackerEarth Little Jhool and his punishment problem solution

HackerEarth Little Jhool and his punishment problem solution.

tc = int(raw_input())
while (tc>0):
tc = tc - 1
n = int(raw_input())
b, g = sorted(map(int, raw_input().split()))
if g-1>b:
print "Little Jhool wins!"
else:
print "The teacher wins!"

Second solution

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


bool f(string A)
{
int a=0,b=0;
int n = A.size();
for(int i=0; i<n-1;i++)
{
if(A[i]==A[i+1])a++; else b++;
}
return (a>b);
}

int main()
{
int t; cin>>t;
while(t--)
{
int n,b,g;
cin>>n>>b>>g;
string A;
int ans=0;

A="b";
int B=b-1; int G=g;

for(int i=0; i<n-1;i++)
{
if(i%2==0)
{
if(A[i]=='b')
{
if(G)
{
A+='g'; G--;
}
else
{
B--; A+='b';
}
}
else
{
if(B)
{
A+='b'; B--;
}
else
{
A+='g'; G--;
}
}
}
else
{
if(A[i]=='g')
{
if(G)
{
A+='g'; G--;
}
else
{
B--; A+='b';
}
}
else
{
if(B)
{
A+='b'; B--;
}
else
{
A+='g'; G--;
}
}
}
}
if(f(A))ans=1;
A="g";
B=b;G=g-1;
for(int i=0; i<n-1;i++)
{
if(i%2==0)
{
if(A[i]=='b')
{
if(G)
{
A+='g'; G--;
}
else
{
B--; A+='b';
}
}
else
{
if(B)
{
A+='b'; B--;
}
else
{
A+='g'; G--;
}
}
}
else
{
if(A[i]=='g')
{
if(G)
{
A+='g'; G--;
}
else
{
B--; A+='b';
}
}
else
{
if(B)
{
A+='b'; B--;
}
else
{
A+='g'; G--;
}
}
}
}
if(f(A))ans=1;
if(ans) cout<<"Little Jhool wins!n";
else cout<<"The teacher wins!n";
}
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