Minimum cost to convert M to N by repeated addition of its even divisors | C # program for the above approach ; Function to find the value of minimum steps to convert m to n ; Base Case ; If n exceeds m ; Iterate through all possible even divisors of m ; If m is divisible by i , then find the minimum cost ; Add the cost to convert m to m + i and recursively call next state ; Return min_cost ; Driver Code ; Function Call ; If conversion is not possible ; Print the cost
using System ; class GFG { static int inf = 1000000008 ; public static int minSteps ( int m , int n ) { if ( n == m ) return 0 ; if ( m > n ) return inf ; int min_cost = inf ; for ( int i = 2 ; i < m ; i += 2 ) { if ( m % i == 0 ) { min_cost = Math . Min ( min_cost , m / i + minSteps ( m + i , n ) ) ; } } return min_cost ; } public static void Main ( String [ ] args ) { int M = 6 ; int N = 24 ; int minimum_cost = minSteps ( M , N ) ; minimum_cost = minimum_cost == inf ? - 1 : minimum_cost ; Console . WriteLine ( minimum_cost ) ; } }