# Maximum subsequence sum such that no three are consecutive

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

In this tutorial, we will be discussing a program to find maximum subsequence sum such that no three are consecutive.

For this we will be provided with a series of positive integers. Our task is to find the maximum sum without taking in their consecutive positive integers in the sum value.

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
//returning maximum subsequence without involving
//three consecutive numbers
int maxSumWO3Consec(int arr[], int n) {
int sum[n]; if (n >= 1) sum = arr;
if (n >= 2) sum = arr + arr;
if (n > 2) sum = max(sum, max(arr + arr, arr + arr));
for (int i = 3; i < n; i++)
sum[i] = max(max(sum[i - 1], sum[i - 2] + arr[i]), arr[i] + arr[i - 1] + sum[i - 3]);
return sum[n - 1];
}
int main() {
int arr[] = { 100, 1000 };
int n = sizeof(arr) / sizeof(arr);
cout << maxSumWO3Consec(arr, n);
return 0;
}

## Output

1100