Maximum sum of elements from each row in the matrix
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.
Examples :
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.
Implementation:
C++
#include <bits/stdc++.h>
#define N 3
using namespace std;
int getGreatestSum( int a[][N])
{
int prev_max = 0;
for ( int j = 0; j < N; j++)
if (prev_max < a[N - 1][j])
prev_max = a[N - 1][j];
int sum = prev_max;
for ( int i = N - 2; i >= 0; i--) {
int curr_max = INT_MIN;
for ( int j = 0; j < N; j++)
if (prev_max > a[i][j] && a[i][j] > curr_max)
curr_max = a[i][j];
if (curr_max == INT_MIN)
return -1;
prev_max = curr_max;
sum += prev_max;
}
return sum;
}
int main()
{
int a[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
cout << getGreatestSum(a) << endl;
int b[3][3] = { { 4, 5, 6 }, { 4, 5, 6 }, { 4, 5, 6 } };
cout << getGreatestSum(b) << endl;
return 0;
}
|
Java
class GFG {
static final int N = 3 ;
static int getGreatestSum( int a[][])
{
int prev_max = 0 ;
for ( int j = 0 ; j < N; j++)
if (prev_max < a[N - 1 ][j])
prev_max = a[N - 1 ][j];
int sum = prev_max;
for ( int i = N - 2 ; i >= 0 ; i--) {
int curr_max = - 2147483648 ;
for ( int j = 0 ; j < N; j++)
if (prev_max > a[i][j] && a[i][j] > curr_max)
curr_max = a[i][j];
if (curr_max == - 2147483648 )
return - 1 ;
prev_max = curr_max;
sum += prev_max;
}
return sum;
}
public static void main(String arg[])
{
int a[][] = { { 1 , 2 , 3 },
{ 4 , 5 , 6 },
{ 7 , 8 , 9 } };
System.out.println(getGreatestSum(a));
int b[][] = { { 4 , 5 , 6 },
{ 4 , 5 , 6 },
{ 4 , 5 , 6 } };
System.out.println(getGreatestSum(b));
}
}
|
Python3
N = 3
def getGreatestSum(a):
prev_max = 0
for j in range (N):
if (prev_max < a[N - 1 ][j]):
prev_max = a[N - 1 ][j]
sum = prev_max
for i in range (N - 2 , - 1 , - 1 ):
curr_max = - 2147483648
for j in range (N):
if (prev_max > a[i][j] and a[i][j] > curr_max):
curr_max = a[i][j]
if (curr_max = = - 2147483648 ):
return - 1
prev_max = curr_max
sum = sum + prev_max
return sum
a = [ [ 1 , 2 , 3 ],
[ 4 , 5 , 6 ],
[ 7 , 8 , 9 ] ]
print (getGreatestSum(a))
b = [ [ 4 , 5 , 6 ],
[ 4 , 5 , 6 ],
[ 4 , 5 , 6 ] ]
print (getGreatestSum(b))
|
C#
using System;
class GFG {
static int N = 3;
static int getGreatestSum( int [, ] a)
{
int prev_max = 0;
for ( int j = 0; j < N; j++)
if (prev_max < a[N - 1, j])
prev_max = a[N - 1, j];
int sum = prev_max;
for ( int i = N - 2; i >= 0; i--) {
int curr_max = -2147483648;
for ( int j = 0; j < N; j++)
if (prev_max > a[i, j] && a[i, j] > curr_max)
curr_max = a[i, j];
if (curr_max == -2147483648)
return -1;
prev_max = curr_max;
sum += prev_max;
}
return sum;
}
public static void Main()
{
int [, ] a = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
Console.WriteLine(getGreatestSum(a));
int [, ] b = { { 4, 5, 6 },
{ 4, 5, 6 },
{ 4, 5, 6 } };
Console.WriteLine(getGreatestSum(b));
}
}
|
PHP
<?php
$N = 3;
function getGreatestSum( $a )
{
global $N ;
$prev_max = 0;
for ( $j = 0; $j < $N ; $j ++)
if ( $prev_max < $a [ $N - 1][ $j ])
$prev_max = $a [ $N - 1][ $j ];
$sum = $prev_max ;
for ( $i = $N - 2; $i >= 0; $i --)
{
$curr_max = PHP_INT_MIN;
for ( $j = 0; $j < $N ; $j ++)
if ( $prev_max > $a [ $i ][ $j ] and
$a [ $i ][ $j ] > $curr_max )
$curr_max = $a [ $i ][ $j ];
if ( $curr_max == PHP_INT_MIN)
return -1;
$prev_max = $curr_max ;
$sum += $prev_max ;
}
return $sum ;
}
$a = array ( array (1, 2, 3),
array (4, 5, 6),
array (7, 8, 9));
echo getGreatestSum( $a ), "\n" ;
$b = array ( array (4, 5, 6),
array (4, 5, 6),
array (4, 5, 6));
echo getGreatestSum( $b ), "\n" ;
?>
|
Javascript
<script>
let N = 3;
function getGreatestSum(a)
{
let prev_max = 0;
for (let j = 0; j < N; j++)
if (prev_max < a[N - 1][j])
prev_max = a[N - 1][j];
let sum = prev_max;
for (let i = N - 2; i >= 0; i--) {
let curr_max = -2147483648;
for (let j = 0; j < N; j++)
if (prev_max > a[i][j] && a[i][j] > curr_max)
curr_max = a[i][j];
if (curr_max == -2147483648)
return -1;
prev_max = curr_max;
sum += prev_max;
}
return sum;
}
let a = [[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]];
document.write(getGreatestSum(a) + "<br/>" );
let b = [[ 4, 5, 6 ],
[ 4, 5, 6 ],
[4, 5, 6 ]];
document.write(getGreatestSum(b));
</script>
|
Time complexity: O(N2)
Auxiliary space: O(1)
Last Updated :
13 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...