 💯Best Solution | Same as Base 2 | Eassy Approach
IntuitionThe problem requires converting a given integer n into its representation in base -2. Unlike base 2, where we divide by 2, in base -2, we repeatedly di
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem requires converting a given integer n into its representation in base -2. Unlike base 2, where we divide by 2, in base -2, we repeatedly divide by -2 and store the remainders.



# Approach
<!-- Describe your approach to solving the problem. -->
1. Initialize an empty string ans to store the binary representation.
2. Use a loop while n is not zero:
    - Compute the remainder when dividing n by 2 (n & 1). If the remainder is 1, append "1" to ans; otherwise, append "0".
     - Update n by dividing it by -2 (n = -1 * (n >> 1)). This step ensures we handle the negative base conversion correctly.
3. If ans is empty (meaning n was initially 0), return "0".
4. Return the final string representation.

# Complexity
- Time complexity:
    - The number of iterations in the loop depends on the number of bits required to represent n in base -2, which is log₂(n).
    - Hence, the time complexity is O(log n).

- Space complexity:
    - The result string stores at most log₂(n) characters.
    - Hence, the space complexity is O(log n).

# Code
```cpp []
class Solution {
public:
    string baseNeg2(int n) {
        string ans = "";
        while(n!=0){
            if(n&1){
                ans = "1"+ans;
            }
            else{
                ans = "0"+ ans;
            }
            n = -1*(n>>1);
        }
        if(ans.length() == 0) ans +="0";
        return ans;
    }
};
```
['C++']