Minimum element of each row and each column in a matrix
Last Updated :
09 Sep, 2022
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, 2}
Minimum element of each column is {1, 2, 3 , 2}
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++
#include<bits/stdc++.h>
using namespace std;
const int MAX = 100;
void smallestInRow( int mat[][MAX], int n, int m)
{
cout << " { " ;
for ( int i = 0; i < n; i++) {
int minm = mat[i][0];
for ( int j = 1; j < m; j++) {
if (mat[i][j] < minm)
minm = mat[i][j];
}
cout << minm << ", " ;
}
cout << "}" ;
}
void smallestInCol( int mat[][MAX], int n, int m)
{
cout << " { " ;
for ( int i = 0; i < m; i++) {
int minm = mat[0][i];
for ( int j = 1; j < n; j++) {
if (mat[j][i] < minm)
minm = mat[j][i];
}
cout << minm << ", " ;
}
cout << "}" ;
}
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;
}
|
C
#include <stdio.h>
#define MAX 100
void smallestInRow( int mat[][MAX], int n, int m)
{
printf ( " { " );
for ( int i = 0; i < n; i++) {
int minm = mat[i][0];
for ( int j = 1; j < m; j++) {
if (mat[i][j] < minm)
minm = mat[i][j];
}
printf ( "%d, " ,minm);
}
printf ( "}" );
}
void smallestInCol( int mat[][MAX], int n, int m)
{
printf ( " { " );
for ( int i = 0; i < m; i++) {
int minm = mat[0][i];
for ( int j = 1; j < n; j++) {
if (mat[j][i] < minm)
minm = mat[j][i];
}
printf ( "%d, " ,minm);
}
printf ( "}" );
}
int main()
{
int n = 3, m = 3;
int mat[][MAX] = { { 2, 1, 7 },
{ 3, 7, 2 },
{ 5, 4, 9 } };
printf ( "Minimum element of each row is " );
smallestInRow(mat, n, m);
printf ( "\nMinimum element of each column is " );
smallestInCol(mat, n, m);
return 0;
}
|
Java
public class GFG {
final static int MAX = 100 ;
static void smallestInRow( int mat[][], int n, int m) {
System.out.print( " { " );
for ( int i = 0 ; i < n; i++) {
int minm = mat[i][ 0 ];
for ( int j = 1 ; j < m; j++) {
if (mat[i][j] < minm) {
minm = mat[i][j];
}
}
System.out.print(minm + ", " );
}
System.out.println( "}" );
}
static void smallestInCol( int mat[][], int n, int m) {
System.out.print( " { " );
for ( int i = 0 ; i < m; i++) {
int minm = mat[ 0 ][i];
for ( int j = 1 ; j < n; j++) {
if (mat[j][i] < minm) {
minm = mat[j][i];
}
}
System.out.print(minm + ", " );
}
System.out.print( "}" );
}
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);
}
}
|
Python3
MAX = 100
def smallestInRow(mat, n, m):
print ( "{" , end = "")
for i in range (n):
minm = mat[i][ 0 ]
for j in range ( 1 , m, 1 ):
if (mat[i][j] < minm):
minm = mat[i][j]
print (minm, end = "," )
print ( "}" )
def smallestInCol(mat, n, m):
print ( "{" , end = "")
for i in range (m):
minm = mat[ 0 ][i]
for j in range ( 1 , n, 1 ):
if (mat[j][i] < minm):
minm = mat[j][i]
print (minm, end = "," )
print ( "}" )
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)
|
C#
using System;
class GFG
{
readonly static int MAX = 100;
static void smallestInRow( int [,]mat,
int n, int m)
{
Console.Write( " { " );
for ( int i = 0; i < n; i++)
{
int minm = mat[i, 0];
for ( int j = 1; j < m; j++)
{
if (mat[i, j] < minm)
{
minm = mat[i, j];
}
}
Console.Write(minm + ", " );
}
Console.WriteLine( "}" );
}
static void smallestInCol( int [,]mat,
int n, int m)
{
Console.Write( " { " );
for ( int i = 0; i < m; i++)
{
int minm = mat[0, i];
for ( int j = 1; j < n; j++)
{
if (mat[j, i] < minm)
{
minm = mat[j, i];
}
}
Console.Write(minm + ", " );
}
Console.Write( "}" );
}
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);
}
}
|
PHP
<?php
$MAX = 100;
function smallestInRow(& $mat , $n , $m )
{
echo " { " ;
for ( $i = 0; $i < $n ; $i ++)
{
$minm = $mat [ $i ][0];
for ( $j = 1; $j < $m ; $j ++)
{
if ( $mat [ $i ][ $j ] < $minm )
$minm = $mat [ $i ][ $j ];
}
echo $minm . ", " ;
}
echo "}" ;
}
function smallestInCol(& $mat , $n , $m )
{
echo " { " ;
for ( $i = 0; $i < $m ; $i ++)
{
$minm = $mat [0][ $i ];
for ( $j = 1; $j < $n ; $j ++)
{
if ( $mat [ $j ][ $i ] < $minm )
$minm = $mat [ $j ][ $i ];
}
echo $minm . ", " ;
}
echo "}" ;
}
$n = 3;
$m = 3;
$mat = array ( array ( 2, 1, 7 ),
array ( 3, 7, 2 ),
array ( 5, 4, 9 ));
echo "Minimum element of each row is " ;
smallestInRow( $mat , $n , $m );
echo "\nMinimum element of each column is " ;
smallestInCol( $mat , $n , $m );
?>
|
Javascript
<script>
let MAX = 100;
function smallestInRow(mat,n,m) {
document.write( " { " );
for (let i = 0; i < n; i++) {
let minm = mat[i][0];
for (let j = 1; j < m; j++) {
if (mat[i][j] < minm) {
minm = mat[i][j];
}
}
document.write(minm + ", " );
}
document.write( "}" + "<br>" );
}
function smallestInCol(mat,n,m) {
document.write( " { " );
for (let i = 0; i < m; i++) {
let minm = mat[0][i];
for (let j = 1; j < n; j++) {
if (mat[j][i] < minm) {
minm = mat[j][i];
}
}
document.write(minm + ", " );
}
document.write( "}" );
}
let n = 3, m = 3;
let mat = [[2, 1, 7],
[3, 7, 2],
[5, 4, 9]];
document.write( "Minimum element of each row is " );
smallestInRow(mat, n, m);
document.write( "\nMinimum element of each column is " );
smallestInCol(mat, n, m);
</script>
|
Output
Minimum element of each row is { 1, 2, 4, }
Minimum element of each column is { 2, 1, 2, }
Complexity Analysis:
- Time complexity: O(n*m), as we are using nested for loops to traverse the Matrix.
- Auxiliary Space: O(1), as we are not using any extra space.
Share your thoughts in the comments
Please Login to comment...