2Brute2Best
🔥0

Max Sum Increasing Subsequence

Medium·GeeksforGeeks Pro OnlySlight Variation
GeeksforGeeks
01

Problem Statement

You are given an array arr of positive integers.

Pick a subsequence whose values are strictly increasing, and add up its values. Return the largest sum that any such subsequence can give. The answer is a sum, not a count of items.

The function is int maxSumIS(vector<int>& arr).

Input

An array of positive integers arr.

Output

One number: the largest sum of a strictly increasing subsequence.

Example 1
Input: arr = [1, 101, 2, 3, 100]
Output: 106

The chain 1, 2, 3, 100 sums to 106. The chain 1, 101 sums to 102, which is less.

Example 2
Input: arr = [4, 1, 2, 3]
Output: 6

The chain 1, 2, 3 sums to 6. The single 4 gives only 4.

Example 3
Input: arr = [4, 1, 2, 4]
Output: 7

The chain 1, 2, 4 sums to 7. Two 4s cannot be in a strictly increasing chain.

Constraints
  • • 1 ≤ arr.size() ≤ 10^3
  • • 1 ≤ arr[i] ≤ 10^5
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“subsequence”
→ Items can be skipped, so the chain is any set of positions in order.
“strictly increasing”
→ Each chosen item must be bigger than the one before it in the chain. Equal values are not allowed.
“largest sum”
→ Each item adds its value. A longer chain is not always better, since a short chain can have a bigger sum.
03

Approach Complexity Ladder

Brute Force — take or skip each itemO(2^N)O(N)

Recurse over the items: skip one, or take it if it is bigger than the last one taken, adding its value

Optimal — msis[i]: the best sum of a chain that ends at arr[i]O(N^2)O(N)

msis[i] = arr[i] + the best msis[j] over earlier smaller j; the answer is the largest msis

Brute Force Intuition

Walk through the items from left to right. For each one, either skip it, or take it when it is bigger than the last value taken, and add its value to the sum. The answer is the best sum over all the paths.

Recognition rule for this sub-pattern:

O(n²): try every earlier j. O(n log n): binary search the tails array.
Remember this · DP — Longest Increasing Subsequence
Trigger
Longest chain where each item must be bigger/fit inside the previous one (numbers, pairs, envelopes, words).
Think
Does each element extend the best chain ending at some smaller earlier element?
Key question
Sort first? Then what does "increasing" mean for these items?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like DP — Longest Increasing Subsequence translate into production architectures and low-level memory mechanics.