Give an integer **N**, the task is to find the minimum number of moves to reduce **N** to **0** by one of the following operations:

- Reduce N by 1.
- Reduce N to (N/2), if N is divisible by 2.
- Reduce N to (N/3), if N is divisible by 3.

**Examples:**

Input:N = 10Output:4Explanation:

Here N = 10

Step 1: Reducing N by 1 i.e., 10 – 1 = 9.

Step 2: Since 9 is divisible by 3, reduce it to N/3 = 9/3 = 3

Step 3: Since again 3 is divisible by 3 again repeating step 2, i.e., 3/3 = 1.

Step 4: 1 can be reduced by the step 1, i.e., 1-1 = 0

Hence, 4 steps are needed to reduce N to 0.

Input:N = 6Output:3Explanation:

Here N = 6

Step 1: Since 6 is divisible by 2, then 6/2 =3

Step 2: since 3 is divisible by 3, then 3/3 = 1.

Step 3: Reduce N to N-1 by 1, 1-1 = 0.

Hence, 3 steps are needed to reduce N to 0.

**Naive Approach:** The idea is to use recursion for all the possible moves. Below are the steps:

- Observe that base case for the problem, if
**N < 2**then for all the cases the answer will be N itself. - At every value of
**N**, choose between 2 possible cases:- Reduce n till n % 2 == 0 and then update n /= 2 with count = 1 + n%2 + f(n/2)
- Reduce n till n % 3 == 0 and then update n /= 3 with count = 1 + n%3 + f(n/3)

- Both the computation results in the recurrence relation as:

count = 1 + min(n%2 + f(n/2), n%3 + f(n/3))

