Problem of the day
Ninja is trying to write a function that takes two integers and returns their sum. But, due to some faults in his keyboard, he can not use the “+”, operator, which means he is not able to simply return ‘A’ + ‘B’, where ‘A’ and 'B' are the numbers to be added. You need to help Ninja in finding the sum of two numbers without using the "+" operator anywhere in your code.
Note: You should also not use the increment "++" operator too.
For example :
You are given A = 4, B = 6, their sum = 10, you need to return 10.
The first line contains an integer ‘T’ which denotes the number of test cases or queries to be run. Then the test cases are as follows.
The first and only line of each test case contains two space-separated integers denoting ‘A’ and ‘B’ respectively.
Output Format:
For each test case, print the sum of the two numbers without using the “+” operator.
Print the output of each test case in a separate line.
Note:
You don’t need to print anything; It has already been taken care of.
1 <= T <= 100
-10000 <= A, B <= 10000
where ‘T’ is the number of test cases and ‘A’ and ‘B’ are the two integers.
Time limit: 1 sec
2
1 1
4 6
2
10
In the first test case,
Sum of the two numbers = 1 + 1 = 2.
In the second test case,
Sum of the two numbers = 4 + 6 = 10.
2
-1 1
0 1
0
1
In the first test case,
Sum of the two numbers = (-1) + 1 = 0.
In the second test case,
Sum of the two numbers = 0 + 1 = 1.
Can you think about using the subtraction operator?
This is a very basic approach. In this approach, we will be simply subtracting the negative of the second number from the first number.
O(1)
Since we are using a simple arithmetic operation, the overall time complexity is O(1).
O(1)
Since we are not using any extra space, the overall space complexity is O(1).