Add Two Integers solution and explanation
9 more videos on this problem
Add Two Integers - LeetCode 2235 Solution in C++ (LeetCode Math)nexTRIE · 1:05 · 1K views
2235. Add Two Integers (Leetcode Easy)Programming Live with Larry · 1:11 · 876 views
Add Two Integers - LeetCode 2235 - Java Solutionalgobest - Computer Science · 1:29 · 644 views
Solving LeetCode problem 2235 in APL (Add Two Integers)Rodrigo's APL Corner · 1:05 · 630 views
2235. Add Two Integers | Leetcode Solutionpredecode · 2:06 · 534 views
Add Two Integers - Leetcode 2235 - PythonTim Huang · 1:11 · 481 views
LeetCode 2235. Add Two Integers - Interview Prep Ep 126 #shortsFisher Coder · 0:11 · 448 views
2235. Add Two Integers Explanation - LeetCode Solution in Rust | Coding Programming DevelopmentThe Hacker's Gym | Austin Patkos (APex) · 4:55 · 317 views
LeetCode 2235 | Add Two Integers | Math | JavaSleepy Cracker · 0:12 · 270 views
2235. Add Two Integers Explanation - LeetCode Solution in JavaScript JS Coding Node WebDevThe Hacker's Gym | Austin Patkos (APex) · 2:18 · 212 views
The problem
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 <= 100
Approach overview
Problem 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.
Approach 1: Simple Arithmetic Approach
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.
def add(num1, num2):
return num1 + num2
print(add(12, 5)) # Output: 17public class AddTwoIntegers {
public static int add(int num1, int num2) {
return num1 + num2;
}
public static void main(String[] args) {
System.out.println(add(12, 5)); // Output: 17
}
}#include <iostream>
int add(int num1, int num2) {
return num1 + num2;
}
int main() {
std::cout << add(12, 5) << std::endl; // Output: 17
return 0;
}function add(num1, num2) {
return num1 + num2;
}
console.log(add(12, 5)); // Output: 17#include <stdio.h>
int add(int num1, int num2) {
return num1 + num2;
}
int main() {
int result = add(12, 5);
printf("%d\n", result); // Output: 17
return 0;
}using System;
public class AddTwoIntegers {
public static int Add(int num1, int num2) {
return num1 + num2;
}
public static void Main(String[] args) {
Console.WriteLine(Add(12, 5)); // Output: 17
}
}The add function returns the sum of two given integers and the result is printed directly.
Time Complexity: O(1).
Space Complexity: O(1).
Approach 2: Bit Manipulation Approach
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.
def add(a, b):
while b != 0:
carry = a & b
a = a ^ b
b = carry << 1
return a
print(add(12, 5)) # Output: 17public class AddTwoIntegers {
public static int add(int a, int b) {
while (b != 0) {
int carry = a & b;
a = a ^ b;
b = carry << 1;
}
return a;
}
public static void main(String[] args) {
System.out.println(add(12, 5)); // Output: 17
}
}#include <iostream>
int add(int a, int b) {
while (b != 0) {
int carry = a & b;
a = a ^ b;
b = carry << 1;
}
return a;
}
int main() {
std::cout << add(12, 5) << std::endl; // Output: 17
return 0;
}function add(a, b) {
while (b != 0) {
let carry = a & b;
a = a ^ b;
b = carry << 1;
}
return a;
}
console.log(add(12, 5)); // Output: 17#include <stdio.h>
int add(int a, int b) {
while (b != 0) {
int carry = a & b;
a = a ^ b;
b = carry << 1;
}
return a;
}
int main() {
printf("%d\n", add(12, 5)); // Output: 17
return 0;
}using System;
public class AddTwoIntegers {
public static int Add(int a, int b) {
while (b != 0) {
int carry = a & b;
a = a ^ b;
b = carry << 1;
}
return a;
}
public static void Main(String[] args) {
Console.WriteLine(Add(12, 5)); // Output: 17
}
}The Python version follows the bitwise operation logic with while-loop to manage iterations until carry is zero.
Time Complexity: O(log(min(a, b))).
Space Complexity: O(1).
Approach 3: Default Approach
function sum(num1: number, num2: number): number {
return num1 + num2;
}func sum(num1 int, num2 int) int {
return num1 + num2
}impl Solution {
pub fn sum(num1: i32, num2: i32) -> i32 {
num1 + num2
}
}Complexity comparison
| Approach | Complexity |
|---|---|
| Simple Arithmetic Approach | Time Complexity: O(1). |
| Bit Manipulation Approach | Time Complexity: O(log(min(a, b))). |
| Default Approach | – |
Complexity analysis
| 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 + |
Frequently asked questions
Is Add Two Integers easy or hard?
Add Two Integers is classified as an Easy problem. It checks basic programming ability and understanding of arithmetic operations. The optional bit manipulation approach introduces a slightly deeper concept but still remains straightforward.
Add Two Integers Python/Java solution
In Python or Java, the solution is simply returning the sum of the two parameters. Example: return num1 + num2. The same logic works across C++, JavaScript, C#, and other languages because integer addition is a built-in constant-time operation.
How to solve Add Two Integers in O(n)?
The problem does not require O(n) time because the inputs are two integers rather than arrays or lists. The optimal solution runs in O(1) time by directly computing num1 + num2 or using bit manipulation with XOR and carry operations.
What is the best approach for Add Two Integers?
The best approach is the simple arithmetic method that returns num1 + num2. It runs in O(1) time and O(1) space because the CPU performs integer addition directly. The bit manipulation approach is useful for demonstrating how addition works internally using XOR and carry operations.
Is Add Two Integers asked at Google/Amazon/Meta?
Add Two Integers is typically used as a warm-up question rather than a core interview problem. It appears in coding practice sets to verify environment setup and basic syntax before moving to harder algorithmic questions.
What data structure is used in Add Two Integers?
No data structures are required. The solution operates directly on primitive integer values. The bit manipulation variant uses bitwise operations such as XOR and AND instead of additional structures.
What is the time complexity of Add Two Integers?
The time complexity is O(1). Only a single arithmetic or bitwise operation is performed regardless of the input values. No loops over input size are required, so the runtime remains constant.
