Minimize steps to reach K from 0 by adding 1 or doubling at each step | Java program to implement the above approach ; Function to find minimum operations ; dp is initialised to store the steps ; For all even numbers ; Driver Code
class GFG { static int minOperation ( int k ) { int dp [ ] = new int [ k + 1 ] ; for ( int i = 1 ; i <= k ; i ++ ) { dp [ i ] = dp [ i - 1 ] + 1 ; if ( i % 2 == 0 ) { dp [ i ] = Math . min ( dp [ i ] , dp [ i / 2 ] + 1 ) ; } } return dp [ k ] ; } public static void main ( String [ ] args ) { int K = 12 ; System . out . print ( minOperation ( K ) ) ; } }