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

YASH PAL, 31 July 202416 February 2026
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 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