Adding Two Negabinary Numbers - Complete Solution Guide
Adding Two Negabinary Numbers is LeetCode problem 1073, 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 numbers arr1 and arr2 in base -2 , return the result of adding them together. Each number is given in array format : as an array of 0s and 1s, from most significant bit to least significant bit. For example, arr = [1,1,0,1] represents the number (-2)^3 + (-2)^2 + (-2)^0 = -3 . A number arr in array, format is also guaranteed to have no leading zeros: either arr == [0] or arr[0] == 1 . Return the result of adding arr1 and arr2 in the same format: as an array of 0s and 1s with no leading
Detailed Explanation
The problem asks us to add two numbers represented in base -2 (negabinary) and return the result in the same negabinary format (an array of 0s and 1s). The input arrays `arr1` and `arr2` represent the numbers, with the most significant bit at the beginning of the array. The output should also be an array of 0s and 1s, representing the sum in base -2, without any leading zeros (unless the result is 0 itself, represented as [0]).
Solution Approach
The provided solution performs addition digit by digit, from the least significant bit to the most significant bit, similar to how we add numbers manually. It maintains a `carry` variable that stores the carry from the previous addition. Since we're working in base -2, the carry can be negative. The core of the algorithm involves adding the corresponding digits from the input arrays and the carry, then determining the resulting digit (0 or 1) and the new carry for the next iteration. After the addition is complete, the result array is trimmed to remove any leading zeros, and then reversed to give the result in the correct order.
Step-by-Step Algorithm
- Step 1: Initialize pointers `i` and `j` to the last indices of `arr1` and `arr2` respectively. Initialize `carry` to 0 and `result` to an empty list.
- Step 2: Iterate while either `i` or `j` is within the bounds of their respective arrays, or `carry` is not 0.
- Step 3: Inside the loop, add the digit at `arr1[i]` to `carry` if `i` is within bounds, and decrement `i`. Do the same for `arr2[j]` and `j`.
- Step 4: Calculate the current digit to be added to the `result` list by taking the least significant bit of the `carry` (carry & 1). This essentially gets remainder when carry is divided by 2.
- Step 5: Calculate the new carry by dividing the previous carry by -2 (carry = -(carry >> 1)). This is the carry to the next digit. Since carry can be negative it's important to carry out the result to the most significant digits.
- Step 6: After the loop finishes, remove any leading zeros from the `result` list, ensuring that the list contains at least one element (0).
- Step 7: Reverse the `result` list to obtain the final negabinary representation.
Key Insights
- Insight 1: Negabinary addition requires handling carries differently than standard binary addition. Instead of carrying over a '1' when the sum is 2, we need to adjust for the negative base, potentially requiring multiple carry adjustments.
- Insight 2: The carry can be negative. In each position, the sum can be 0, 1, or 2 from the input digits and the carry from the previous step. When the sum is greater than or equal to 2, or less than 0, we need to adjust the current digit to either 0 or 1 and propogate the 'carry' which can be negative.
- Insight 3: After addition, leading zeros must be trimmed. Also, handling the carry bit after exhausting the input arrays is critical to the correct result.
Complexity Analysis
Time Complexity: O(max(n, m))
Space Complexity: O(max(n, m))
Topics
This problem involves: Array, Math.
Companies
Asked at: Grab.