Dado un entero grande X representado como una array arr[] donde cada arr[i] almacena un dígito en X . La tarea es verificar si el número representado por la array es divisible por el entero Y dado .
Ejemplos:
Entrada: arr[] = {1, 2, 1, 5, 6}, Y = 4
Salida: Sí
12156 / 4 = 3039
Entrada: arr[] = {1, 1, 1, 1, 1, 1, 1, 1, 1}, Y = 14
Salida: No
Enfoque: comience a recorrer los dígitos del número dado desde la izquierda y tome el número más grande que sea menor o igual que Y y divídalo con Y. Si el resto es distinto de 0 , se trasladará al siguiente número posible formado a partir de los dígitos restantes, al igual que en la división larga . Después de procesar el número completo, si el resto sigue siendo algo distinto de 0, entonces el número representado no es divisible por Y , de lo contrario lo es.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <iostream> using namespace std; // Function that returns true if the number represented // by the given array is divisible by y bool isDivisible(int* arr, int n, int y) { int d = 0, i = 0; // While there are digits left while (i < n) { // Select the next part of the number // i.e. the maximum number which is <= y while (d < y && i < n) d = d * 10 + arr[i++]; // Get the current remainder d = d % y; } // If the final remainder is 0 if (d == 0) return true; return false; } // Driver code int main() { int arr[] = { 1, 2, 1, 5, 6 }; int x = sizeof(arr) / sizeof(int); int y = 4; cout << (isDivisible(arr, x, y) ? "Yes" : "No"); return 0; }
Java
// Java implementation of the approach class GFG { // Function that returns true if the number represented // by the given array is divisible by y static boolean isDivisible(int [] arr, int n, int y) { int d = 0, i = 0; // While there are digits left while (i < n) { // Select the next part of the number // i.e. the maximum number which is <= y while (d < y && i < n) d = d * 10 + arr[i++]; // Get the current remainder d = d % y; } // If the final remainder is 0 if (d == 0) return true; return false; } // Driver code public static void main (String[] args) { int [] arr = { 1, 2, 1, 5, 6 }; int x = arr.length; int y = 4; System.out.println(isDivisible(arr, x, y) ? "Yes" : "No"); } } // This code is contributed by ihritik
Python3
# Python3 implementation of the approach # Function that returns true if the number represented # by the given array is divisible by y def isDivisible(arr, n, y): d, i = 0, 0 # While there are digits left while i < n: # Select the next part of the number # i.e. the maximum number which is <= y while d < y and i < n: d = d * 10 + arr[i] i += 1 # Get the current remainder d = d % y # If the final remainder is 0 if d == 0: return True return False # Driver code if __name__ == "__main__": arr = [ 1, 2, 1, 5, 6 ] x = len(arr) y = 4 if (isDivisible(arr, x, y)): print("Yes") else: print("No") # This code is contributed by # sanjeev2552
C#
// C# implementation of the approach using System; class GFG { // Function that returns true if the number represented // by the given array is divisible by y static bool isDivisible(int [] arr, int n, int y) { int d = 0, i = 0; // While there are digits left while (i < n) { // Select the next part of the number // i.e. the maximum number which is <= y while (d < y && i < n) d = d * 10 + arr[i++]; // Get the current remainder d = d % y; } // If the final remainder is 0 if (d == 0) return true; return false; } // Driver code public static void Main () { int [] arr = { 1, 2, 1, 5, 6 }; int x = arr.Length; int y = 4; Console.WriteLine(isDivisible(arr, x, y) ? "Yes" : "No"); } } // This code is contributed by ihritik
Javascript
<script> // JavaScript implementation of the approach // Function that returns true if the number represented // by the given array is divisible by y function isDivisible(vararr , n , y) { var d = 0, i = 0; // While there are digits left while (i < n) { // Select the next part of the number // i.e. the maximum number which is <= y while (d < y && i < n) d = d * 10 + arr[i++]; // Get the current remainder d = d % y; } // If the final remainder is 0 if (d == 0) return true; return false; } // Driver code var arr = [ 1, 2, 1, 5, 6 ]; var x = arr.length; var y = 4; document.write(isDivisible(arr, x, y) ? "Yes" : "No"); // This code is contributed by 29AjayKumar </script>
Yes