Open In App

Find and remove maximum value in each row of a given Matrix

Last Updated : 05 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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)


Output

Total 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)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads