Given a positive number N, we need to reach to 1 in minimum number of steps where a step is defined as converting N to (N-1) or converting N to its one of the bigger divisor.

Formally, if we are at N, then in 1 step we can reach to (N – 1) or if N = u*v then we can reach to max(u, v) where u > 1 and v > 1.

Examples:

Input : N = 17 Output : 4 We can reach to 1 in 4 steps as shown below, 17 -> 16(from 17 - 1) -> 4(from 4 * 4) -> 2(from 2 * 2) -> 1(from 2 - 1) Input : N = 50 Output : 5 We can reach to 1 in 5 steps as shown below, 50 -> 10(from 5 * 10) -> 5(from 2 * 5) -> 4(from 5 - 1) -> 2(from 2 *2) -> 1(from 2 - 1)

We can solve this problem using breadth first search because it works level by level so we will reach to 1 in minimum number of steps where next level for N contains (N – 1) and bigger proper factors of N.

Complete BFS procedure will be as follows, First we will push N with steps 0 into the data queue then at each level we will push their next level elements with 1 step more than its previous level elements. In this way when 1 will be popped out from queue, it will contain minimum number of steps with it, which will be our final result.

In below code a queue of a structure of ‘data’ type is used which stores value and steps from N in it, another set of integer type is used to save ourselves from pushing the same element more than once which can lead to an infinite loop. So at each step, we push the value into set after pushing that into the queue so that the value won’t be visited more than once.

Please see below code for better understanding,

## C++

`// C++ program to get minimum step to reach 1 ` `// under given constraints` `#include <bits/stdc++.h>` `using` `namespace` `std;` ` ` `// structure represent one node in queue` `struct` `data` `{` ` ` `int` `val;` ` ` `int` `steps;` ` ` `data(` `int` `val, ` `int` `steps) : val(val), steps(steps)` ` ` `{}` `};` ` ` `// method returns minimum step to reach one` `int` `minStepToReachOne(` `int` `N)` `{` ` ` `queue<data> q;` ` ` `q.push(data(N, 0));` ` ` ` ` `// set is used to visit numbers so that they` ` ` `// won't be pushed in queue again` ` ` `set<` `int` `> st;` ` ` ` ` `// loop untill we reach to 1` ` ` `while` `(!q.empty())` ` ` `{` ` ` `data t = q.front(); q.pop();` ` ` ` ` `// if current data value is 1, return its` ` ` `// steps from N` ` ` `if` `(t.val == 1)` ` ` `return` `t.steps;` ` ` ` ` `// check curr - 1, only if it not visited yet` ` ` `if` `(st.find(t.val - 1) == st.end())` ` ` `{` ` ` `q.push(data(t.val - 1, t.steps + 1));` ` ` `st.insert(t.val - 1);` ` ` `}` ` ` ` ` `// loop from 2 to sqrt(value) for its divisors` ` ` `for` `(` `int` `i = 2; i*i <= t.val; i++)` ` ` `{` ` ` ` ` `// check divisor, only if it is not visited yet` ` ` `// if i is divisor of val, then val / i will` ` ` `// be its bigger divisor` ` ` `if` `(t.val % i == 0 && st.find(t.val / i) == st.end())` ` ` `{` ` ` `q.push(data(t.val / i, t.steps + 1));` ` ` `st.insert(t.val / i);` ` ` `}` ` ` `}` ` ` `} ` `}` ` ` `// Driver code to test above methods` `int` `main()` `{` ` ` `int` `N = 17;` ` ` `cout << minStepToReachOne(N) << endl; ` `}` |

## Java

