Page Contents
Alternate Additions Solution Codechef
Chef has 2 numbers A and B (A<B).
Chef will perform some operations on A.
In the ith operation:
- Chef will add 1 to A if i is odd.
- Chef will add 2 to A if i is even.
Chef can stop at any instant. Can Chef make A equal to B?
Input Format
- The first line contains a single integer T — the number of test cases. Then the test cases follow.
- The first and only line of each test case contains two space separated integers A and B.
Output Format
For each test case, output YES
if Chef can make A and B equal, NO
otherwise.
Note that the checker is case-insensitive. So, YES
, Yes
, yEs
are all considered same.
Constraints
- 1≤T≤1000
- 1≤A<B≤109
Sample 1:
Input
4 1 2 3 6 4 9 10 20
Output
YES YES NO YES
Explanation:

SOLUTION
Program: Alternate Additions Solution in Python
for _ in range(int(input())): a,b = map(int,input().split(' ')) N = b-a if(N%3==0 or N%3==1): print('yes') else: print('No')
Program: Alternate Additions Solution in C++
#include <iostream> using namespace std; int main() { int t; cin>>t; while(t--) { int a,b; cin>>a>>b; if(b-a<0) { cout<<"NO"<<endl; } else { if((b-a)%3==0||(b-a-1)%3==0) { cout<<"YES"<<endl; } else { cout<<"NO"<<endl; } } } return 0; }
Program: Alternate Additions Solution in Java
import java.util.*; public class Main { 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(); if(Math.abs(b-a)%3!=1 && Math.abs(b-a)%3!=0) System.out.println("NO"); else System.out.println("YES"); } } }
Related:
- Subscriptions Solution Codechef
- Alternate Additions Solution Codechef
- Equal Strings Solution Codechef
- Divisible by i Solution Codechef
- Possible GCD Solution Codechef
- Expected move Solution Codechef
- Reduce to zero Solution Codechef
- Full Path Eraser Solution Codechef