Skip to main content

Add Two Integers - Solution & Explanation

EasyMath8 min readAsked at: Amazon, Microsoft, Meta +6
Practice this problem

Problem Statement

Given two integers 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.

The function add takes two integers as input parameters, performs the addition operation, and returns the sum.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(1) because addition is a constant-time operation.
Space Complexity: O(1) since no additional space is required beyond input storage.

Try this approach in the editor →

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.

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(log(min(num1, num2))) due to carry propagation.
Space Complexity: O(1).

Try this approach in the editor →

Approach 3: Default Approach

Code

Python

Java

C++

Go

TypeScript

Rust

C

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Simple Arithmetic Approach

Time Complexity: O(1) because addition is a constant-time operation.
Space Complexity: O(1) since no additional space is required beyond input storage.

Bit Manipulation Approach

Time Complexity: O(log(min(num1, num2))) due to carry propagation.
Space Complexity: O(1).

Default Approach—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Simple Arithmetic ApproachO(1)O(1)Best for normal implementations when the + operator is allowed
Bit Manipulation ApproachO(1)O(1)Useful when addition must be implemented using bitwise operations without +

Video Solution

Add Two Integers - LeetCode 2235 Solution in C++ (LeetCode Math) • nexTRIE • 1,519 views views

Watch 9 more video solutions →

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.

Ready to solve this problem?

Practice Add Two Integers with our built-in code editor and test cases.

Practice on FleetCode