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.

**Examples:**

Input:matrix = [[3, 4, 4, 4], [1, 3, 4, 4], [3, 2, 3, 4], [4, 4, 4, 4]]Output:16Explanation: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 = 16Input:matrix = [[0, 1, 0, 1], [3, 0, 0, 2], [1, 0, 2, 0], [0, 2, 0, 0]]Output:8Explanation: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

**Approach:**

- 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:

`# Python code for maximum sum of ` `# a Matrix where each value is ` `# from a unique row and column ` ` ` `# Permutations using library function ` `from` `itertools ` `import` `permutations ` ` ` `# Function MaxSum to find ` `# maximum sum in matrix ` `def` `MaxSum(side, matrix): ` ` ` ` ` `s ` `=` `'' ` ` ` `# Generating the string ` ` ` `for` `i ` `in` `range` `(` `0` `, side): ` ` ` `s ` `=` `s ` `+` `str` `(i) ` ` ` ` ` `# Permutations of s string ` ` ` `permlist ` `=` `permutations(s) ` ` ` ` ` `# List all possible case ` ` ` `cases ` `=` `[] ` ` ` ` ` `# Append all possible case in cases list ` ` ` `for` `perm ` `in` `list` `(permlist): ` ` ` `cases.append(''.join(perm)) ` ` ` ` ` `# List to store all Sums ` ` ` `sum` `=` `[] ` ` ` ` ` `# Iterate over all case ` ` ` `for` `c ` `in` `cases: ` ` ` `p ` `=` `[] ` ` ` `tot ` `=` `0` ` ` `for` `i ` `in` `range` `(` `0` `, side): ` ` ` `p.append(matrix[` `int` `(s[i])][` `int` `(c[i])]) ` ` ` `p.sort() ` ` ` `for` `i ` `in` `range` `(side` `-` `1` `, ` `-` `1` `, ` `-` `1` `): ` ` ` `tot ` `=` `tot ` `+` `p[i] ` ` ` `sum` `.append(tot) ` ` ` ` ` ` ` `# Maximum of sum list is ` ` ` `# the max sum ` ` ` `print` `(` `max` `(` `sum` `)) ` ` ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `side ` `=` `4` ` ` `matrix ` `=` `[[` `3` `, ` `4` `, ` `4` `, ` `4` `], [` `1` `, ` `3` `, ` `4` `, ` `4` `], [` `3` `, ` `2` `, ` `3` `, ` `4` `], [` `4` `, ` `4` `, ` `4` `, ` `4` `]] ` ` ` `MaxSum(side, matrix) ` ` ` `side ` `=` `3` ` ` `matrix ` `=` `[[` `1` `, ` `2` `, ` `3` `], [` `6` `, ` `5` `, ` `4` `], [` `7` `, ` `9` `, ` `8` `]] ` ` ` `MaxSum(side, matrix) ` |

*chevron_right*

*filter_none*

**Output:**

16 18

* 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.

## Recommended Posts:

- Check if the rows of a binary matrix can be made unique by removing a single column
- Find column with maximum sum in a Matrix
- Replace every matrix element with maximum of GCD of row or column
- Find maximum element of each column in a matrix
- Find pair with maximum difference in any column of a Matrix
- Check whether row or column swaps produce maximum size binary sub-matrix with all 1s
- Check if a given matrix can be converted to another given matrix by row and column exchanges
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix
- Sum of middle row and column in Matrix
- Find unique elements in a matrix
- Construct a unique matrix n x n for an input n
- Unique cells in a binary matrix
- Print matrix in zig-zag fashion from the last column
- Check if sums of i-th row and i-th column are same in matrix
- Print an N x M matrix such that each row and column has all the vowels in it
- 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
- Print unique rows in a given boolean matrix
- Find a Square Matrix such that sum of elements in every row and column is K
- Check if the given matrix is increasing row and column wise

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.