Bear and Ladder Codechef Solution

Hello coders, today we are going to solve Bear and Ladder Codechef Solution.

Bear and Ladder Codechef Solution
Bear and Ladder Codechef Solution

Problem

Bearland has infinitely many cities, numbered starting from 1. Some pairs of cities are connected with bidirectional roads:

  • There are roads 1-2, 3-4, 5-6, 7-8, and so on (there is a road between cities 2*i+1 and 2*i+2 for every non-negative integer i).
  • There are roads 1-3, 3-5, 5-7, 7-9, … (between every two consecutive odd numbers).
  • There are roads 2-4, 4-6, 6-8, 8-10, … (between every two consecutive even numbers).

This is how the first few cities and roads between them look like:

Bear and Ladder Codechef Solution

You are given Q queries. In each query, for the given pair of different cities a and b, you should check if there is a road between them. For each query, print “YES” or “NO” accordingly.

Input

The first line of the input contains an integer Q, denoting the number of queries.

Each of the following Q lines contains two distinct integers a and b, denoting two cities in one query.

Output

For each query, output a single line containing the answer — “YES” if there is a road between the given cities a and b, and “NO” otherwise (without the quotes).

Constraints

  • 1 ≤ Q ≤ 1000
  • 1 ≤ ab ≤ 109
  • a ≠ b

Sample Input 1 

7
1 4
4 3
5 4
10 12
1 3
999999999 1000000000
17 2384823

Sample Output 1 

NO
YES
NO
YES
YES
YES
NO

Explanation

In the example test, the answer is “YES” for pairs (4, 3), (10, 12), (1, 3) and (999999999, 1000000000). Roads 3-4 and 1-3 you can see on the drawing in the statement.

The answer is “NO” for example for a pair (1, 4), because there is no road between cities 1 and 4.

Bear and Ladder CodeChef Solution in JAVA

import java.util.*;
import java.lang.*;
import java.io.*;
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner sc=new Scanner(System.in);
		int t=sc.nextInt();
		while(t-->0){
		    int a=sc.nextInt();
		    int b=sc.nextInt();
		    if(a%2!=0){
		        if(a-2==b || a+1==b ||a+2==b){
		            System.out.println("YES");
		        }else{
		            System.out.println("NO");
		        }
		    }
		    else if(a%2==0){
		        if(a-2==b || a-1==b || a+2==b){
		            System.out.println("YES");
		        }else{
		            System.out.println("NO");
		        }
		    }
		}
	}
}

Bear and Ladder CodeChef Solution in CPP

#include<bits/stdc++.h>
using namespace std;
int main(){
    int tc;
    cin>>tc;
    while(tc--){
        int a,b;
        cin>>a>>b;
        if(a>b) swap(a,b);
        if(a+2==b){
            cout<<"YES"<<endl;
        }else if(a+1==b && a%2==1){
            cout<<"YES"<<endl;
        }else{
            cout<<"NO"<<endl;
        }
    }
    return 0;
}

Breaking Bricks CodeChef Solution in Python

T = int(input())
for i in range(T):
    (a,b) = map(int,input().split())
    if((a%2 == 0 and b%2 == 0) or (a%2 != 0 and b%2 != 0)):
        if(a == b+2 or b == a+2):
            print('YES')
        else:
            print('NO')
    else:
        if((a%2 == 0 and b == a-1) or (b%2 == 0 and a == b-1)):
            print('YES')
        else:
            print('NO')

Disclaimer: The above Problem (Bear and Ladder) is generated by CodeChef but the solution is provided by Chase2learn.This tutorial is only for Educational and Learning purpose.

Sharing Is Caring

Leave a Comment