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] <= 105When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The width of any subsequence is determined solely by its maximum and minimum elements, so the original order of elements does not matter. After sorting the array, each element acts as the maximum in a fixed number of subsequences and as the minimum in another fixed number of subsequences. By precomputing powers of two, we can calculate each element's total contribution to the answer in linear time.
Here's how the algorithm would work step-by-step:
def sum_subseq_widths(nums):
# Sort the array so that every element's rank is known
nums.sort()
modulo = 10**9 + 7
total_width = 0
length = len(nums)
# Precompute powers of two modulo 10^9 + 7
powers_of_two = [1] * length
for index in range(1, length):
powers_of_two[index] = (powers_of_two[index - 1] * 2) % modulo
# Accumulate contributions of each element as maximum and minimum
for index in range(length):
max_contributions = powers_of_two[index]
min_contributions = powers_of_two[length - 1 - index]
element_contribution = (max_contributions - min_contributions) * nums[index]
total_width = (total_width + element_contribution) % modulo
return total_width
The width of any subsequence depends solely on its maximum and minimum values, which are invariant to the order of elements. By sorting the array, each element's contribution as a maximum and as a minimum can be calculated based on the number of elements smaller and larger than it. We can then compute the sum of all widths in a single linear pass using precomputed or on-the-fly powers of two.
Here's how the algorithm would work step-by-step:
def sum_subseq_widths(nums: list[int]) -> int:
# Sort the array to easily determine min and max contributions.
nums.sort()
modulo = 1_000_000_007
total_width = 0
current_power = 1
length = len(nums)
# Accumulate contributions where each element acts as a maximum and minimum.
for index in range(length):
total_width = (total_width + nums[index] * current_power - nums[length - 1 - index] * current_power) % modulo
# Update powers of two for the next pair of elements.
current_power = (current_power * 2) % modulo
# Return the final aggregated width modulo 10^9 + 7.
return (total_width + modulo) % modulo
| Case | How to Handle |
|---|---|
| Array with a single element | Return zero because a subsequence of size one has a width of zero since the maximum and minimum are the same. |
| Array with only two elements | Compute the absolute difference between the two sorted elements and return it as the only subsequence width. |
| Array with all identical elements | Return zero because every subsequence will have equal maximum and minimum values, resulting in a width of zero. |
| Array containing negative numbers and mixed signs | Sort the array in ascending order so that relative magnitudes are correctly established for width calculations regardless of sign. |
| Maximum constraint array size up to 100,000 elements | Use an O(N log N) sorting approach with precomputed powers of two to ensure the solution executes well within time limits. |
| Result exceeding standard integer limits and requiring modulo arithmetic | Apply modulo 10^9 + 7 at each addition and multiplication step using 64-bit integers to prevent arithmetic overflow. |
| Array with duplicate values | Sorting groups duplicates adjacent to each other, and the mathematical positional contribution formula correctly accounts for identical values. |