|
| 1 | +# Sum of Two Integers |
| 2 | + |
| 3 | +Given two integers a and b, return the sum of the two integers without using the operators + and -. |
| 4 | + |
| 5 | + |
| 6 | +## Examples |
| 7 | + |
| 8 | +Example 1: |
| 9 | +```text |
| 10 | +Input: a = 1, b = 2 |
| 11 | +Output: 3 |
| 12 | +``` |
| 13 | + |
| 14 | +Example 2: |
| 15 | +```text |
| 16 | +Input: a = 2, b = 3 |
| 17 | +Output: 5 |
| 18 | +``` |
| 19 | + |
| 20 | +## Constraints |
| 21 | + |
| 22 | +- -1000 <= a, b <= 1000 |
| 23 | + |
| 24 | +## Topics |
| 25 | + |
| 26 | +- Math |
| 27 | +- Bit Manipulation |
| 28 | + |
| 29 | +## Solution |
| 30 | + |
| 31 | +We will use the bitwise manipulation pattern to calculate the sum of two integers without using the arithmetic operators. |
| 32 | +To calculate the desired sum, we'll use the bitwise operations. |
| 33 | + |
| 34 | +Let's dive into the logic to get a better idea of how to use bitwise operations for this task: |
| 35 | + |
| 36 | +- Bitwise XOR (`^`) operation: This operation allows us to calculate the sum of corresponding bits in a and b without |
| 37 | + considering the carry (if any). |
| 38 | + |
| 39 | +- Bitwise AND (`&`) operation: The AND operation will help us determine where carry propagation is necessary. By applying |
| 40 | + this operation to a and b, we can identify which bit positions require carry consideration. |
| 41 | + |
| 42 | +- Iterative Process: The algorithm functions iteratively, much like how binary addition is carried out. We will start |
| 43 | + with the least significant bits and advance toward the most significant bits of a and b. At each bit position, we |
| 44 | + calculate the sum while considering the carry from the previous bit's addition. This ensures that any carry is |
| 45 | + appropriately propagated throughout the process. |
| 46 | + |
| 47 | +The algorithm to obtain the sum of the given integers is as follows: |
| 48 | + |
| 49 | +1. To limit the result to 32-bits, we set a mask variable to `0xFFFFFFFF`. |
| 50 | +2. Start a loop that continues while b is not equal to 0. We calculate the sum of the two integers without carrying, by |
| 51 | + taking XOR and then AND the result with the mask. |
| 52 | +3. For calculating the carry which needs to be added to the next pair of integers, we take the AND of a and b, |
| 53 | + leftshifting the result by 1-bit, and then AND this result with the mask. |
| 54 | +4. We update a and b with the new values for the next iteration, which are the result and carry calculated in steps 2 |
| 55 | + and 3, respectively. |
| 56 | +5. At the end, to handle overflow when performing the addition of two integers, we have a condition that checks if the |
| 57 | + result exceeds the maximum value of a signed 32-bit integer. If it does, we return the two’s complement of the result |
| 58 | + to ensure that the returned value remains within the valid range. |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +### Time Complexity |
| 69 | + |
| 70 | +The time complexity of this solution is O(1), because the while loop runs for a maximum of 32 times, since the integers |
| 71 | +are represented by 32−bits. Each iteration does bitwise operations, which takes constant O(1), time hence making the |
| 72 | +total time complexity as O(1)*O(32) = O(32), which is equivalent to O(1). |
| 73 | + |
| 74 | +### Space Complexity |
| 75 | + |
| 76 | +The solution uses fixed numbers of variables to compute the result. Hence the space complexity is constant. O(1). |
0 commit comments