- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

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

# Maximum sum subsequence with at-least k distant elements in C++

In this tutorial, we will be discussing a program to find maximum sum subsequence with at-least k distant elements.

For this we will be provided with an array containing integers and a value K. Our task is to find the subsequence having maximum sum such that all the elements are at least K elements apart.

## Example

#include <bits/stdc++.h> using namespace std; //finding maximum sum subsequence int maxSum(int arr[], int N, int k) { int MS[N]; MS[N - 1] = arr[N - 1]; for (int i = N - 2; i >= 0; i--) { if (i + k + 1 >= N) MS[i] = max(arr[i], MS[i + 1]); else MS[i] = max(arr[i] + MS[i + k + 1], MS[i + 1]); } return MS[0]; } int main() { int N = 10, k = 2; int arr[] = { 50, 70, 40, 50, 90, 70, 60, 40, 70, 50 }; cout << maxSum(arr, N, k); return 0; }

## Output

230

- Related Questions & Answers
- Maximum sum subsequence with at-least k distant elements in C++ program
- Shortest Subarray with Sum at Least K in C++
- Largest sum subarray with at-least k numbers in C++
- Subarray sum with at least two elements in JavaScript
- Maximum value K such that array has at-least K elements that are >= K in C++
- Program to find k where k elements have value at least k in Python
- Program to find length of longest increasing subsequence with at least k odd values in Python
- Maximum Sum Increasing Subsequence
- Python program to find Non-K distant elements
- Program to find minimum sum subsequence by taking at least one element from consecutive 3 elements in python
- Longest Substring with At Least K Repeating Characters in C++
- Maximum subarray sum by flipping signs of at most K array elements in C++
- Maximum sum alternating subsequence in C++
- Maximum Sum Decreasing Subsequence in C++
- Maximum sum alternating subsequence in C++ program

Advertisements