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.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
You are given an integer array nums. A good subsequence is defined as a subsequence of nums where the absolute difference between any two consecutive elements in the subsequence is exactly 1.
Return the sum of all possible good subsequences of nums.
Since the answer may be very large, return it modulo 109 + 7.
Note that a subsequence of size 1 is considered good by definition.
Example 1:
Input: nums = [1,2,1]
Output: 14
Explanation:
[1], [2], [1], [1,2], [2,1], [1,2,1].Example 2:
Input: nums = [3,4,5]
Output: 40
Explanation:
[3], [4], [5], [3,4], [4,5], [3,4,5].Constraints:
1 <= nums.length <= 1050 <= nums[i] <= 105Problem summary: You are given an integer array nums. A good subsequence is defined as a subsequence of nums where the absolute difference between any two consecutive elements in the subsequence is exactly 1. Return the sum of all possible good subsequences of nums. Since the answer may be very large, return it modulo 109 + 7. Note that a subsequence of size 1 is considered good by definition.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Dynamic Programming
[1,2,1]
[3,4,5]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3351: Sum of Good Subsequences
class Solution {
public int sumOfGoodSubsequences(int[] nums) {
final int mod = (int) 1e9 + 7;
int mx = 0;
for (int x : nums) {
mx = Math.max(mx, x);
}
long[] f = new long[mx + 1];
long[] g = new long[mx + 1];
for (int x : nums) {
f[x] += x;
g[x] += 1;
if (x > 0) {
f[x] = (f[x] + f[x - 1] + g[x - 1] * x % mod) % mod;
g[x] = (g[x] + g[x - 1]) % mod;
}
if (x + 1 <= mx) {
f[x] = (f[x] + f[x + 1] + g[x + 1] * x % mod) % mod;
g[x] = (g[x] + g[x + 1]) % mod;
}
}
long ans = 0;
for (long x : f) {
ans = (ans + x) % mod;
}
return (int) ans;
}
}
// Accepted solution for LeetCode #3351: Sum of Good Subsequences
func sumOfGoodSubsequences(nums []int) (ans int) {
mod := int(1e9 + 7)
mx := slices.Max(nums)
f := make([]int, mx+1)
g := make([]int, mx+1)
for _, x := range nums {
f[x] += x
g[x] += 1
if x > 0 {
f[x] = (f[x] + f[x-1] + g[x-1]*x%mod) % mod
g[x] = (g[x] + g[x-1]) % mod
}
if x+1 <= mx {
f[x] = (f[x] + f[x+1] + g[x+1]*x%mod) % mod
g[x] = (g[x] + g[x+1]) % mod
}
}
for _, x := range f {
ans = (ans + x) % mod
}
return
}
# Accepted solution for LeetCode #3351: Sum of Good Subsequences
class Solution:
def sumOfGoodSubsequences(self, nums: List[int]) -> int:
mod = 10**9 + 7
f = defaultdict(int)
g = defaultdict(int)
for x in nums:
f[x] += x
g[x] += 1
f[x] += f[x - 1] + g[x - 1] * x
g[x] += g[x - 1]
f[x] += f[x + 1] + g[x + 1] * x
g[x] += g[x + 1]
return sum(f.values()) % mod
// Accepted solution for LeetCode #3351: Sum of Good Subsequences
// Rust example auto-generated from java 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 (java):
// // Accepted solution for LeetCode #3351: Sum of Good Subsequences
// class Solution {
// public int sumOfGoodSubsequences(int[] nums) {
// final int mod = (int) 1e9 + 7;
// int mx = 0;
// for (int x : nums) {
// mx = Math.max(mx, x);
// }
// long[] f = new long[mx + 1];
// long[] g = new long[mx + 1];
// for (int x : nums) {
// f[x] += x;
// g[x] += 1;
// if (x > 0) {
// f[x] = (f[x] + f[x - 1] + g[x - 1] * x % mod) % mod;
// g[x] = (g[x] + g[x - 1]) % mod;
// }
// if (x + 1 <= mx) {
// f[x] = (f[x] + f[x + 1] + g[x + 1] * x % mod) % mod;
// g[x] = (g[x] + g[x + 1]) % mod;
// }
// }
// long ans = 0;
// for (long x : f) {
// ans = (ans + x) % mod;
// }
// return (int) ans;
// }
// }
// Accepted solution for LeetCode #3351: Sum of Good Subsequences
function sumOfGoodSubsequences(nums: number[]): number {
const mod = 10 ** 9 + 7;
const mx = Math.max(...nums);
const f: number[] = Array(mx + 1).fill(0);
const g: number[] = Array(mx + 1).fill(0);
for (const x of nums) {
f[x] += x;
g[x] += 1;
if (x > 0) {
f[x] = (f[x] + f[x - 1] + ((g[x - 1] * x) % mod)) % mod;
g[x] = (g[x] + g[x - 1]) % mod;
}
if (x + 1 <= mx) {
f[x] = (f[x] + f[x + 1] + ((g[x + 1] * x) % mod)) % mod;
g[x] = (g[x] + g[x + 1]) % mod;
}
}
return f.reduce((acc, cur) => (acc + cur) % mod, 0);
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
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: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
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.