Convert to Binary || Beats 100% || C++ Solution
IntuitionThe problem asks to convert a given number n into its base -2 representation. The base -2 system behaves differently than regular binary, and handling
# Intuition 
The problem asks to convert a given number `n` into its base -2 representation. The base -2 system behaves differently than regular binary, and handling negative bases requires special attention to the remainder and quotient.

When converting a number to a negative base, we need to handle two main parts:
1. **Remainder**: In base -2, the remainder when dividing by -2 can be negative, which isn't standard for a typical base system. We need to adjust the remainder to be in the range [0, 1].
2. **Quotient Adjustment**: When the remainder is negative, we adjust the quotient by incrementing it by 1.

# Approach 
1. **Handle Special Case (n = 0)**: If the input number is zero, we can directly return `"0"`, as any number in any base representation of zero is just `0`.
2. **Repeated Division by -2**: For each iteration, we divide `n` by -2, calculate the remainder, and adjust it to be either `0` or `1` by modifying both the remainder and the quotient.
3. **Push the Result**: The binary digits (remainder) are collected in reverse order during the iteration, so we need to reverse the result at the end.
4. **Edge Case**: Ensure the solution works even for edge cases, such as when `n` is negative.

# Complexity 
- **Time complexity**: $$O(\log n)$$  
    - The number of iterations is proportional to the number of bits in the base -2 representation, which is logarithmic in terms of the input number `n`.

- **Space complexity**: $$O(\log n)$$  
    - We use a string to store the result, which grows in size proportional to the number of bits in the base -2 representation.

# Code 
```cpp
class Solution { 
public: 
    string baseNeg2(int n) { 
        // Special case: if n is 0, return "0"
        if(n == 0) return "0"; 
        
        // Initialize an empty string to store the result
        string res = ""; 
        
        // Repeat division until n becomes 0
        while(n != 0){ 
            // Calculate the remainder when dividing by -2
            int r = n % -2; 
            
            // Divide n by -2
            n /= -2; 
            
            // Adjust the remainder to be in the range [0, 1]
            if(r < 0){ 
                r += 2; 
                n++; // Adjust the quotient when remainder is negative
            } 
            
            // Add the remainder to the result string
            res.push_back(r + '0'); 
        } 
        
        // Reverse the result string to get the correct order
        reverse(res.begin(), res.end()); 
        
        // Return the base -2 representation
        return res; 
    } 
};

['C++']