Sum of Two Integers
MEDIUMDescription
Given two integers a and b, return the sum of the two integers without using the operators + and -.
Example 1:
Input: a = 1, b = 2 Output: 3
Example 2:
Input: a = 2, b = 3 Output: 5
Constraints:
-1000 <= a, b <= 1000
Approaches
Checkout 2 different approaches to solve Sum of Two Integers. Click on different approaches to view the approach and algorithm in detail.
Bit Manipulation (Recursive)
This approach uses recursion to simulate the process of binary addition. It's based on the principle that a + b can be expressed as the sum of two parts: the sum without considering carries (a XOR b) and the carries themselves ((a AND b) << 1). The function calls itself with these two new values until the carry part becomes zero.
Algorithm
- The function
getSum(a, b)is defined to compute the sum recursively. - Base Case: If
b(which represents the carry) is 0, it means there are no more carries to add. The current value ofais the final sum, so we returna. - Recursive Step: If
bis not 0, we need to perform another round of addition.- The sum of bits without considering the carry is calculated as
a ^ b. - The new carry is calculated as
(a & b) << 1. The&operation finds where both bits are 1, and<< 1shifts this carry to the next significant bit position. - The function calls itself with these two new values:
getSum(a ^ b, (a & b) << 1).
- The sum of bits without considering the carry is calculated as
The core idea is to break down the addition into simpler bitwise operations.
a ^ bgives the sum ofaandbat each bit position, but without handling the carry. For example,1+1=0(correct sum bit),1+0=1,0+1=1.a & bidentifies the bit positions where a carry is generated (i.e., where both bits are 1).(a & b) << 1shifts these carries one position to the left, so they can be added to the next higher bit position. The problema + bis thus transformed into(a ^ b) + ((a & b) << 1). We can solve this new addition problem recursively. The base case for the recursion is when the second number (representing the carries) becomes 0. At this point, the first number holds the final sum.
class Solution {
public int getSum(int a, int b) {
// Base case: if the second number (carry) is 0, the first number is the sum.
if (b == 0) {
return a;
}
// Recursive step:
// a ^ b is the sum without carry
// (a & b) << 1 is the carry
return getSum(a ^ b, (a & b) << 1);
}
}
Complexity Analysis
Pros and Cons
- Elegant and concise implementation.
- Correctly handles negative numbers due to two's complement arithmetic.
- Slightly more overhead than the iterative version due to function calls.
- In theory, could lead to a stack overflow if integers had a very large number of bits, though this is not an issue for standard 32-bit or 64-bit integers.
Code Solutions
Checking out 3 solutions in different languages for Sum of Two Integers. Click on different languages to view the code.
public class Sum_of_Two_Integers { public static void main ( String [] args ) { Sum_of_Two_Integers out = new Sum_of_Two_Integers (); // System.out.println(out.getSum(1, 2)); // no carry // 2+2 // recurion-1: sum=0, carry= (10)左移1位 =(100)=4 // recursion-2: a=0,b=4 // recursion-3: b=0 System . out . println ( out . getSum ( 2 , 2 )); // with carry } public int getSum ( int a , int b ) { if ( b == 0 ){ // complete the operation when there is no carry return a ; } int sum , carry ; sum = a ^ b ; // step-1 sum carry = ( a & b )<< 1 ; // step-2 sum return getSum ( sum , carry ); } } ///////// class Solution { public int getSum ( int a , int b ) { return b == 0 ? a : getSum ( a ^ b , ( a & b ) << 1 ); } }Video Solution
Watch the video walkthrough for Sum of Two Integers
Similar Questions
5 related questions you might find useful
Patterns:
Companies:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.