# Count the minimum steps to reach 0 from the given integer N

Given **two integers N and K** where K represents the number of jumps that we are allowed to make directly from N reducing N to N – K, our task is to count minimum steps to reach 0 following the given operations:

- We can jump by a amount of K from N that is N = N – K
- Decrement N by 1 that is N = N -1.

**Examples:**

Input:N = 11, K = 4

Output:5

Explanation:

For the given value N we can perform the operation in the given sequence: 11 -> 7 -> 3 -> 2 -> 1 -> 0

Input:N = 6, K = 3

Output:2

Explanation:

For the given value N we can perform the operation in the given sequence: 6 -> 3 -> 0.

**Approach:**

To solve the problem mentioned above we know that it will take **N / K** steps to directly jump from value N to least divisible value with K and **N % K** steps to decrement it by 1 such as to reduce the count to 0. So the total number of steps required to **reach 0 from N will be **

(N / K) + (N % K)

Below is the implementation of the above approach:

## C++

`// C++ program to Count the minimum steps ` `// to reach 0 from the given integer N ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function retuns min step ` `// to reach 0 from N ` `int` `getMinSteps(` `int` `n, ` `int` `jump) ` `{ ` ` ` `// Direct possible ` ` ` `// reduction of value N ` ` ` `int` `quotient = n / jump; ` ` ` ` ` `// Remaining steps needs ` ` ` `// to be reduced by 1 ` ` ` `int` `remainder = n % jump; ` ` ` ` ` `// Summation of both the values ` ` ` `int` `steps = quotient + remainder; ` ` ` ` ` `// Return the final answer ` ` ` `return` `steps; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 6, K = 3; ` ` ` ` ` `cout << getMinSteps(N, K); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count the minimum steps ` `// to reach 0 from the given integer N ` `class` `GFG{ ` ` ` `// Function retuns min step ` `// to reach 0 from N ` `static` `int` `getMinSteps(` `int` `n, ` `int` `jump) ` `{ ` ` ` ` ` `// Direct possible ` ` ` `// reduction of value N ` ` ` `int` `quotient = n / jump; ` ` ` ` ` `// Remaining steps needs ` ` ` `// to be reduced by 1 ` ` ` `int` `remainder = n % jump; ` ` ` ` ` `// Summation of both the values ` ` ` `int` `steps = quotient + remainder; ` ` ` ` ` `// Return the final answer ` ` ` `return` `steps; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `6` `, K = ` `3` `; ` ` ` ` ` `System.out.print(getMinSteps(N, K)); ` `} ` `} ` ` ` `// This code is contributed by Rohit_ranjan ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to Count the minimum steps ` `# to reach 0 from the given integer N ` ` ` `# Function retuns min step ` `# to reach 0 from N ` `def` `getMinSteps(n, jump): ` ` ` ` ` `# Direct possible ` ` ` `# reduction of value N ` ` ` `quotient ` `=` `int` `(n ` `/` `jump) ` ` ` ` ` `# Remaining steps needs ` ` ` `# to be reduced by 1 ` ` ` `remainder ` `=` `n ` `%` `jump ` ` ` ` ` `# Summation of both the values ` ` ` `steps ` `=` `quotient ` `+` `remainder ` ` ` ` ` `# Return the final answer ` ` ` `return` `steps ` ` ` `# Driver code ` `N ` `=` `6` `K ` `=` `3` ` ` `print` `(getMinSteps(N, K)) ` ` ` `# This code is contributed by PratikBasu ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count the minimum steps ` `// to reach 0 from the given integer N ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function retuns min step ` `// to reach 0 from N ` `static` `int` `getMinSteps(` `int` `n, ` `int` `jump) ` `{ ` ` ` ` ` `// Direct possible ` ` ` `// reduction of value N ` ` ` `int` `quotient = n / jump; ` ` ` ` ` `// Remaining steps needs ` ` ` `// to be reduced by 1 ` ` ` `int` `remainder = n % jump; ` ` ` ` ` `// Summation of both the values ` ` ` `int` `steps = quotient + remainder; ` ` ` ` ` `// Return the final answer ` ` ` `return` `steps; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(` `string` `[] args) ` `{ ` ` ` `int` `N = 6, K = 3; ` ` ` ` ` `Console.Write(getMinSteps(N, K)); ` `} ` `} ` ` ` `// This code is contributed by rutvik_56 ` |

*chevron_right*

*filter_none*

**Output:**

2

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Minimum steps to reach N from 1 by multiplying each step by 2, 3, 4 or 5
- Find the minimum number of steps to reach M from N
- Minimum steps to reach the Nth stair in jumps of perfect power of 2
- Count ways to reach the Nth stair using multiple 1 or 2 steps and a single step 3
- Count minimum steps to get the given desired array
- Count minimum factor jumps required to reach the end of an Array
- Number of steps required to reach point (x,y) from (0,0) using zig-zag way
- Minimize the number of steps required to reach the end of the array | Set 2
- Minimize steps to reach K from 0 by adding 1 or doubling at each step
- Minimum decrements to make integer A divisible by integer B
- Minimum step to reach one
- Minimum steps to color the tree with given colors
- Generate array with minimum sum which can be deleted in P steps
- Minimum number of jumps to reach end
- Minimum steps to make sum and the product of all elements of array non-zero
- Minimum steps to make the product of the array equal to 1
- Minimum steps required to reduce all the elements of the array to zero
- Minimum number of moves to reach N starting from (1, 1)
- Minimum steps to come back to starting point in a circular tour
- Minimum cost to reach a point N from 0 with two different operations allowed

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.