
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Minimum Elements in Each Row of 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.
As per the problem statement we have to find the minimum element present in every row of a given matrix of a multi-dimensional array.
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
Suppose the original matrix is = { {2, 3,11, 4, 6}, {21,12,32,45,2}, {41,12,3,45, 2} }
The minimum element present in row-0 is 1
The minimum element present in row-1 is 2
The minimum element present in row-2 is 2
Instance-2
Suppose the original matrix is = { {2, 3, 1, 4, 6}, {21, 12, 32, -4, 2}, {4, 12, 3, 45, 2}, {-12, -13, 6, 1, 8} }
The minimum element present in row-0 is 1
The minimum element present in row-1 is -4
The minimum element present in row-2 is 2
The minimum element present in row-3 is -13
Instance-3
Suppose the original matrix is = { {2, 3, 1}, 21,12,32}, {4,12,13} }
The minimum element present in row-0 is 1
The minimum element present in row-1 is 12
The minimum element present in row-2 is 4
Algorithm
Step 1 ? Declare and initialize an integer multi-dimensional array.
Step 2 ? Take a loop to iterate the rows of the given matrix.
Step 3 ? Inside that loop take another for loop to iterate the columns, by this method we can now check through all the available elements in rows.
Step 4 ? We pass those row's elements in a condition to get the minimum value.
Step 5 ? After getting the minimum value in each loop, we print those values as output with its respective row index numbers.
Syntax
To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length
Below refers to the syntax of it-
array.length
where, ?array' refers to the array reference.
Multiple Approaches:
We have provided the solution in different approaches.
By Using Static Initialization of Array 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 Array Elements
In this approach, array elements will be initialized in the program. Then as per the algorithm the minimum values for each row of the matrix will be printed as output.
Example
public class Main{ public static void main(String[] args) { //declare an integer type multi dimentional array //here we take a 3X3 Matrix with some random values int[][] inputMatrix = { {21,34,76}, {2,1,5}, {90,23,-23} }; for (int r = 0; r < inputMatrix.length; r++) { int minimumValue = inputMatrix[r][0]; for (int c = 0; c < inputMatrix[r].length; c++){ //if the current index value is smaller than the Minimum value if (inputMatrix[r][c] < minimumValue) { //store the minimum value to the variable each time //when the above condition satisfied minimumValue = inputMatrix[r][c]; } } //print the row index value along with the derived minimum value System.out.println("The minimum element present in row-" + r + " is " + minimumValue); } } }
Output
The minimum element present in row-0 is 21 The minimum element present in row-1 is 1 The minimum element present in row-2 is -23
Approach-2: By Using User Defined Method
In this approach, array elements will be initialized in the program. Then call a user defined method by passing the array as parameter and inside method as per the algorithm the minimum values for each row of the matrix will be printed as output.
Example
public class Main { public static void main(String[] args) { //declare an integer type multi dimentional array //a 3X3 Matrix with some random values int[][] inputMatrix ={ {2, 3, 1}, {21,12,32}, {4,12,3} }; //call the user-defined method Min(inputMatrix); } //user defined method public static void Min(int[][] mat) { for (int r = 0; r < mat.length; r++) { int minimumValue = mat[r][0]; //initiate the loop to check through the columns for (int c = 0; c < mat[r].length; c++){ //if the current index value is smaller than the Minimum value if (mat[r][c] < minimumValue) { //store the minimum value to the variable each time //when the above condition satisfied minimumValue = mat[r][c]; } } //print the row index value along with the derived minimum value System.out.println("The minimum element present in row-" + r + " is " + minimumValue); } } }
Output
The minimum element present in row-0 is 1 The minimum element present in row-1 is 12 The minimum element present in row-2 is 3
In this article, we explored different approaches to find the minimum element in each row of a matrix by using Java programming language.