304 North Cardinal St.
Dorchester Center, MA 02124

# Divisible by 3 CodeChef Solution

## Problem – Divisible by 3 CodeChef Solution

Stack likes the number 3 a lot.

He has two non-negative integers A and B.
In one operation, Stack can do either of the following:

• A:=∣AB∣ (change A to ∣AB∣)
• B:=∣AB∣ (change B to ∣AB∣)

Note that ∣X∣ denotes absolute value of X. For example ∣−7∣=7 and ∣4∣=4.

Find the minimum number of operations after which at least one integer out of A and B becomes divisible by 3.

### Input Format

• The first line of input contains a single 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.

### Output Format

For each test case, output in a single line the minimum number of operations after which at least one integer out of A and B becomes divisible by 3.

• 1≤T≤10^5
• 0≤A,B≤10^9

### Sample 1:

``````Input:
2
0 343
1 1
Output:
0
1``````

### Explanation:

Test case 1: A=0 is already divisible by 3.

Test case 2: In the only operation, Stack can change A=1 to A=∣AB∣=∣1−1∣=0. Now A=0 is divisible by 3.

## Divisible by 3 CodeChef Solution in C++17

``````#include <iostream>
#include<cstdlib>
using namespace std;

int main() {
int t;
cin>>t;
while(t--){
int a,b;
cin>>a>>b;
if((a%3==0) || (b%3==0)){
cout<<0<<endl;
}
else if((abs(a-b))%3==0){
cout<<1<<endl;
}
else{
cout<<2<<endl;
}
}
return 0;
}
``````

## Divisible by 3 CodeChef Solution in Java

``````/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
int a=sc.nextInt();
int b=sc.nextInt();
int count=0;
while(!(a%3==0 || b%3==0)){
if(a>b) a=Math.abs(a-b);
else b=Math.abs(a-b);
count++;
}
System.out.println(count);
}
}
}
``````

## Divisible by 3 CodeChef Solution in Pyth 3

``````# cook your dish here
for i in range(int(input())):
a,b=map(int,input().split())
if (a*b)%3==0:
print(0)
elif (a-b)%3 == 0:
print(1)
else:
print(2)``````
##### Divisible by 3 CodeChef Solution Review:

In our experience, we suggest you solve this Divisible by 3 CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Divisible by 3 CodeChef Solution

Find on CodeChef

##### Conclusion:

I hope this Divisible by 3 CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions