JavaScript Program for Frequencies of even and odd numbers in a matrix


In this tutorial, we will implement a JavaScript program for finding the frequencies of even and odd numbers. We will be given a 2D matrix that is the size of MXN and we have to find the frequencies (means the count of element present) of all odd and even numbers present in the matrix. We will see two approaches, one will be the brute force approach (using for loops) and another will be the bitwise and operator approach.

Introduction to Problem

In this problem, we are given a 2d matrix that contains odd and even numbers in an unsorted manner we have to find the frequencies of odd and even numbers in the 2d matrix. For example −

In this problem, we are given a 2d matrix that contains odd and even numbers in an unsorted manner we have to find the frequencies of odd and even numbers in the 2d matrix. For example −

Row ( m ) = 3; Column ( n ) = 3;
Matrix = [[1, 2, 3], 
          [4, 5, 6], 
          [7, 8, 9]];

From the given matrix of size 3X3, we have frequencies of odd and even numbers are −

Frequency of odd number =  5 
Frequency of even number = 4

We can return the frequencies, we can simply print them as it is, let’s move to the approach of the problem −

Approach 1 (Using mod ‘%’ operator)

In this approach, we simply traverse the matrix using nested for loop. While traversing the for loops we calculated the frequencies of odd and even numbers in the 2d matrix. For checking whether the value is odd or even here we the used mod (‘%’) operator basically taking mod of 2 with the numbers if the mod value is ‘1’ it means it is an odd value otherwise it is an even value and in the end, we return those count of odd and even numbers. Let’s move to the code of this approach for a better understanding.

Example

Below is a JavaScript Program to find the frequency of even and odd numbers in a matrix using mode operator −

let MAX = 100;
// function for calculating frequency
function freq(arr,row,col) {
   let evenNum = 0, oddNum = 0;
   for (let i = 0; i < row; ++i) {
      for (let j = 0; j < col; ++j) {
         // checking evenNum or oddNum using mod operator
         if ((arr[i][j] % 2) == 0)
         ++evenNum;
         else
         ++oddNum;
      }
   }
   // print Frequency of odd and even numbers
   console.log(" Frequency of odd numbers = " + oddNum );
   console.log(" Frequency of even numbers = " + evenNum );
}
let m = 3, n = 3;
let arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
freq(arr, m, n);

Time and Space Complexity

The time complexity of the above code is O(M*N), where M is the size of row and N is the size of the column of the 2d matrix.

The space complexity of the above code is O(1) because we have used a int value to store only a integer.

Approach 2 (Using bitwise ‘&’ operator)

In this approach, we are going to simply traverse the matrix using a nested for loop. While traversing the for loops we calculated the frequencies of odd and even numbers in the 2d matrix. For checking whether the value is odd or even here we used the bitwise (‘&’) operator basically taking a bitwise of 1 with the numbers, if bitwise value is ‘0’ it means it is an even value otherwise it is an odd value and in the end we return those count of odd and even numbers. Let’s move to the code of this approach for a better understanding.

Example

Below is a JavaScript program to find the frequency of even and odd numbers in a matrix using the bitwise & operator −

let MAX = 100;
// function for calculating frequency
function freq(arr,row,col) {
   let evenNum = 0, oddNum = 0;
   for (let i = 0; i < row; ++i) {
      for (let j = 0; j < col; ++j) {
         // checking evenNum or oddNum using bitwise operator
         if ((arr[i][j] & 1) == 0)
         ++evenNum;
         else
         ++oddNum;
      }
   }
   // print Frequency of odd and even numbers
   console.log(" Frequency of odd numbers = " + oddNum );
   console.log(" Frequency of even numbers = " + evenNum );
}
let m = 3, n = 3;
let arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
freq(arr, m, n);

Time and Space Complexity

The time complexity of the above code is O(M*N), where M is the size of the row and N is the size of the column of the 2d matrix.

The space complexity of the above code is O(1) because we have used an int value to store only an integer.

Conclusion

In this tutorial, we have implemented a javascript program for finding the frequencies of even and odd numbers in a 2d matrix. We have given a 2d matrix of size NXM and we have to find the frequencies of the odd values and even values of the 2d matrix. We have seen two approaches one is by using the mod operator and another one is by using the bitwise operator.

Updated on: 24-Mar-2023

88 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements