Minimum steps to reach N from 1 by multiplying each step by 2 , 3 , 4 or 5 | C ++ implementation to find minimum number of steps to reach N from 1 ; Function to find a minimum number of steps to reach N from 1 ; Check until N is greater than 1 and operations can be applied ; Condition to choose the operations greedily ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Minsteps ( int n ) { int ans = 0 ; while ( n > 1 ) { if ( n % 5 == 0 ) { ans ++ ; n = n / 5 ; continue ; } else if ( n % 4 == 0 ) { ans ++ ; n = n / 4 ; continue ; } else if ( n % 3 == 0 ) { ans ++ ; n = n / 3 ; continue ; } else if ( n % 2 == 0 ) { ans ++ ; n = n / 2 ; continue ; } return -1 ; } return ans ; } int main ( ) { int n = 10 ; cout << Minsteps ( n ) ; return 0 ; }