NINJA KI GANIT

Moderate
0/80
Average time to solve is 20m
profile
Contributed by
2 upvotes
Asked in company
Intuit

Problem statement

Ninja always uses his calculator for doing addition, subtraction, multiplication, and division but due to some technical fault in his calculator only the “+” operator is working and all other operators are not in working condition.

Now his teacher gives him the question of subtraction, multiplication, and division, and now ninja is thinking of the solution of these questions by using the “+” operator as only the “+” operator is in working condition.

So help our ninja to do the subtraction, division, and multiplication with the help of addition.

So your task is to write a code that can do subtraction, multiplication, and division, and you are allowed to use only the “+” operator or we can say addition.

Detailed explanation ( Input/output format, Notes, Images )

Input Format :

The first line of input contains the ‘T’ number of test cases.

The first line of each test case contains two integers ‘N’ and ‘M’.

Output Format :

For each test case print all three results subtraction result, multiplication result, division result respectively in form of a vector.
Note:
You do not need to print anything; it has already been taken care of. Just implement the function.

Constraints :

1 <= T <= 100
-1000 <= N <= 1000
-1000 <= M <= 1000  and M != 0 

Time Limit: 1 sec

Sample Input 1 :

2
8 2
16 4

Sample Output 1 :

6 16 4
12 64 4

Explanation Of Sample Input 1 :

Test Case 1:

In the first line, there is the number of test cases i.e., 2, and in the next line‘8’and ‘2’is the integer on which operations are to be performed.
So after operations like after subtraction by using addition we get “16-4=12”, after multiplication by using addition we get “16*4=64”, after division by using addition we get “16/4=4”.


Test Case 2:

For this test case integers are “16” and “4”.
So after operations like after subtraction by using addition we get “8-2=6”, after multiplication by using addition we get “8*2=16”, after division by using addition we get 8/2=4.

Sample Input 2 :

2
4 -2
9 -3

Sample Output 2 :

6 -8 -2
12 -27 -3
Hint
  • For subtraction can you think of changing the sign?
  • Can you relate multiplication with addition?
  • Think of quotient, dividend, divisor.
Approaches (1)
Iterative Approach
  • For subtraction
    • Just change the sign of the second integer and simply add them.
    • For ex- integers ‘N=8’and ‘M=2’now after reversing the sign of ‘M’ we get ‘M=-2’now ‘N+M=8+(-2)=6’
  • For multiplication
    • Add the integer ‘N’ in itself for the ‘M’ times.
    • For ex- integers ‘N=8’ and ‘M=2’
    • Now add ‘N’ into itself for ‘2’ times
  • For division continuously subtract dividend from divisor and count is your quotient.
Time Complexity

O(min(N, M)),  where N and M are the integers on which operations are to be performed,

 

As during multiplication, our loop will run-up to the minimum of two integer times and during division also for flipping the sign.

Space Complexity

O(1).

 

As we are using constant space.

Code Solution
(100% EXP penalty)
NINJA KI GANIT
Full screen
Console