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.
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;
}