Given a matrix of order m*n then the task is to find the frequency of even and odd numbers in matrix
Examples:
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
Implementation:
CPP
#include<bits/stdc++.h>
using namespace std;
#define MAX 100
void freq( int ar[][MAX], int m, int n)
{
int even = 0, odd = 0;
for ( int i = 0; i < m; ++i)
{
for ( int j = 0; j < n; ++j)
{
if ((ar[i][j] % 2) == 0)
++even;
else
++odd;
}
}
printf ( " Frequency of odd number = %d \n" , odd);
printf ( " Frequency of even number = %d \n" , even);
}
int main()
{
int m = 3, n = 3;
int array[][MAX] = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
freq(array, m, n);
return 0;
}
|
Java
class GFG {
static final int MAX = 100 ;
static void freq( int ar[][], int m, int n) {
int even = 0 , odd = 0 ;
for ( int i = 0 ; i < m; ++i)
{
for ( int j = 0 ; j < n; ++j)
{
if ((ar[i][j] % 2 ) == 0 )
++even;
else
++odd;
}
}
System.out.print( " Frequency of odd number =" +
odd + " \n" );
System.out.print( " Frequency of even number = " +
even + " \n" );
}
public static void main(String[] args) {
int m = 3 , n = 3 ;
int array[][] = {{ 1 , 2 , 3 }, { 4 , 5 , 6 }, { 7 , 8 , 9 }};
freq(array, m, n);
}
}
|
Python3
MAX = 100
def freq(ar, m, n):
even = 0
odd = 0
for i in range (m):
for j in range (n):
if ((ar[i][j] % 2 ) = = 0 ):
even + = 1
else :
odd + = 1
print ( " Frequency of odd number =" , odd)
print ( " Frequency of even number =" , even)
m = 3
n = 3
array = [[ 1 , 2 , 3 ],
[ 4 , 5 , 6 ],
[ 7 , 8 , 9 ]]
freq(array, m, n)
|
C#
using System;
class GFG
{
static void freq( int [,]ar, int m, int n)
{
int even = 0, odd = 0;
for ( int i = 0; i < m; ++i)
{
for ( int j = 0; j < n; ++j)
{
if ((ar[i, j] % 2) == 0)
++even;
else
++odd;
}
}
Console.WriteLine( " Frequency of odd number =" +
odd );
Console.WriteLine( " Frequency of even number = " +
even );
}
public static void Main()
{
int m = 3, n = 3;
int [,]array = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
freq(array, m, n);
}
}
|
PHP
<?php
$MAX = 100;
function freq( $ar , $m , $n )
{
$even = 0; $odd = 0;
for ( $i = 0; $i < $m ; ++ $i )
{
for ( $j = 0; $j < $n ; ++ $j )
{
if (( $ar [ $i ][ $j ] % 2) == 0)
++ $even ;
else
++ $odd ;
}
}
echo " Frequency of odd number = "
, $odd , "\n" ;
echo " Frequency of even number = "
, $even ;
}
$m = 3; $n = 3;
$array = array ( array (1, 2, 3),
array (4, 5, 6),
array (7, 8, 9));
freq( $array , $m , $n );
?>
|
Javascript
<script>
let MAX = 100;
function freq(ar,m,n) {
let even = 0, odd = 0;
for (let i = 0; i < m; ++i)
{
for (let j = 0; j < n; ++j)
{
if ((ar[i][j] % 2) == 0)
++even;
else
++odd;
}
}
document.write( " Frequency of odd number =" +
odd + " <br>" );
document.write( " Frequency of even number = " +
even + "<br>" );
}
let m = 3, n = 3;
let array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
freq(array, m, n);
</script>
|
Output
Frequency of odd number = 5
Frequency of even number = 4
Time Complexity: O(n*m)
Auxiliary Space: O(1), as no extra space is used
Method: Using bitwise & operator
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX 100
void freq( int ar[][MAX], int m, int n)
{
int even = 0, odd = 0;
for ( int i = 0; i < m; ++i) {
for ( int j = 0; j < n; ++j) {
if ((ar[i][j] & 1) == 0)
++even;
else
++odd;
}
}
cout << "Frequency of odd number = " << odd << endl;
cout << "Frequency of even number = " << even << endl;
}
int main()
{
int m = 3, n = 3;
int array[][MAX]
= { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
freq(array, m, n);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static void freq( int [][] ar, int m, int n)
{
int even = 0 , odd = 0 ;
for ( int i = 0 ; i < m; i++) {
for ( int j = 0 ; j < n; j++) {
if ((ar[i][j] & 1 ) == 0 )
++even;
else
++odd;
}
}
System.out.println( "Frequency of odd number = " + odd);
System.out.println( "Frequency of even number = " + even);
}
public static void main(String[] args)
{
int m = 3 , n = 3 ;
int [][] array= { { 1 , 2 , 3 }, { 4 , 5 , 6 }, { 7 , 8 , 9 } };
freq(array, m, n);
}
}
|
Python3
def freq(ar, m, n):
even = 0
odd = 0
for i in range (m):
for j in range (n):
if ((ar[i][j] & 1 ) = = 0 ):
even + = 1
else :
odd + = 1
print ( "Frequency of odd numbers = " , end = "")
print (odd)
print ( "Frequency of even numbers = " , end = "")
print (even)
m = 3
n = 3
array = [[ 1 , 2 , 3 ], [ 4 , 5 , 6 ], [ 7 , 8 , 9 ]]
freq(array, m, n)
|
C#
using System;
public class Program
{
public static void Freq( int [,] ar, int m, int n)
{
int even = 0, odd = 0;
for ( int i = 0; i < m; i++)
{
for ( int j = 0; j < n; j++)
{
if ((ar[i, j] & 1) == 0)
++even;
else
++odd;
}
}
Console.WriteLine( "Frequency of odd number = " + odd);
Console.WriteLine( "Frequency of even number = " + even);
}
public static void Main( string [] args)
{
int m = 3, n = 3;
int [,] array = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
Freq(array, m, n);
}
}
|
Javascript
function freq(ar, m, n){
let even = 0;
let odd = 0;
for (let i = 0; i<m; i++)
{
for (let j = 0; j<n; j++)
{
if ((ar[i][j] & 1) == 0){
++even;
}
else {
++odd;
}
}
}
console.log( "Frequency of odd numbers = " + odd + "\n" );
console.log( "Frequency of even numbers = " + even + "\n" );
}
let m = 3, n = 3;
let array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
freq(array, m, n);
|
Output
Frequency of odd number = 5
Frequency of even number = 4
Time Complexity: O(n*m)
Auxiliary Space: O(1), as no extra space is used
This article is contributed by Aarti_Rathi. Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!