Divisible By 3

Was a fairly simple problem from CodeChef, just needs a bit of sample cases which we generally make based on our own understanding of the problem's logic, well, making Test Cases by ourself is a severely underrated practice that people tend to ignore, you gotta endure this process or relent later.

The problem might look complicated but it's not, just about perspective.

Relatable :|


Problem Statement:-

Stack likes the number 3 a lot.

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

  • := (change  to )
  • := (change  to )

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

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

Input Format

  • The first line of input contains a single integer , denoting the number of test cases. The description of  test cases follows.
  • The only line of each test case contains two integers  and .

Output Format

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

Constraints

  • 1105
  • 0,109

Sample 1:

Input
Output
2
0 343
1 1
0
1

Explanation:

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

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

Explanation Part:-

The main logic of this problem is that when we get the difference of 2 numbers, then if we subtract that difference from the larger number out of the 2 numbers, we will get the 1st number, and we don't want this to happen so the simple logic is that we will always replace the bigger number. In my algo I am considering b to be the bigger num and if suppose b is smaller then 'a' then I am swapping them both, so that 'b' becomes the bigger number. You can try by replacing the difference with the smaller number and you will notice that for the next operation we will again get the number 'a' which was present in the first operation, so this is the reason we always replace with the bigger number out of the 2 nums. Lastly we are always checking if any of the nums 'a' or 'b' is divisible by 3 then we will just print the answer by breaking out of the loop( remember we are maintaining a count variable which will store the number of operations required to get one of the numbers divisible by 3. Which we will be using to get the answer. That's it!

Code Implementation:-


for _ in range(int(input())):
    a,b=map(int,input().split())
    count=0
    if(a>b):
        a,b=b,a
    while(a%3!=0 and b%3!=0):
        # Will do something here
        count+=1
        b=abs(a-b)
    print(count)


Ed's Drunk is playing in background, I have finished with most of the works for the day, been practicing questions on Computer Organization. For lectures part I am working on Computer Networking cause there were some grey areas that needed to be jettisoned. The plan is simple and in motion, the only thing is to keep going. 

After these lectures are done, I'll get enough time for OS and DBMS, also they won't take long and I ought to take them simultaneously. The main bone of contention is Graph Theory cause that's my weak point so gotta work on that. 

It seems like the dog days are near, already started to heat up so much. 

Well, that's it for today, cya!

Comments

  1. Nice and very well explained. It may sound strange at first, but after reading the actual code, it is totally understandable.

    ReplyDelete

Post a Comment

Popular Posts