- Related Questions & Answers
- Maximum sum possible for a sub-sequence such that no two elements appear at a distance < K in the array in C++
- Maximum sum such that no two elements are adjacent in C++
- Maximum sum in circular array such that no two elements are adjacent in C++
- Maximum sum such that no two elements are adjacent Alternate Method in C++ program
- Maximum sum such that no two elements are adjacent - Set 2 in C++
- Maximum value K such that array has at-least K elements that are >= K in C++
- Maximum sum in a 2 x n grid such that no two elements are adjacent in C++
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST in C++ program
- Maximum subsequence sum such that no three are consecutive in C++ Program
- Find maximum sum of triplets in an array such than i < j < k and a[i] < a[j] < a[k] in C++
- Place k elements such that minimum distance is maximized in C++
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST in C++
- Find maximum sum of triplets in an array such than i < j < k and a[i] < a[j] < a[k] in Python
- Maximum sum of nodes in Binary tree such that no two are adjacent in C++
- Maximum subsequence sum such that no three are consecutive

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this problem, we are given an array arr[] of size n and an integer k. Our task is to create a program to find the maximum sum possible for a subsequence such that no two elements appear at a distance < K in the array.

**Problem Description** − We need to find the maximum sum of sub−seqeunce that considers elements that are k distance from each other.

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

arr[] = {6, 2, 5, 1, 9, 11, 4} k = 2

16

All possible sub−sequences of elements that differ by k or more. {6, 1, 4}, sum = 11 {2, 9}, sum = 11 {5, 11}, sum = 16 {1, 4}, sum = 5 ... maxSum = 16

A solution to the problem is using dynamic programming. For the solution, we will be finding the maximum possible sum until the current element of the array. And store it into DP[i], for this we will find the max possible sum. For i-th index, we need to check if adding the current index value increases the sub−sequence sum or not.

if( DP[i − (k+1)] + arr[i] > DP[i − 1] ) −> DP[i] = DP[i − (k+1)] + arr[i] otherwise DP[i] = DP[i−1]

The maximum element of the dynamic array gives the max subsequence sum.

**Initialize** −

maxSumSubSeq = −1, maxSumDP[n]

**Step 1** −

Initialize maxSumDP[0] = arr[0]

**Step 2** −

Loop for i −> 1 to n.

**Step 2.1** −

if i < k −> maxSumDP[i] = maximum of arr[i] or maxSumDP[i− 1].

**Step 2.2** −

else, maxSumDP[i] = maximum of arr[i] or maxSumDP[i − (k + 1)] + arr[i].

**Step 3** −

Find the maximum value of all elements from maxSumDP and store it to maxSumSubSeq.

**Step 4** −

Return maxSumSubSeq

**Program to illustrate the working of our solution,**

#include <iostream> using namespace std; int retMaxVal(int a, int b){ if(a > b) return a; return b; } int calcMaxSumSubSeq(int arr[], int k, int n) { int maxSumDP[n]; int maxSum = −1; maxSumDP[0] = arr[0]; for (int i = 1; i < n; i++){ if(i < k ){ maxSumDP[i] = retMaxVal(arr[i], maxSumDP[i − 1]); } else maxSumDP[i] = retMaxVal(arr[i], maxSumDP[i − (k + 1)] + arr[i]); } for(int i = 0; i < n; i++) maxSum = retMaxVal(maxSumDP[i], maxSum); return maxSum; } int main() { int arr[] = {6, 2, 5, 1, 9, 11, 4}; int n = sizeof(arr) / sizeof(arr[0]); int k = 2; cout<<"The maximum sum possible for a sub−sequence such that no two elements appear at a distance < "<<k<<" in the array is "<<calcMaxSumSubSeq(arr, k, n); return 0; }

The maximum sum possible for a sub−sequence such that no two elements appear at a distance < 2 in the array is 16

Advertisements