Matrix is simply a two-dimensional array. Just like in one-dimensional traversal operation is required to figure out. The task is to find the frequencies of odd and even number in the given matrix.

The size of matrix is defined as product of numbers of columns and rows. So, the size of the matrix is m × n

Here in the matrix

- M = number of rows in the matrix
- N = number of columns

**Example:**

Input :M = 3 N = 5Output:Odd Number Frequency = 9 Even Number Frequency = 6Input :M = 3 N = 3 {{1, 2, 5}, {6, 7, 9}, {3, 2, 1}}Output:Odd Number Frequency = 6 Even Number Frequency = 3

**Approach**

- Initialize two counters as even = 0 & odd = 0
- Traverse through every element present in the matrix and check whether (element % 2 == 0)
- If Yes then increment even++
- Else increment odd++

Below is the implementation of the approach in Java

## Java

`// Importing Classes/Files ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to compute frequency of odd/even numbers ` ` ` `public` `static` `void` `findFreq(` `int` `[][] arr, ` `int` `m, ` `int` `n) ` ` ` `{ ` ` ` `// Initializing the counter variables ` ` ` `int` `even = ` `0` `, odd = ` `0` `; ` ` ` ` ` `// Nested if loops ` ` ` `// Traversing through each ` ` ` `// element in the matrix ` ` ` `for` `(` `int` `i = ` `0` `; i < m; i++) { ` ` ` ` ` `for` `(` `int` `j = ` `0` `; j < n; j++) { ` ` ` ` ` `// Checking if the element ` ` ` `// is divisible by 2 ` ` ` `if` `(arr[i][j] % ` `2` `== ` `0` `) { ` ` ` ` ` `// Increment even counter ` ` ` `even++; ` ` ` `} ` ` ` `else` `{ ` ` ` ` ` `// Increment odd counter ` ` ` `odd++; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Printing Counts of Enen ` ` ` `// and odd numbers in matrix ` ` ` `System.out.println(` `"Odd Number Frequency: "` `+ odd); ` ` ` `System.out.println(` `"Even Number Frequence: "` ` ` `+ even); ` ` ` `} ` ` ` ` ` `// Main Driver Method ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `// Providing inputs to the matrix ` ` ` `int` `m = ` `3` `, n = ` `5` `; ` ` ` `// Here, m = Number of rows, ` ` ` `// n = Number of columns ` ` ` ` ` `// Entering elements in the matrix ` ` ` `int` `[][] arr = { { ` `3` `, ` `4` `, ` `5` `, ` `6` `, ` `3` `}, ` ` ` `{ ` `4` `, ` `3` `, ` `2` `, ` `7` `, ` `9` `}, ` ` ` `{ ` `1` `, ` `5` `, ` `7` `, ` `2` `, ` `4` `} }; ` ` ` ` ` `// Calling function to count frequency by ` ` ` `// passing inputs to the function findFreq ` ` ` `findFreq(arr, m, n); ` ` ` `} ` `}` |

*chevron_right*

*filter_none*

**Output**

Odd Number Frequency: 9 Even Number Frequence: 6

**Time Complexity****: **As the single-dimensional array is traversed it depends on the size of the array over which the operation performs because rest execution either is of the same order or taking some constant space in the memory(variables). Here the traversal is over all the elements of the row and for every row in the matrix. So it depends on the product of rows and columns. Hence, the time complexity is of order(degree)

**Space Complexity****: **Just assigning memory to variables that later gets released with the scope of the variables. As no other auxiliary space is created for the operations to execute. Hence, the space required is constant which is released afterward. 1 is assigned by default hence space complexity is of order 1.

Time Complexity = O(Rows*Columns) or O(m * n) Space Complexity = O(1)

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the **Fundamentals of Java and Java Collections Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Java Program to Find the Sum of First N Odd & Even Numbers
- Print even and odd numbers in increasing order using two threads in Java
- Java Program for Odd-Even Sort / Brick Sort
- Java Program to Check if count of divisors is even or odd
- Java Program for Difference between sums of odd and even digits
- Java Program to Check if a Given Integer is Odd or Even
- Java Program to Store Even & Odd Elements of an Array into Separate Arrays
- Java Program to Print All the Repeated Numbers with Frequency in an Array
- Java Program to Determine if a given Matrix is a Sparse Matrix
- Java Program to Find the Number Occurring Odd Number of Times
- Java Program to Find Maximum Odd Number in Array Using Stream and Filter
- Java Program to Find sum of even factors of a number
- Java.util.Collections.frequency() in Java
- Java.util.Collections.frequency() in Java with Examples
- TCS Coding Practice Question | Check Odd or Even
- Java Program to find transpose of a matrix
- Program for Find sum of odd factors of a number
- Java Program for Number of elements with odd factors in given range
- Java Program to Print the Elements of an Array Present on Odd Position
- Sort elements by frequency | Set 5 (using Java Map)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.