Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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:
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.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.x = 3, the capped array is [3, 3, 2, 3]. A subsequence [2, 3] sums to 5, so it is possible.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 <= 40001 <= nums[i] <= n1 <= k <= 4000Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Two Pointers · Dynamic Programming
[4,3,2,4] 5
[1,2,3,4,5] 3
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3685: Subsequence Sum After Capping Elements
// Auto-generated Java example from go.
class Solution {
public void exampleSolution() {
}
}
// Reference (go):
// // Accepted solution for LeetCode #3685: Subsequence Sum After Capping Elements
// package main
//
// import (
// "math/big"
// "slices"
// )
//
// // https://space.bilibili.com/206214
// func subsequenceSumAfterCapping1(nums []int, k int) []bool {
// slices.Sort(nums)
//
// n := len(nums)
// ans := make([]bool, n)
// f := make([]bool, k+1)
// f[0] = true // 不选元素,和为 0
//
// i := 0
// for x := 1; x <= n; x++ {
// // 增量地考虑所有等于 x 的数
// // 小于 x 的数在之前的循环中已计算完毕,无需重复计算
// for i < n && nums[i] == x {
// for j := k; j >= nums[i]; j-- {
// f[j] = f[j] || f[j-nums[i]] // 0-1 背包:不选 or 选
// }
// i++
// }
//
// // 枚举(从大于 x 的数中)选了 j 个 x
// for j := range min(n-i, k/x) + 1 {
// if f[k-j*x] {
// ans[x-1] = true
// break
// }
// }
// }
// return ans
// }
//
// func subsequenceSumAfterCapping(nums []int, k int) []bool {
// slices.Sort(nums)
//
// n := len(nums)
// ans := make([]bool, n)
// f := big.NewInt(1)
// u := new(big.Int).Lsh(big.NewInt(1), uint(k+1))
// u.Sub(u, big.NewInt(1))
//
// i := 0
// for x := 1; x <= n; x++ {
// // 增量地考虑所有等于 x 的数
// for i < n && nums[i] == x {
// shifted := new(big.Int).Lsh(f, uint(nums[i]))
// f.Or(f, shifted).And(f, u) // And(f, u) 保证 f 的二进制长度 <= k+1
// i++
// }
//
// // 枚举(从大于 x 的数中)选了 j 个 x
// for j := range min(n-i, k/x) + 1 {
// if f.Bit(k-j*x) > 0 {
// ans[x-1] = true
// break
// }
// }
// }
// return ans
// }
// Accepted solution for LeetCode #3685: Subsequence Sum After Capping Elements
package main
import (
"math/big"
"slices"
)
// https://space.bilibili.com/206214
func subsequenceSumAfterCapping1(nums []int, k int) []bool {
slices.Sort(nums)
n := len(nums)
ans := make([]bool, n)
f := make([]bool, k+1)
f[0] = true // 不选元素,和为 0
i := 0
for x := 1; x <= n; x++ {
// 增量地考虑所有等于 x 的数
// 小于 x 的数在之前的循环中已计算完毕,无需重复计算
for i < n && nums[i] == x {
for j := k; j >= nums[i]; j-- {
f[j] = f[j] || f[j-nums[i]] // 0-1 背包:不选 or 选
}
i++
}
// 枚举(从大于 x 的数中)选了 j 个 x
for j := range min(n-i, k/x) + 1 {
if f[k-j*x] {
ans[x-1] = true
break
}
}
}
return ans
}
func subsequenceSumAfterCapping(nums []int, k int) []bool {
slices.Sort(nums)
n := len(nums)
ans := make([]bool, n)
f := big.NewInt(1)
u := new(big.Int).Lsh(big.NewInt(1), uint(k+1))
u.Sub(u, big.NewInt(1))
i := 0
for x := 1; x <= n; x++ {
// 增量地考虑所有等于 x 的数
for i < n && nums[i] == x {
shifted := new(big.Int).Lsh(f, uint(nums[i]))
f.Or(f, shifted).And(f, u) // And(f, u) 保证 f 的二进制长度 <= k+1
i++
}
// 枚举(从大于 x 的数中)选了 j 个 x
for j := range min(n-i, k/x) + 1 {
if f.Bit(k-j*x) > 0 {
ans[x-1] = true
break
}
}
}
return ans
}
# Accepted solution for LeetCode #3685: Subsequence Sum After Capping Elements
# Time: O(nlogn + n * k + klogn) = O(nlogn + n * k)
# Space: O(k)
# sort, dp, bitmasks
class Solution(object):
def subsequenceSumAfterCapping(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[bool]
"""
result = [False]*len(nums)
nums.sort()
mask = (1<<(k+1))-1
dp = 1
i = 0
for x in xrange(1, len(nums)+1):
while i < len(nums) and nums[i] < x:
dp |= (dp<<nums[i])&mask
i += 1
for j in xrange(max(k%x, k-(len(nums)-i)*x), k+1, x):
if dp&(1<<j):
result[x-1] = True
break
return result
# Time: O(nlogn + n * k + klogn) = O(nlogn + n * k)
# Space: O(k)
# sort, dp
class Solution2(object):
def subsequenceSumAfterCapping(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[bool]
"""
result = [False]*len(nums)
nums.sort()
dp = [False]*(k+1)
dp[0] = True
i = 0
for x in xrange(1, len(nums)+1):
while i < len(nums) and nums[i] < x:
for j in reversed(xrange(nums[i], k+1)):
dp[j] = dp[j] or dp[j-nums[i]]
i += 1
for j in xrange(max(k%x, k-(len(nums)-i)*x), k+1, x):
if dp[j]:
result[x-1] = True
break
return result
// Accepted solution for LeetCode #3685: Subsequence Sum After Capping Elements
// Rust example auto-generated from go reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (go):
// // Accepted solution for LeetCode #3685: Subsequence Sum After Capping Elements
// package main
//
// import (
// "math/big"
// "slices"
// )
//
// // https://space.bilibili.com/206214
// func subsequenceSumAfterCapping1(nums []int, k int) []bool {
// slices.Sort(nums)
//
// n := len(nums)
// ans := make([]bool, n)
// f := make([]bool, k+1)
// f[0] = true // 不选元素,和为 0
//
// i := 0
// for x := 1; x <= n; x++ {
// // 增量地考虑所有等于 x 的数
// // 小于 x 的数在之前的循环中已计算完毕,无需重复计算
// for i < n && nums[i] == x {
// for j := k; j >= nums[i]; j-- {
// f[j] = f[j] || f[j-nums[i]] // 0-1 背包:不选 or 选
// }
// i++
// }
//
// // 枚举(从大于 x 的数中)选了 j 个 x
// for j := range min(n-i, k/x) + 1 {
// if f[k-j*x] {
// ans[x-1] = true
// break
// }
// }
// }
// return ans
// }
//
// func subsequenceSumAfterCapping(nums []int, k int) []bool {
// slices.Sort(nums)
//
// n := len(nums)
// ans := make([]bool, n)
// f := big.NewInt(1)
// u := new(big.Int).Lsh(big.NewInt(1), uint(k+1))
// u.Sub(u, big.NewInt(1))
//
// i := 0
// for x := 1; x <= n; x++ {
// // 增量地考虑所有等于 x 的数
// for i < n && nums[i] == x {
// shifted := new(big.Int).Lsh(f, uint(nums[i]))
// f.Or(f, shifted).And(f, u) // And(f, u) 保证 f 的二进制长度 <= k+1
// i++
// }
//
// // 枚举(从大于 x 的数中)选了 j 个 x
// for j := range min(n-i, k/x) + 1 {
// if f.Bit(k-j*x) > 0 {
// ans[x-1] = true
// break
// }
// }
// }
// return ans
// }
// Accepted solution for LeetCode #3685: Subsequence Sum After Capping Elements
// Auto-generated TypeScript example from go.
function exampleSolution(): void {
}
// Reference (go):
// // Accepted solution for LeetCode #3685: Subsequence Sum After Capping Elements
// package main
//
// import (
// "math/big"
// "slices"
// )
//
// // https://space.bilibili.com/206214
// func subsequenceSumAfterCapping1(nums []int, k int) []bool {
// slices.Sort(nums)
//
// n := len(nums)
// ans := make([]bool, n)
// f := make([]bool, k+1)
// f[0] = true // 不选元素,和为 0
//
// i := 0
// for x := 1; x <= n; x++ {
// // 增量地考虑所有等于 x 的数
// // 小于 x 的数在之前的循环中已计算完毕,无需重复计算
// for i < n && nums[i] == x {
// for j := k; j >= nums[i]; j-- {
// f[j] = f[j] || f[j-nums[i]] // 0-1 背包:不选 or 选
// }
// i++
// }
//
// // 枚举(从大于 x 的数中)选了 j 个 x
// for j := range min(n-i, k/x) + 1 {
// if f[k-j*x] {
// ans[x-1] = true
// break
// }
// }
// }
// return ans
// }
//
// func subsequenceSumAfterCapping(nums []int, k int) []bool {
// slices.Sort(nums)
//
// n := len(nums)
// ans := make([]bool, n)
// f := big.NewInt(1)
// u := new(big.Int).Lsh(big.NewInt(1), uint(k+1))
// u.Sub(u, big.NewInt(1))
//
// i := 0
// for x := 1; x <= n; x++ {
// // 增量地考虑所有等于 x 的数
// for i < n && nums[i] == x {
// shifted := new(big.Int).Lsh(f, uint(nums[i]))
// f.Or(f, shifted).And(f, u) // And(f, u) 保证 f 的二进制长度 <= k+1
// i++
// }
//
// // 枚举(从大于 x 的数中)选了 j 个 x
// for j := range min(n-i, k/x) + 1 {
// if f.Bit(k-j*x) > 0 {
// ans[x-1] = true
// break
// }
// }
// }
// return ans
// }
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.