Find and remove maximum value in each row of a given Matrix
Last Updated :
05 Mar, 2023
Given a matrix mat[][] of size N * M, the task is to find and remove the maximum of each row from the matrix and add the largest among them and return the final sum. Perform these operations till the matrix becomes empty.
Examples:
Input: M = 3, N = 2, mat[][] = [[1, 2, 4], [3, 3, 1]]
Output: 8
Explanation: In the first operation, we remove 4 from the first row and 3 from the second row (notice that, there are two cells with value 3 and we can remove any of them). Maximum of 3 and 4 is 4, now add 4 to the answer.
In the second operation, we remove 2 from the first row and 3 from the second row. Maximum of 2 and 3 is 3, now add 3 to the answer.
In the third operation, we remove 1 from the first row and 1 from the second row, now add 1 to the answer.
The final answer = 4 + 3 + 1 = 8.
Input: M = 3, N = 2, mat = [[6, 2, 4], [3, 8, 1]]
Output: 14
Explanation:In the first operation, we remove 6 from the first row and 8 from the second row. Maximum of 6 and 8 is 8, now add 8 to the answer.
In the second operation, we remove 4 from the first row and 3 from the second row. maximum of 4 and 3 is 4, now add 4 to the answer.
In the third operation, we remove 2 from the first row and 1 from the second row, now add 2 to the answer.
The final answer = 8 + 4+ 2 = 14.
Approach: Below are the steps to solve this problem:
- Sort the Matrix in Descending order.
- Initialize a variable ans = 0 to store the result.
- Initialize a variable val as the first element of each row, to store the largest element among the maximum element of each row.
- Find the maximum element from each row in the matrix by running two loops.
- Keep adding the maximum element i.e val to ans.
Below is the code for the above approach:
C++
// C++ Program To delete maximum element
// in rows and add to ans
#include <bits/stdc++.h>
using namespace std;
int deleteMaximumValue(vector<vector<int> >& vect)
{
for (int i = 0; i < vect.size(); i++) {
sort(vect[i].rbegin(), vect[i].rend());
}
int ans = 0;
for (int i = 0; i < vect[0].size(); i++) {
int val = vect[0][i];
for (int j = 1; j < vect.size(); j++) {
val = max(val, vect[j][i]);
ans += val;
}
}
return ans;
}
// Drivers code
int main()
{
vector<vector<int> > vect{
{ 1, 2, 4 },
{ 3, 3, 1 },
};
// Calling the function
int totalsum = deleteMaximumValue(vect);
cout << "Total Sum After deleting maximum Elements :"
<< totalsum << endl;
return 0;
}
Java
// Java Program To delete maximum element
// in rows and add to ans
import java.util.*;
class GFG {
public static int deleteMaximumValue(List<List<Integer>> vect) {
for (int i = 0; i < vect.size(); i++) {
Collections.sort(vect.get(i), Collections.reverseOrder());
}
int ans = 0;
for (int i = 0; i < vect.get(0).size(); i++) {
int val = vect.get(0).get(i);
for (int j = 1; j < vect.size(); j++) {
val = Math.max(val, vect.get(j).get(i));
ans += val;
}
}
return ans;
}
// Drivers code
public static void main(String[] args) {
List<List<Integer>> vect = new ArrayList<>();
vect.add(Arrays.asList(1, 2, 4));
vect.add(Arrays.asList(3, 3, 1));
// Calling the function
int totalSum = deleteMaximumValue(vect);
System.out.println("Total Sum After deleting maximum Elements :" + totalSum);
}
}
// This Code is Contributed by Prasad Kandekar(prasad264)
Python3
from typing import List
def deleteMaximumValue(vect: List[List[int]]) -> int:
for i in range(len(vect)):
vect[i].sort(reverse=True)
ans = 0
for i in range(len(vect[0])):
val = vect[0][i]
for j in range(1, len(vect)):
val = max(val, vect[j][i])
ans += val
return ans
# Drivers code
if __name__ == "__main__":
vect = [[1, 2, 4], [3, 3, 1]]
# Calling the function
totalsum = deleteMaximumValue(vect)
print(f"Total Sum After deleting maximum Elements: {totalsum}")
C#
// C# Program To delete maximum element
// in rows and add to ans
using System;
using System.Collections.Generic;
using System.Linq;
class GFG {
public static int
deleteMaximumValue(List<List<int> > vect)
{
for (int i = 0; i < vect.Count(); i++) {
vect[i].Sort();
vect[i].Reverse();
}
int ans = 0;
for (int i = 0; i < vect[0].Count(); i++) {
int val = vect[0][i];
for (int j = 1; j < vect.Count(); j++) {
val = Math.Max(val, vect[j][i]);
ans += val;
}
}
return ans;
}
// Drivers code
static void Main(string[] args)
{
List<List<int> > vect = new List<List<int> >{
new List<int>{ 1, 2, 4 },
new List<int>{ 3, 3, 1 }
};
// Calling the function
int totalSum = deleteMaximumValue(vect);
Console.WriteLine(
"Total Sum After deleting maximum Elements :"
+ totalSum);
}
}
JavaScript
// JavaScript Program To delete maximum element
// in rows and add to ans
function deleteMaximumValue(vect) {
for (var i = 0; i < vect.length; i++) {
vect[i].sort((a, b) => b - a);
}
var ans = 0;
for (var i = 0; i < vect[0].length; i++) {
var val = vect[0][i];
for (var j = 1; j < vect.length; j++) {
val = Math.max(val, vect[j][i]);
ans += val;
}
}
return ans;
}
// Driver code
var vect = [
[1, 2, 4],
[3, 3, 1],
];
// Calling the function
var totalsum = deleteMaximumValue(vect);
console.log("Total Sum After deleting maximum Elements: ", totalsum);
// This Code is Contributed by Prasad Kandekar(prasad264)
OutputTotal Sum After deleting maximum Elements :8
Time Complexity: O(N*M LOG M), where N*M is the size of the matrix.
Auxiliary Space: O(1)
Similar Reads
Find maximum element of each row in a matrix Given a matrix mat[][], the task is to find the maximum element of each row.Examples: Input: mat[][] = [[1, 2, 3] [1, 4, 9] [76, 34, 21]]Output :3976Input: mat[][] = [[1, 2, 3, 21] [12, 1, 65, 9] [1, 56, 34, 2]]Output :216556The idea is to run the loop for no_of_rows. Check each element inside the r
4 min read
Javascript Program to Find maximum element of each row in a matrix 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 :3976Input : [1, 2, 3, 21] [12, 1, 65, 9] [1, 56, 34, 2]Output :216556Approach : Approach is very simple. The idea is to run the loop for no_of_rows. Check each element inside
2 min read
Maximum difference of sum of elements in two rows in a matrix Given a matrix of m*n order, the task is to find the maximum difference between two rows Rj and Ri such that i < j, i.e., we need to find maximum value of sum(Rj) - sum(Ri) such that row i is above row j. Examples: Input : mat[5][4] = {{-1, 2, 3, 4}, {5, 3, -2, 1}, {6, 7, 2, -3}, {2, 9, 1, 4}, {2
10 min read
Find maximum element of each column in a matrix Given a matrix, the task is to find the maximum element of each column. Examples: Input: [1, 2, 3] [1, 4, 9] [76, 34, 21] Output: 76 34 21 Input: [1, 2, 3, 21] [12, 1, 65, 9] 1, 56, 34, 2] Output: 12 56 65 21 Approach: The idea is to run the loop for no_of_cols. Check each element inside the column
6 min read
Maximum sum of elements from each row in the matrix Given a matrix, find the maximum sum we can have by selecting just one element from every row. Condition is element selected from nth row must be strictly greater than element from (n-1)th row, else no element must be taken from row. Print the sum if possible else print -1. Examples : Input : 1 2 31
7 min read