Knowee
Questions
Features
Study Tools

Given an array of n positive integers. Find the sum of the maximum sum subsequence of the given array such that the integers in the subsequence are sorted in strictly increasing order i.e. a strictly increasing subsequence. Example 1:Input: N = 5, arr[] = {1, 101, 2, 3, 100} Output: 106Explanation:The maximum sum of a increasing sequence is obtained from {1, 2, 3, 100},Example 2:Input: N = 4, arr[] = {4, 1, 2, 3}Output: 6Explanation:The maximum sum of a increasing sequence is obtained from {1, 2, 3}.Your Task:  You don't need to read input or print anything. Complete the function maxSumIS() which takes N and array arr as input parameters and returns the maximum value.Expected Time Complexity: O(N2)Expected Auxiliary Space: O(N)Constraints:1 ≤ N ≤ 1031 ≤ arr[i] ≤ 105

Question

Given an array of n positive integers. Find the sum of the maximum sum subsequence of the given array such that the integers in the subsequence are sorted in strictly increasing order i.e. a strictly increasing subsequence. Example 1:Input: N = 5, arr[] = {1, 101, 2, 3, 100} Output: 106Explanation:The maximum sum of a increasing sequence is obtained from {1, 2, 3, 100},Example 2:Input: N = 4, arr[] = {4, 1, 2, 3}Output: 6Explanation:The maximum sum of a increasing sequence is obtained from {1, 2, 3}.Your Task:  You don't need to read input or print anything. Complete the function maxSumIS() which takes N and array arr as input parameters and returns the maximum value.Expected Time Complexity: O(N2)Expected Auxiliary Space: O(N)Constraints:1 ≤ N ≤ 1031 ≤ arr[i] ≤ 105

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

To solve this problem, we can use dynamic programming to find the maximum sum of the increasing subsequence.

  1. Initialize an array dp of size n, where dp[i] represents the maximum sum of the increasing subsequence ending at index i.

  2. Initialize dp[0] as arr[0], since the maximum sum of an increasing subsequence ending at the first element is the element itself.

  3. Iterate from index 1 to n-1:

    • For each index i, initialize dp[i] as arr[i].
    • Iterate from index 0 to i-1:
      • If arr[i] is greater than arr[j], update dp[i] as the maximum of dp[i] and dp[j] + arr[i]. This means that we can extend the increasing subsequence ending at index j by including the element at index i, if it is greater.
  4. Find the maximum value in the dp array and return it as the result.

The time complexity of this approach is O(N^2) and the space complexity is O(N).

Here is the implementation of the maxSumIS() function in Python:

def maxSumIS(N, arr):
    dp = [0] * N
    dp[0] = arr[0]
    
    for i in range(1, N):
        dp[i] = arr[i]
        for j in range(i):
            if arr[i] > arr[j]:
                dp[i] = max(dp[i], dp[j] + arr[i])
    
    return max(dp)

# Example usage
N = 5
arr = [1, 101, 2, 3, 100]
print(maxSumIS(N, arr))  # Output: 106

N = 4
arr = [4, 1, 2, 3]
print(maxSumIS(N, arr))  # Output: 6

Note: The code provided above is in Python.

This problem has been solved

Similar Questions

Given an array Arr of size N containing positive integers. Find the maximum sum of a any possible subsequence such that no two numbers in the subsequence should be adjacent in Arr.

Given an integer array arr, write a program to find the subarray which has the largest sum and return its sum.Sample Input:9-2 -1 -3 4 -1 2 1 -5 4Sample Output:6Explanation:The subarray is [4, -1, 2, 1] and the sum is 6

Given an integer array nums, find the subarray with the largest sum, and return its sum.

Given an integer array arr, partition the array into (contiguous) subarrays of length at most k. After partitioning, each subarray has their values changed to become the maximum value of that subarray.Return the largest sum of the given array after partitioning. Test cases are generated so that the answer fits in a 32-bit integer. Example 1:Input: arr = [1,15,7,9,2,5,10], k = 3Output: 84Explanation: arr becomes [15,15,15,9,10,10,10]

You are given an array nums consisting of integers. You are also given a 2D array queries, where queries[i] = [posi, xi].For query i, we first set nums[posi] equal to xi, then we calculate the answer to query i which is the maximum sum of a subsequence of nums where no two adjacent elements are selected.Return the sum of the answers to all queries.Since the final answer may be very large, return it modulo 109 + 7.A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements. Example 1:Input: nums = [3,5,9], queries = [[1,-2],[0,-3]]Output: 21Explanation:After the 1st query, nums = [3,-2,9] and the maximum sum of a subsequence with non-adjacent elements is 3 + 9 = 12.After the 2nd query, nums = [-3,-2,9] and the maximum sum of a subsequence with non-adjacent elements is 9.Example 2:Input: nums = [0,-1], queries = [[0,-5]]Output: 0Explanation:After the 1st query, nums = [-5,-1] and the maximum sum of a subsequence with non-adjacent elements is 0 (choosing an empty subsequence). Constraints:1 <= nums.length <= 5 * 104-105 <= nums[i] <= 1051 <= queries.length <= 5 * 104queries[i] == [posi, xi]0 <= posi <= nums.length - 1-105 <= xi <= 105

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.