Bear and Candies 123 Codechef Solution: Bears love candies and games involving eating them. Limak and Bob play the following game. Limak eats 1 candy, then Bob eats 2 candies, then Limak eats 3 candies, then Bob eats 4 candies, and so on. Once someone can’t eat what he is supposed to eat, he loses.
Limak can eat at most A candies in total (otherwise he would become sick), while Bob can eat at most B candies in total. Who will win the game? Print “Limak” or “Bob” accordingly.
Input
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The only line of each test case contains two integers A and B denoting the maximum possible number of candies Limak can eat and the maximum possible number of candies Bob can eat respectively.
Output
For each test case, output a single line containing one string — the name of the winner (“Limak” or “Bob” without the quotes).
Constraints
- 1 ≤ T ≤ 1000
- 1 ≤ A, B ≤ 1000
Sample Input 1
10 3 2 4 2 1 1 1 2 1 3 9 3 9 11 9 12 9 1000 8 11
Sample Output 1
Bob Limak Limak Bob Bob Limak Limak Bob Bob Bob
Explanation
Test case 1. We have A = 3 and B = 2. Limak eats 1 candy first, and then Bob eats 2 candies. Then Limak is supposed to eat 3 candies but that would mean 1 + 3 = 4 candies in total. It’s impossible because he can eat at most A candies, so he loses. Bob wins, and so we print “Bob”.
Test case 2. Now we have A = 4 and B = 2. Limak eats 1 candy first, and then Bob eats 2 candies, then Limak eats 3 candies (he has 1 + 3 = 4 candies in total, which is allowed because it doesn’t exceed A). Now Bob should eat 4 candies but he can’t eat even a single one (he already ate 2 candies). Bob loses and Limak is the winner.
Test case 8. We have A = 9 and B = 12. The game looks as follows:
- Limak eats 1 candy.
- Bob eats 2 candies.
- Limak eats 3 candies (4 in total).
- Bob eats 4 candies (6 in total).
- Limak eats 5 candies (9 in total).
- Bob eats 6 candies (12 in total).
- Limak is supposed to eat 7 candies but he can’t — that would exceed A. Bob wins.
Bear and Candies 123 CodeChef Solution in JAVA
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; class practice { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int t= sc.nextInt(); while(t -- > 0) { int a = sc.nextInt(); int b = sc.nextInt(); for(int i=0; i<1000; i++) { if(i % 2 == 1) { a=a-i; if(a < 0) { System.out.println("Bob"); break; } } else { b=b-i; if(b < 0) { System.out.println("Limak"); break; } } } } } }
Bear and Candies 123 CodeChef Solution in CPP
#include <iostream> using namespace std; int main() { // your code goes here int t; cin>>t; while(t--){ int a,k; int b; cin>>a>>b; int i=1; int j=0; int counter1=0; int counter2=0; int t1=1; int t2=0; while(t1<=a){ counter1++; i=i+2; t1=t1+i; } while(t2<=b){ counter2++; j=j+2; t2=t2+j; } k=counter2-1; if(counter1>k){ cout<<"Limak"<<endl; } else { cout<<"Bob"<<endl; } } return 0; }
Bear and Candies 123 CodeChef Solution in Python
t = int(input()) for i in range(t): a,b = map(int,input().split()) a_1 = 0 b_1 = 0 temp = 1 while True: if temp%2 == 1: if a_1+temp > a: print('Bob') break a_1 += temp temp += 1 else: if b_1 + temp > b: print('Limak') break b_1 += temp temp +=1
Disclaimer: The above Problem (Bear and Candies 123) is generated by CodeChef but the solution is provided by Chase2learn.This tutorial is only for Educational and Learning purpose.