Frequencies of Even and Odd Numbers in a Matrix in JavaScript



To get frequencies of even and odd numbers in a matrix, we will be discussing three different approaches. We will discuss each approach with code examples and solution of the algorithm being used.

In this article we are having a 2D matrix, our task is to write a JavaScript program for frequencies of even and odd numbers in a matrix. Users must be familiar with conditional statement, nested loops and javascript operators.

Example

Input:
Matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];

Even numbers: {2, 4, 6, 8}
Odd numbers: {1, 3, 5, 7, 9}

Output: 
Even: 4, Odd: 5

Approaches for Even and Odd Frequency in Matrix

Here is a list of approaches to write a JavaScript program for frequencies of even and odd numbers in a matrix which we will be discussing in this article with stepwise explanation and complete example codes.

Using Modulus Operator

To get frequencies of even and odd numbers in a matrix, we have used modulus(%) operator, which checks if the element is divisible by 2 and then increases the even or odd counter accordingly.

  • We have declared a 2D matrix and defined a function freq() that accepts matrix, m and n as argument where m and n represents rows and columns of matrix respectively.
  • Initially we have set the count value of even and odd to 0.
  • Then we have used nested for loop to traverse over the elements of matrix to check if they are even or odd using modulus (%) operator.
  • We have used if/else statement to check if modulus of the element by 2 is 0. If 0, then even otherwise odd. If the number is even, increase the count of even, similary for odd.
  • After completing the loop count of even and odd is displayed in web console using console.log() method. At the end function freq() is called.

Example

Here is a complete example code implementing above mentioned steps to get the frequencies of even and odd numbers in a matrix using modulus operator.

let m = 3, n = 3;
let arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];

function freq(arr, row, col) {
    let even = 0, odd = 0;
    for (let i = 0; i 


Using Bitwise AND Operator

In this approach we have used AND operator for frequencies of even and odd numbers in a matrix. We have used AND operator on element with 1. If AND operation results in 0, then even otherwise odd.

  • We have declared a 2D matrix and defined a function freq() that accepts matrix as argument.
  • Initially we have set the count value of even and odd to 0. The length property is used to get the number of rows and columns.
  • Then we have used nested for loop to traverse over the elements of matrix.
  • We have checked whether the element matrix[i][j] is even or odd by using AND operation of element with 1. If the result is 0, then even, increase the even counter by 1. If the result is non-zero increase the odd counter by 1.
  • After the completion of loop, we have returned the even and odd counter. Then the function freq() is called and the result is displayed in web console using console.log() method.

Solution

Matrix = 
[[1, 2, 3],
 [4, 5, 6],
 [7, 8, 9]];

let matrix[i][j] = 6
6 = 0110, 1 = 0001
6 & 1 = 0 => Even

let matrix[i][j] = 9
9 = 1001, 1 = 0001
9 & 1 = 1 => Odd

Example

Here is a complete example code implementing above mentioned steps to get the frequencies of even and odd numbers in a matrix using bitwise AND operator.

const matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];

function freq(matrix) {
    let even = 0, odd = 0;
    let m = matrix.length;
    let n = matrix[0].length;

    for (let i = 0; i 


Using Right Shift Operator

In this approach for frequencies of even and odd numbers in a matrix, we have used right shift (>>) and then left shift ( operation on matrix elements by 1. It is like first dividing the number by 2 and then multiplying that number by 2.

  • We have declared a 2D matrix and defined a function freq() that accepts matrix as argument.
  • Initially we have set the count value of even and odd to 0. The length property is used to get the number of rows and columns.
  • Then we have used nested for loop to traverse over the elements of matrix.
  • We have checked whether the element matrix[i][j] is even or odd by using right shift and then left shift operation of element by 1.
  • First we perform the right shift operation on element by 1 and then left shift on resultant value by 1.
  • If the initial value of element is same as resultant value after left and right shift operation, then it is even and increase the even counter by 1, otherwise increase the odd counter by 1.
  • After the completion of loop we have return the even and odd counter. Then the function freq() is called and the result is displayed in web console using console.log() method.

Solution

Matrix = 
[[1, 2, 3],
 [4, 5, 6],
 [7, 8, 9]];

let num = 6 = 0110
6 >> 1 = 011 = 3 
3  true(Even)

let num = 9 = 1001
9 >> 1 = 100 = 4
4  false(Odd)

Example

Here is a complete example code implementing above mentioned steps to get the frequencies of even and odd numbers in a matrix using right and left shift operator.

const matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];

function freq(matrix) {
    let even = 0, odd = 0;
    let m = matrix.length;
    let n = matrix[0].length;

    for (let i = 0; i > 1 


Complexity Comparison

Here is a comparison of time and space complexity of all the above approaches.

Approach Time Complexity Space Complexity
Modulus Operator O(m*n) O(1)
Bitwise AND Operator O(m*n) O(1)
Right Shift Operator O(m*n) O(1)

Conclusion

In this article to write a JavaScript program for frequencies of even and odd numbers in a matrix, we have used three different approaches. These approaches are: by using modulus operator (%), AND operator and right shift operator.

Practice and learn from a wide range of JavaScript examples, including event handling, form validation, and advanced techniques. Interactive code snippets for hands-on learning.
Updated on: 2024-12-15T14:05:22+05:30

203 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements