Maximum subsequence sum such that no three are consecutive
Last Updated :
05 Jul, 2023
Given a sequence of positive numbers, find the maximum sum that can be formed which has no three consecutive elements present.
Examples :
Input: arr[] = {1, 2, 3}
Output: 5
We can't take three of them, so answer is
2 + 3 = 5Input: arr[] = {3000, 2000, 1000, 3, 10}
Output: 5013
3000 + 2000 + 3 + 10 = 5013Input: arr[] = {100, 1000, 100, 1000, 1}
Output: 2101
100 + 1000 + 1000 + 1 = 2101Input: arr[] = {1, 1, 1, 1, 1}
Output: 4Input: arr[] = {1, 2, 3, 4, 5, 6, 7, 8}
Output: 27
This problem is mainly an extension of below problem.
Maximum sum such that no two elements are adjacent
We maintain an auxiliary array sum[] (of same size as input array) to find the result.
sum[i] : Stores result for subarray arr[0..i], i.e.,
maximum possible sum in subarray arr[0..i]
such that no three elements are consecutive.sum[0] = arr[0]// Note : All elements are positive
sum[1] = arr[0] + arr[1]// We have three cases
// 1) Exclude arr[2], i.e., sum[2] = sum[1]
// 2) Exclude arr[1], i.e., sum[2] = sum[0] + arr[2]
// 3) Exclude arr[0], i.e., sum[2] = arr[1] + arr[2]
sum[2] = max(sum[1], arr[0] + arr[2], arr[1] + arr[2])In general,
// We have three cases
// 1) Exclude arr[i], i.e., sum[i] = sum[i-1]
// 2) Exclude arr[i-1], i.e., sum[i] = sum[i-2] + arr[i]
// 3) Exclude arr[i-2], i.e., sum[i-3] + arr[i] + arr[i-1]
sum[i] = max(sum[i-1], sum[i-2] + arr[i],
sum[i-3] + arr[i] + arr[i-1])
Below is implementation of above idea.
C++
// C++ program to find the maximum sum such that
// no three are consecutive
#include <bits/stdc++.h>
using namespace std;
// Returns maximum subsequence sum such that no three
// elements are consecutive
int maxSumWO3Consec(int arr[], int n)
{
// Stores result for subarray arr[0..i], i.e.,
// maximum possible sum in subarray arr[0..i]
// such that no three elements are consecutive.
int sum[n];
// Base cases (process first three elements)
if (n >= 1)
sum[0] = arr[0];
if (n >= 2)
sum[1] = arr[0] + arr[1];
if (n > 2)
sum[2] = max(sum[1], max(arr[1] +
arr[2], arr[0] + arr[2]));
// Process rest of the elements
// We have three cases
// 1) Exclude arr[i], i.e., sum[i] = sum[i-1]
// 2) Exclude arr[i-1], i.e., sum[i] = sum[i-2] + arr[i]
// 3) Exclude arr[i-2], i.e., sum[i-3] + arr[i] + arr[i-1]
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];
}
// Driver code
int main()
{
int arr[] = { 100, 1000 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << maxSumWO3Consec(arr, n);
return 0;
}
C
// C program to find the maximum sum such that
// no three are consecutive
#include <stdio.h>
// Find maximum between two numbers.
int max(int num1, int num2)
{
return (num1 > num2) ? num1 : num2;
}
// Returns maximum subsequence sum such that no three
// elements are consecutive
int maxSumWO3Consec(int arr[], int n)
{
// Stores result for subarray arr[0..i], i.e.,
// maximum possible sum in subarray arr[0..i]
// such that no three elements are consecutive.
int sum[n];
// Base cases (process first three elements)
if (n >= 1)
sum[0] = arr[0];
if (n >= 2)
sum[1] = arr[0] + arr[1];
if (n > 2)
sum[2] = max(sum[1],
max(arr[1] + arr[2], arr[0] + arr[2]));
// Process rest of the elements
// We have three cases
// 1) Exclude arr[i], i.e., sum[i] = sum[i-1]
// 2) Exclude arr[i-1], i.e., sum[i] = sum[i-2] + arr[i]
// 3) Exclude arr[i-2], i.e., sum[i-3] + arr[i] +
// arr[i-1]
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];
}
// Driver code
int main()
{
int arr[] = { 100, 1000 };
int n = sizeof(arr) / sizeof(arr[0]);
printf("%d \n", maxSumWO3Consec(arr, n));
return 0;
}
// This code is contributed by Aditya Kumar (adityakumar129)
Java
// java program to find the maximum sum
// such that no three are consecutive
import java.io.*;
class GFG {
// Returns maximum subsequence sum such that no three
// elements are consecutive
static int maxSumWO3Consec(int arr[], int n)
{
// Stores result for subarray arr[0..i], i.e.,
// maximum possible sum in subarray arr[0..i]
// such that no three elements are consecutive.
int sum[] = new int[n];
// Base cases (process first three elements)
if (n >= 1)
sum[0] = arr[0];
if (n >= 2)
sum[1] = arr[0] + arr[1];
if (n > 2)
sum[2] = Math.max(sum[1], Math.max(arr[1] + arr[2], arr[0] + arr[2]));
// Process rest of the elements
// We have three cases
// 1) Exclude arr[i], i.e., sum[i] = sum[i-1]
// 2) Exclude arr[i-1], i.e., sum[i] = sum[i-2] + arr[i]
// 3) Exclude arr[i-2], i.e., sum[i-3] + arr[i] + arr[i-1]
for (int i = 3; i < n; i++)
sum[i] = Math.max(Math.max(sum[i - 1], sum[i - 2] + arr[i]),
arr[i] + arr[i - 1] + sum[i - 3]);
return sum[n - 1];
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 100, 1000, 100, 1000, 1 };
int n = arr.length;
System.out.println(maxSumWO3Consec(arr, n));
}
}
// This code is contributed by vt_m
Python3
# Python program to find the maximum sum such that
# no three are consecutive
# Returns maximum subsequence sum such that no three
# elements are consecutive
def maxSumWO3Consec(arr, n):
# Stores result for subarray arr[0..i], i.e.,
# maximum possible sum in subarray arr[0..i]
# such that no three elements are consecutive.
sum = [0 for k in range(n)]
# Base cases (process first three elements)
if n >= 1 :
sum[0] = arr[0]
if n >= 2 :
sum[1] = arr[0] + arr[1]
if n > 2 :
sum[2] = max(sum[1], max(arr[1] + arr[2], arr[0] + arr[2]))
# Process rest of the elements
# We have three cases
# 1) Exclude arr[i], i.e., sum[i] = sum[i-1]
# 2) Exclude arr[i-1], i.e., sum[i] = sum[i-2] + arr[i]
# 3) Exclude arr[i-2], i.e., sum[i-3] + arr[i] + arr[i-1]
for i in range(3, n):
sum[i] = max(max(sum[i-1], sum[i-2] + arr[i]), arr[i] + arr[i-1] + sum[i-3])
return sum[n-1]
# Driver code
arr = [100, 1000, 100, 1000, 1]
n = len(arr)
print (maxSumWO3Consec(arr, n))
# This code is contributed by Afzal Ansari
C#
// C# program to find the maximum sum
// such that no three are consecutive
using System;
class GFG {
// Returns maximum subsequence
// sum such that no three
// elements are consecutive
static int maxSumWO3Consec(int[] arr,
int n)
{
// Stores result for subarray
// arr[0..i], i.e., maximum
// possible sum in subarray
// arr[0..i] such that no
// three elements are consecutive.
int[] sum = new int[n];
// Base cases (process
// first three elements)
if (n >= 1)
sum[0] = arr[0];
if (n >= 2)
sum[1] = arr[0] + arr[1];
if (n > 2)
sum[2] = Math.Max(sum[1], Math.Max(arr[1] + arr[2], arr[0] + arr[2]));
// Process rest of the elements
// We have three cases
// 1) Exclude arr[i], i.e.,
// sum[i] = sum[i-1]
// 2) Exclude arr[i-1], i.e.,
// sum[i] = sum[i-2] + arr[i]
// 3) Exclude arr[i-2], i.e.,
// sum[i-3] + arr[i] + arr[i-1]
for (int i = 3; i < n; i++)
sum[i] = Math.Max(Math.Max(sum[i - 1],
sum[i - 2] + arr[i]),
arr[i] + arr[i - 1] + sum[i - 3]);
return sum[n - 1];
}
// Driver code
public static void Main()
{
int[] arr = { 100, 1000, 100, 1000, 1 };
int n = arr.Length;
Console.Write(maxSumWO3Consec(arr, n));
}
}
// This code is contributed by nitin mittal.
JavaScript
<script>
// JavaScript program to find the maximum sum
// such that no three are consecutive
// Returns maximum subsequence sum such that no three
// elements are consecutive
function maxSumWO3Consec(arr, n)
{
// Stores result for subarray arr[0..i], i.e.,
// maximum possible sum in subarray arr[0..i]
// such that no three elements are consecutive.
let sum = [];
// Base cases (process first three elements)
if (n >= 1)
sum[0] = arr[0];
if (n >= 2)
sum[1] = arr[0] + arr[1];
if (n > 2)
sum[2] = Math.max(sum[1], Math.max(arr[1] + arr[2], arr[0] + arr[2]));
// Process rest of the elements
// We have three cases
// 1) Exclude arr[i], i.e., sum[i] = sum[i-1]
// 2) Exclude arr[i-1], i.e., sum[i] = sum[i-2] + arr[i]
// 3) Exclude arr[i-2], i.e., sum[i-3] + arr[i] + arr[i-1]
for (let i = 3; i < n; i++)
sum[i] = Math.max(Math.max(sum[i - 1], sum[i - 2] + arr[i]),
arr[i] + arr[i - 1] + sum[i - 3]);
return sum[n - 1];
}
// Driver Code
let arr = [ 100, 1000, 100, 1000, 1 ];
let n = arr.length;
document.write(maxSumWO3Consec(arr, n));
// This code is contributed by chinmoy1997pal.
</script>
PHP
<?php
// PHP program to find the maximum
// sum such that no three are consecutive
// Returns maximum subsequence
// sum such that no three
// elements are consecutive
function maxSumWO3Consec($arr, $n)
{
// Stores result for subarray
// arr[0..i], i.e., maximum
// possible sum in subarray
// arr[0..i] such that no three
// elements are consecutive$.
$sum = array();
// Base cases (process
// first three elements)
if ( $n >= 1)
$sum[0] = $arr[0];
if ($n >= 2)
$sum[1] = $arr[0] + $arr[1];
if ( $n > 2)
$sum[2] = max($sum[1], max($arr[1] + $arr[2],
$arr[0] + $arr[2]));
// Process rest of the elements
// We have three cases
// 1) Exclude arr[i], i.e.,
// sum[i] = sum[i-1]
// 2) Exclude arr[i-1], i.e.,
// sum[i] = sum[i-2] + arr[i]
// 3) Exclude arr[i-2], i.e.,
// sum[i-3] + arr[i] + arr[i-1]
for ($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];
}
// Driver code
$arr = array(100, 1000, 100, 1000, 1);
$n =count($arr);
echo maxSumWO3Consec($arr, $n);
// This code is contributed by anuj_67.
?>
Output:
1100
Time Complexity: O(n)
Auxiliary Space: O(n)
Another approach: (Using recursion)
Java
// C++ program to find the maximum sum such that
// no three are consecutive using recursion.
#include<bits/stdc++.h>
using namespace std;
// Returns maximum subsequence sum such that no three
// elements are consecutive
int maxSum(int arr[], int i, vector<int> &dp)
{
// base case
if (i < 0)
return 0;
// this condition check is necessary to avoid segmentation fault at line 21
if (i == 0)
return arr[i];
// returning maxSum for already processed indexes of array
if (dp[i] != -1)
return dp[i];
// including current element and the next consecutive element in subsequence
int a = arr[i] + arr[i - 1] + maxSum(arr, i - 3, dp);
// not including the current element in subsequence
int b = maxSum(arr, i - 1, dp);
// including current element but skipping next consecutive element
int c = arr[i] + maxSum(arr, i - 2, dp);
// returning the max of above 3 cases
return dp[i] = max(a, max(b, c));
}
// Driver code
int main()
{
int arr[] = {100, 1000, 100, 1000, 1};
int n = sizeof(arr) / sizeof(arr[0]);
vector<int> dp(n, -1); // declaring and initializing dp vector
cout << maxSum(arr, n - 1, dp) << endl;
return 0;
}
// This code is contributed by Ashish Kumar Yadav
Java
// Java program to find the maximum
// sum such that no three are
// consecutive using recursion.
import java.util.Arrays;
class GFG
{
static int arr[] = {100, 1000, 100, 1000, 1};
static int sum[] = new int[10000];
// Returns maximum subsequence
// sum such that no three
// elements are consecutive
static int maxSumWO3Consec(int n)
{
if(sum[n] != -1)
return sum[n];
//Base cases (process first three elements)
if(n == 0)
return sum[n] = 0;
if(n == 1)
return sum[n] = arr[0];
if(n == 2)
return sum[n] = arr[1] + arr[0];
// Process rest of the elements
// We have three cases
return sum[n] = Math.max(arr[n]+maxSumWO3Consec(n - 1),
maxSumWO3Consec(n - 3) + arr[n]+arr[n-1]);
}
// Driver code
public static void main(String[] args)
{
int n = arr.length;
Arrays.fill(sum, -1);
System.out.println(maxSumWO3Consec(n-1));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 program to find the maximum
# sum such that no three are consecutive
# using recursion.
arr = [100, 1000, 100, 1000, 1]
sum = [-1] * 10000
# Returns maximum subsequence sum such
# that no three elements are consecutive
def maxSumWO3Consec(n) :
if(sum[n] != -1):
return sum[n]
# 3 Base cases (process first
# three elements)
if(n == 0) :
sum[n] = 0
return sum[n]
if(n == 1) :
sum[n] = arr[0]
return sum[n]
if(n == 2) :
sum[n] = arr[1] + arr[0]
return sum[n]
# Process rest of the elements
# We have three cases
sum[n] = max(max(maxSumWO3Consec(n - 1),
maxSumWO3Consec(n - 2) + arr[n-1]),
arr[n-1] + arr[n - 2] +
maxSumWO3Consec(n - 3))
return sum[n]
# Driver code
if __name__ == "__main__" :
n = len(arr)
print(maxSumWO3Consec(n))
# This code is contributed by Ryuga
C#
// C# program to find the maximum
// sum such that no three are
// consecutive using recursion.
using System;
class GFG
{
static int []arr = {100, 1000,
100, 1000, 1};
static int []sum = new int[10000];
// Returns maximum subsequence
// sum such that no three
// elements are consecutive
static int maxSumWO3Consec(int n)
{
if(sum[n] != -1)
return sum[n];
//Base cases (process first
// three elements)
if(n == 0)
return sum[n] = 0;
if(n == 1)
return sum[n] = arr[0];
if(n == 2)
return sum[n] = arr[1] + arr[0];
// Process rest of the elements
// We have three cases
return sum[n] = Math.Max(Math.Max(maxSumWO3Consec(n - 1),
maxSumWO3Consec(n - 2) + arr[n]),
arr[n] + arr[n - 1] + maxSumWO3Consec(n - 3));
}
// Driver code
public static void Main(String[] args)
{
int n = arr.Length;
for(int i = 0; i < sum.Length; i++)
sum[i] = -1;
Console.WriteLine(maxSumWO3Consec(n));
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// JavaScript program to find the maximum
// sum such that no three are
// consecutive using recursion.
let arr = [100, 1000, 100, 1000, 1];
let sum = new Array(10000);
for(let i = 0; i < 10000; i++)
{
sum[i] = -1;
}
// Returns maximum subsequence
// sum such that no three
// elements are consecutive
function maxSumWO3Consec(n)
{
if(sum[n] != -1)
{
return sum[n];
}
//Base cases (process first three elements)
if(n == 0)
{
return sum[n] = 0;
}
if(n == 1)
{
return sum[n] = arr[0];
}
if(n == 2)
{
return sum[n] = arr[1] + arr[0];
}
// Process rest of the elements
// We have three cases
return sum[n] =
500+Math.max(Math.max(maxSumWO3Consec(n - 1),
maxSumWO3Consec(n - 2) + arr[n]),
arr[n] + arr[n - 1] + maxSumWO3Consec(n - 3));
}
let n = arr.length - 1;
document.write(maxSumWO3Consec(n) + 1);
</script>
PHP
<?php
// PHP program to find the maximum sum such that
// no three are consecutive using recursion.
$arr = array(100, 1000, 100, 1000, 1);
$sum = array_fill(0, count($arr) + 1, -1);
// Returns maximum subsequence sum such that
// no three elements are consecutive
function maxSumWO3Consec($n)
{
global $sum,$arr;
if($sum[$n] != -1)
return $sum[$n];
// Base cases (process first three elements)
if($n == 0)
return $sum[$n] = 0;
if($n == 1)
return $sum[$n] = $arr[0];
if($n == 2)
return $sum[$n] = $arr[1] + $arr[0];
// Process rest of the elements
// We have three cases
return $sum[$n] = max(max(maxSumWO3Consec($n - 1),
maxSumWO3Consec($n - 2) + $arr[$n]),
$arr[$n] + $arr[$n - 1] +
maxSumWO3Consec($n - 3));
}
// Driver code
$n = count($arr);
echo maxSumWO3Consec($n);
// This code is contributed by mits
?>
Output:
2101
Time Complexity: O(n)
Auxiliary Space: O(n)
Similar Reads
Maximum subsequence sum such that no three are consecutive in O(1) space
Given an array A[] of N positive numbers, the task is to find the maximum sum that can be formed which has no three consecutive elements present. Examples: Input: A[] = {1, 2, 3}, N=3Output: 5Explanation: Three of them can't be taken together so answer is 2 + 3 = 5 Input: A[] = {3000, 2000, 1000, 3,
8 min read
Maximum subsequence sum such that no K elements are consecutive
Given an array arr[] of N positive integers, the task is to find the maximum sum of a subsequence consisting of no K consecutive array elements. Examples: Input: arr[] = {10, 5, 8, 16, 21}, K = 4Output: 55Explanation:Maximum sum is obtained by picking 10, 8, 16, 21. Input: arr[] = {4, 12, 22, 18, 34
9 min read
Maximum length String so that no three consecutive characters are same
Given the maximum occurrences of a, b, and c in a string, the task is to make the string containing only a, b, and c such that no three consecutive characters are the same. If the resultant string equals a+b+c, return the length (a+b+c) otherwise -1. Examples: Input: a = 3, b = 3, c = 3Output: 9Expl
4 min read
Maximum Subsequence sum with difference among consecutive numbers less than K
Given an array arr[], find the maximum sum that can be achieved by picking a subsequence such that the difference between consecutive elements in the subsequence is less than or equal to a given number 'K'. Examples: Input: arr[] = {1, 8, 9, 4, 6, 7}, K = 2Output: 24. Explanation: The maximum sum ca
7 min read
Maximum subsequence sum such that all elements are K distance apart
Given an array arr[] of N integers and another integer K. The task is to find the maximum sum of a subsequence such that the difference of the indices of all consecutive elements in the subsequence in the original array is exactly K. For example, if arr[i] is the first element of the subsequence the
10 min read
Maximum sum such that no two are adjacent
Given an array of positive numbers, find the maximum sum of a subsequence such that no two numbers in the subsequence should be adjacent in the array. Examples: Input: arr[] = {5, 5, 10, 100, 10, 5}Output: 110Explanation: Pick the subsequence {5, 100, 5}.The sum is 110 and no two elements are adjace
15+ min read
Find K consecutive integers such that their sum is N
Given two integers N and K, the task is to find K consecutive integers such that their sum if N.Note: If there is no such K integers print -1. Examples: Input: N = 15, K = 5 Output: 1 2 3 4 5 Explanation: N can be represented as sum of 5 consecutive integers as follows - => N => 1 + 2 + 3 + 4
5 min read
Minimum sum subsequence such that at least one of every four consecutive elements is picked
Given an array arr[] of positive integers. The task is to find minimum sum subsequence from the array such that at least one value among all groups of four consecutive elements is picked. Examples : Input: arr[] = {1, 2, 3, 4, 5, 6, 7, 8}Output: 66 is sum of output subsequence {1, 5}Note that we hav
15+ min read
Maximize the sum by choosing a Subsequence
Given an array X[] of length N along with an integer K. Then the task is to choose a sub-sequence to maximize the sum by selecting the starting index i (1 ? i ? N ) and make the subsequence of all the elements, which are at a continuous distance (i + K) till we are not out of the array X[] or at the
6 min read
Find minimum sum such that one of every three consecutive elements is taken
Given an array of n non-negative numbers, the task is to find the minimum sum of elements (picked from the array) such that at least one element is picked out of every 3 consecutive elements in the array. Examples : Input : arr[] = {1, 2, 3} Output : 1 Input : arr[] = {1, 2, 3, 6, 7, 1} Output : 4 W
9 min read