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.
Input : 1 2 3 1 2 3 7 8 9 Output : 14 (2 + 3 + 9) (values we are adding are strictly increasing) Input : 4 2 3 3 2 1 1 2 2 Output : -1 (No subsequent increasing elements can be picked from consecutive rows)
Approach :- One can simply run the loop from last row, get the greatest element from there say it prev_max, and keep record for the minimum difference among the elements of the row just above it, if any element found with positive difference, then add it to prev_max else print -1. Continue the same process for every row.
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
- Maximize sum of K elements selected from a Matrix such that each selected element must be preceded by selected row elements
- Minimum element of each row and each column in a matrix
- Minimum difference between adjacent elements of array which contain elements from each row of a matrix
- Maximum sum of a Matrix where each value is from a unique row and column
- Sum of matrix element where each elements is integer division of row and column
- Maximum path sum that starting with any cell of 0-th row and ending with any cell of (N-1)-th row
- Find maximum element of each row in a matrix
- Sum of matrix in which each element is absolute difference of its row and column numbers
- Sum of GCDs of each row of the given matrix
- Construct a Binary Matrix whose sum of each row and column is a Prime Number
- Find sum of all elements in a matrix except the elements in row and/or column of given cell?
- Construct a Matrix such that each cell consists of sum of adjacent elements of respective cells in given Matrix
- Find all matrix elements which are minimum in their row and maximum in their column
- 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
- Program to print elements of a Matrix row-wise skipping alternate elements
- Find row with maximum sum in a Matrix
- Maximum sum of any submatrix of a Matrix which is sorted row-wise 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 firstname.lastname@example.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.