# Minimum division by 10 and multiplication by 2 required to reduce given number to 1

Given an integer **N**, the task is to reduce **N** to **1** by a minimum number of operations involving multiplication by **2 **and division by **10**. If **1** cannot be obtained, then print **“-1”**.

**Examples:**

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****.**

Input:N = 5Output:2Explanation:

Below are the operations performed:1st operation:Multiply N by 2. Therefore, N = 5 * 2 = 10.2nd operation:Divide N by 10. Therefore, N = 10/10 = 1.

Therefore, minimum number of operations required is 2.

Input:N = 4Output:-1

**Approach:** The idea is to check prime factors of the given number M. If the given number has **prime factors** other than **2** and **5**, then it is not possible to reduce the given number to **1** by the given operations. If the count of **2** exceeds that of **5** in its prime factors, then it is **not possible** to reduce **N** to **1** as all powers of **2** can’t be reduced.

Follow the steps below to solve the problem:

- Count the number of
**2**s present in prime factors of**N**and store it in a variable, say**cnt2**, and update**N**to**N / 2**.^{cnt2} - Count the number of
**5**s present in prime factors of**N**and store it in a variable, say**cnt5**, and update**N**to**N / 5**.^{cnt5} - After completing the above steps, if
**N**is**1**and**cnt2 ≤ cnt5**, then the minimum number of steps required is**2 * cnt5 – cnt2**. - Otherwise, print
**“-1”**as**N**can’t be reduced to**1**with the given operations.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find the minimum number` `// operations required to reduce N to 1` `int` `minimumMoves(` `int` `n)` `{` ` ` `// Stores count of powers of 2 and 5` ` ` `int` `cnt2 = 0, cnt5 = 0;` ` ` `// Calculating the primefactors 2` ` ` `while` `(n % 2 == 0) {` ` ` `n /= 2;` ` ` `cnt2++;` ` ` `}` ` ` `// Calculating the primefactors 5` ` ` `while` `(n % 5 == 0) {` ` ` `n /= 5;` ` ` `cnt5++;` ` ` `}` ` ` `// If n is 1 and cnt2 <= cnt5` ` ` `if` `(n == 1 && cnt2 <= cnt5) {` ` ` `// Return the minimum operations` ` ` `return` `2 * cnt5 - cnt2;` ` ` `}` ` ` `// Otherwise, n can't be reduced` ` ` `else` ` ` `return` `-1;` `}` `// Driver Code` `int` `main()` `{` ` ` `// Given Number N` ` ` `int` `N = 25;` ` ` `// Function Call` ` ` `cout << minimumMoves(N);` ` ` `return` `0;` `}` |

## Java

`// Java program for the above approach` `import` `java.util.*;` `class` `GFG{` `// Function to find the minimum number` `// operations required to reduce N to 1` `static` `int` `minimumMoves(` `int` `n)` `{` ` ` ` ` `// Stores count of powers of 2 and 5` ` ` `int` `cnt2 = ` `0` `, cnt5 = ` `0` `;` ` ` `// Calculating the primefactors 2` ` ` `while` `(n % ` `2` `== ` `0` `)` ` ` `{` ` ` `n /= ` `2` `;` ` ` `cnt2++;` ` ` `}` ` ` `// Calculating the primefactors 5` ` ` `while` `(n % ` `5` `== ` `0` `)` ` ` `{` ` ` `n /= ` `5` `;` ` ` `cnt5++;` ` ` `}` ` ` `// If n is 1 and cnt2 <= cnt5` ` ` `if` `(n == ` `1` `&& cnt2 <= cnt5)` ` ` `{` ` ` ` ` `// Return the minimum operations` ` ` `return` `2` `* cnt5 - cnt2;` ` ` `}` ` ` `// Otherwise, n can't be reduced` ` ` `else` ` ` `return` `-` `1` `;` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` ` ` `// Given Number N` ` ` `int` `N = ` `25` `;` ` ` `// Function Call` ` ` `System.out.print(minimumMoves(N));` `}` `}` `// This code is contributed by Amit Katiyar` |

## Python3

