Minimize given flips required to reduce N to 0 | Java program to implement the above approach ; Function to find the minimum count of operations required to Reduce N to 0 ; Stores count of bits in N ; Recurrence relation ; Driver code
class GFG { public static int MinOp ( int N ) { if ( N <= 1 ) return N ; int bit = ( int ) ( Math . log ( N ) / Math . log ( 2 ) ) + 1 ; return ( ( 1 << bit ) - 1 ) - MinOp ( N - ( 1 << ( bit - 1 ) ) ) ; } public static void main ( String [ ] args ) { int N = 4 ; System . out . println ( MinOp ( N ) ) ; } }