Faulty Key

Moderate
0/80
Average time to solve is 25m
profile
Contributed by
12 upvotes
Asked in companies
FacebookInca Infotech Technologies Private Limited

Problem statement

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.
Detailed explanation ( Input/output format, Notes, Images )
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.
Note:
You don’t need to print anything; It has already been taken care of.
Constraints:
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
Sample Input 1:
2
1 1
4 6
Sample Output 1:
2
10
Explanation of sample input 1:
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.
Sample Input 2:
2
-1 1
0 1
Sample Output 2:
0
1
Explanation of sample input 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.
Hint

Can you think about using the subtraction operator?

Approaches (2)
Using a 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.

  • 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)
Time Complexity

O(1)

 

Since we are using a simple arithmetic operation, the overall time complexity is O(1).

Space Complexity

O(1)

 

Since we are not using any extra space, the overall space complexity is O(1).

Code Solution
(100% EXP penalty)
Faulty Key
Full screen
Console