Description:
Given two integers a
and b
, return the sum of the two integers without using the operators + and -.
Example 1: Input: a = 44, b = 32 Output: 76
Example 2: Input: a = 3, b = 2 Output: 5
Algorithmic Steps: This problem is solved with the help of bitwise operators technique. The addition of two binary numbers is calculated by the sum of carry and XOR between the numbers until the carry becomes 0. The algorithmic approach can be summarized as follows:
-
Get the two numbers
a
andb
as input parameters. -
Iterate the bitwise operations until
b
is not zero. -
Calculate the carry of the two numbers using Bitwise AND operation. Also, shift the carry to the left 1. The can be store in a
temp
variable. -
Calculate the XOR operation between the numbers using Bitwise XOR operation. Store the result in
a
for next iteration. -
Update
b
value withtemp
calculated in step3. -
Repeat steps 3-5 steps until the carry(
b
) is zero. -
After the completion of loop, the addition of binary numbers returned from
a
. -
Return
[-1, -1]
incase of no matching numbers whose sum not equal to the target.
Time and Space complexity:
This algorithm takes a time complexity of O(1)
(~O(32)
). This is because we are iterating the loop until the carry becomes zero. Considering 32-bit integers, the loop may run at most 32 times for maximum possible value of carry.
Here, we don't use any additional datastructure other than temporary variable. Hence, the space complexity will be O(1)
.