Talentd
FleetCode
Make and follow sheets.Sign in

Add Two Integers solution and explanation

EasyMath4 min read88.0% acceptanceUpdated 9 Apr 2026
Add Two Integers - LeetCode 2235 Solution in C++ (LeetCode Math)nexTRIE · 1:05 · 1K viewsWatch on YouTube
9 more videos on this problem
  1. Add Two Integers - LeetCode 2235 Solution in C++ (LeetCode Math)nexTRIE · 1:05 · 1K views
  2. 2235. Add Two Integers (Leetcode Easy)Programming Live with Larry · 1:11 · 876 views
  3. Add Two Integers - LeetCode 2235 - Java Solutionalgobest - Computer Science · 1:29 · 644 views
  4. Solving LeetCode problem 2235 in APL (Add Two Integers)Rodrigo's APL Corner · 1:05 · 630 views
  5. 2235. Add Two Integers | Leetcode Solutionpredecode · 2:06 · 534 views
  6. Add Two Integers - Leetcode 2235 - PythonTim Huang · 1:11 · 481 views
  7. LeetCode 2235. Add Two Integers - Interview Prep Ep 126 #shortsFisher Coder · 0:11 · 448 views
  8. 2235. Add Two Integers Explanation - LeetCode Solution in Rust | Coding Programming DevelopmentThe Hacker's Gym | Austin Patkos (APex) · 4:55 · 317 views
  9. LeetCode 2235 | Add Two Integers | Math | JavaSleepy Cracker · 0:12 · 270 views
  10. 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

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.

def add(num1, num2):
    return num1 + num2

print(add(12, 5)) # Output: 17
public 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.

Complexity

Time Complexity: O(1).
Space Complexity: O(1).

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.

def add(a, b):
    while b != 0:
        carry = a & b
        a = a ^ b
        b = carry << 1
    return a

print(add(12, 5)) # Output: 17
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) {
        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.

Complexity

Time Complexity: O(log(min(a, b))).
Space Complexity: O(1).

Try this approach in the editor

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
    }
}
Try this approach in the editor

Complexity comparison

ApproachComplexity
Simple Arithmetic Approach

Time Complexity: O(1).
Space Complexity: O(1).

Bit Manipulation Approach

Time Complexity: O(log(min(a, b))).
Space Complexity: O(1).

Default Approach–

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 +

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.

Comments

Now write it yourselfClose this page and solve Add Two Integers in the editor, without looking.
Practise Add Two Integers