Thursday, 6 June 2013 Given a number print the next largest number and just smallest number that have same number of 1's in binary representation. /* NEXT GREATER NUMBER ------------------- 1. Scan number from right to left bits. 2. As soon as first 1 bit is encountered set the encounterFlag. 3. If encounterFlag is set Turn on the first 0 bit. 4. Turn off the 1 bit on right of above bit. 5. Make this as small as possible to get just next larger number, Push all 1's after this bit to right end. NEXT SMALLER NUMBER ------------------- 1. Scan number from right to left bits. 2.
Read full article from Amazon or Microsoft programming interview questions and solutions: Given a number print the next largest number and just smallest number that have same number of 1's in binary representation.
No comments:
Post a Comment