Best approach for beginners using Java
IntuitionWe need to convert a number to its binary representation in base -2 (negative two). To do this, we must understand that for base -2, instead of dividin
# Intuition
We need to convert a number to its binary representation in base -2 (negative two). To do this, we must understand that for base -2, instead of dividing by positive 2, we divide by -2. The remainder for negative bases can be tricky, so we must adjust the remainder whenever it is negative to ensure the digits are correctly represented in binary.

# Approach
1. **Base Case**: If `n` is 0, the result is simply "0".
2. **Building the Binary String**:
   - Use a `StringBuilder` to build the binary representation.
   - While `n` is not zero:
     - Compute the remainder `n % 2`. If the remainder is negative (which can happen when dividing by negative 2), adjust it by adding 2 to make it non-negative.
     - Append the adjusted remainder to the string.
     - Update `n` by subtracting the remainder and then dividing by -2 to continue the process.
3. **Reverse the String**: After the loop, reverse the string since the binary digits are appended in reverse order (from least significant to most significant bit).
4. **Return Result**: Convert the `StringBuilder` to a string and return it.

# Complexity
- **Time complexity**: $$O(\log |n|)$$. The number of iterations is proportional to the logarithm of the absolute value of `n`, as we keep dividing by -2.
- **Space complexity**: $$O(\log |n|)$$. The space required for storing the result is proportional to the number of digits in the binary representation, which is logarithmic in size.

# Code
```java []
class Solution {

    public String baseNeg2(int n) {
        if (n == 0) return "0";
        StringBuilder sb = new StringBuilder();
        
        while (n != 0) {
            int rem = n % 2;
            if (rem < 0) {
                rem += 2;
            }
            sb.append(rem);
            n = (n - rem) / -2;
        }

        return sb.reverse().toString();
    }
}

```
['Java']