Find Largest & Smallest Element in Primary and Secondary Diagonal 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 largest and smallest element in primary and secondary diagonal of a matrix.

Let’s deep dive into this article, to know how it can be done by using Java programming language.

To show you some instances

Instance-1

Given matrix =

21 	22	23
24	25	26
27	28	29
  • Largest element in primary diagonal: 29

  • Smallest element in primary diagonal: 21

  • Largest element in secondary diagonal: 27

  • Smallest element in secondary diagonal: 23

Instance-2

Given matrix =

121 	222	243	432
124	245	256	657
237	258	229	345
176	453	756	343
  • Largest element in primary diagonal: 343

  • Smallest element in primary diagonal: 121

  • Largest element in secondary diagonal: 432

  • Smallest element in secondary diagonal: 176

Instance-3

Given matrix =

1 	2	3
4	5	6
7	8	9
  • Largest element in primary diagonal: 9

  • Smallest element in primary diagonal: 1

  • Largest element in secondary diagonal: 7

  • Smallest element in secondary diagonal: 3

Algorithm

  • Step-1 − Take the matrix.

  • Step-2 − Iterate the primary diagonals and secondary elements.

  • Step-3 − Print the largest and smallest element in each diagonal.

Syntax

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.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Static Initialization of matrix Elements

  • By Using User Defined Method

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

Approach-1: By Using Static Initialization of Matrix Elements

In this approach, matrix elements will be initialized in the program. Then as per the algorithm find the largest and smallest element in primary and secondary diagonal of that matrix.

Example

public class Main {
   public static void main(String[] args) {      
      // define a matrix and initialize it with hardcoded values
      int[][] inputMatrix = {{11, 22, 33}, {44, 55, 66}, {77, 88, 99}}; 
      
      // set initial values for the maximum and minimum elements in the primary diagonal
      int max_primary = inputMatrix[0][0];
      int min_primary = inputMatrix[0][0];
      
      // set initial values for the maximum and minimum elements in the secondary diagonal
      int max_secondary = inputMatrix[0][inputMatrix.length-1]; 
      int min_secondary = inputMatrix[0][inputMatrix.length-1];
      
      for(int i = 0; i < inputMatrix.length; i++) { 
         
         // iterate over the rows of the matrix
         if(inputMatrix[i][i] > max_primary) { 
            
            // check if the current element belongs to the primary diagonal and update the maximum and minimum values as needed
            max_primary = inputMatrix[i][i];
         }
         if(inputMatrix[i][i] < min_primary) {
            min_primary = inputMatrix[i][i];
         }
         if(inputMatrix[i][inputMatrix.length-i-1] > max_secondary) { 
            
            // check if the current element belongs to the secondary diagonal and update the maximum and minimum values as needed 
            max_secondary = inputMatrix[i][inputMatrix.length-i-1];
         }
         if(inputMatrix[i][inputMatrix.length-i-1] < min_secondary) {
            min_secondary = inputMatrix[i][inputMatrix.length-i-1];
         }
      }
      
      // print out the largest and smallest elements in the primary and secondary diagonal of the matrix
      System.out.println("Largest element in primary diagonal: " + max_primary);
      System.out.println("Smallest element in primary diagonal: " + min_primary);
      System.out.println("Largest element in secondary diagonal: " + max_secondary);
      System.out.println("Smallest element in secondary diagonal: " + min_secondary);
   }
}

Output

Largest element in primary diagonal: 99
Smallest element in primary diagonal: 11
Largest element in secondary diagonal: 77
Smallest element in secondary diagonal: 33

Approach-2: By Using User Defined Method

In this approach, matrix elements will be initialized in the program. Then call a user defined method by passing the matrix as parameter and inside method as per the algorithm finds the largest and smallest element in primary and secondary diagonal of that matrix.

Example

public class Main {
   public static void main(String[] args) {
      
      // Define a square matrix and initialize it with hardcoded values
      int[][] inputMatrix = {{21, 32, 32}, {14, 53, 69}, {71, 84, 95}};
      
      // Find the largest and smallest elements in the primary diagonal
      int[] primaryMinMax = findMinMax(inputMatrix, true);
      int max_primary = primaryMinMax[0];
      int min_primary = primaryMinMax[1];
      
      // Find the largest and smallest elements in the secondary diagonal
      int[] secondaryMinMax = findMinMax(inputMatrix, false);
      int max_secondary = secondaryMinMax[0];
      int min_secondary = secondaryMinMax[1];
      
      // Print out the largest and smallest elements in the primary and secondary diagonal of the matrix
      System.out.println("Largest element in primary diagonal: " + max_primary);
      System.out.println("Smallest element in primary diagonal: " + min_primary);
      System.out.println("Largest element in secondary diagonal: " + max_secondary);
      System.out.println("Smallest element in secondary diagonal: " + min_secondary);
   }
   
   // A user-defined method to find the largest and smallest elements in a diagonal of a matrix
   public static int[] findMinMax(int[][] mat, boolean primary) {
      int[] minMax = new int[2];
      if (primary) {
         minMax[0] = mat[0][0];
         minMax[1] = mat[0][0];
         for (int i = 1; i < mat.length; i++) {
            if (mat[i][i] > minMax[0]) {
               minMax[0] = mat[i][i];
            }
            if (mat[i][i] < minMax[1]) {
               minMax[1] = mat[i][i]; 
            }
         }
      } else {
         minMax[0] = mat[0][mat.length-1]; 
         minMax[1] = mat[0][mat.length-1];
         for (int i = 1; i < mat.length; i++) {
            if (mat[i][mat.length-i-1] > minMax[0]) {
               minMax[0] = mat[i][mat.length-i-1];
            }
            if (mat[i][mat.length-i-1] < minMax[1]) {
               minMax[1] = mat[i][mat.length-i-1]; 
            }
         }
      }
      return minMax;
   }
}

Output

Largest element in primary diagonal: 95
Smallest element in primary diagonal: 21
Largest element in secondary diagonal: 71
Smallest element in secondary diagonal: 32

In this article, we explored different approaches to find largest and smallest element in primary and secondary diagonal of the matrix by using Java programming language.

Updated on: 04-May-2023

282 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements