Maximum Subsequence sum with difference among consecutive numbers less than K – GeeksforGeeks

Given an array arr[], find the maximum sum that can be achieved by picking a subsequence such that the difference between consecutive elements in the subsequence is less than or equal to a given number ‘K’.

Examples:

Input: arr[] = {1, 8, 9, 4, 6, 7}, K = 2
Output: 24. 
Explanation: The maximum sum can be obtained by taking the subsequence {8, 9, 7}. As 8 + 9 + 7 = 24

Input: arr[] = {1, -2, 3, 14, 6, -17, 16, 25}, K = 5
Output: 30.
Explanation: The maximum sum can be obtained by taking the subsequence {14, 16}. As 14 + 16 = 30

Naive Approach: The Brute force approach to solve this problem is to generate all subsequences with no consecutive elements with differences greater than K. Then find the sum of all such subsequences and print the maximum sum. 

Time complexity: O(2n)
Auxiliary Space: O(1)

Efficient Approach: To solve the problem follow the below approach:

The idea to solve this problem is by using the concept of dynamic programming. In this approach, first, we initialize an array dp[] which stores the maximum sum ending at index i. Then we run a loop and for every index i, we have dp[i] as the sum of the current element arr[i] plus the maximum achievable sum in previous positions which has an absolute difference of less than or equal to k with arr[i].

Follow the steps involved in the approach:

  • Initialize a vector say dp of the size of the array that will store the maximum sum till index i, dp[i].
  • Initialize the base case, dp[0] with arr[0] as for a single element the maximum sum is the element itself.
  • Run a loop from i = 1 to i < n.
  • Initialize a variable say maxSum, that will store the maximum sum achievable so far.
  • For each index i, run a loop from j = i – 1 to j ≥ 0 and check if the difference between the current element and the previous element is less than or equal to K, arr[i] – arr[j] ≤ K.
  • If arr[i] – arr[j] ≤ K, update maxSum = max(maxSum, dp[j]). Here maxSum is the maximum sum achievable ending at the previous position.
  • Update dp[i] with the maximum sum, dp[i] = maxSum + arr[i].
  • In the last return the maximum element of the dp array which will be the maximum sum of subsequences with given conditions.

Below is the code for the above approach.

C++

#include <bits/stdc++.h>

using namespace std;

  

int max_sum_subseq(vector<int> arr, int k)

{

    int n = arr.size();

  

    

    

    vector<int> dp(n, 0);

    dp[0] = arr[0];

    for (int i = 1; i < n; i++) {

  

        

        

        int maxSum = 0;

        for (int j = i - 1; j >= 0; j--) {

  

            

            

            

            if (abs(arr[i] - arr[j]) <= k) {

                maxSum = max(maxSum, dp[j]);

            }

        }

  

        

        dp[i] = maxSum + arr[i];

    }

  

    

    return *max_element(dp.begin(), dp.end());

}

  

int main()

{

    vector<int> arr = { 1, 8, 9, 4, 6, 7 };

    int K = 2;

  

    

    cout << "Maximum sum: " << max_sum_subseq(arr, K)

         << endl;

    return 0;

}

Time Complexity: O(N2)
Auxiliary Space: O(N)

 

Stay connected with us on social media platform for instant update click here to join our  Twitter, & Facebook We are now on Telegram. Click here to join our channel (@TechiUpdate) and stay updated with the latest Technology headlines. For all the latest Technology News Click Here 

Read original article here

Denial of responsibility! FineRadar is an automatic aggregator around the global media. All the content are available free on Internet. We have just arranged it in one platform for educational purpose only. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials on our website, please contact us by email – abuse@fineradar.com. The content will be deleted within 24 hours.
Amongconsecutivedifferencefineradar updateFree Fire Redeem Codesgadget updateGeeksforGeeksLatest tech newsmaximumnumbersSubsequencesumTech Headlinestech newsTech News UpdatesTechnologyTechnology News
Comments (0)
Add Comment