Ctrl K

Writing Numbers in Base Minus Two

Medium
In base -2 the only digits are 0 and 1, and digit number i (counting from the right, starting at zero) is worth (-2)^i: 1, -2, 4, -8, 16, and so on. For example, the string "110" means 4 − 2 = 2. Given a non-negative integer n, return its base -2 representation as a string without leading zeros. For n = 0 the answer is "0".
Example 1
n=6 → '11010'

(-2)⁴ + (-2)³ + (-2)¹ = 16 − 8 − 2 = 6, hence "11010".

Example 2
n=0 → '0'

Zero is written as the single digit "0".

Example 3
n=7 → '11011'
Constraints
  • 0 ≤ n ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsAdding Digit Lists MediumBackwards Digits in 32 Bits MediumNumber in Roman Numerals MediumDivision Without Dividing MediumMultiplying Digit Strings Medium
loading Python…
Input
n=6
Expected
'11010'

Run checks the first 3 tests in your browser. Submit checks all 64 tests on the server, hidden ones included.