Convert to Base -2 - Complete Solution Guide
Convert to Base -2 is LeetCode problem 1017, 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 an integer n , return a binary string representing its representation in base -2 . Note that the returned string should not have leading zeros unless the string is "0" . Example 1: Input: n = 2 Output: "110" Explantion: (-2) 2 + (-2) 1 = 2 Example 2: Input: n = 3 Output: "111" Explantion: (-2) 2 + (-2) 1 + (-2) 0 = 3 Example 3: Input: n = 4 Output: "100" Explantion: (-2) 2 = 4 Constraints: 0 <= n <= 10 9
Detailed Explanation
The problem asks us to convert a given integer `n` into its equivalent representation in base -2. The output should be a string representing this base -2 number. Leading zeros should be avoided, except when the input is 0, in which case the output should be "0". The input `n` is constrained to be between 0 and 10^9 inclusive.
Solution Approach
The solution uses an iterative approach. It repeatedly divides the given number `n` by -2. The remainder of each division (which will be either 0 or 1) becomes a digit in the base -2 representation. The digits are collected in reverse order (least significant digit first), and then reversed at the end to form the final result. The bitwise AND operator `& 1` is used to efficiently extract the remainder when dividing by -2. The loop continues until the number becomes 0. A special case is handled when the input `n` is 0, where the output is directly "0".
Step-by-Step Algorithm
- Step 1: Handle the base case where n is 0. If n is 0, return "0".
- Step 2: Initialize an empty string or list to store the base -2 digits.
- Step 3: While n is not equal to 0, repeat the following steps:
- Step 4: Extract the least significant bit (LSB) of n using the bitwise AND operator (`n & 1`). Convert this bit to a string and append it to the result string/list.
- Step 5: Divide n by -2. This is done using `n = -(n >> 1)`. The right shift operator (`>> 1`) effectively divides by 2, and the negation handles the base -2 aspect.
- Step 6: After the loop finishes, reverse the string/list to obtain the correct order of digits.
- Step 7: Return the reversed string.
Key Insights
- Insight 1: The core idea is to repeatedly divide the number by -2 and keep track of the remainders. Since remainders can only be 0 or 1, the problem effectively converts to finding these binary digits.
- Insight 2: The division by -2 can result in negative quotients. We can leverage the bitwise AND operator (`& 1`) to extract the least significant bit (LSB), which will be the remainder (0 or 1).
- Insight 3: The negative division needs careful handling. We use `n = -(n >> 1)` to propagate the negative sign properly, which is crucial for obtaining the correct representation.
Complexity Analysis
Time Complexity: O(logN)
Space Complexity: O(logN)
Topics
This problem involves: Math.
Companies
Asked at: Airbnb.