- 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

In this tutorial, we will be discussing a program to find maximum sum possible for a sub-sequence such that no two elements appear at a distance < K in the array.

For this we will be provided with an array containing N intergers and a value K. Our task is to find the maximum sum of the subsequence including elements not nearer than K.

#include <bits/stdc++.h> using namespace std; //returning maximum sum int maxSum(int* arr, int k, int n) { if (n == 0) return 0; if (n == 1) return arr[0]; if (n == 2) return max(arr[0], arr[1]); int dp[n]; dp[0] = arr[0]; for (int i = 1; i <= k; i++) dp[i] = max(arr[i], dp[i - 1]); for (int i = k + 1; i < n; i++) dp[i] = max(arr[i], dp[i - (k + 1)] + arr[i]); int max = *(std::max_element(dp, dp + n)); return max; } int main() { int arr[] = { 6, 7, 1, 3, 8, 2, 4 }; int n = sizeof(arr) / sizeof(arr[0]); int k = 2; cout << maxSum(arr, k, n); return 0; }

15

- 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++ program
- 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 - Set 2 in C++
- Maximum value K such that array has at-least K elements that are >= K in C++
- Maximum sum such that no two elements are adjacent Alternate Method in C++ program
- Maximum sum in a 2 x n grid such that no two elements are adjacent in C++
- 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 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
- Maximum possible sum of a window in an array such that elements of same window in other array are unique in c++

Advertisements