# Minimum element of each row and each column in a matrix

Given a matrix, the task is to find the minimum element of each row and each column.

Examples:

```Input: [1, 2, 3]
[1, 4, 9]
[76, 34, 21]
Output: Minimum element of each row is {1, 1, 21}
Minimum element of each column is {1, 2, 3}

Input: [1, 2, 3, 21]
[12, 1, 65, 9]
[11, 56, 34, 2]
Output: Minimum element of each row is {1, 1, 21}
Minimum element of each column is {1, 2, 3}

```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The idea is to run the loop for no_of_rows. Check each element inside the row and find for the minimum element. Finally, print the element. Similarly, check each element inside the column and find for the minimum element. Finally, print the element.

Below is the implementation of the above approach:

 `// C++ program to find the minimum ` `// element of each row and each column ` `#include ` `using` `namespace` `std; ` `const` `int` `MAX = 100; ` ` `  `// function to find the minimum ` `// element of each row. ` `void` `smallestInRow(``int` `mat[][MAX], ``int` `n, ``int` `m) ` `{ ` `    ``cout << ``" { "``; ` `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``// initialize the minimum element ` `        ``// as first element ` `        ``int` `minm = mat[i]; ` ` `  `        ``for` `(``int` `j = 1; j < m; j++) { ` ` `  `            ``// check if any element is smaller ` `            ``// than the minimum element of the row  ` `            ``// and replace it ` `            ``if` `(mat[i][j] < minm) ` `                ``minm = mat[i][j]; ` `        ``} ` ` `  `        ``// print the smallest element of the row ` `        ``cout << minm << ``", "``; ` `    ``} ` `    ``cout << ``"}"``; ` `} ` ` `  `// function to find the minimum ` `// element of each column. ` `void` `smallestInCol(``int` `mat[][MAX], ``int` `n, ``int` `m) ` `{ ` ` `  `    ``cout << ``" { "``; ` `    ``for` `(``int` `i = 0; i < m; i++) { ` ` `  `        ``// initialize the minimum element ` `        ``// as first element ` `        ``int` `minm = mat[i]; ` ` `  `        ``// Run the inner loop for columns ` `        ``for` `(``int` `j = 1; j < n; j++) { ` ` `  `            ``// check if any element is smaller ` `            ``// than the minimum element of the column ` `            ``// and replace it ` `            ``if` `(mat[j][i] < minm) ` `                ``minm = mat[j][i]; ` `        ``} ` ` `  `        ``// print the smallest element of the row ` `        ``cout << minm << ``", "``; ` `    ``} ` ` `  `    ``cout << ``"}"``; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` ` `  `    ``int` `n = 3, m = 3; ` `    ``int` `mat[][MAX] = { { 2, 1, 7 }, ` `                       ``{ 3, 7, 2 }, ` `                       ``{ 5, 4, 9 } }; ` ` `  `    ``cout << ``"Minimum element of each row is "``; ` `    ``smallestInRow(mat, n, m); ` ` `  `    ``cout << ``"\nMinimum element of each column is "``; ` `    ``smallestInCol(mat, n, m); ` ` `  `    ``return` `0; ` `} `

 `// Java program to find the minimum  ` `// element of each row and each column  ` ` `  `public` `class` `GFG { ` ` `  `    ``final` `static` `int` `MAX = ``100``; ` ` `  `// function to find the minimum  ` `// element of each row.  ` `    ``static` `void` `smallestInRow(``int` `mat[][], ``int` `n, ``int` `m) { ` `        ``System.out.print(``" { "``); ` `        ``for` `(``int` `i = ``0``; i < n; i++) { ` ` `  `            ``// initialize the minimum element  ` `            ``// as first element  ` `            ``int` `minm = mat[i][``0``]; ` ` `  `            ``for` `(``int` `j = ``1``; j < m; j++) { ` ` `  `                ``// check if any element is smaller  ` `                ``// than the minimum element of the row  ` `                ``// and replace it  ` `                ``if` `(mat[i][j] < minm) { ` `                    ``minm = mat[i][j]; ` `                ``} ` `            ``} ` ` `  `            ``// print the smallest element of the row  ` `            ``System.out.print(minm + ``", "``); ` `        ``} ` `        ``System.out.println(``"}"``); ` `    ``} ` ` `  `// function to find the minimum  ` `// element of each column.  ` `    ``static` `void` `smallestInCol(``int` `mat[][], ``int` `n, ``int` `m) { ` ` `  `        ``System.out.print(``" { "``); ` `        ``for` `(``int` `i = ``0``; i < m; i++) { ` ` `  `            ``// initialize the minimum element  ` `            ``// as first element  ` `            ``int` `minm = mat[``0``][i]; ` ` `  `            ``// Run the inner loop for columns  ` `            ``for` `(``int` `j = ``1``; j < n; j++) { ` ` `  `                ``// check if any element is smaller  ` `                ``// than the minimum element of the column  ` `                ``// and replace it  ` `                ``if` `(mat[j][i] < minm) { ` `                    ``minm = mat[j][i]; ` `                ``} ` `            ``} ` ` `  `            ``// print the smallest element of the row  ` `            ``System.out.print(minm + ``", "``); ` `        ``} ` ` `  `        ``System.out.print(``"}"``); ` `    ``} ` ` `  `// Driver code ` `    ``public` `static` `void` `main(String args[]) { ` `        ``int` `n = ``3``, m = ``3``; ` `        ``int` `mat[][] = {{``2``, ``1``, ``7``}, ` `        ``{``3``, ``7``, ``2``}, ` `        ``{``5``, ``4``, ``9``}}; ` ` `  `        ``System.out.print(``"Minimum element of each row is "``); ` `        ``smallestInRow(mat, n, m); ` ` `  `        ``System.out.print(``"\nMinimum element of each column is "``); ` `        ``smallestInCol(mat, n, m); ` `    ``} ` `} ` ` `  `/*This code is contributed by 29AjayKumar*/`

 `# Python 3 program to find the minimum ` ` `  `MAX` `=` `100` ` `  `# function to find the minimum ` `# element of each row. ` `def` `smallestInRow(mat, n, m): ` `    ``print``(``"{"``, end ``=` `"") ` `    ``for` `i ``in` `range``(n): ` `         `  `        ``# initialize the minimum element ` `        ``# as first element ` `        ``minm ``=` `mat[i][``0``] ` ` `  `        ``for` `j ``in` `range``(``1``, m, ``1``): ` `             `  `            ``# check if any element is smaller ` `            ``# than the minimum element of the  ` `            ``# row and replace it ` `            ``if` `(mat[i][j] < minm): ` `                ``minm ``=` `mat[i][j] ` `         `  `        ``# print the smallest element ` `        ``# of the row ` `        ``print``(minm, end ``=` `","``) ` ` `  `    ``print``(``"}"``) ` ` `  `# function to find the minimum ` `# element of each column. ` `def` `smallestInCol(mat, n, m): ` `    ``print``(``"{"``, end ``=` `"") ` `    ``for` `i ``in` `range``(m): ` `         `  `        ``# initialize the minimum element ` `        ``# as first element ` `        ``minm ``=` `mat[``0``][i] ` ` `  `        ``# Run the inner loop for columns ` `        ``for` `j ``in` `range``(``1``, n, ``1``): ` `             `  `            ``# check if any element is smaller ` `            ``# than the minimum element of the  ` `            ``# column and replace it ` `            ``if` `(mat[j][i] < minm): ` `                ``minm ``=` `mat[j][i] ` ` `  `        ``# print the smallest element  ` `        ``# of the row ` `        ``print``(minm, end ``=` `","``) ` ` `  `    ``print``(``"}"``) ` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``n ``=` `3` `    ``m ``=` `3` `    ``mat ``=` `[[``2``, ``1``, ``7``], ` `           ``[``3``, ``7``, ``2` `], ` `           ``[ ``5``, ``4``, ``9` `]]; ` ` `  `    ``print``(``"Minimum element of each row is"``,  ` `                                 ``end ``=` `" "``) ` `    ``smallestInRow(mat, n, m) ` ` `  `    ``print``(``"Minimum element of each column is"``,  ` `                                    ``end ``=` `" "``) ` `    ``smallestInCol(mat, n, m) ` ` `  `# This code is contributed by ` `# Shashank_Sharma `

 `// C# program to find the minimum  ` `// element of each row and each column  ` `using` `System; ` ` `  `class` `GFG ` `{  ` ` `  `readonly` `static` `int` `MAX = 100;  ` ` `  `// function to find the minimum  ` `// element of each row.  ` `static` `void` `smallestInRow(``int` `[,]mat,  ` `                          ``int` `n, ``int` `m)  ` `{  ` `    ``Console.Write(``" { "``);  ` `     `  `    ``for` `(``int` `i = 0; i < n; i++)  ` `    ``{  ` ` `  `        ``// initialize the minimum element  ` `        ``// as first element  ` `        ``int` `minm = mat[i, 0];  ` ` `  `        ``for` `(``int` `j = 1; j < m; j++)  ` `        ``{  ` ` `  `            ``// check if any element is smaller  ` `            ``// than the minimum element of the  ` `            ``// row and replace it  ` `            ``if` `(mat[i, j] < minm)  ` `            ``{  ` `                ``minm = mat[i, j];  ` `            ``}  ` `        ``}  ` ` `  `        ``// print the smallest element  ` `        ``// of the row  ` `        ``Console.Write(minm + ``", "``);  ` `    ``}  ` `    ``Console.WriteLine(``"}"``);  ` `}  ` ` `  `// function to find the minimum  ` `// element of each column.  ` `static` `void` `smallestInCol(``int` `[,]mat,  ` `                          ``int` `n, ``int` `m) ` `{  ` ` `  `    ``Console.Write(``" { "``);  ` `    ``for` `(``int` `i = 0; i < m; i++) ` `    ``{  ` ` `  `        ``// initialize the minimum element  ` `        ``// as first element  ` `        ``int` `minm = mat[0, i];  ` ` `  `        ``// Run the inner loop for columns  ` `        ``for` `(``int` `j = 1; j < n; j++)  ` `        ``{  ` ` `  `            ``// check if any element is smaller  ` `            ``// than the minimum element of the  ` `            ``// column and replace it  ` `            ``if` `(mat[j, i] < minm)  ` `            ``{  ` `                ``minm = mat[j, i];  ` `            ``}  ` `        ``}  ` ` `  `        ``// print the smallest element ` `        ``// of the row  ` `        ``Console.Write(minm + ``", "``);  ` `    ``}  ` ` `  `    ``Console.Write(``"}"``);  ` `}  ` ` `  `// Driver code  ` `public` `static` `void` `Main()  ` `{  ` `    ``int` `n = 3, m = 3;  ` `    ``int` `[,]mat = {{2, 1, 7},  ` `                   ``{3, 7, 2},  ` `                  ``{5, 4, 9}};  ` ` `  `    ``Console.Write(``"Minimum element of "` `+  ` `                         ``"each row is "``);  ` `    ``smallestInRow(mat, n, m);  ` ` `  `    ``Console.Write(``"\nMinimum element of "` `+  ` `                        ``"each column is "``);  ` `    ``smallestInCol(mat, n, m);  ` `}  ` `}  ` ` `  `// This code is contributed ` `// by 29AjayKumar `

 ` `

Output:
```Minimum element of each row is  { 1, 2, 4, }
Minimum element of each column is  { 2, 1, 2, }
```

Time complexity: O(n*m)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :