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++
// 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] = { { 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
// C program to find the minimum
// element of each row and each column
#include <stdio.h>
#define MAX 100
// function to find the minimum
// element of each row.
void smallestInRow(int mat[][MAX], int n, int m)
{
printf(" { ");
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
printf("%d, ",minm);
}
printf("}");
}
// function to find the minimum
// element of each column.
void smallestInCol(int mat[][MAX], int n, int m)
{
printf(" { ");
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
printf("%d, ",minm);
}
printf("}");
}
// Driver code
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;
}
// This code is contributed by kothavvasskash.
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[][] = {{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*/
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 = [[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#
// 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
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( 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);
// This code is contributed by ita_c
?>
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 = [[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);
// This code is contributed by Bobby
</script>
OutputMinimum 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.
Similar Reads
DSA Tutorial - Learn Data Structures and Algorithms
DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Quick Sort
QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials
Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
Bubble Sort Algorithm
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Breadth First Search or BFS for a Graph
Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Data Structures Tutorial
Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Binary Search Algorithm - Iterative and Recursive Implementation
Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read
Insertion Sort Algorithm
Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Dijkstra's Algorithm to find Shortest Paths from a Source to all
Given a weighted undirected graph represented as an edge list and a source vertex src, find the shortest path distances from the source vertex to all other vertices in the graph. The graph contains V vertices, numbered from 0 to V - 1.Note: The given graph does not contain any negative edge. Example
12 min read
Selection Sort
Selection Sort is a comparison-based sorting algorithm. It sorts an array by repeatedly selecting the smallest (or largest) element from the unsorted portion and swapping it with the first unsorted element. This process continues until the entire array is sorted.First we find the smallest element an
8 min read