Dada una array de orden m*n, la tarea es encontrar la frecuencia de los números pares e impares en la array
. Ejemplos:
Input : m = 3, n = 3 { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } Output : Frequency of odd number = 5 Frequency of even number = 4 Input : m = 3, n = 3 { 10, 11, 12 }, { 13, 14, 15 }, { 16, 17, 18 } Output : Frequency of odd number = 4 Frequency of even number = 5
Python3
# Python Program to Find the frequency # of even and odd numbers in a matrix MAX=100 # Function for calculating frequency def freq(ar, m, n): even = 0 odd = 0 for i in range(m): for j in range(n): # modulo by 2 to check # even and odd if ((ar[i][j] % 2) == 0): even += 1 else: odd += 1 # print Frequency of numbers print(" Frequency of odd number =", odd) print(" Frequency of even number =", even) # Driver code m = 3 n = 3 array = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] freq(array, m, n) # This code is contributed # by Anant Agarwal.
Producción:
Frequency of odd number = 5 Frequency of even number = 4
Complejidad del tiempo: O(m*n)
Espacio Auxiliar: O(1)
Consulte el artículo completo sobre Frecuencias de números pares e impares en una array para obtener más detalles.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA