- 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

In this problem, we are given an array arr[]. Our task is to create a program to find the Maximum sum such that no two elements are adjacent in C++.

We need to find the maximum sum of the sequence from the array such that no 2 numbers from the sum sequence are adjacent in the array.

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

arr[] = {5, 1, 3, 7, 9, 2, 5}

22

Taking sum sequence from index 0 with alternate elements : 5 + 3 + 9 + 5 = 22 Taking sum sequence from index 1 with alternate elements : 1 + 7 + 2 = 10

In the last set, we have seen one approach to solve the problem. Here, we will learn about the dynamic programming approach to solve the problem.

To solve the problem using the Dynamic Approach, we need to create a DP[] array that stores that max sum till the current index. And then find the sum index using this dynamic array.

The current DP max is the max of dp[i+2]+ arr[i] and dp[i+1].

Program to illustrate the working of our solution,

#include <iostream> using namespace std; int DP[100]; bool currState[100]; int maxVal(int a, int b){ if(a > b) return a; return b; } int calcMaxSumWOAdj(int arr[], int i, int n){ if (i >= n) return 0; if (currState[i]) return DP[i]; currState[i] = 1; DP[i] = maxVal(calcMaxSumWOAdj(arr, i + 1, n), arr[i] + calcMaxSumWOAdj(arr, i + 2, n)); return DP[i]; } int main(){ int arr[] = { 5, 1, 3, 7, 9, 2, 5 }; int n = sizeof(arr) / sizeof(int); cout<<"The maximum sum such that no two elements are adjacent is "<<calcMaxSumWOAdj(arr, 0, n); return 0; }

The maximum sum such that no two elements are adjacent is 22

- Related Questions & Answers
- Maximum sum such that no two elements are adjacent in C++
- Maximum sum in a 2 x n grid 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
- Maximum subsequence sum such that no three are consecutive
- Maximum subsequence sum such that no three are consecutive in C++ Program
- Maximum set bit sum in array without considering adjacent elements in C++
- Ways to paint stairs with two colors such that two adjacent are not yellow in C++
- Maximum sum of difference of adjacent elements 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
- Maximum sum subarray such that start and end values are same in C++

Advertisements