Sum of Two Integers
Problem Statement
Given two integers a
and b
, return the sum of the two integers without using the +
and -
operators.
Example 1:
Input: a = 1, b = 1
Output: 2
Example 2:
Input: a = 4, b = 7
Output: 11
Constraints:
-1000 <= a, b <= 1000
Recommended Time and Space Complexity
O(1)
time and O(1)
space.
Hint 1
Think about how addition works at the bit level.
Hint 2
Can you simulate the carry operation using bitwise operators?
Hint 3
Consider using XOR for addition and AND for carry.
Solution
Brute Force
def get_sum(a: int, b: int) -> int: return a + b
Time complexity: O(1)
Space complexity: O(1)