Minimum number of moves to reach N starting from ( 1 , 1 ) | Java implementation of the approach ; Function to return the minimum number of moves required to reach the cell containing N starting from ( 1 , 1 ) ; To store the required answer ; For all possible values of divisors ; If i is a divisor of n ; Get the moves to reach n ; Return the required answer ; Driver code
class GFG { static int min_moves ( int n ) { int ans = Integer . MAX_VALUE ; for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { ans = Math . min ( ans , i + n / i - 2 ) ; } } return ans ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( min_moves ( n ) ) ; } }