Last Updated: 5 Mar, 2021

Faulty Key

Moderate
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.
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

Approaches

01 Approach

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)

02 Approach

In this approach, we will use the logic behind the half adder that is used to find the sum of single bits and extend it so that we could add integers. 

 

  • We will start with XOR of the two numbers A and B.
  • Then we will find the AND of A and B in order to find the carry, and then left shift the carry until it becomes 0.
  • Finally we will do the OR of XOR and Carry to get the sum of the numbers.

 

To understand it better, let’s take an example of some numbers that use carry:

 

Suppose A = 6 and B = 7:

 

  • Binary conversion gives: A = 6 = 00110 and B = 7 = 00111
  • First you find the carries:
    • that is all positions where both a and b has its bit set:
      • int carry = (a & b) ;
  • Then it does the addition of digits, ignoring the carry, and stores it in a:
    • a = a ^ b:
      • This will respond to 6+7=3 in the example.
  • The last part shifts the carry to the next digit position, i.e. ensuring the 1-carry in the example is "moved" from the 1's to the 10's:
    • carry << 1;
  • The while loop continues as long as there are carries that has not been included in the sum.

 

NOTE: We can also use the “print” to find the sum, by using “%*s”, Here, the “%*s” specifier print value of a variable, the value of variable times, and printf return how many characters print on the screen., but this approach will not work in case of negative numbers, hence not included in this question.