Skip to main content

Subsequence Sum After Capping Elements - Solution & Explanation

MediumArrayTwo PointersDynamic ProgrammingSorting4 min readAsked at: Google
Practice this problem

Problem Statement

You are given an integer array nums of size n and a positive integer k.

An array capped by value x is obtained by replacing every element nums[i] with min(nums[i], x).

For each integer x from 1 to n, determine whether it is possible to choose a subsequence from the array capped by x such that the sum of the chosen elements is exactly k.

Return a 0-indexed boolean array answer of size n, where answer[i] is true if it is possible when using x = i + 1, and false otherwise.

 

Example 1:

Input: nums = [4,3,2,4], k = 5

Output: [false,false,true,true]

Explanation:

  • For x = 1, the capped array is [1, 1, 1, 1]. Possible sums are 1, 2, 3, 4, so it is impossible to form a sum of 5.
  • For x = 2, the capped array is [2, 2, 2, 2]. Possible sums are 2, 4, 6, 8, so it is impossible to form a sum of 5.
  • For x = 3, the capped array is [3, 3, 2, 3]. A subsequence [2, 3] sums to 5, so it is possible.
  • For x = 4, the capped array is [4, 3, 2, 4]. A subsequence [3, 2] sums to 5, so it is possible.

Example 2:

Input: nums = [1,2,3,4,5], k = 3

Output: [true,true,true,true,true]

Explanation:

For every value of x, it is always possible to select a subsequence from the capped array that sums exactly to 3.

 

Constraints:

  • 1 <= n == nums.length <= 4000
  • 1 <= nums[i] <= n
  • 1 <= k <= 4000

Approach Overview

Problem Overview: You are given an array where elements larger than a specified cap are reduced to that cap. After applying this transformation, the task is to evaluate subsequences and compute valid sums based on the capped values. The challenge comes from handling many subsequences efficiently without enumerating all possibilities.

Approach 1: Brute Force Subsequence Enumeration (O(2^n) time, O(n) space)

The most direct strategy generates every possible subsequence using recursion or bitmasking. For each subsequence, apply the capping rule (value = min(value, cap)) and compute the resulting sum. While simple, this requires checking all 2^n subsequences, which quickly becomes infeasible for moderate input sizes. This approach is useful only for validating small test cases or understanding the effect of the capping operation on subsequence sums.

Approach 2: Dynamic Programming on Subsequence Sums (O(n * S) time, O(S) space)

After transforming each element to its capped value, the problem becomes a classic subsequence-sum style dynamic programming task. Maintain a DP structure where dp[s] represents whether or how many subsequences can produce sum s. Iterate through the array and update states in reverse to avoid reusing elements in the same step. This technique leverages patterns from dynamic programming and avoids explicit subsequence generation. It works well when the maximum possible sum S is reasonably bounded.

Approach 3: Sorting + Two Pointers Optimization (O(n log n) time, O(n) space)

An efficient solution sorts the capped values first, enabling structured exploration of valid subsequences. Sorting helps group identical capped values and allows pointer-based scans when evaluating combinations that satisfy sum constraints. Using prefix sums and a two pointers sweep, you can efficiently determine how many elements can participate in subsequences without exceeding target conditions. The preprocessing step uses sorting, after which pointer movement avoids nested enumeration. This reduces the effective complexity while keeping memory usage linear.

Recommended for interviews: Interviewers expect you to move beyond brute force quickly. Demonstrating the exponential enumeration first shows understanding of the subsequence space. The optimized approach—sorting combined with two pointers or DP—shows algorithmic maturity by exploiting order and cumulative sums. Most production solutions use the optimized method because it scales to large arrays while maintaining predictable complexity.

Solutions for this problem are being prepared.

Try solving it yourself

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Subsequence EnumerationO(2^n)O(n)Small arrays or conceptual understanding of subsequences
Dynamic Programming on SumsO(n * S)O(S)When the maximum possible sum is limited and DP states remain manageable
Sorting + Two Pointers OptimizationO(n log n)O(n)General case with large arrays where enumeration is infeasible

Video Solution

Subsequence Sum After Capping Elements | LeetCode 3685 | Weekly Contest 467Sanyam IIT Guwahati2,344 views views

Watch 8 more video solutions →

Frequently Asked Questions

Is Subsequence Sum After Capping Elements easy or hard?
The problem is rated Medium because the individual ideas—capping values, subsequences, sorting, and DP—are straightforward, but combining them efficiently requires recognizing patterns that avoid exponential enumeration.
Subsequence Sum After Capping Elements Python/Java solution
Most Python or Java solutions first transform the array with value = min(value, cap), then sort the array and apply a DP or two pointer technique to compute valid subsequence sums. The overall complexity is typically O(n log n) time with linear auxiliary space.
How to solve Subsequence Sum After Capping Elements in O(n)?
Pure O(n) solutions are possible after preprocessing if the array is already sorted. Once values are capped and ordered, prefix sums and a two pointer sweep can evaluate valid subsequences in a single linear pass.
What is the best approach for Subsequence Sum After Capping Elements?
The most practical solution sorts the capped array and applies a two pointers or prefix-sum based strategy to evaluate subsequence combinations efficiently. This avoids enumerating all subsequences and typically runs in O(n log n) time due to the sorting step with O(n) extra space.
Is Subsequence Sum After Capping Elements asked at Google/Amazon/Meta?
Problems combining subsequences, capping constraints, and optimized scanning patterns are common in interviews at companies like Google, Amazon, and Meta. They test familiarity with dynamic programming, sorting strategies, and pointer-based optimizations.
What data structure is used in Subsequence Sum After Capping Elements?
Common implementations rely on arrays with prefix sums, dynamic programming tables for subset sums, and sorting utilities. Two pointers are often used after sorting to efficiently evaluate subsequence combinations.
What is the time complexity of Subsequence Sum After Capping Elements?
The optimal implementation usually runs in O(n log n) time because the array is sorted before applying a linear scan or two pointer technique. Brute force enumeration takes O(2^n) time, which is impractical for large inputs.

Ready to solve this problem?

Practice Subsequence Sum After Capping Elements with our built-in code editor and test cases.

Practice on FleetCode