Dados tres enteros K , L y R (rango [L, R] ), la tarea es encontrar el número mínimo de elementos por los que se debe extender el rango para que el conteo de elementos en el rango sea divisible por K .
Ejemplos:
Entrada: K = 3, L = 10, R = 10
Salida: 2 La
cantidad de elementos en L a R es 1.
Entonces, para hacerlo divisible por 3, increméntelo en 2.Entrada: K = 5, L = 9, R = 12
Salida: 1
Acercarse:
- Cuente el número total de elementos en el rango y guárdelo en una variable llamada count = R – L + 1 .
- Ahora, el número mínimo de elementos que deben agregarse al rango será K – (recuento % K).
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; int minimumMoves(int k, int l, int r) { // Total elements in the range int count = r - l + 1; // If total elements are already divisible by k if (count % k == 0) return 0; // Value that must be added to count // in order to make it divisible by k return (k - (count % k)); } // Driver Program to test above function int main() { int k = 3, l = 10, r = 10; cout << minimumMoves(k, l, r); return 0; }
Java
// Java implementation of the approach import java.io.*; class GFG { static int minimumMoves(int k, int l, int r) { // Total elements in the range int count = r - l + 1; // If total elements are already divisible by k if (count % k == 0) return 0; // Value that must be added to count // in order to make it divisible by k return (k - (count % k)); } // Driver Program to test above function public static void main (String[] args) { int k = 3, l = 10, r = 10; System.out.print(minimumMoves(k, l, r)); } } // This code is contributed // by inder_verma..
Python3
# Python 3 implementation of the approach def minimumMoves(k, l, r): # Total elements in the range count = r - l + 1 # If total elements are already divisible by k if (count % k == 0): return 0 # Value that must be added to count # in order to make it divisible by k return (k - (count % k)) # Driver Program to test above function if __name__ == '__main__': k = 3 l = 10 r = 10 print(minimumMoves(k, l, r)) # This code is contributed by # Surendra_Gangwar
C#
// C# implementation of the approach using System; class GFG { static int minimumMoves(int k, int l, int r) { // Total elements in the range int count = r - l + 1; // If total elements are already divisible by k if (count % k == 0) return 0; // Value that must be added to count // in order to make it divisible by k return (k - (count % k)); } // Driver Program to test above function public static void Main () { int k = 3, l = 10, r = 10; Console.WriteLine(minimumMoves(k, l, r)); } } // This code is contributed // by inder_verma..
PHP
<?php // PHP implementation of the approach function minimumMoves($k, $l, $r) { // Total elements in the range $count = $r - $l + 1; // If total elements are already divisible by k if ($count % $k == 0) return 0; // Value that must be added to count // in order to make it divisible by k return ($k - ($count % $k)); } // Driver Program to test above function $k = 3; $l = 10; $r = 10; echo minimumMoves($k, $l, $r); // This code is contributed // by inder_verma.. ?>
Javascript
<script> // Javascript implementation of the approach function minimumMoves(k, l, r) { // Total elements in the range let count = r - l + 1; // If total elements are already // divisible by k if (count % k == 0) return 0; // Value that must be added to count // in order to make it divisible by k return (k - (count % k)); } // Driver code let k = 3, l = 10, r = 10; document.write(minimumMoves(k, l, r)); // This code is contributed by souravmahato348 </script>
Producción:
2
Complejidad de tiempo: O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por Abdullah Aslam y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA