# Find maximum element of each row in a matrix

The problem at hand involves finding the maximum element of each row in a given matrix. A matrix is a two-dimensional array of elements, where rows and columns define its structure. The task is to identify and print the largest element in each row.

## Problem Statement

Given a matrix with dimensions ROW x COL, where each element is an integer, we need to find the maximum element in each row and print those maximum values.

## Example

Consider the following matrix:

``````1  0  2  3
4  1  1  4
0 -3  1  0
2  5  1  2``````

The maximum elements in each row are: 3, 4, 1, and 5.

## Idea to Solve

To solve this problem, we need to iterate through each row of the matrix and find the maximum element in that row. We can initialize a variable to hold the maximum element for each row and update it as we traverse the row.

## Pseudocode

Here's the pseudocode for the algorithm:

``````function row_max_element(matrix):
for i from 0 to ROW-1:
max_element = matrix[i][0] // Initialize max_element with the first element of the row
for j from 1 to COL-1:
if matrix[i][j] > max_element:
max_element = matrix[i][j] // Update max_element if a larger element is found
print max_element``````

## Algorithm Explanation

1. Start by defining a function `row_max_element` that takes a 2D matrix `matrix` as its input.
2. Initialize a loop that iterates through each row of the matrix from 0 to ROW-1 (inclusive).
3. Inside the outer loop, initialize a variable `max_element` with the value of the first element in the current row (matrix[i][0]).
4. Implement an inner loop that starts from 1 and iterates through each column of the current row (from 1 to COL-1).
5. Within the inner loop, compare the current element (matrix[i][j]) with the `max_element`. If the current element is greater than `max_element`, update `max_element` with the value of the current element.
6. After the inner loop completes, print the value of `max_element`, which represents the maximum element in the current row.
7. The outer loop will continue to the next row, and the process repeats until all rows have been processed.

## Program List

``````//C Program
//Find maximum element of each row in a matrix
#include<stdio.h>

//Matrix size
#define ROW 4
#define COL 4

//print max element in each row
void row_max_element(int arr[][COL])
{
int result=0;

for (int i = 0; i < ROW; ++i)
{
//Get first element of i row
result=arr[i][0];

for (int j = 1; j < COL; ++j)
{
if(result<arr[i][j])
{
//Get new max element
result=arr[i][j];

}
}
printf(" %d\n",result);
}
}
int main()
{

int arr[][COL]= {
{1, 0, 2, 3},
{4, 1, 1, 4},
{0, -3, 1, 0},
{2, 5, 1, 2}
};

row_max_element(arr);

return 0;
}

```
```

#### Output

`````` 3
4
1
5``````
``````/*
C++ Program
Find maximum element of each row in a matrix
*/
#include<iostream>
#define ROW 4
#define COL 5
using namespace std;

class MyMatrix {
public:

//print max element in each row
void row_max_element(int matrix[][COL])
{
int result=0;

for (int i = 0; i < ROW; ++i)
{
//Get first element of i row
result=matrix[i][0];

for (int j = 1; j < COL; ++j)
{
if(result<matrix[i][j])
{
//Get new max element
result=matrix[i][j];

}
}
cout<<"  "<<result<<endl;
}
}
};
int main() {
MyMatrix obj;
int matrix[][COL] ={
{1, 0, 2, 3},
{4, 1, 1, 4},
{0, -3, 1, 0},
{2, 5, 1, 2}
};
obj.row_max_element(matrix);

return 0;
}```
```

#### Output

``````  3
4
1
5``````
``````/*
Java Program
Find maximum element of each row in a matrix
*/
public class MyMatrix {

//print max element in each row
public void row_max_element(int [][]matrix)
{
int result=0;
//Get the size of array
int row = matrix.length;
int col = matrix[0].length;
for (int i = 0; i < row; ++i)
{
//Get first element of i row
result=matrix[i][0];

for (int j = 1; j < col; ++j)
{
if(result<matrix[i][j])
{
//Get new max element
result=matrix[i][j];

}
}
System.out.print("  "+result+"\n");
}
}
public static void main(String[] args) {
MyMatrix obj = new MyMatrix();
//Define matrix
int [][]matrix ={
{1, 0, 2, 3},
{4, 1, 1, 4},
{0, -3, 1, 0},
{2, 5, 1, 2}
};
obj.row_max_element(matrix);

}
}```
```

#### Output

``````  3
4
1
5``````
``````/*
C# Program
Find maximum element of each row in a matrix
*/
using System;
public class MyMatrix {
//print max element in each row
public void row_max_element(int[,] matrix) {
int result = 0;
//Get the size of array
int row = matrix.GetLength(0);
int col = matrix.GetLength(1);
for (int i = 0; i < row; ++i) {
//Get first element of i row
result = matrix[i,0];
for (int j = 1; j < col; ++j) {
if (result < matrix[i,j]) {
//Get new max element
result = matrix[i,j];
}
}
Console.Write(" " + result + "\n");
}
}
public static void Main(String[] args) {
MyMatrix obj = new MyMatrix();
//Define matrix
int[,] matrix = {
{
1,
0,
2,
3
},
{
4,
1,
1,
4
},
{
0,
-3,
1,
0
},
{
2,
5,
1,
2
}
};
obj.row_max_element(matrix);
}
}```
```

#### Output