where, f(n) is the minimum of moves to reduce N to 0.

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 to steps to reduce N to 0` `int` `minDays(` `int` `n)` `{` ` ` ` ` `// Base case` ` ` `if` `(n < 1)` ` ` `return` `n;` ` ` ` ` `// Recursive call to count the` ` ` `// minimum steps needed` ` ` `int` `cnt = 1 + min(n % 2 + minDays(n / 2),` ` ` `n % 3 + minDays(n / 3));` ` ` `// Return the answer` ` ` `return` `cnt;` `}` `// Driver Code` `int` `main()` `{` ` ` ` ` `// Given number N` ` ` `int` `N = 6;` ` ` ` ` `// Function call` ` ` `cout << minDays(N);` ` ` ` ` `return` `0;` `}` `// This code is contributed by 29AjayKumar` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach` `class` `GFG{` ` ` ` ` `// Function to find the minimum` ` ` `// number to steps to reduce N to 0` ` ` `static` `int` `minDays(` `int` `n)` ` ` `{` ` ` `// Base case` ` ` `if` `(n < ` `1` `)` ` ` `return` `n;` ` ` `// Recursive Call to count the` ` ` `// minimum steps needed` ` ` `int` `cnt = ` `1` `+ Math.min(n % ` `2` `+ minDays(n / ` `2` `),` ` ` `n % ` `3` `+ minDays(n / ` `3` `));` ` ` `// Return the answer` ` ` `return` `cnt;` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{` ` ` `// Given Number N` ` ` `int` `N = ` `6` `;` ` ` `// Function Call` ` ` `System.out.print(minDays(N));` ` ` `}` `}` `// This code is contributed by PrinciRaj1992` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach` `# Function to find the minimum` `# number to steps to reduce N to 0` `def` `minDays(n):` ` ` `# Base case` ` ` `if` `n < ` `1` `:` ` ` `return` `n` ` ` ` ` `# Recursive Call to count the` ` ` `# minimum steps needed` ` ` `cnt ` `=` `1` `+` `min` `(n ` `%` `2` `+` `minDays(n ` `/` `/` `2` `), ` ` ` `n ` `%` `3` `+` `minDays(n ` `/` `/` `3` `))` ` ` `# Return the answer` ` ` `return` `cnt` `# Driver Code` `# Given Number N` `N ` `=` `6` `# Function Call` `print` `(` `str` `(minDays(N)))` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach` `using` `System;` `class` `GFG{` `// Function to find the minimum` `// number to steps to reduce N to 0` `static` `int` `minDays(` `int` `n)` `{ ` ` ` `// Base case` ` ` `if` `(n < 1)` ` ` `return` `n;` ` ` `// Recursive call to count the` ` ` `// minimum steps needed` ` ` `int` `cnt = 1 + Math.Min(n % 2 + minDays(n / 2),` ` ` `n % 3 + minDays(n / 3));` ` ` `// Return the answer` ` ` `return` `cnt;` `}` `// Driver Code` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `// Given number N` ` ` `int` `N = 6;` ` ` `// Function call` ` ` `Console.Write(minDays(N));` `}` `}` `// This code is contributed by Rajput-Ji` |

*chevron_right*

*filter_none*

**Output:**

4

**Time Complexity:** O(2^{N})**Auxiliary Space:** O(1)

**Efficient Approach: **The idea is to use Dynamic Programming. The above recursive approach results in TLE because of the number of repeated subproblems. To optimize the above method using a dictionary to keep track of values whose recursive call is already performed to reduce the further computation such that value can be accessed faster.

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 to steps to reduce N to 0` `int` `count(` `int` `n)` `{` ` ` `// Dictionary for storing` ` ` `// the precomputed sum` ` ` `map<` `int` `, ` `int` `> dp;` ` ` `// Bases Cases` ` ` `dp[0] = 0;` ` ` `dp[1] = 1;` ` ` `// Check if n is not in dp then` ` ` `// only call the function so as` ` ` `// to reduce no of recursive calls` ` ` `if` `((dp.find(n) == dp.end()))` ` ` `dp[n] = 1 + min(n % 2 + ` ` ` `count(n / 2), n % 3 +` ` ` `count(n / 3));` ` ` `// Return the answer` ` ` `return` `dp[n];` `}` `// Driver Code` `int` `main()` `{ ` ` ` `// Given number N` ` ` `int` `N = 6;` ` ` `// Function call` ` ` `cout << count(N);` `}` `// This code is contributed by gauravrajput1` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach` `import` `java.util.HashMap;` `class` `GFG{` ` ` `// Function to find the minimum` `// number to steps to reduce N to 0` `static` `int` `count(` `int` `n)` `{` ` ` ` ` `// Dictionary for storing` ` ` `// the precomputed sum` ` ` `HashMap<Integer, ` ` ` `Integer> dp = ` `new` `HashMap<Integer,` ` ` `Integer>();` ` ` `// Bases Cases` ` ` `dp.put(` `0` `, ` `0` `);` ` ` `dp.put(` `1` `, ` `1` `);` ` ` `// Check if n is not in dp then` ` ` `// only call the function so as` ` ` `// to reduce no of recursive calls` ` ` `if` `(!dp.containsKey(n))` ` ` `dp.put(n, ` `1` `+ Math.min(n % ` `2` `+ ` ` ` `count(n / ` `2` `), n % ` `3` `+` ` ` `count(n / ` `3` `)));` ` ` `// Return the answer` ` ` `return` `dp.get(n);` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` ` ` `// Given number N` ` ` `int` `N = ` `6` `;` ` ` `// Function call` ` ` `System.out.println(String.valueOf((count(N))));` `}` `}` `// This code is contributed by Amit Katiyar` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach` `# Function to find the minimum` `# number to steps to reduce N to 0` `def` `count(n):` ` ` ` ` `# Dictionary for storing` ` ` `# the precomputed sum` ` ` `dp ` `=` `dict` `()` ` ` `# Bases Cases` ` ` `dp[` `0` `] ` `=` `0` ` ` `dp[` `1` `] ` `=` `1` ` ` `# Check if n is not in dp then` ` ` `# only call the function so as` ` ` `# to reduce no of recursive calls` ` ` `if` `n ` `not` `in` `dp:` ` ` `dp[n] ` `=` `1` `+` `min` `(n ` `%` `2` `+` `count(n` `/` `/` `2` `), n ` `%` `3` `+` `count(n` `/` `/` `3` `))` ` ` `# Return the answer` ` ` `return` `dp[n]` `# Driver Code` `# Given Number N` `N ` `=` `6` `# Function Call` `print` `(` `str` `(count(N)))` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach` `using` `System;` `using` `System.Collections.Generic;` `public` `class` `GFG{` ` ` `// Function to find the minimum` `// number to steps to reduce N to 0` `static` `int` `count(` `int` `n)` `{ ` ` ` `// Dictionary for storing` ` ` `// the precomputed sum` ` ` `Dictionary<` `int` `, ` ` ` `int` `> dp = ` `new` `Dictionary<` `int` `,` ` ` `int` `>();` ` ` `// Bases Cases` ` ` `dp.Add(0, 0);` ` ` `dp.Add(1, 1);` ` ` `// Check if n is not in dp then` ` ` `// only call the function so as` ` ` `// to reduce no of recursive calls` ` ` `if` `(!dp.ContainsKey(n))` ` ` `dp.Add(n, 1 + Math.Min(n % 2 + count(n / 2), ` ` ` `n % 3 + count(n / 3)));` ` ` `// Return the answer` ` ` `return` `dp[n];` `}` `// Driver Code` `public` `static` `void` `Main(String[] args)` `{ ` ` ` `// Given number N` ` ` `int` `N = 6;` ` ` `// Function call` ` ` `Console.WriteLine(String.Join(` `""` `, ` ` ` `(count(N))));` `}` `}` `// This code is contributed by Rajput-Ji` |

*chevron_right*

*filter_none*

**Output:**

3

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimum steps required to reduce all the elements of the array to zero
- Minimum number of given operations required to reduce the array to 0 element
- Reduce N to 1 with minimum number of given operations
- Minimum number of steps required to obtain the given Array by the given operations
- Reduce a given number to form a key by the given operations
- Minimum number of operations required to reduce N to 1
- Minimum number of operations required to reduce N to 0
- Steps to reduce N to zero by subtracting its most significant digit at every step
- Minimum Steps to obtain N from 1 by the given operations
- Reduce a number to 1 by performing given operations
- Count operations of the given type required to reduce N to 0
- Count the number of operations required to reduce the given number
- Reduce a number to 1 by performing given operations | Set 2
- Minimize Cost to reduce the Array to a single element by given operations
- Reduce N to 0 or less by given X and Y operations
- Pyramid form (increasing then decreasing) consecutive array using reduce operations
- Find maximum operations to reduce N to 1
- Min number of operations to reduce N to 0 by subtracting any digits from N
- Min operations to reduce N to 1 by multiplying by A or dividing by B
- Min operations to reduce N by multiplying by any number or taking square root

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.