# Find Minimum Element Of Each Row And Each Column In A Matrix

Suppose there is a matrix, and the task is find out minimum element of each row and each column in a matrix.

Take an Example:

``````Input: [10, 2, 30]
[12, 41, 9]
[7, 3, 1]
Output: Minimum element of each row is {2, 9, 1}
Minimum element of each column is {7, 2, 1}

Input: [10, 2, 3, 20]
[18, 17, 65, 9]
[1, 56, 3, 2]
Output: Minimum element of each row is {2, 9, 1}
Minimum element of each column is {1, 2, 3, 2}``````

### How to solve this problem?

The idea is to run the loops on basis of number of rows.

Check each element inside the row, then find minimum element.

Finally, print the element.

Likewise, check each element inside the column, the find minimum element.

Finally, print the element.

Below is the implementation of above approach, please go through it.

### Find Minimum Element Of Each Row And Each Column In A Matrix In C++:

```// C++ program to find the minimum
// element of each row and each column
#include<bits/stdc++.h>
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][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
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[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
cout << minm << ", ";
}

cout << "}";
}

// Driver code
int main()
{

int n = 3, m = 3;
int mat[][MAX] = { { 20, 1, 70 },
{ 30, 7, 2 },
{ 5, 40, 90 } };

cout << "Minimum element of each row is ";
smallestInRow(mat, n, m);

cout << "\nMinimum element of each column is ";
smallestInCol(mat, n, m);

return 0;
}```

### Find Minimum Element Of Each Row And Each Column In A Matrix In Java:

```// 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[][] = {{20, 1, 70},
{30, 7, 2},
{5, 40, 90}};

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*/```

### Find Minimum Element Of Each Row And Each Column In A Matrix In Python3:

```# 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 = [[20, 1, 70],
[30, 7, 2 ],
[ 5, 40, 90 ]];

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```

### Find Minimum Element Of Each Row And Each Column In A Matrix In C#:

```// 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 = {{20, 1, 70},
{30, 7, 2},
{5, 40, 90}};

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```

### Find Minimum Element Of Each Row And Each Column In A Matrix In PHP:

```<?php
// PHP program to find the minimum
// element of each row and each column
\$MAX = 100;

// function to find the minimum
// element of each row.
function smallestInRow(&\$mat, \$n, \$m)
{
echo " { ";
for (\$i = 0; \$i < \$n; \$i++)
{

// initialize the minimum element
// as first element
\$minm = \$mat[\$i][0];

for (\$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
echo \$minm . ", ";
}
echo "}";
}

// function to find the minimum
// element of each column.
function smallestInCol(&\$mat, \$n, \$m)
{
echo " { ";
for (\$i = 0; \$i < \$m; \$i++)
{

// initialize the minimum element
// as first element
\$minm = \$mat[0][\$i];

// Run the inner loop for columns
for (\$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
echo \$minm . ", ";
}

echo "}";
}

// Driver code
\$n = 3;
\$m = 3;
\$mat = array(array( 20, 1, 70 ),
array( 30, 7, 2 ),
array( 5, 40, 90 ));

echo "Minimum element of each row is ";
smallestInRow(\$mat, \$n, \$m);

echo "\nMinimum element of each column is ";
smallestInCol(\$mat, \$n, \$m);

// This code is contributed by ita_c
?>```

### Find Minimum Element Of Each Row And Each Column In A Matrix In JavaScript:

```<script>
// Java script program to find the minimum
// element of each row and each column

let MAX = 100;

// function to find the minimum
// element of each row.
function smallestInRow(mat,n,m) {
document.write(" { ");
for (let i = 0; i < n; i++) {

// initialize the minimum element
// as first element
let minm = mat[i][0];

for (let 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
document.write(minm + ", ");
}
document.write("}"+"<br>");
}

// function to find the minimum
// element of each column.
function smallestInCol(mat,n,m) {

document.write(" { ");
for (let i = 0; i < m; i++) {

// initialize the minimum element
// as first element
let minm = mat[0][i];

// Run the inner loop for columns
for (let 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
document.write(minm + ", ");
}

document.write("}");
}

// Driver code

let n = 3, m = 3;
let mat = [[20, 1, 70],
[30, 7, 2],
[5, 40, 90]];

document.write("Minimum element of each row is ");
smallestInRow(mat, n, m);

document.write("\nMinimum element of each column is ");
smallestInCol(mat, n, m);

// This code is contributed by Bobby
</script>
```

Output:

``````Minimum element of each row is  { 1, 2, 5, }
Minimum element of each column is  { 5, 1, 2, }``````

Time complexity: O(n*m).