- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Maximum sum in a 2 x n grid such that no two elements are adjacent in C++

In this problem, we are given a rectangular grid of size 2 x n. Our task is to create a program to find the Maximum sum in a 2 x n grid such that no two elements are adjacent in C++.

## Problem Description

To find the maximum sum, we cannot select elements that are adjacent to the current element, vertically, horizontally or diagonally.

**Let’s take an example to understand the problem,**

## Input

rectGrid[2][] =389 411

## Output

13

## Explanation

all possible sums are

If we start from rectGrid[0][0] i.e. 3, then we can add only 9 or 1. The maxSum is 12.

If we start from rectGrid[1][0] i.e. 4, then we can add only 9 or 1. The maxSum is 13.

If we start from rectGrid[0][1] i.e. 8, then we cannot add any element. The maxSum is 8.

If we start from rectGrid[1][1] i.e. 1, then we cannot add any element. The maxSum is 1.

If we start from rectGrid[0][2] i.e. 9, then we can add only 3 or 4. The maxSum is 13.

If we start from rectGrid[1][2] i.e. 1, then we can add only 3 or 4. The maxSum is 5.

The overall maximum sum is 13.

## Solution Approach

The problem is similar to maximum sum such that no two elements are adjacent that we have seen in the last post. The difference is the array which is 2D here and the condition for adjacent elements. So, we will be considering the maximum element using conditions for both rows and columns. As each column has two rows, we will take maximum possible consideration of alternate elements.

## Example

Program to illustrate the working of solution,

#include<iostream> using namespace std; int findMax(int a, int b){ if(a > b) return a; return b; } int calcMaxSum(int rectGrid[2][20], int N){ int currectSum = 0; int nextSum = 0; int altSum; for (int i = 0; i<N; i++ ){ altSum = findMax(nextSum, currectSum); currectSum = nextSum + findMax(rectGrid[0][i], rectGrid[1][i]); nextSum = altSum; } int maxSum = findMax(nextSum, currectSum); return maxSum; } int main(){ int rectGrid[2][20] = {{3, 8, 9, 5}, {4, 1, 2, 7}}; int N = 4; cout<<"The maximum sum in a 2 x "<<N<<" grid such that no two elements are adjacent is "<<calcMaxSum(rectGrid, N); return 0; }

## Output

The maximum sum in a 2 x 4 grid such that no two elements are adjacent is 15

- Related Questions & Answers
- Maximum sum such that no two elements are adjacent - Set 2 in C++
- Maximum sum such that no two elements are adjacent in C++
- Maximum sum in circular array such that no two elements are adjacent in C++
- Maximum sum such that no two elements are adjacent Alternate Method in C++ program
- Maximum sum of nodes in Binary tree such that no two are adjacent in C++
- Maximum sum of nodes in Binary tree such that no two are adjacent | Dynamic Programming In C++
- Maximum sum of nodes in Binary tree such that no two are adjacent using Dynamic Programming in C++ program
- Rearrange characters in a string such that no two adjacent are same in C++
- Maximum subsequence sum such that no three are consecutive
- Maximum subsequence sum such that no three are consecutive in C++ Program
- Find maximum value of x such that n! % (k^x) = 0 in C++
- Maximum sum possible for a sub-sequence such that no two elements appear at a distance < K in the array in C++
- Maximum sum possible for a sub-sequence such that no two elements appear at a distance < K in the array in C++ program
- Find minimum x such that (x % k) * (x / k) == n in C++
- Print n 0s and m 1s such that no two 0s and no three 1s are together in C Program