Maximum sub-sequence sum such that indices of any two adjacent elements differs at least by 3
Last Updated :
19 Apr, 2023
Given an array arr[] of integers, the task is to find the maximum sum of any sub-sequence in the array such that any two adjacent elements in the selected sequence have at least a difference of 3 in their indices in the given array.
In other words, if you select arr[i] then the next element you can select is arr[i + 3], arr[i + 4], and so on… but you cannot select arr[i + 1] and arr[i + 2].
Examples:
Input: arr[] = {1, 2, -2, 4, 3}
Output: 5
{1, 4} and {2, 3} are the only sub-sequences
with maximum sum.
Input: arr[] = {1, 2, 72, 4, 3, 9}
Output: 81
Naive approach: We generate all possible subsets of the array and check if the current subset satisfies the condition.If yes, then we compare its sum with the largest sum we have obtained till now. It is not an efficient approach as it takes exponential time .
Efficient approach: This problem can be solved using dynamic programming. Let’s decide the states of the dp. Let dp[i] be the largest possible sum for the sub-sequence starting from index 0 and ending at index i. Now, we have to find a recurrence relation between this state and a lower-order state.
In this case for an index i, we will have two choices:
- Choose the current index: In this case, the relation will be dp[i] = arr[i] + dp[i – 3].
- Skip the current index: Relation will be dp[i] = dp[i – 1].
We will choose the path that maximizes our result. Thus the final relation will be:
dp[i] = max(dp[i – 3] + arr[i], dp[i – 1])
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int max_sum( int a[], int n)
{
int dp[n];
if (n == 1) {
dp[0] = max(0, a[0]);
}
else if (n == 2) {
dp[0] = max(0, a[0]);
dp[1] = max(a[1], dp[0]);
}
else if (n >= 3) {
dp[0] = max(0, a[0]);
dp[1] = max(a[1], max(0, a[0]));
dp[2] = max(a[2], max(a[1], max(0, a[0])));
int i = 3;
while (i < n) {
dp[i] = max(dp[i - 1], a[i] + dp[i - 3]);
i++;
}
}
return dp[n - 1];
}
int main()
{
int arr[] = { 1, 2, -2, 4, 3 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << max_sum(arr, n);
return 0;
}
|
Java
class GFG
{
static int max_sum( int a[], int n)
{
int []dp = new int [n];
if (n == 1 )
{
dp[ 0 ] = Math.max( 0 , a[ 0 ]);
}
else if (n == 2 )
{
dp[ 0 ] = Math.max( 0 , a[ 0 ]);
dp[ 1 ] = Math.max(a[ 1 ], dp[ 0 ]);
}
else if (n >= 3 )
{
dp[ 0 ] = Math.max( 0 , a[ 0 ]);
dp[ 1 ] = Math.max(a[ 1 ], Math.max( 0 , a[ 0 ]));
dp[ 2 ] = Math.max(a[ 2 ], Math.max(a[ 1 ], Math.max( 0 , a[ 0 ])));
int i = 3 ;
while (i < n)
{
dp[i] = Math.max(dp[i - 1 ], a[i] + dp[i - 3 ]);
i++;
}
}
return dp[n - 1 ];
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , - 2 , 4 , 3 };
int n = arr.length;
System.out.println(max_sum(arr, n));
}
}
|
Python3
def max_sum(a, n) :
dp = [ 0 ] * n;
if (n = = 1 ) :
dp[ 0 ] = max ( 0 , a[ 0 ]);
elif (n = = 2 ) :
dp[ 0 ] = max ( 0 , a[ 0 ]);
dp[ 1 ] = max (a[ 1 ], dp[ 0 ]);
elif (n > = 3 ) :
dp[ 0 ] = max ( 0 , a[ 0 ]);
dp[ 1 ] = max (a[ 1 ], max ( 0 , a[ 0 ]));
dp[ 2 ] = max (a[ 2 ], max (a[ 1 ], max ( 0 , a[ 0 ])));
i = 3 ;
while (i < n) :
dp[i] = max (dp[i - 1 ], a[i] + dp[i - 3 ]);
i + = 1 ;
return dp[n - 1 ];
if __name__ = = "__main__" :
arr = [ 1 , 2 , - 2 , 4 , 3 ];
n = len (arr);
print (max_sum(arr, n));
|
C#
using System;
class GFG
{
static int max_sum( int []a, int n)
{
int []dp = new int [n];
if (n == 1)
{
dp[0] = Math.Max(0, a[0]);
}
else if (n == 2)
{
dp[0] = Math.Max(0, a[0]);
dp[1] = Math.Max(a[1], dp[0]);
}
else if (n >= 3)
{
dp[0] = Math.Max(0, a[0]);
dp[1] = Math.Max(a[1], Math.Max(0, a[0]));
dp[2] = Math.Max(a[2], Math.Max(a[1], Math.Max(0, a[0])));
int i = 3;
while (i < n)
{
dp[i] = Math.Max(dp[i - 1], a[i] + dp[i - 3]);
i++;
}
}
return dp[n - 1];
}
static public void Main ()
{
int []arr = { 1, 2, -2, 4, 3 };
int n = arr.Length;
Console.Write(max_sum(arr, n));
}
}
|
Javascript
<script>
function max_sum(a, n)
{
let dp = new Array(n);
if (n == 1)
{
dp[0] = Math.max(0, a[0]);
}
else if (n == 2)
{
dp[0] = Math.max(0, a[0]);
dp[1] = Math.max(a[1], dp[0]);
}
else if (n >= 3)
{
dp[0] = Math.max(0, a[0]);
dp[1] = Math.max(a[1], Math.max(0, a[0]));
dp[2] = Math.max(a[2], Math.max(a[1], Math.max(0, a[0])));
let i = 3;
while (i < n)
{
dp[i] = Math.max(dp[i - 1], a[i] + dp[i - 3]);
i++;
}
}
return dp[n - 1];
}
let arr = [ 1, 2, -2, 4, 3 ];
let n = arr.length;
document.write(max_sum(arr, n));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)bb
Efficient approach : Space optimization O(1)
To optimize the space complexity of the previous code. Since we only use the values of the previous three elements to compute the value of the current element in the dp array, we can replace the dp array with three variables to keep track of the maximum sum till the previous three indices. This way, we can reduce the space complexity from O(n) to O(1).
Implementation Steps:
- Initialize three variables prev_3, prev_2, and prev_1 to the maximum of 0 and the first, second, and third elements of the input array, respectively. Also, initialize a variable curr to 0.
- Iterate over the remaining elements of the input array from index 3 to n-1.
- Now calculate curr from previous computations.
- Update the variables prev_3, prev_2, prev_1 for further iterations.
- After iterating over all the elements, return the value of curr as the maximum sum of the sub-sequence.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int max_sum( int a[], int n)
{
int prev_3 = max(0, a[0]);
int prev_2 = max(a[1], max(0, a[0]));
int prev_1 = max(a[2], max(a[1], max(0, a[0])));
int curr = 0;
for ( int i = 3; i < n; i++) {
curr = max(prev_1, a[i] + prev_3);
prev_3 = prev_2;
prev_2 = prev_1;
prev_1 = curr;
}
return curr;
}
int main()
{
int arr[] = { 1, 2, -2, 4, 3 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << max_sum(arr, n);
return 0;
}
|
Java
import java.util.*;
public class Main
{
static int max_sum( int [] a, int n)
{
int prev_3 = Math.max( 0 , a[ 0 ]);
int prev_2 = Math.max(a[ 1 ], Math.max( 0 , a[ 0 ]));
int prev_1 = Math.max(
a[ 2 ], Math.max(a[ 1 ], Math.max( 0 , a[ 0 ])));
int curr = 0 ;
for ( int i = 3 ; i < n; i++) {
curr = Math.max(prev_1, a[i] + prev_3);
prev_3 = prev_2;
prev_2 = prev_1;
prev_1 = curr;
}
return curr;
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , - 2 , 4 , 3 };
int n = arr.length;
System.out.println(max_sum(arr, n));
}
}
|
Python3
def max_sum(a, n):
prev_3 = max ( 0 , a[ 0 ])
prev_2 = max (a[ 1 ], max ( 0 , a[ 0 ]))
prev_1 = max (a[ 2 ], max (a[ 1 ], max ( 0 , a[ 0 ])))
curr = 0
for i in range ( 3 , n):
curr = max (prev_1, a[i] + prev_3)
prev_3 = prev_2
prev_2 = prev_1
prev_1 = curr
return curr
arr = [ 1 , 2 , - 2 , 4 , 3 ]
n = len (arr)
print (max_sum(arr, n))
|
C#
using System;
class MainClass {
public static int max_sum( int [] a, int n)
{
int prev_3 = Math.Max(0, a[0]);
int prev_2 = Math.Max(a[1], Math.Max(0, a[0]));
int prev_1 = Math.Max(
a[2], Math.Max(a[1], Math.Max(0, a[0])));
int curr = 0;
for ( int i = 3; i < n; i++) {
curr = Math.Max(prev_1, a[i] + prev_3);
prev_3 = prev_2;
prev_2 = prev_1;
prev_1 = curr;
}
return curr;
}
public static void Main()
{
int [] arr = { 1, 2, -2, 4, 3 };
int n = arr.Length;
Console.WriteLine(max_sum(arr, n));
}
}
|
Javascript
function maxSum(a, n) {
let prev_3 = Math.max(0, a[0]);
let prev_2 = Math.max(a[1], Math.max(0, a[0]));
let prev_1 = Math.max(a[2], Math.max(a[1], Math.max(0, a[0])));
let curr = 0;
for (let i = 3; i < n; i++) {
curr = Math.max(prev_1, a[i] + prev_3);
prev_3 = prev_2;
prev_2 = prev_1;
prev_1 = curr;
}
return curr;
}
const arr = [1, 2, -2, 4, 3];
const n = arr.length;
console.log(maxSum(arr, n));
|
Output:
5
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...