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.
The width of a sequence is the difference between the maximum and minimum elements in the sequence.
Given an array of integers nums, return the sum of the widths of all the non-empty subsequences of nums. Since the answer may be very large, return it modulo 109 + 7.
A subsequence is a sequence that can be derived from an array by deleting some or no elements without changing the order of the remaining elements. For example, [3,6,2,7] is a subsequence of the array [0,3,1,6,2,2,7].
Example 1:
Input: nums = [2,1,3] Output: 6 Explanation: The subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.
Example 2:
Input: nums = [2] Output: 0
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 105Problem summary: The width of a sequence is the difference between the maximum and minimum elements in the sequence. Given an array of integers nums, return the sum of the widths of all the non-empty subsequences of nums. Since the answer may be very large, return it modulo 109 + 7. A subsequence is a sequence that can be derived from an array by deleting some or no elements without changing the order of the remaining elements. For example, [3,6,2,7] is a subsequence of the array [0,3,1,6,2,2,7].
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
[2,1,3]
[2]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #891: Sum of Subsequence Widths
class Solution {
private static final int MOD = (int) 1e9 + 7;
public int sumSubseqWidths(int[] nums) {
Arrays.sort(nums);
long ans = 0, p = 1;
int n = nums.length;
for (int i = 0; i < n; ++i) {
ans = (ans + (nums[i] - nums[n - i - 1]) * p + MOD) % MOD;
p = (p << 1) % MOD;
}
return (int) ans;
}
}
// Accepted solution for LeetCode #891: Sum of Subsequence Widths
func sumSubseqWidths(nums []int) (ans int) {
const mod int = 1e9 + 7
sort.Ints(nums)
p, n := 1, len(nums)
for i, v := range nums {
ans = (ans + (v-nums[n-i-1])*p + mod) % mod
p = (p << 1) % mod
}
return
}
# Accepted solution for LeetCode #891: Sum of Subsequence Widths
class Solution:
def sumSubseqWidths(self, nums: List[int]) -> int:
mod = 10**9 + 7
nums.sort()
ans, p = 0, 1
for i, v in enumerate(nums):
ans = (ans + (v - nums[-i - 1]) * p) % mod
p = (p << 1) % mod
return ans
// Accepted solution for LeetCode #891: Sum of Subsequence Widths
struct Solution;
const MOD: i64 = 1_000_000_007;
impl Solution {
fn sum_subseq_widths(mut a: Vec<i32>) -> i32 {
let n = a.len();
a.sort_unstable();
let mut res = 0;
let mut c = 1;
for i in 0..n {
res += c * a[i] as i64;
res %= MOD;
c *= 2;
c %= MOD;
}
c = 1;
for i in (0..n).rev() {
res -= c * a[i] as i64;
res %= MOD;
c *= 2;
c %= MOD;
}
((res + MOD) % MOD) as i32
}
}
#[test]
fn test() {
let a = vec![2, 1, 3];
let res = 6;
assert_eq!(Solution::sum_subseq_widths(a), res);
}
// Accepted solution for LeetCode #891: Sum of Subsequence Widths
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #891: Sum of Subsequence Widths
// class Solution {
// private static final int MOD = (int) 1e9 + 7;
//
// public int sumSubseqWidths(int[] nums) {
// Arrays.sort(nums);
// long ans = 0, p = 1;
// int n = nums.length;
// for (int i = 0; i < n; ++i) {
// ans = (ans + (nums[i] - nums[n - i - 1]) * p + MOD) % MOD;
// p = (p << 1) % MOD;
// }
// return (int) ans;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
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: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.