Minimize given flips required to reduce N to 0 | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MinOp ( int N ) { if ( N <= 1 ) return N ; int bit = log2 ( N ) + 1 ; return ( ( 1 << bit ) - 1 ) - MinOp ( N - ( 1 << ( bit - 1 ) ) ) ; } int main ( ) { int N = 4 ; cout << MinOp ( N ) ; return 0 ; }