13. Maximum Sum of Elements Not Part of LIS
β GFG solution to the Maximum Sum of Elements Not Part of LIS problem: find maximum sum of elements not in the Longest Increasing Subsequence using binary search optimization. π
The problem can be found at the following link: π Question Link
π§© Problem Description
Given an array arr[]
of positive integers, your task is to find the maximum possible sum of all elements that are not part of the Longest Increasing Subsequence (LIS).
The approach is to find the LIS with minimum sum among all possible LIS of maximum length, then subtract this from the total sum of the array.
π Examples
Example 1
Input: arr[] = [4, 6, 1, 2, 3, 8]
Output: 10
Explanation: The LIS could be [1, 2, 3, 8] with sum 14.
The elements not in LIS are [4, 6] with sum 10.
Total sum = 24, LIS sum = 14, so answer = 24 - 14 = 10.
Example 2
Input: arr[] = [5, 4, 3, 2, 1]
Output: 14
Explanation: The LIS is [1] (or any single element) with sum 1.
The elements not in LIS are [5, 4, 3, 2] with sum 14.
Total sum = 15, LIS sum = 1, so answer = 15 - 1 = 14.
π Constraints
$1 \le \text{arr.size()} \le 10^3$
$1 \le \text{arr}[i] \le 10^5$
β
My Approach
The optimal approach uses Binary Search to efficiently construct the LIS while maintaining cumulative sums:
Binary Search + Cumulative Sum Tracking
Initialize Data Structures:
dp
vector to store the LIS using binary search techniquesums
vector to track cumulative sums corresponding to each LIS lengthCalculate total sum of the array
Process Each Element:
For each element
x
in the array, find its position in the current LIS usinglower_bound
This gives us the position where
x
should be placed to maintain the increasing property
Update LIS and Sums:
If
x
extends the LIS (position equals current LIS length), append it to bothdp
andsums
Otherwise, replace the element at the found position and update the corresponding cumulative sum
Calculate Result:
The answer is
total_sum - minimum_LIS_sum
The minimum LIS sum is stored in
sums.back()
after processing all elements
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n log n), where n is the size of the array. Each element requires a binary search operation which takes O(log n) time, and we process n elements.
Expected Auxiliary Space Complexity: O(n), where n is the size of the array. We use additional space for the
dp
andsums
vectors, each of which can grow up to size n in the worst case.
π§βπ» Code (C++)
class Solution {
public:
int nonLisMaxSum(vector<int>& arr) {
vector<int> dp, sums;
int total = accumulate(arr.begin(), arr.end(), 0);
for (int x : arr) {
auto it = lower_bound(dp.begin(), dp.end(), x);
int idx = it - dp.begin();
if (it == dp.end()) {
dp.push_back(x);
sums.push_back((sums.empty() ? 0 : sums.back()) + x);
} else {
*it = x;
sums[idx] = (idx ? sums[idx - 1] : 0) + x;
}
}
return total - sums.back();
}
};
π§βπ» Code (Java)
class Solution {
public int nonLisMaxSum(int[] arr) {
ArrayList<Integer> dp = new ArrayList<>();
ArrayList<Integer> sums = new ArrayList<>();
int total = Arrays.stream(arr).sum();
for (int x : arr) {
int idx = Collections.binarySearch(dp, x);
if (idx < 0) idx = -idx - 1;
if (idx == dp.size()) {
dp.add(x);
sums.add((sums.isEmpty() ? 0 : sums.get(sums.size() - 1)) + x);
} else {
dp.set(idx, x);
sums.set(idx, (idx > 0 ? sums.get(idx - 1) : 0) + x);
}
}
return total - sums.get(sums.size() - 1);
}
}
π Code (Python)
import bisect
class Solution:
def nonLisMaxSum(self, arr):
dp, sums = [], []
total = sum(arr)
for x in arr:
i = bisect.bisect_left(dp, x)
if i == len(dp):
dp.append(x)
sums.append((sums[-1] if sums else 0) + x)
else:
dp[i] = x
sums[i] = (sums[i-1] if i else 0) + x
return total - sums[-1]
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated