Advertisement

Sum of Two Integers - LeetCode 371 Solution

Sum of Two Integers - Complete Solution Guide

Sum of Two Integers is LeetCode problem 371, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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

Detailed Explanation

The problem requires us to calculate the sum of two integers, `a` and `b`, without using the standard addition (+) or subtraction (-) operators. This means we need to utilize bitwise operations to simulate the process of addition.

Solution Approach

The provided code uses bitwise operations to simulate the addition of two integers. It iteratively calculates the sum without carry (using XOR) and the carry (using AND followed by a left shift). The process continues until there is no carry left. The code then handles the conversion of the 32-bit representation to a signed integer if necessary.

Step-by-Step Algorithm

  1. Step 1: Initialize a mask `mask` to `0xFFFFFFFF` (32 bits of 1s). This is used to ensure that we're dealing with 32-bit integers, regardless of the underlying language's integer representation.
  2. Step 2: Enter a `while` loop that continues as long as `b` (the carry) is not zero.
  3. Step 3: Calculate the sum without carry using `a ^ b`. This performs a bitwise XOR operation, where each bit in the result is 1 if the corresponding bits in `a` and `b` are different, and 0 if they are the same. Then AND this with the mask.
  4. Step 4: Calculate the carry using `(a & b) << 1`. This performs a bitwise AND operation, where each bit in the result is 1 if the corresponding bits in `a` and `b` are both 1, and 0 otherwise. The result is then left-shifted by 1, effectively multiplying it by 2. Then AND this with the mask.
  5. Step 5: Update `a` and `b` with the calculated sum without carry and carry, respectively. This is crucial for the iterative process.
  6. Step 6: After the loop finishes (when there's no carry), check if `a` is greater than `0x7FFFFFFF`. This checks if the most significant bit of the 32-bit representation is set (i.e., if it's a negative number in 2's complement).
  7. Step 7: If `a` is greater than `0x7FFFFFFF`, convert it to a signed integer using `~(a ^ mask)`. This essentially performs the 2's complement operation to get the negative representation.
  8. Step 8: If `a` is not greater than `0x7FFFFFFF`, it's a positive number, so return `a` directly.

Key Insights

  • Insight 1: Addition can be broken down into two bitwise operations: XOR (^) for calculating the sum without carry, and AND (&) followed by a left shift (<< 1) for calculating the carry.
  • Insight 2: The process of calculating the sum and carry needs to be repeated until the carry becomes zero.
  • Insight 3: We need to handle the representation of integers in Python (and other languages) which may have a different default bit width or sign representation than what's assumed in bitwise operations designed for fixed-size integers (like 32-bit). This requires masking and sign extension handling.

Complexity Analysis

Time Complexity: O(1)

Space Complexity: O(1)

Topics

This problem involves: Math, Bit Manipulation.

Companies

Asked at: Hulu.