Find maximum element of each row in a matrix

10 Jun, 2022
Given a matrix, the task is to find the maximum element of each row.Examples:  
Input :  [1, 2, 3]
         [1, 4, 9]
         [76, 34, 21]

Output :
3
9
76

Input : [1, 2, 3, 21]
        [12, 1, 65, 9]
        [1, 56, 34, 2]
Output :
21
65
56
 
Approach : Approach is very simple. The idea is to run the loop for no_of_rows. Check each element inside the row and find for the maximum element. Finally, print the element.   Below is the implementation :  
C++
Java
Python
C#
PHP
Javascript
// C++ program to find maximum// element of each row in a matrix#include<bits/stdc++.h>using namespace std;const int N = 4;      // Print array element    void printArray(int result[], int no_of_rows) {        for (int i = 0; i < no_of_rows; i++) {            cout<< result[i]<<"\n";        }     }     // Function to get max element    void maxelement(int no_of_rows, int arr[][N]) {        int i = 0;                 // Initialize max to 0 at beginning        // of finding max element of each row        int max = 0;        int result[no_of_rows];        while (i < no_of_rows) {            for (int j = 0; j < N; j++) {                if (arr[i][j] > max) {                    max = arr[i][j];                }            }            result[i] = max;            max = 0;            i++;         }        printArray(result,no_of_rows);     }     // Driver code    int main()    {        int arr[][N] = { {3, 4, 1, 8},                        {1, 4, 9, 11},                        {76, 34, 21, 1},                        {2, 1, 4, 5} };    // Calling the function        maxelement(4, arr);    } // This code is contributed by Rajput-Ji
// Java program to find maximum// element of each row in a matrixpublic class GFG{     // Function to get max element    public static void maxelement(int no_of_rows, int[][] arr) {        int i = 0;                 // Initialize max to 0 at beginning        // of finding max element of each row        int max = 0;        int[] result = new int[no_of_rows];        while (i < no_of_rows) {            for (int j = 0; j < arr[i].length; j++) {                if (arr[i][j] > max) {                    max = arr[i][j];                }            }            result[i] = max;            max =0;            i++;         }        printArray(result);     }     // Print array element    private static void printArray(int[] result) {        for (int i =0; i<result.length;i++) {            System.out.println(result[i]);        }     }     // Driver code    public static void main(String[] args) {        int[][] arr = new int[][] { {3, 4, 1, 8},                                    {1, 4, 9, 11},                                    {76, 34, 21, 1},                                   {2, 1, 4, 5} };       // Calling the function         maxelement(4, arr);    }}
# Python program to find maximum# element of each row in a matrix # importing numpyimport numpy # Function to get max elementdef maxelement(arr):         # get number of rows and columns    no_of_rows = len(arr)    no_of_column = len(arr[0])         for i in range(no_of_rows):                 # Initialize max1 to 0 at beginning        # of finding max element of each row        max1 = 0        for j in range(no_of_column):            if arr[i][j] > max1 :                max1 = arr[i][j]                         # print maximum element of each row        print(max1) # Driver Codearr = [[3, 4, 1, 8],       [1, 4, 9, 11],       [76, 34, 21, 1],       [2, 1, 4, 5]] # Calling the function       maxelement(arr)
// C# program to find maximum// element of each row in a matrixusing System; class GFG{ // Function to get max elementpublic static void maxelement(int no_of_rows,                              int[][] arr){    int i = 0;     // Initialize max to 0 at beginning    // of finding max element of each row    int max = 0;    int[] result = new int[no_of_rows];    while (i < no_of_rows)    {        for (int j = 0;                 j < arr[i].Length; j++)        {            if (arr[i][j] > max)            {                max = arr[i][j];            }        }        result[i] = max;        max = 0;        i++;     }    printArray(result); } // Print array elementprivate static void printArray(int[] result){    for (int i = 0; i < result.Length;i++)    {        Console.WriteLine(result[i]);    } } // Driver codepublic static void Main(string[] args){    int[][] arr = new int[][]    {        new int[] {3, 4, 1, 8},        new int[] {1, 4, 9, 11},        new int[] {76, 34, 21, 1},        new int[] {2, 1, 4, 5}    };         // Calling the function    maxelement(4, arr);}} // This code is contributed by Shrikant13
<?php// PHP program to find maximum// element of each row in a matrix$N = 4;  // Print array elementfunction printArray($result, $no_of_rows){    for ($i = 0; $i < $no_of_rows; $i++)    {        echo $result[$i]."\n";    } } // Function to get max elementfunction maxelement($no_of_rows, $arr){    global $N;    $i = 0;         // Initialize max to 0 at beginning    // of finding max element of each row    $max = 0;    $result=array_fill(0,$no_of_rows,0);    while ($i < $no_of_rows)    {        for ($j = 0; $j < $N; $j++)        {            if ($arr[$i][$j] > $max)            {                $max = $arr[$i][$j];            }        }        $result[$i] = $max;        $max = 0;        $i++;     }    printArray($result,$no_of_rows); } // Driver code$arr = array(array(3, 4, 1, 8),                array(1, 4, 9, 11),                array(76, 34, 21, 1),                array(2, 1, 4, 5));// Calling the functionmaxelement(4, $arr); // This code is contributed by mits?>
<script>// javascript program to find maximum// element of each row in a matrix// Function to get max elementfunction maxelement(no_of_rows, arr){    var i = 0;         // Initialize max to 0 at beginning    // of finding max element of each row    var max = 0;    var result = Array.from({length: no_of_rows}, (_, i) => 0);    while (i < no_of_rows)    {        for (var j = 0; j < arr[i].length; j++)        {            if (arr[i][j] > max)            {                max = arr[i][j];            }        }        result[i] = max;        max = 0;        i++;     }    printArray(result); } // Print array elementfunction printArray(result){    for (var i = 0; i < result.length; i++)    {        document.write(result[i]+"<br>");    } } // Driver code    var arr = [[3, 4, 1, 8],    [ 1, 4, 9, 11],    [ 76, 34, 21, 1],   [ 2, 1, 4, 5] ];    // Calling the function maxelement(4, arr);  // This code is contributed by 29AjayKumar</script>
Output :
8
11
76
5
Time Complexity: O(n*m) (where, n refers to no. of rows and m refers to no. of columns)
Auxiliary Space: O(n) (where, n refers to no. of rows)
ApoorvaSaxena
shrikanth13
Rajput-Ji
Mithun Kumar
shubham_singh
29AjayKumar
ankita_saini
ajaymakvana
Matrix
Searching
Technical Scripter
Searching
Matrix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.