Faulty Key

Posted: 5 Mar, 2021
Difficulty: Moderate


Try Problem

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.

 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.
Input Format:
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.
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
Approach 1

This is a very basic approach. In this approach, we will be simply subtracting the negative of the second number from the first number.

  • For example: A = 4, B = 6
  • Now, negative of second number = -6
  • Subtracting this from the first number = 4 - (-6) = 4 + 6 = 10
  • So, for this approach, you simply need to return A - (- B)
Try Problem