Binary number maximum 1's

okay solved
Last edited on
yes. the maximum number of bits you can set by inverting bits is the number you set. if you are setting 5 bits, the max is 5. (bad question?)

now say you have some simple example..

000111 .. there are a few answers for flipping 5 here.
111001 .. you can say you set 1 bit by flipping 5, or you can say you set 3 and lost 2. Its all in whether you look at it technically or holistically. Technically you set 3. holistically you set 1.

that said I am pretty sure if you do this enough you will see some patterns emerge.

Can you tell what will be the pattern??

EDIT : Solved
Last edited on
Ill make you a deal... you tell me a *useful* practical application of the solution to this thing, and Ill give you a better hint.


Last edited on
That's a good deal.
@jonnin
Sliding windows in data views.

@enemy21
I am a little confused by your inputs and outputs.

Are you trying to find x=number of bits to flip?
Or are you given this number as part of your input?
yeep
Last edited on
@iamnone can u plz explain ur approach.
I could not figure out yet.

or can you plz explain the pattern with the help of example.
Last edited on
Try this problem. I give you a starting number and a goal number and also the number x.

E.g.:
    start =  5
    goal  = 15
    x     =  7

The way you use the number x is by considering how it can be partitioned:

                 Diff
   7 = 7 + 0       7
     = 6 + 1       5
     = 5 + 2       3
     = 4 + 3       1

The diff column indicates how much you can add to start at a time.

Can we reach the goal? If not, how close can you get?

    5 + 7 + 3 = 15 = goal
    So we can reach the goal.

Notice that if x is odd we can always reach the goal. Since two odds sum to an even we can add in even amounts as well as odd amounts to reach any goal.

But what if x is even?
is it even related?

how can this we apply to the problem?
@iamnone
I have figured out the pattern, but still not getting correct for the testcases, is there a boundary condition in this to look out for ?

EDIT: there was a silly mistake in my code.
Last edited on
still stuck lame?
Duthomhas answered so I will give you a few more hints.

if you had 8 bits, and you can flip any 1 per iteration, you know you can solve it. Why?
if you had 8 bits, and you must flip 8 bits per go, you know that there are only 2 answers and the only way you can set all the bits is if the input number is binary zero or binary for 255. Why?

if you can flip 3 at a time, is there any pattern that you cannot convert to all 1s? why?
if you can flip 4 at a time, is there any pattern that you cannot convert to all 1s? why?

for flipping 5 or more at a time, are there inputs that cannot reach 255? This should be obvious by now if you got the above whys.

then you can get into, what you should be seeing here, is how many bits cannot be set for the ones where you cannot set them all to 1s, and how that is related to the # of bits you can flip..

this is going to start to resemble one of those swindler's games that people use to take their drunk friend's money ... ive got 8 packets of sugar here on the table, some facing up and some facing down... blah blah if you can get them all facing up by doing this... loser buys next round...
Last edited on
Topic archived. No new replies allowed.