Dados dos números enteros N y K donde K representa el número de saltos que podemos hacer directamente de N reduciendo N a N – K, nuestra tarea es contar los pasos mínimos para llegar a 0 siguiendo las operaciones dadas:
- Podemos saltar una cantidad de K desde N que es N = N – K
- Decrementa N en 1 que es N = N -1.
Ejemplos:
Entrada: N = 11, K = 4
Salida: 5
Explicación:
Para el valor N dado, podemos realizar la operación en la secuencia dada: 11 -> 7 -> 3 -> 2 -> 1 -> 0Entrada: N = 6, K = 3
Salida: 2
Explicación:
Para el valor N dado podemos realizar la operación en la secuencia dada: 6 -> 3 -> 0.
Enfoque:
para resolver el problema mencionado anteriormente, sabemos que tomará N / K pasos para saltar directamente del valor N al valor menos divisible con K y N % K pasos para disminuirlo en 1, como para reducir el conteo a 0. Entonces el número total de pasos necesarios para llegar a 0 desde N será
(N / K) + (N % K)
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to Count the minimum steps // to reach 0 from the given integer N #include <bits/stdc++.h> using namespace std; // Function returns 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; }
Java
// Java program to count the minimum steps // to reach 0 from the given integer N class GFG{ // Function returns 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
Python3
# Python3 program to Count the minimum steps # to reach 0 from the given integer N # Function returns 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
C#
// C# program to count the minimum steps // to reach 0 from the given integer N using System; class GFG{ // Function returns 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
Javascript
<script> // JavaScript program to Count the minimum steps // to reach 0 from the given integer N // Function returns min step // to reach 0 from N function getMinSteps(n, jump) { // Direct possible // reduction of value N let quotient = Math.floor(n / jump); // Remaining steps needs // to be reduced by 1 let remainder = n % jump; // Summation of both the values let steps = quotient + remainder; // Return the final answer return steps; } // Driver code let N = 6, K = 3; document.write(getMinSteps(N, K)); // This code is contributed by Surbhi Tyagi. </script>
2