Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
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:
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.
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.
Input:
2
0 343
1 1
Output:
0
1
Test case 1: A=0 is already divisible by 3.
Test case 2: In the only operation, Stack can change A=1 to A=∣A−B∣=∣1−1∣=0. Now A=0 is divisible by 3.
#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;
}
/* 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
{
// 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();
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);
}
}
}
# 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)
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
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 >>