The Adding Two Negabinary Numbers coding problem is a challenging math-based question. "Negabinary" is a non-standard positional numeral system where the base is instead of . You are given two arrays representing negabinary numbers and must return their sum as an array.
This problem is a favorite at Grab because it tests a candidate's ability to adapt a well-known algorithm (binary addition) to a completely different set of rules. It tests mathematical flexibility and the ability to handle unusual "carries" (in negabinary, the carry can be ).
This follows the Math Simulation interview pattern. Just like standard binary addition, you work from the least significant bit (end of the array) to the most significant. However, the carry logic is derived from the fact that . In negabinary, if the sum at a position is 2, the carry to the next position is actually .
In base :
1 is 110 is
Adding 1 + 110 (which is ):111 ().+1 when the sum is 2. In negabinary, at index results in at and as a carry for .[0, 0, 1, 1] instead of [1, 1].When dealing with base , remember the rule: if the sum at a position is greater than 1, you can set the current bit to and set the carry to . If the sum is , set the current bit to and the carry to .
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Beautiful Arrangement II | Medium | Solve | |
| Count Alternating Subarrays | Medium | Solve | |
| Escape The Ghosts | Medium | Solve | |
| Find Palindrome With Fixed Length | Medium | Solve | |
| Find the Number of Copy Arrays | Medium | Solve |