Software Development

Most Subsequence sum with distinction amongst consecutive numbers lower than Ok

Most Subsequence sum with distinction amongst consecutive numbers lower than Ok
Written by admin


Given an array arr[], discover the utmost sum that may be achieved by selecting a subsequence such that the distinction between consecutive components within the subsequence is lower than or equal to a given quantity ‘Ok’.

Examples:

Enter: arr[] = {1, 8, 9, 4, 6, 7}, Ok = 2
Output: 24. 
Rationalization: The utmost sum may be obtained by taking the subsequence {8, 9, 7}. As 8 + 9 + 7 = 24

Enter: arr[] = {1, -2, 3, 14, 6, -17, 16, 25}, Ok = 5
Output: 30.
Rationalization: The utmost sum may be obtained by taking the subsequence {14, 16}. As 14 + 16 = 30

Naive Strategy: The Brute power method to resolve this drawback is to generate all subsequences with no consecutive components with variations larger than Ok. Then discover the sum of all such subsequences and print the utmost sum. 

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

Environment friendly Strategy: To resolve the issue observe the under method:

The concept to resolve this drawback is through the use of the idea of dynamic programming. On this method, first, we initialize an array dp[] which shops the utmost sum ending at index i. Then we run a loop and for each index i, we’ve dp[i] because the sum of the present component arr[i] plus the utmost achievable sum in earlier positions which has an absolute distinction of lower than or equal to okay with arr[i].

Observe the steps concerned within the method:

  • Initialize a vector say dp of the scale of the array that may retailer the utmost sum until index i, dp[i].
  • Initialize the bottom case, dp[0] with arr[0] as for a single component the utmost sum is the component itself.
  • Run a loop from i = 1 to i < n.
  • Initialize a variable say maxSum, that may retailer the utmost sum achievable up to now.
  • For every index i, run a loop from j = i – 1 to j ≥ 0 and test if the distinction between the present component and the earlier component is lower than or equal to Ok, arr[i] – arr[j] ≤ Ok.
  • If arr[i] – arr[j] ≤ Ok, replace maxSum = max(maxSum, dp[j]). Right here maxSum is the utmost sum achievable ending on the earlier place.
  • Replace dp[i] with the utmost sum, dp[i] = maxSum + arr[i].
  • Within the final return the utmost component of the dp array which would be the most sum of subsequences with given circumstances.

Under is the code for the above method.

C++

// C++ code for above method
#embody <bits/stdc++.h>
utilizing namespace std;

int max_sum_subseq(vector<int> arr, int okay)
{
    int n = arr.dimension();

    // dp[i] shops the utmost sum
    // ending at index i
    vector<int> dp(n, 0);
    dp[0] = arr[0];
    for (int i = 1; i < n; i++) {

        // maxSum shops the utmost
        // sum achievable up to now
        int maxSum = 0;
        for (int j = i - 1; j >= 0; j--) {

            // Examine if distinction between
            // arr[i] and arr[j] is much less
            // than or equal to okay
            if (abs(arr[i] - arr[j]) <= okay) {
                maxSum = max(maxSum, dp[j]);
            }
        }

        // Replace dp[i] with the utmost sum
        dp[i] = maxSum + arr[i];
    }

    // Return the utmost sum
    return *max_element(dp.start(), dp.finish());
}

// Drivers code
int principal()
{
    vector<int> arr = { 1, 8, 9, 4, 6, 7 };
    int Ok = 2;

    // Operate Name
    cout << "Most sum: " << max_sum_subseq(arr, Ok)
         << endl;
    return 0;
}

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

About the author

admin

Leave a Comment