Find Number of Odd and Even Elements in Each Row of a Matrix in Java


In Java, Array is an object. It is a non-primitive data type which stores values of similar data type. The matrix in java is nothing but a multi-dimensional array which represents multiple rows and columns.

Here we have given a matrix which contains set of elements and as per the problem statement we have to find out the number of odd elements and even elements in each row of a matrix.

Let’s start!

To show you some instances

Instance-1

Given matrix =

21 	22	23
24	25	26
27	28	29
  • Row 1 has 2 odd elements and 1 even element

  • Row 2 has 1 odd element and 2 even elements

  • Row 3 has 2 odd elements and 1 even element

Instance-2

Given matrix =

121 	222	243	432
124	245	256	657
237	258	229	345
176	453	756	343
  • Row 1 has 2 odd elements and 2 even elements

  • Row 2 has 2 odd elements and 2 even elements

  • Row 3 has 3 odd elements and 1 even element

  • Row 4 has 2 odd elements and 2 even elements

Instance-3

Given matrix =

1 	2	3
4	5	6
7	8	9
  • Row 1 has 2 odd elements and 1 even element

  • Row 2 has 1 odd element and 2 even elements

  • Row 3 has 2 odd elements and 1 even element

Algorithm

Algorithm-1: (By Using for Loop)

  • Step-1 − Use a nested for loop to iterate through each row and each element in the row of the matrix.

  • Step-2 − Take two variables, oddCount and evenCount, to keep track of the number of odd and even elements in each row.

  • Step-3 − For each element in the row, it checks whether it's odd or even using the modulo operator (%).

  • Step-4 − If the element is even, it increments evenCount; otherwise, it increments oddCount.

  • Step-5 − After iterating through all the elements in the row, it prints out the number of odd and even elements for that row.

Algorithm-2: (By using Java Streams)

  • Step-1 − Use the IntStream class from the Java Streams API to process the elements in each row of the matrix.

  • Step-2 − Take two variables, oddCount and evenCount, to keep track of the number of odd and even elements in each row.

  • Step-3 − For each row, it creates an IntStream from the elements in the row, and then uses the filter method to get the count of odd and even elements.

  • Step-4 − The filter method takes a Predicate as an argument that returns true for odd elements and false for even elements.

  • Step-5 − After iterating through all the rows, it prints out the number of odd and even elements for each row.

Algorithm-3: (By Using Arrays Class)

  • Step-1 − Use the Arrays class from the Java Standard Library to process the elements in each row of the matrix.

  • Step-2 − Take two variables, oddCount and evenCount, to keep track of the number of odd and even elements in each row.

  • Step-3 − For each row, it creates a stream from the elements in the row using the stream method from the Arrays class, and then uses the filter method to get the count of odd and even elements.

  • Step-4 − The filter method takes a Predicate as an argument that returns true for odd elements and false for even elements.

  • Step-5 − After iterating through all the rows, it prints out the number of odd and even elements for each row.

Syntax

1. The Matrix.length() method in Java returns the length of the given matrix.

Below refers to the syntax of it −

inputMatrix.lenght

where, ‘inputMatrix’ refers to the given matrix.

2. The IntStream.of() method is a part of the Java Stream API and is used to create a stream of primitive int values from an array of int values.

IntStream.of(inputMatrix[i])

where, ‘inputMatrix’ refers to the given matrix.

3. The filter() method is used to filter elements from a stream based on a condition. The elements that satisfy the condition are kept in the resulting stream, while the rest are discarded. In this case, the filter method is used to filter odd and even elements from the stream of int values created by IntStream.of() method.

filter(x -> x % 2 != 0)

Multiple Approaches

We have provided the solution in different approaches.

  • By Using for Loop

  • By Using Java Stream

  • By Using Arrays Class

Let’s see the program along with its output one by one.

Approach-1: By Using for Loop

In this approach, matrix elements will be initialized in the program. Then as per the algorithm-1 using for loop calculate the number of odd elements and even elements in each row of that matrix.

Example

public class Main {
   public static void main(String[] args) {
      
      //declare and initialize a 2D matrix
      int[][] inputMatrix = {{11, 21, 23, 44}, {52, 36, 47, 58}, {49, 10, 11, 12}};
      
      //loop to find odd/even counts
      for (int a = 0; a < inputMatrix.length; a++) {
         int oddCnt = 0, evenCnt = 0;
         for (int b = 0; b < inputMatrix[a].length; b++) {
            if (inputMatrix[a][b] % 2 == 0) {
               evenCnt++;
            } else {
               oddCnt++;
            }
         }
         
         //print the result
         System.out.println("Row " + (a + 1) + " has " + oddCnt + " odd elements and " + evenCnt + " even elements");
      }
   }
}

Output

Row 1 has 3 odd elements and 1 even elements
Row 2 has 1 odd elements and 3 even elements
Row 3 has 2 odd elements and 2 even elements

Approach-2: By Using Java Stream

In this approach, matrix elements will be initialized in the program. Then as per the algorithm-2 using java streams calculate the number of odd elements and even elements in each row of that matrix.

Example

import java.util.stream.IntStream;
public class Main {
   public static void main(String[] args) {
      
      //declare and initialize a 2D matrix
      int[][] inputMatrix = {{1, 2, 3}, {5, 6, 7}, {10, 11, 12}};
      for (int i = 0; i < inputMatrix.length; i++) {
         
         // create a stream from the elements in the row and filter odd and even elements
         long oddCount = IntStream.of(inputMatrix[i]).filter(x -> x % 2 != 0).count();
         long evenCount = IntStream.of(inputMatrix[i]).filter(x -> x % 2 == 0).count();
         
         //print the results
         System.out.println("Row " + (i + 1) + " has " + oddCount + " odd elements and " + evenCount + " even elements");
      }
   }
}

Output

Row 1 has 2 odd elements and 1 even elements
Row 2 has 2 odd elements and 1 even elements
Row 3 has 1 odd elements and 2 even elements

Approach-3: By Using Arrays Class

In this approach, matrix elements will be initialized in the program. Then as per the algorithm-3 using the arrays class calculate the number of odd elements and even elements in each row of that matrix.

Example

import java.util.Arrays;
public class Main {
   public static void main(String[] args) {
      
      //declare and initialize a 2D matrix
      int[][] inputMatrix = {{21, 24, 23, 42}, {25, 16, 77, 68}, {96, 10, 11, 12}, {99, 10, 11, 12}};
      for (int a = 0; a < inputMatrix.length; a++) {
         
         // create arrays stream from the elements in the row and filter odd and even elements
         int oddCount = (int) Arrays.stream(inputMatrix[a]).filter(x -> x % 2 != 0).count();
         int evenCount = (int) Arrays.stream(inputMatrix[a]).filter(x -> x % 2 == 0).count();
         System.out.println("Row " + (a + 1) + " has " + oddCount + " odd elements and " + evenCount + " even elements");
      }
   }
}

Output

Row 1 has 2 odd elements and 2 even elements
Row 2 has 2 odd elements and 2 even elements
Row 3 has 1 odd elements and 3 even elements
Row 4 has 2 odd elements and 2 even elements

In this article, we explored different approaches to find number of odd and even elements in each row of a matrix by using Java programming language.

Updated on: 04-May-2023

347 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements