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.
- Sum of all maximum frequency elements in Matrix
- Maximum difference of sum of elements in two rows in a matrix
- Maximum product of 4 adjacent elements in matrix
- Maximum Sum possible by selecting X elements from a Matrix based on given conditions
- Finding the maximum square sub-matrix with all equal elements
- Maximum increase in value of Matrix to keep maximum rows and columns unchanged
- Program to print elements of a Matrix row-wise skipping alternate elements
- Minimum difference between adjacent elements of array which contain elements from each row of a matrix
- Maximum trace possible for any sub-matrix of the given matrix
- Find sum of all elements in a matrix except the elements in row and/or column of given cell?
- Move matrix elements in given direction and add elements with same value
- Program to swap upper diagonal elements with lower diagonal elements of matrix.
- Maximum XOR value in matrix
- Maximum sum path in a Matrix
- Maximum sum rectangle in a 2D matrix | DP-27
- Find row with maximum sum in a Matrix
- Maximum path sum in matrix
- Pair with maximum sum in a Matrix
- Find a sub matrix with maximum XOR
- Maximum sum path in a matrix from top to bottom
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.