1017. Convert to Base -2. C. O(log(n))
ApproachI sent my intuition to hell this time and just read about the negative number system https://en.wikipedia.org/wiki/Negative_base and just coded a plan d
# Approach
I sent my intuition to hell this time and just read about the negative number system $$ $$ https://en.wikipedia.org/wiki/Negative_base $$ $$ and just coded a plan described there.

# Complexity
- Time complexity: $$O(log(n))$$

- Space complexity: $$O(log(n))$$

# Code
```c []
char* baseNeg2(int n) {
    int r, pos;
    char *buf, c;

    buf = (char *)calloc(2, 1);
    if (!buf)
        return NULL;

    if (n == 0) {
        buf[0] = '0';
        return buf;
    }

    pos = 0;
    while (n) {
        r = n % (-2);
        n /= -2;
        if (r < 0) {
            n++;
            r += 2;
        }
        buf = realloc(buf, pos + 2);
        if (!buf)
            return NULL;
        buf[pos++] = r + '0';
    }
    buf[pos] = 0;

    /* reverse sting */
    r = 0;
    pos--;
    while (r < pos) {
        c = buf[r];
        buf[r] = buf[pos];
        buf[pos] = c;
        r++;
        pos--;
    }

    return buf;    
}
```
['C']