`# Python3 program for the above approach` `# Function to find the minimum number` `# operations required to reduce N to 1` `def` `minimumMoves(n):` ` ` `# Stores count of powers of 2 and 5` ` ` `cnt2 ` `=` `0` ` ` `cnt5 ` `=` `0` ` ` `# Calculating the primefactors 2` ` ` `while` `(n ` `%` `2` `=` `=` `0` `):` ` ` `n ` `/` `/` `=` `2` ` ` `cnt2 ` `+` `=` `1` ` ` `# Calculating the primefactors 5` ` ` `while` `(n ` `%` `5` `=` `=` `0` `):` ` ` `n ` `/` `/` `=` `5` ` ` `cnt5 ` `+` `=` `1` ` ` `# If n is 1 and cnt2 <= cnt5` ` ` `if` `(n ` `=` `=` `1` `and` `cnt2 <` `=` `cnt5):` ` ` ` ` `# Return the minimum operations` ` ` `return` `2` `*` `cnt5 ` `-` `cnt2` ` ` `# Otherwise, n can't be reduced` ` ` `else` `:` ` ` `return` `-` `1` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` ` ` `# Given Number N` ` ` `N ` `=` `25` ` ` `# Function Call` ` ` `print` `(minimumMoves(N))` `# This code is contributed by mohit kumar 29` |

## C#

`// C# program for the above approach` `using` `System;` `class` `GFG{` `// Function to find the minimum number` `// operations required to reduce N to 1` `static` `int` `minimumMoves(` `int` `n)` `{` ` ` ` ` `// Stores count of powers of 2 and 5` ` ` `int` `cnt2 = 0, cnt5 = 0;` ` ` `// Calculating the primefactors 2` ` ` `while` `(n % 2 == 0)` ` ` `{` ` ` `n /= 2;` ` ` `cnt2++;` ` ` `}` ` ` `// Calculating the primefactors 5` ` ` `while` `(n % 5 == 0)` ` ` `{` ` ` `n /= 5;` ` ` `cnt5++;` ` ` `}` ` ` `// If n is 1 and cnt2 <= cnt5` ` ` `if` `(n == 1 && cnt2 <= cnt5)` ` ` `{` ` ` ` ` `// Return the minimum operations` ` ` `return` `2 * cnt5 - cnt2;` ` ` `}` ` ` `// Otherwise, n can't be reduced` ` ` `else` ` ` `return` `-1;` `}` `// Driver Code` `public` `static` `void` `Main()` `{` ` ` ` ` `// Given Number N` ` ` `int` `N = 25;` ` ` `// Function Call` ` ` `Console.WriteLine(minimumMoves(N));` `}` `}` `// This code is contributed by SURENDRA_GANGWAR` |

## Javascript

`<script>` `// Javascript program to implement` `// the above approach` `// Function to find the minimum number` `// operations required to reduce N to 1` `function` `minimumMoves(n)` `{` ` ` ` ` `// Stores count of powers of 2 and 5` ` ` `let cnt2 = 0, cnt5 = 0;` ` ` ` ` `// Calculating the primefactors 2` ` ` `while` `(n % 2 == 0)` ` ` `{` ` ` `n /= 2;` ` ` `cnt2++;` ` ` `}` ` ` ` ` `// Calculating the primefactors 5` ` ` `while` `(n % 5 == 0)` ` ` `{` ` ` `n /= 5;` ` ` `cnt5++;` ` ` `}` ` ` ` ` `// If n is 1 and cnt2 <= cnt5` ` ` `if` `(n == 1 && cnt2 <= cnt5)` ` ` `{` ` ` ` ` `// Return the minimum operations` ` ` `return` `2 * cnt5 - cnt2;` ` ` `}` ` ` ` ` `// Otherwise, n can't be reduced` ` ` `else` ` ` `return` `-1;` `}` ` ` `// Driver Code` ` ` ` ` `// Given Number N` ` ` `let N = 25;` ` ` ` ` `// Function Call` ` ` `document.write(minimumMoves(N));` ` ` `</script>` |

**Output:**

4

**Time Complexity:** O(log N)**Auxiliary Space:** O(1)