algorithms kotlin

Problem 5.4 from EPI -> Closest integer with the same weight

This seems like a greedy problem but its quite simple. Look at a place where you can swap 1 and 0 in the given number, the right most the location is the smaller difference will be, that is why the right most is called the least significant bit. So go from right to left and the moment you encounter two bits that differ swap them, take a look at a solution here.

Leave a Reply

Your email address will not be published. Required fields are marked *