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
This problem is an extension of Maximum sum such that no two elements are adjacent. Only thing to be changed is to take maximum element of both row of a particular column. We traverse column by column and maintain maximum sum considering two cases.
1) An element of current column is included. In this case we take maximum of two elements in current column.
2) An element of current column is excluded (or not included)
Below is the implementation of above steps.
C++
// C++ program to find maximum sum in a grid such that // no two elements are adjacent. #include<bits/stdc++.h> #define MAX 1000 using namespace std; // Function to find max sum without adjacent int maxSum(int grid[2][MAX], int n) { // Sum including maximum element of first column int incl = max(grid[0][0], grid[1][0]); // Not including first column's element int excl = 0, excl_new; // Traverse for further elements for (int i = 1; i<n; i++ ) { // Update max_sum on including or excluding // of previous column excl_new = max(excl, incl); // Include current column. Add maximum element // from both row of current column incl = excl + max(grid[0][i], grid[1][i]); // If current column doesn't to be included excl = excl_new; } // Return maximum of excl and incl // As that will be the maximum sum return max(excl, incl); } // Driver code 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; } |
Java
// Java Code for Maximum sum in a 2 x n grid // such that no two elements are adjacent import java.util.*; class GFG { // Function to find max sum without adjacent public static int maxSum(int grid[][], int n) { // Sum including maximum element of first // column int incl = Math.max(grid[0][0], grid[1][0]); // Not including first column's element int excl = 0, excl_new; // Traverse for further elements for (int i = 1; i < n; i++ ) { // Update max_sum on including or // excluding of previous column excl_new = Math.max(excl, incl); // Include current column. Add maximum element // from both row of current column incl = excl + Math.max(grid[0][i], grid[1][i]); // If current column doesn't to be included excl = excl_new; } // Return maximum of excl and incl // As that will be the maximum sum return Math.max(excl, incl); } /* Driver program to test above function */ 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)); } } // This code is contributed by Arnav Kr. Mandal. |
Python3
# Python3 program to find maximum sum in a grid such that # no two elements are adjacent. # Function to find max sum without adjacent def maxSum(grid, n) : # Sum including maximum element of first column incl = max(grid[0][0], grid[1][0]) # Not including first column's element excl = 0 # Traverse for further elements for i in range(1, n) : # Update max_sum on including or excluding # of previous column excl_new = max(excl, incl) # Include current column. Add maximum element # from both row of current column incl = excl + max(grid[0][i], grid[1][i]) # If current column doesn't to be included excl = excl_new # Return maximum of excl and incl # As that will be the maximum sum return max(excl, incl) # Driver code 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#
// C# program Code for Maximum sum // in a 2 x n grid such that no two // elements are adjacent using System; class GFG { // Function to find max sum // without adjacent public static int maxSum(int [,]grid, int n) { // Sum including maximum element // of first column int incl = Math.Max(grid[0, 0], grid[1, 0]); // Not including first column's // element int excl = 0, excl_new; // Traverse for further elements for (int i = 1; i < n; i++ ) { // Update max_sum on including or // excluding of previous column excl_new = Math.Max(excl, incl); // Include current column. Add // maximum element from both // row of current column incl = excl + Math.Max(grid[0, i], grid[1, i]); // If current column doesn't // to be included excl = excl_new; } // Return maximum of excl and incl // As that will be the maximum sum return Math.Max(excl, incl); } // Driver Code 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)); } } // This code is contributed // by PrinciRaj1992 |
PHP
<?php // PHP program to find maximum sum // in a grid such that no two elements // are adjacent. // Function to find max sum // without adjacent function maxSum($grid, $n) { // Sum including maximum element // of first column $incl = max($grid[0][0], $grid[1][0]); // Not including first column's element $excl = 0; $excl_new; // Traverse for further elements for ($i = 1; $i < $n; $i++ ) { // Update max_sum on including or // excluding of previous column $excl_new = max($excl, $incl); // Include current column. Add maximum // element from both row of current column $incl = $excl + max($grid[0][$i], $grid[1][$i]); // If current column doesn't // to be included $excl = $excl_new; } // Return maximum of excl and incl // As that will be the maximum sum return max($excl, $incl); } // Driver code $grid = array(array(1, 2, 3, 4, 5), array(6, 7, 8, 9, 10)); $n = 5; echo maxSum($grid, $n); // This code is contributed by Sachin.. ?> |
Output:
24
Time Complexity: O(n)
Space Complexity: O(2n) which is equal to O(n)
This article is contributed by Sahil Chhabra. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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:
- Minimum product in a grid of adjacent elements
- Maximum sum such that no two elements are adjacent | Set 2
- Maximum sum such that no two elements are adjacent
- Maximum subsequence sum of at most K-distant adjacent elements
- Maximum sum in circular array such that no two elements are adjacent | Set 2
- Maximum sum in circular array such that no two elements are adjacent
- Maximum product of 4 adjacent elements in matrix
- Maximum sum such that exactly half of the elements are selected and no two adjacent
- Maximum sub-sequence sum such that indices of any two adjacent elements differs at least by 3
- Maximum length subsequence with difference between adjacent elements as either 0 or 1
- Find maximum sum from top to bottom row with no adjacent diagonal elements
- Maximum subsequence sum with adjacent elements having atleast K difference in index
- Minimal product subsequence where adjacent elements are separated by a maximum distance of K
- Sum of the count of number of adjacent squares in an M X N grid
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Check if a grid can become row-wise and column-wise sorted after adjacent swaps
- Check if all the elements can be made of same parity by inverting adjacent elements
- Minimum difference between adjacent elements of array which contain elements from each row of a matrix
- Maximum perimeter of a square in a 2D grid
- Collect maximum points in a grid using two traversals

