#891
Sum of Subsequence Widths
master · 1715 · lc hard +32 · verified · 40.3% accepted · 736 likes · top 20%
Description
Define the width of a sequence as the difference between its largest and smallest element. Given an integer array nums, sum the widths of every non-empty subsequence and return the result modulo 109 + 7.
A subsequence preserves the original element order after some deletions (possibly none).
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
Code
1
2
3