Maximum Sum of Subsequence With Non-adjacent Elements - Problem

You are given an array nums consisting of integers. You are also given a 2D array queries, where queries[i] = [pos_i, x_i].

For query i, we first set nums[pos_i] equal to x_i, 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 10^9 + 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.

Input & Output

Example 1 — Basic Case
$ Input: nums = [1,2,3,1], queries = [[1,10],[3,5]]
Output: 26
💡 Note: Query [1,10]: nums=[1,10,3,1]. DP: dp[0]=1, dp[1]=10, dp[2]=max(10,1+3)=10, dp[3]=max(10,10+1)=11. Query [3,5]: nums=[1,10,3,5]. DP: dp[0]=1, dp[1]=10, dp[2]=10, dp[3]=max(10,10+5)=15. Total=11+15=26
Example 2 — Single Element
$ Input: nums = [5], queries = [[0,2],[0,7]]
Output: 9
💡 Note: After query [0,2]: nums = [2], max sum = 2. After query [0,7]: nums = [7], max sum = 7. Total = 2 + 7 = 9.
Example 3 — All Negative
$ Input: nums = [-1,-2], queries = [[0,1],[1,3]]
Output: 4
💡 Note: After query [0,1]: nums = [1,-2], max sum = 1 (take first element). After query [1,3]: nums = [1,3], max sum = 3 (take second element, as taking both would violate adjacency). Actually max sum = max(1,3) = 3. Wait, let me recalculate. For [1,3]: we can't take both since they're adjacent. So max sum = max(1,3) = 3. Total = 1 + 3 = 4.

Constraints

  • 1 ≤ nums.length ≤ 5 × 104
  • 1 ≤ queries.length ≤ 5 × 104
  • -105 ≤ nums[i] ≤ 105
  • 0 ≤ posi < nums.length
  • -105 ≤ xi ≤ 105

Visualization

Tap to expand
Maximum Sum of Subsequence (Non-adjacent) INPUT nums array: 1 [0] 2 [1] 3 [2] 1 [3] queries: Query 1: [1, 10] Query 2: [3, 5] Query 1: set nums[1]=10 Query 2: set nums[3]=5 Find max sum with non-adjacent elements after each query ALGORITHM STEPS (DP) 1 Process Query 1 nums=[1,10,3,1] 2 DP for Query 1 dp[i]=max(dp[i-1],dp[i-2]+n[i]) i: 0 1 2 3 n: 1 10 3 1 dp: 1 10 10 11 3 Process Query 2 nums=[1,10,3,5] 4 DP for Query 2 Apply same DP formula i: 0 1 2 3 n: 1 10 3 5 dp: 1 10 10 15 Q1=11, Q2=8 (15-7 adj fix) Best: pick 10+5=15 OR 1+3+5 Q2 max = 8 (select 3,5) FINAL RESULT After Query 1: 1 10 3 1 Max sum = 10 + 1 = 11 After Query 2: 1 10 3 5 Max sum = 1 + 3 + 5 = 8 (or 10 alone, but 1+3+5 better) Total Answer: 11 + 8 = 19 Output: 19 -- OK Key Insight: The classic "House Robber" DP pattern: dp[i] = max(dp[i-1], dp[i-2] + nums[i]) For each query, update the array first, then run DP to find max non-adjacent sum. Optimization: Use Segment Tree for O(log n) updates instead of O(n) per query. TutorialsPoint - Maximum Sum of Subsequence With Non-adjacent Elements | DP Approach
Asked in
Google 12 Amazon 8 Microsoft 6 Meta 4
23.4K Views
Medium Frequency
~35 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen