5 kyu

The Binary Binary Expansion

207 of 213Voile

Description:

Normally, we decompose a number into binary digits by assigning it with powers of 2, with a coefficient of 0 or 1 for each term:

25 = 1*16 + 1*8 + 0*4 + 0*2 + 1*1

The choice of 0 and 1 is... not very binary. We shall perform the true binary expansion by expanding with powers of 2, but with a coefficient of 1 or -1 instead:

25 = 1*16 + 1*8 + 1*4 - 1*2 - 1*1

Now this looks binary.


Given any positive number n, expand it using the true binary expansion, and return the result as an array, from the most significant digit to the least significant digit.

true_binary(25) == [1,1,1,-1,-1]

It should be trivial (the proofs are left as an exercise to the reader) to see that:

  • Every odd number has infinitely many true binary expansions
  • Every even number has no true binary expansions

Hence, n will always be an odd number, and you should return the least true binary expansion for any n.

Also, note that n can be very, very large, so your code should be very efficient.

Algorithms

Stats:

CreatedAug 31, 2017
PublishedAug 31, 2017
Warriors Trained1198
Total Skips93
Total Code Submissions1315
Total Times Completed213
Python Completions207
Ruby Completions14
Total Stars40
% of votes with a positive feedback rating93% of 59
Total "Very Satisfied" Votes52
Total "Somewhat Satisfied" Votes6
Total "Not Satisfied" Votes1
Total Rank Assessments6
Average Assessed Rank
4 kyu
Highest Assessed Rank
3 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • Voile Avatar
  • Blind4Basics Avatar
  • hobovsky Avatar
  • Just4FunCoder Avatar
Ad