Company: Oracle
Difficulty: medium
Binary Manipulation Problem Description Given an integer, store its binary representation bin . Perform the minimum number of operations allowed (to length(bin)-1 ). Reduce its binary representation to zero by following these two rules: Change the i th binary only if (i+1) th binary digit is 1 and all other binary digits from (i+2) to the end are zeros. Change the leftmost digit without restriction. What is the minimum number of operations required to complete this task? Examples Example 1: The binary representation of 4 is 100 so for the example, bin = [1, 0, 0] . 100 -> 101 -> 111 -> 110 -> 010 -> 011 -> 000 bin , from left to right, has indices 0 to 3. Stepping through the operations: Use rule 2 to change bin[0] to 1. 100 -> 101 Use rule 1 to change bin[1] to 1. 101 -> 111 Use rule 2 to change bin[0] to 0. 111 -> 110 Use rule 1 to change bin[2] to 0. 110 -> 010 Use rule 2 to change bin[0] to 0. 010 -> 011 Use rule 1 to change bin[1] to 0. 011 -> 001 Use rule 2 to change bin[2] to 0.