num1 and num2, return the sum of the two integers.
Example 1:
Input: num1 = 12, num2 = 5 Output: 17 Explanation: num1 is 12, num2 is 5, and their sum is 12 + 5 = 17, so 17 is returned.
Example 2:
Input: num1 = -10, num2 = 4 Output: -6 Explanation: num1 + num2 = -6, so -6 is returned.
Constraints:
-100 <= num1, num2 <= 100Problem Overview: You are given two integers num1 and num2. The task is straightforward: return their sum. The problem mainly checks your understanding of basic math operations and, in some variations, how addition can be implemented using bit manipulation.
Approach 1: Simple Arithmetic Approach (O(1) time, O(1) space)
The most direct solution uses the built-in addition operator. Return num1 + num2 and the problem is solved. This works because modern programming languages already implement optimized integer addition at the hardware level. The CPU performs the addition in constant time, so the algorithm runs in O(1) time with O(1) extra space.
This approach is ideal when the problem simply asks for the result of adding two integers. There is no need for loops, data structures, or special logic. You read the inputs and perform a single arithmetic operation. Most solutions on coding platforms use this approach because it directly matches the problem statement.
Approach 2: Bit Manipulation Approach (O(1) time, O(1) space)
Addition can also be implemented using bitwise operations without using the + operator. This technique relies on two operations: XOR and AND. The XOR operation (a ^ b) computes the sum of bits without carrying, while the AND operation (a & b) identifies the carry bits. Shifting the carry left by one position ((a & b) << 1) aligns it with the next higher bit.
The algorithm repeatedly updates the values: compute the partial sum using XOR, compute the carry using AND and left shift, then repeat until the carry becomes zero. At that point, the partial sum contains the final result. Each iteration processes fixed-width integers, so the algorithm still runs in O(1) time and uses O(1) space. This technique is a classic example of bitwise operators simulating arithmetic.
Recommended for interviews: The arithmetic approach is what most interviewers expect for this specific problem because the prompt simply asks for the sum. However, understanding the bit manipulation technique demonstrates deeper knowledge of how addition works at the binary level. Showing the basic solution first confirms you recognize the simplest implementation. Explaining the XOR + carry method shows stronger low-level problem-solving skills.
This approach involves using the most straightforward arithmetic operation: addition. Since the task is to return the sum of two integers, we directly perform this addition operation and return the result.
The function add takes two integers as input parameters, performs the addition operation, and returns the sum.
Time Complexity: O(1) because addition is a constant-time operation.
Space Complexity: O(1) since no additional space is required beyond input storage.
This approach uses bitwise operations to compute the sum of two integers without using the '+' operator directly. It involves using XOR for addition without carry and AND to compute the carry.
Using bitwise operators, this code calculates the sum without the '+' operator by continuously updating numbers with their XOR and carrying over bits using AND and shift operations.
Time Complexity: O(log(min(num1, num2))) due to carry propagation.
Space Complexity: O(1).
| Approach | Complexity |
|---|---|
| Simple Arithmetic Approach | Time Complexity: O(1) because addition is a constant-time operation. |
| Bit Manipulation Approach | Time Complexity: O(log(min(num1, num2))) due to carry propagation. |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Simple Arithmetic Approach | O(1) | O(1) | Best for normal implementations when the + operator is allowed |
| Bit Manipulation Approach | O(1) | O(1) | Useful when addition must be implemented using bitwise operations without + |
Add Two Integers - LeetCode 2235 Solution in C++ (LeetCode Math) • nexTRIE • 1,490 views views
Watch 9 more video solutions →Practice Add Two Integers with our built-in code editor and test cases.
Practice on FleetCode