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.
Input: N = 11, K = 4
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
For the given value N we can perform the operation in the given sequence: 6 -> 3 -> 0.
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:
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.