`// Java program to get minimum step to reach 1 ` `// under given constraints ` `import` `java.util.*;` ` ` `class` `GFG` `{ ` ` ` `// structure represent one node in queue ` `static` `class` `data ` `{ ` ` ` `int` `val; ` ` ` `int` `steps;` ` ` `public` `data(` `int` `val, ` `int` `steps) ` ` ` `{` ` ` `this` `.val = val;` ` ` `this` `.steps = steps;` ` ` `} ` ` ` `}; ` ` ` `// method returns minimum step to reach one ` `static` `int` `minStepToReachOne(` `int` `N) ` `{ ` ` ` `Queue<data> q = ` `new` `LinkedList<>(); ` ` ` `q.add(` `new` `data(N, ` `0` `)); ` ` ` ` ` `// set is used to visit numbers so that they ` ` ` `// won't be pushed in queue again ` ` ` `HashSet<Integer> st = ` `new` `HashSet<Integer>(); ` ` ` ` ` `// loop untill we reach to 1 ` ` ` `while` `(!q.isEmpty()) ` ` ` `{ ` ` ` `data t = q.peek(); q.remove(); ` ` ` ` ` `// if current data value is 1, return its ` ` ` `// steps from N ` ` ` `if` `(t.val == ` `1` `) ` ` ` `return` `t.steps; ` ` ` ` ` `// check curr - 1, only if it not visited yet ` ` ` `if` `(!st.contains(t.val - ` `1` `)) ` ` ` `{ ` ` ` `q.add(` `new` `data(t.val - ` `1` `, t.steps + ` `1` `)); ` ` ` `st.add(t.val - ` `1` `); ` ` ` `} ` ` ` ` ` `// loop from 2 to Math.sqrt(value) for its divisors ` ` ` `for` `(` `int` `i = ` `2` `; i*i <= t.val; i++) ` ` ` `{ ` ` ` ` ` `// check divisor, only if it is not visited yet ` ` ` `// if i is divisor of val, then val / i will ` ` ` `// be its bigger divisor ` ` ` `if` `(t.val % i == ` `0` `&& !st.contains(t.val / i) ) ` ` ` `{ ` ` ` `q.add(` `new` `data(t.val / i, t.steps + ` `1` `)); ` ` ` `st.add(t.val / i); ` ` ` `} ` ` ` `} ` ` ` `}` ` ` `return` `-` `1` `;` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `17` `; ` ` ` `System.out.print(minStepToReachOne(N) +` `"\n"` `); ` `}` `} ` ` ` `// This code is contributed by 29AjayKumar` |

## C#

`// C# program to get minimum step to reach 1 ` `// under given constraints ` `using` `System;` `using` `System.Collections.Generic;` ` ` `class` `GFG` `{ ` ` ` `// structure represent one node in queue ` `class` `data ` `{ ` ` ` `public` `int` `val; ` ` ` `public` `int` `steps;` ` ` `public` `data(` `int` `val, ` `int` `steps) ` ` ` `{` ` ` `this` `.val = val;` ` ` `this` `.steps = steps;` ` ` `} ` `}; ` ` ` `// method returns minimum step to reach one ` `static` `int` `minStepToReachOne(` `int` `N) ` `{ ` ` ` `Queue<data> q = ` `new` `Queue<data>(); ` ` ` `q.Enqueue(` `new` `data(N, 0)); ` ` ` ` ` `// set is used to visit numbers so that they ` ` ` `// won't be pushed in queue again ` ` ` `HashSet<` `int` `> st = ` `new` `HashSet<` `int` `>(); ` ` ` ` ` `// loop untill we reach to 1 ` ` ` `while` `(q.Count != 0) ` ` ` `{ ` ` ` `data t = q.Peek(); q.Dequeue(); ` ` ` ` ` `// if current data value is 1, return its ` ` ` `// steps from N ` ` ` `if` `(t.val == 1) ` ` ` `return` `t.steps; ` ` ` ` ` `// check curr - 1, only if it not visited yet ` ` ` `if` `(!st.Contains(t.val - 1)) ` ` ` `{ ` ` ` `q.Enqueue(` `new` `data(t.val - 1, t.steps + 1)); ` ` ` `st.Add(t.val - 1); ` ` ` `} ` ` ` ` ` `// loop from 2 to Math.Sqrt(value) for its divisors ` ` ` `for` `(` `int` `i = 2; i*i <= t.val; i++) ` ` ` `{ ` ` ` ` ` `// check divisor, only if it is not visited yet ` ` ` `// if i is divisor of val, then val / i will ` ` ` `// be its bigger divisor ` ` ` `if` `(t.val % i == 0 && !st.Contains(t.val / i) ) ` ` ` `{ ` ` ` `q.Enqueue(` `new` `data(t.val / i, t.steps + 1)); ` ` ` `st.Add(t.val / i); ` ` ` `} ` ` ` `} ` ` ` `}` ` ` `return` `-1;` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `N = 17; ` ` ` `Console.Write(minStepToReachOne(N) +` `"\n"` `); ` `}` `}` ` ` `// This code is contributed by 29AjayKumar` |

**Output:**

4

This article is contributed by **Utkarsh Trivedi**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**