Given a matrix of size N X N, the task is to find maximum sum of this Matrix where each value picked is from a unique column for every row.
Input: matrix = [[3, 4, 4, 4], [1, 3, 4, 4], [3, 2, 3, 4], [4, 4, 4, 4]] Output: 16 Explanation: Selecting (0, 1) from row 1 = 4 Selecting (1, 2) from row 2 = 4 Selecting (2, 3) from row 3 = 4 Selecting (3, 0) from row 4 = 4 Therefore, max sum = 4 + 4 + 4 + 4 = 16 Input: matrix = [[0, 1, 0, 1], [3, 0, 0, 2], [1, 0, 2, 0], [0, 2, 0, 0]] Output: 8 Explanation: Selecting (0, 3) from row 1 = 1 Selecting (1, 0) from row 2 = 3 Selecting (2, 2) from row 3 = 2 Selecting (3, 1) from row 4 = 2 Therefore, max sum = 1 + 3 + 2 + 2 = 8
- Genrate a numeric string of size N containing numbers from 1 to N
- Find the permutation of this string (N!).
- Now pairing is done between the permutations, such that each N! pairing has a unique column for every row.
- Then calculate the sum of values for all the pairs.
Below is the implementation of the above approach:
Time complexity: O(K), where K = N!
Auxiliary Space complexity: O(K), where K = N!
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Program to find the Sum of each Row and each Column of a Matrix
- Minimum element of each row and each column in a matrix
- Sum of matrix in which each element is absolute difference of its row and column numbers
- Sum of matrix element where each elements is integer division of row and column
- Construct a Binary Matrix whose sum of each row and column is a Prime Number
- Minimum operations required to make each row and column of matrix equals
- Print an N x M matrix such that each row and column has all the vowels in it
- Count of ways to generate a Matrix with product of each row and column as 1 or -1
- Maximum sum of any submatrix of a Matrix which is sorted row-wise and column-wise
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix
- Check if a given matrix can be converted to another given matrix by row and column exchanges
- Find all matrix elements which are minimum in their row and maximum in their column
- Replace every matrix element with maximum of GCD of row or column
- Check whether row or column swaps produce maximum size binary sub-matrix with all 1s
- Find sum of all elements in a matrix except the elements in row and/or column of given cell?
- Sum of middle row and column in Matrix
- Filling diagonal to make the sum of every row, column and diagonal equal of 3x3 matrix
- Find a Square Matrix such that sum of elements in every row and column is K
- Maximum sum of elements from each row in the matrix
- Maximum path sum that starting with any cell of 0-th row and ending with any cell of (N-1)-th row
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.