Dada una array arr[] de tamaño N que consta de enteros positivos distintos de cero. La tarea es determinar si el número que se forma seleccionando los últimos dígitos de todos los números es divisible por 10 o no. Si el número es divisible por 10, escriba Sí; de lo contrario, escriba No.
Ejemplos:
Entrada: arr[] = {12, 65, 46, 37, 99}
Salida: No
25679 no es divisible por 10.
Entrada: arr[] = {24, 37, 46, 50}
Salida: Sí
4760 es divisible por 10 .
Enfoque: para que un número entero sea divisible por 10, debe terminar en 0. Entonces, el último elemento de la array decidirá si el número formado será divisible por 10 o no. Si el último dígito del último elemento es 0, imprima Sí; de lo contrario, imprima No.
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 formed by the last digits of // all the elements is divisible by 10 bool isDivisible(int arr[], int n) { // Last digit of the last element int lastDigit = arr[n - 1] % 10; // Number formed will be divisible by 10 if (lastDigit == 0) return true; return false; } // Driver code int main() { int arr[] = { 12, 65, 46, 37, 99 }; int n = sizeof(arr) / sizeof(arr[0]); if (isDivisible(arr, n)) cout << "Yes"; else cout << "No"; return 0; }
Java
// Java implementation of the approach import java.util.*; class GFG { // Function that returns true if the // number formed by the last digits of // all the elements is divisible by 10 static boolean isDivisible(int arr[], int n) { // Last digit of the last element int lastDigit = arr[n - 1] % 10; // Number formed will be divisible by 10 if (lastDigit == 0) return true; return false; } // Driver code static public void main ( String []arg) { int arr[] = { 12, 65, 46, 37, 99 }; int n = arr.length; if (isDivisible(arr, n)) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by Rajput-Ji
Python3
# Python3 implementation of the approach # Function that returns true if the # number formed by the last digits of # all the elements is divisible by 10 def isDivisible(arr, n) : # Last digit of the last element lastDigit = arr[n - 1] % 10; # Number formed will be divisible by 10 if (lastDigit == 0) : return True; return False; # Driver code if __name__ == "__main__" : arr = [ 12, 65, 46, 37, 99 ]; n = len(arr); if (isDivisible(arr, n)) : print("Yes"); else : print("No"); # This code is contributed by AnkitRai01
C#
// C# implementation of the approach using System; class GFG { // Function that returns true if the // number formed by the last digits of // all the elements is divisible by 10 static bool isDivisible(int []arr, int n) { // Last digit of the last element int lastDigit = arr[n - 1] % 10; // Number formed will be divisible by 10 if (lastDigit == 0) return true; return false; } // Driver code static public void Main(String []arg) { int []arr = { 12, 65, 46, 37, 99 }; int n = arr.Length; if (isDivisible(arr, n)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code is contributed by Rajput-Ji
Javascript
<script> // Javascript implementation of the approach // Function that returns true if the // number formed by the last digits of // all the elements is divisible by 10 function isDivisible(arr, n) { // Last digit of the last element let lastDigit = arr[n - 1] % 10; // Number formed will be divisible by 10 if (lastDigit == 0) return true; return false; } // Driver code let arr = [ 12, 65, 46, 37, 99 ]; let n = arr.length; if (isDivisible(arr, n)) document.write("Yes"); else document.write("No"); </script>
No
Complejidad de tiempo: O(1)
Espacio Auxiliar: O(1)