- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

# Maximum subsequence sum such that no three are consecutive in C++ Program

In this problem, we are given an array arr[] consisting of n positive integers. Our task is to create a program to find the maximum subsequence sum such that no three are consecutive.

**Problem Description** − Here, we need to find the sum of sequences created from the array such that there are no three consecutive elements.

**Consecutive elements of** an array are those elements that have followed the same order of index.

arr[0], arr[1], arr[2], …

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

## Input

arr[] = {5, 9, 12, 15}

## Output

32

## Explanation

Sum = 5 + 12 + 15 = 32

## Solution Approach

A simple solution to the problem is by creating an auxiliary array to store the sum until the current index. And then find the sum and check the sum till the index by checking for consecutive sums.

For the first two sum values, sumVal[0] = arr[0] sumVal[1] = arr[0] + arr[1]

Then the third value to be considered cannot be directly considered. And for considering the sum, we will check for the conditions with the current three,If considering arr[i], increases the sum value, exclude arr[i−1] or arr[i−2].Else do not consider arr[i], the sum remains the same.

sum[i] = max(sum[i−3] + arr[i−1] + arr[i], sum[i−2] + arr[i], sum[i−1])

## Example

**Program to show the implementation of our solution,**

#include <iostream> using namespace std; int findMaxSubSeqSum(int arr[], int n) { int maxSumArr[n]; maxSumArr[0] = arr[0]; maxSumArr[1] = arr[0] + arr[1]; maxSumArr[2] = max(maxSumArr[1], max(arr[1] + arr[2], arr[0] + arr[2])); for (int i = 3; i < n; i++){ int sum1 = maxSumArr[i − 2] + arr[i]; int sum2 = arr[i] + arr[i − 1] + maxSumArr[i − 3]; maxSumArr[i] = max(max(maxSumArr[i − 1], sum1), sum2); } return maxSumArr[n − 1]; } int main() { int arr[] = { 5, 9, 12, 15 }; int n = sizeof(arr) / sizeof(arr[0]); cout<<"The maximum subsequence sum such that no three are consecutive is "<<findMaxSubSeqSum(arr, n); return 0; }

## Output

The maximum subsequence sum such that no three are consecutive is 32

- Related Articles
- Maximum subsequence sum such that no three are consecutive
- Maximum sum such that no two elements are adjacent Alternate Method in C++ program
- Maximum sum such that no two elements are adjacent in C++
- Program to print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime Using C++
- Maximum sum such that no two elements are adjacent - Set 2 in C++
- Maximum sum in circular array such that no two elements are adjacent in C++
- 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 using Dynamic Programming in C++ program
- Maximum sum alternating subsequence in C++ program
- Maximum sum of nodes in Binary tree such that no two are adjacent | Dynamic Programming In C++
- Maximum sum in a 2 x n grid such that no two elements are adjacent in C++
- Find minimum sum such that one of every three consecutive elements is taken in C++
- Print n 0s and m 1s such that no two 0s and no three 1s are together in C Program
- Maximum sum subarray such that start and end values are same in C++ Program
- Maximum Sum Increasing Subsequence using DP in C++ program