Given a rectangular grid of dimension 2 x n. We need to find out the maximum sum such that no two chosen numbers are adjacent, vertically, diagonally, or horizontally.
Examples:
Input: 1 4 5
2 0 0
Output: 7
If we start from 1 then we can add only 5 or 0.
So max_sum = 6 in this case.
If we select 2 then also we can add only 5 or 0.
So max_sum = 7 in this case.
If we select from 4 or 0 then there is no further
elements can be added.
So, Max sum is 7.
Input: 1 2 3 4 5
6 7 8 9 10
Output: 24
Approach:
This problem is an extension of Maximum sum such that no two elements are adjacent. The only thing to be changed is to take a maximum element of both rows of a particular column. We traverse column by column and maintain the maximum sum considering two cases.
1) An element of the current column is included. In this case, we take a maximum of two elements in the current column.
2) An element of the current column is excluded (or not included)
Below is the implementation of the above steps.
C++
#include<bits/stdc++.h>
#define MAX 1000
using namespace std;
int maxSum( int grid[2][MAX], int n)
{
int incl = max(grid[0][0], grid[1][0]);
int excl = 0, excl_new;
for ( int i = 1; i<n; i++ )
{
excl_new = max(excl, incl);
incl = excl + max(grid[0][i], grid[1][i]);
excl = excl_new;
}
return max(excl, incl);
}
int main()
{
int grid[2][MAX] = {{ 1, 2, 3, 4, 5},
{ 6, 7, 8, 9, 10}};
int n = 5;
cout << maxSum(grid, n);
return 0;
}
|
C
#include <stdio.h>
#define MAX 1000
int maxSum( int grid[2][MAX], int n)
{
int max = grid[0][0];
if (max < grid[1][0])
max = grid[1][0];
int incl = max;
int excl = 0, excl_new;
for ( int i = 1; i<n; i++ )
{
max = excl;
if (max < incl)
max = incl;
excl_new = max;
max = grid[0][i];
if (max < grid[1][i])
max = grid[1][i];
incl = excl + max;
excl = excl_new;
}
max = excl;
if (max < incl)
max = incl;
return max;
}
int main()
{
int grid[2][MAX] = {{ 1, 2, 3, 4, 5},
{ 6, 7, 8, 9, 10}};
int n = 5;
printf ( "%d" ,maxSum(grid, n));
return 0;
}
|
Java
import java.util.*;
class GFG {
public static int maxSum( int grid[][], int n)
{
int incl = Math.max(grid[ 0 ][ 0 ], grid[ 1 ][ 0 ]);
int excl = 0 , excl_new;
for ( int i = 1 ; i < n; i++ )
{
excl_new = Math.max(excl, incl);
incl = excl + Math.max(grid[ 0 ][i], grid[ 1 ][i]);
excl = excl_new;
}
return Math.max(excl, incl);
}
public static void main(String[] args)
{
int grid[][] = {{ 1 , 2 , 3 , 4 , 5 },
{ 6 , 7 , 8 , 9 , 10 }};
int n = 5 ;
System.out.println(maxSum(grid, n));
}
}
|
Python3
def maxSum(grid, n) :
incl = max (grid[ 0 ][ 0 ], grid[ 1 ][ 0 ])
excl = 0
for i in range ( 1 , n) :
excl_new = max (excl, incl)
incl = excl + max (grid[ 0 ][i], grid[ 1 ][i])
excl = excl_new
return max (excl, incl)
if __name__ = = "__main__" :
grid = [ [ 1 , 2 , 3 , 4 , 5 ],
[ 6 , 7 , 8 , 9 , 10 ] ]
n = 5
print (maxSum(grid, n))
/ / This code is contributed by Ryuga
|
C#
using System;
class GFG
{
public static int maxSum( int [,]grid, int n)
{
int incl = Math.Max(grid[0, 0],
grid[1, 0]);
int excl = 0, excl_new;
for ( int i = 1; i < n; i++ )
{
excl_new = Math.Max(excl, incl);
incl = excl + Math.Max(grid[0, i],
grid[1, i]);
excl = excl_new;
}
return Math.Max(excl, incl);
}
public static void Main(String[] args)
{
int [,]grid = {{ 1, 2, 3, 4, 5},
{ 6, 7, 8, 9, 10}};
int n = 5;
Console.Write(maxSum(grid, n));
}
}
|
PHP
<?php
function maxSum( $grid , $n )
{
$incl = max( $grid [0][0], $grid [1][0]);
$excl = 0;
$excl_new ;
for ( $i = 1; $i < $n ; $i ++ )
{
$excl_new = max( $excl , $incl );
$incl = $excl + max( $grid [0][ $i ],
$grid [1][ $i ]);
$excl = $excl_new ;
}
return max( $excl , $incl );
}
$grid = array ( array (1, 2, 3, 4, 5),
array (6, 7, 8, 9, 10));
$n = 5;
echo maxSum( $grid , $n );
?>
|
Javascript
<script>
function maxSum(grid,n)
{
let incl = Math.max(grid[0][0], grid[1][0]);
let excl = 0, excl_new;
for (let i = 1; i < n; i++ )
{
excl_new = Math.max(excl, incl);
incl = excl + Math.max(grid[0][i], grid[1][i]);
excl = excl_new;
}
return Math.max(excl, incl);
}
let grid =[[ 1, 2, 3, 4, 5], [6, 7, 8, 9, 10]];
let n = 5;
document.write(maxSum(grid, n));
</script>
|
Output:
24
Time Complexity: O(n) where n is number of elements in given array. As, we are using a loop to traverse N times so it will cost us O(N) time
Auxiliary Space: O(1), as we are not using any extra space.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!