![]() |
VOOZH | about |
Given an integer N, the task is to find base -2 representation of the number N in the form of a string, such that S0 * (- 2)0 + S1 * (- 2)1 + ... + Sk * (- 2)k = N. The string should only consist of 0s and 1s and unless the string is equal to zero, the initial character should be 1.
Examples:
Input: N = -9
Output: 1011
Explanation: 1011 is -2 representation of -9
(-2)0+ (-2)1+ (-2)3 = 1+ (-2) + (-8) = -9Input: N = -7
Output: 1001
Approach: Follow the steps below to solve the problem:
Below is the implementation of the above approach:
1011
Time Complexity: O(N)
Auxiliary Space: O(1)