`````` 3
4
1
5``````
``````<?php
/*
Php Program
Find maximum element of each row in a matrix
*/
class MyMatrix {
//print max element in each row

public 	function row_max_element(\$matrix) {
\$result = 0;
//Get the size of array
\$row = count(\$matrix);
\$col = count(\$matrix[0]);
for (\$i = 0; \$i < \$row; ++\$i) {
//Get first element of i row
\$result = \$matrix[\$i][0];
for (\$j = 1; \$j < \$col; ++\$j) {
if (\$result < \$matrix[\$i][\$j]) {
//Get new max element
\$result = \$matrix[\$i][\$j];
}
}
echo(" ". \$result ."\n");
}
}
};

function main() {
\$obj = new MyMatrix();
//Define matrix
\$matrix = array(array(1, 0, 2, 3),
array(4, 1, 1, 4),
array(0, -3, 1, 0),
array(2, 5, 1, 2));
\$obj->row_max_element(\$matrix);

}
main();```
```

#### Output

`````` 3
4
1
5``````
``````/*
Node Js Program
Find maximum element of each row in a matrix
*/
class MyMatrix {
//print max element in each row
row_max_element(matrix) {
var result = 0;
//Get the size of array
var row = matrix.length;
var col = matrix[0].length;
for (var i = 0; i < row; ++i) {
//Get first element of i row
result = matrix[i][0];
for (var j = 1; j < col; ++j) {
if (result < matrix[i][j]) {
//Get new max element
result = matrix[i][j];
}
}

process.stdout.write(" " + result + "\n");
}
}
}

function main(args) {
var obj = new MyMatrix();
//Define matrix
var matrix = [
[1, 0, 2, 3],
[4, 1, 1, 4],
[0, -3, 1, 0],
[2, 5, 1, 2]
];
obj.row_max_element(matrix);
}
main();```
```

#### Output

`````` 3
4
1
5``````
``````# Python 3 Program

# Find maximum element of each row in a matrix
class MyMatrix :
# print max element in each row
def row_max_element(self, matrix) :
result = 0
row = len(matrix)
col = len(matrix[0])
i = 0
while (i < row) :
# Get first element of i row
result = matrix[i][0]
j = 1
while (j < col) :
if (result < matrix[i][j]) :
# Get new max element
result = matrix[i][j]

j += 1

print(" ", result ,"\n", end = "")
i += 1

def main() :
obj = MyMatrix()
matrix = [
[1, 0, 2, 3],
[4, 1, 1, 4],
[0, -3, 1, 0],
[2, 5, 1, 2]
]
obj.row_max_element(matrix)

if __name__ == "__main__":
main()```
```

#### Output

``````  3
4
1
5``````
``````# Ruby Program
# Find maximum element of each row in a matrix
class MyMatrix
# print max element in each row
def row_max_element(matrix)
result = 0
row = matrix.length
col = matrix[0].length
i = 0
while (i < row)
# Get first element of i row
result = matrix[i][0]
j = 1
while (j < col)
if (result < matrix[i][j])
# Get new max element
result = matrix[i][j]
end
j += 1
end
print(" ", result ,"\n")
i += 1
end
end
end
def main()
obj = MyMatrix.new()
matrix = [
[1, 0, 2, 3],
[4, 1, 1, 4],
[0, -3, 1, 0],
[2, 5, 1, 2]
]
obj.row_max_element(matrix)
end
main()```
```

#### Output

`````` 3
4
1
5
``````
``````/*
Scala Program
Find maximum element of each row in a matrix
*/
class MyMatrix {
//print max element in each row
def row_max_element(matrix: Array[Array[Int]]): Unit = {
var result: Int = 0;
val row: Int = matrix.length;
val col: Int = matrix(0).length;
var i: Int = 0;
while (i < row) {
//Get first element of i row
result = matrix(i)(0);
var j: Int = 1;
while (j < col) {
if (result < matrix(i)(j)) {
//Get new max element
result = matrix(i)(j);
}
j += 1;
}
print(" " + result + "\n");
i += 1;
}
}
}
object Main {
def main(args: Array[String]): Unit = {
val obj: MyMatrix = new MyMatrix();
val matrix: Array[Array[Int]] = Array(
Array(1, 0, 2, 3),
Array(4, 1, 1, 4),
Array(0, -3, 1, 0),
Array(2, 5, 1, 2));
obj.row_max_element(matrix);
}
}```
```

#### Output

`````` 3
4
1
5``````
``````/*
Swift 4 Program
Find maximum element of each row in a matrix
*/
class MyMatrix {
//print max element in each row
func row_max_element(_ matrix: [
[Int]
]) {
var result: Int = 0;
let row: Int = matrix.count;
let col: Int = matrix[0].count;
var i: Int = 0;
while (i < row) {
//Get first element of i row
result = matrix[i][0];
var j: Int = 1;
while (j < col) {
if (result < matrix[i][j]) {
//Get new max element
result = matrix[i][j];
}
j += 1;
}
print(" ", result ,"\n", terminator: "");
i += 1;
}
}
}
func main() {
let obj: MyMatrix = MyMatrix();
let matrix: [
[Int]
] = [
[1, 0, 2, 3],
[4, 1, 1, 4],
[0, -3, 1, 0],
[2, 5, 1, 2]
];
obj.row_max_element(matrix);
}
main();```
```

#### Output

``````  3
4
1
5``````

## Time Complexity

The time complexity of this algorithm is O(ROW * COL), where ROW is the number of rows in the matrix and COL is the number of columns. This is because we iterate through each element of the matrix exactly once to find the maximum element in each row. The nested loops contribute to the complexity.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.