Minimum number of moves to reach N starting from (1, 1)

• Difficulty Level : Medium
• Last Updated : 07 Mar, 2022

Given an integer N and an infinite table where ith row and jth column contains the value i *j. The task is to find the minimum number of moves to reach the cell containing N starting from the cell (1, 1)
Note: From (i, j) only valid moves are (i + 1, j) and (i, j + 1)
Examples:

Input: N = 10
Output:
(1, 1) -> (2, 1) -> (2, 2) -> (2, 3) -> (2, 4) -> (2, 5)
Input: N = 7
Output:

Approach: Note that any cell (i, j) can be reached in i + j – 2 steps. Thus, only the pair (i, j) is required with i * j = N that minimizes i + j. It can be found out by finding all the possible pairs (i, j) and check them in O(âˆšN). To do this, without loss of generality, it can be assumed that i â‰¤ j and i â‰¤ âˆšN since N = i * j â‰¥ i2. So âˆšN â‰¥ i2 i.e. âˆšN â‰¥ i
Thus, iterate over all the possible values of i from 1 to âˆšN and, among all the possible pairs (i, j), pick the lowest value of i + j – 2 and that is the required answer.
Below is the implementation of the above approach:

C++

 `// C++ implementation of the approach``#include ``using` `namespace` `std;` `// Function to return the minimum number``// of moves required to reach the cell``// containing N starting from (1, 1)``int` `min_moves(``int` `n)``{``    ``// To store the required answer``    ``int` `ans = INT_MAX;` `    ``// For all possible values of divisors``    ``for` `(``int` `i = 1; i * i <= n; i++) {` `        ``// If i is a divisor of n``        ``if` `(n % i == 0) {` `            ``// Get the moves to reach n``            ``ans = min(ans, i + n / i - 2);``        ``}``    ``}` `    ``// Return the required answer``    ``return` `ans;``}` `// Driver code``int` `main()``{``    ``int` `n = 10;` `    ``cout << min_moves(n);` `    ``return` `0;``}`

Java

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

Python3

 `# Python3 implementation of the approach``import` `sys` `from` `math ``import` `sqrt` `# Function to return the minimum number``# of moves required to reach the cell``# containing N starting from (1, 1)``def` `min_moves(n) :` `    ``# To store the required answer``    ``ans ``=` `sys.maxsize;` `    ``# For all possible values of divisors``    ``for` `i ``in` `range``(``1``, ``int``(sqrt(n)) ``+` `1``) :` `        ``# If i is a divisor of n``        ``if` `(n ``%` `i ``=``=` `0``) :` `            ``# Get the moves to reach n``            ``ans ``=` `min``(ans, i ``+` `n ``/``/` `i ``-` `2``);` `    ``# Return the required answer``    ``return` `ans;` `# Driver code``if` `__name__ ``=``=` `"__main__"` `:` `    ``n ``=` `10``;` `    ``print``(min_moves(n));` `# This code is contributed by AnkitRai01`

C#

 `// C# implementation of the approach``using` `System;``    ` `class` `GFG``{``    ` `// Function to return the minimum number``// of moves required to reach the cell``// containing N starting from (1, 1)``static` `int` `min_moves(``int` `n)``{``    ``// To store the required answer``    ``int` `ans = ``int``.MaxValue;` `    ``// For all possible values of divisors``    ``for` `(``int` `i = 1; i * i <= n; i++)``    ``{` `        ``// If i is a divisor of n``        ``if` `(n % i == 0)``        ``{` `            ``// Get the moves to reach n``            ``ans = Math.Min(ans, i + n / i - 2);``        ``}``    ``}` `    ``// Return the required answer``    ``return` `ans;``}` `// Driver code``public` `static` `void` `Main(String[] args)``{``    ``int` `n = 10;` `    ``Console.WriteLine(min_moves(n));``}``}` `// This code is contributed by 29AjayKumar`

Javascript

 ``

Output:

`5`

Time Complexity: O(sqrt(n))

Auxiliary Space: O(1)

My Personal Notes arrow_drop_up