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 staring 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++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the maximum sum // of the sub-sequence such that two // consecutive elements have a difference of // at least 3 in their indices // in the given array int max_sum(int a[], int n) { int dp[n]; // If there is a single element in the array if (n == 1) { // Either select it or don't dp[0] = max(0, a[0]); } // If there are two elements else if (n == 2) { // Either select the first // element or don't dp[0] = max(0, a[0]); // Either select the first or the second element // or don't select any element dp[1] = max(a[1], dp[0]); } else if (n >= 3) { // Either select the first // element or don't dp[0] = max(0, a[0]); // Either select the first or the second element // or don't select any element dp[1] = max(a[1], max(0, a[0])); // Either select first, second, third or nothing dp[2] = max(a[2], max(a[1], max(0, a[0]))); int i = 3; // For the rest of the elements while (i < n) { // Either select the best sum till // previous_index or select the current // element + best_sum till index-3 dp[i] = max(dp[i - 1], a[i] + dp[i - 3]); i++; } } return dp[n - 1]; } // Driver code int main() { int arr[] = { 1, 2, -2, 4, 3 }; int n = sizeof(arr) / sizeof(arr[0]); cout << max_sum(arr, n); return 0; } |
Java
// Java implementation of the approach class GFG { // Function to return the maximum sum // of the sub-sequence such that two // consecutive elements have a difference of // at least 3 in their indices // in the given array static int max_sum(int a[], int n) { int []dp = new int[n]; // If there is a single element in the array if (n == 1) { // Either select it or don't dp[0] = Math.max(0, a[0]); } // If there are two elements else if (n == 2) { // Either select the first // element or don't dp[0] = Math.max(0, a[0]); // Either select the first or the second element // or don't select any element dp[1] = Math.max(a[1], dp[0]); } else if (n >= 3) { // Either select the first // element or don't dp[0] = Math.max(0, a[0]); // Either select the first or the second element // or don't select any element dp[1] = Math.max(a[1], Math.max(0, a[0])); // Either select first, second, third or nothing dp[2] = Math.max(a[2], Math.max(a[1], Math.max(0, a[0]))); int i = 3; // For the rest of the elements while (i < n) { // Either select the best sum till // previous_index or select the current // element + best_sum till index-3 dp[i] = Math.max(dp[i - 1], a[i] + dp[i - 3]); i++; } } return dp[n - 1]; } // Driver code public static void main(String[] args) { int arr[] = { 1, 2, -2, 4, 3 }; int n = arr.length; System.out.println(max_sum(arr, n)); } } // This code is contributed by Rajput-Ji |
Python3
# Python3 implementation of the approach # Function to return the maximum sum # of the sub-sequence such that two # consecutive elements have a difference of # at least 3 in their indices # in the given array def max_sum(a, n) : dp = [0]*n; # If there is a single element in the array if (n == 1) : # Either select it or don't dp[0] = max(0, a[0]); # If there are two elements elif (n == 2) : # Either select the first # element or don't dp[0] = max(0, a[0]); # Either select the first or the second element # or don't select any element dp[1] = max(a[1], dp[0]); elif (n >= 3) : # Either select the first # element or don't dp[0] = max(0, a[0]); # Either select the first or the second element # or don't select any element dp[1] = max(a[1], max(0, a[0])); # Either select first, second, third or nothing dp[2] = max(a[2], max(a[1], max(0, a[0]))); i = 3; # For the rest of the elements while (i < n) : # Either select the best sum till # previous_index or select the current # element + best_sum till index-3 dp[i] = max(dp[i - 1], a[i] + dp[i - 3]); i += 1; return dp[n - 1]; # Driver code if __name__ == "__main__" : arr = [ 1, 2, -2, 4, 3 ]; n = len(arr); print(max_sum(arr, n)); # This code is contributed by AnkitRai01 |
C#
// C# implementation of the approach using System; class GFG { // Function to return the maximum sum // of the sub-sequence such that two // consecutive elements have a difference of // at least 3 in their indices // in the given array static int max_sum(int []a, int n) { int []dp = new int[n]; // If there is a single element in the array if (n == 1) { // Either select it or don't dp[0] = Math.Max(0, a[0]); } // If there are two elements else if (n == 2) { // Either select the first // element or don't dp[0] = Math.Max(0, a[0]); // Either select the first or the second element // or don't select any element dp[1] = Math.Max(a[1], dp[0]); } else if (n >= 3) { // Either select the first // element or don't dp[0] = Math.Max(0, a[0]); // Either select the first or the second element // or don't select any element dp[1] = Math.Max(a[1], Math.Max(0, a[0])); // Either select first, second, third or nothing dp[2] = Math.Max(a[2], Math.Max(a[1], Math.Max(0, a[0]))); int i = 3; // For the rest of the elements while (i < n) { // Either select the best sum till // previous_index or select the current // element + best_sum till index-3 dp[i] = Math.Max(dp[i - 1], a[i] + dp[i - 3]); i++; } } return dp[n - 1]; } // Driver code static public void Main () { int []arr = { 1, 2, -2, 4, 3 }; int n = arr.Length; Console.Write(max_sum(arr, n)); } } // This code is contributed by ajit.. |
5
Time Complexity: O(N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count maximum occurrence of subsequence in string such that indices in subsequence is in A.P.
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Longest subsequence such that adjacent elements have at least one common digit
- Count of 0s to be flipped to make any two adjacent 1s at least K 0s apart
- Minimum sum subsequence such that at least one of every four consecutive elements is picked
- Length of the longest increasing subsequence such that no two adjacent elements are coprime
- Maximum sum subsequence with at-least k distant elements
- Maximum sum such that no two elements are adjacent
- Maximum sum in a 2 x n grid such that no two elements are adjacent
- Maximum sum in circular array such that no two elements are adjacent
- Maximum sum such that no two elements are adjacent | Set 2
- Maximum sum such that exactly half of the elements are selected and no two adjacent
- Maximum sum in circular array such that no two elements are adjacent | Set 2
- Longest subarray such that adjacent elements have at least one common digit | Set 1
- Longest subarray such that adjacent elements have at least one common digit | Set - 2
- Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements
- Maximum subsequence sum with adjacent elements having atleast K difference in index
- Maximum subsequence sum of at most K-distant adjacent elements
- Cost of creating smallest subsequence with sum of difference between adjacent elements maximum
- First subarray having sum at least half the maximum sum of any subarray of size K
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

