Program to find sum of widths of all subsequences of list of numbers in Python

Given a list of numbers, we need to find the sum of widths of all subsequences. The width of a subsequence is the difference between its maximum and minimum elements. We'll calculate this sum modulo 10^9 + 7 to handle large results.

So, if the input is like nums = [7, 4, 9], then the output will be 15. The subsequences are: [7], [4], [9], [7, 4], [7, 9], [4, 9], [7, 4, 9] with widths 0, 0, 0, 3, 2, 5, 5 respectively, giving us a sum of 15.

Algorithm

To solve this efficiently, we follow these steps ?

  • Sort the array to make calculations easier

  • For each element, calculate how many times it appears as maximum minus how many times it appears as minimum in all subsequences

  • Use powers of 2 to count subsequence combinations

  • Apply modulo operation to prevent overflow

Example

Let's implement the solution step by step ?

class Solution:
    def solve(self, nums):
        m = 10**9 + 7
        nums.sort()
        ans = 0
        n = len(nums)
        
        # Precompute powers of 2
        power = [1] * (n + 1)
        for i in range(1, n + 1):
            power[i] = power[i - 1] * 2 % m
        
        # For each element, calculate its contribution
        for i in range(n):
            # Times element appears as maximum
            positive = (power[i] - 1) * nums[i]
            # Times element appears as minimum  
            negative = (power[n - i - 1] - 1) * nums[i]
            # Add the difference to answer
            ans = (ans + positive - negative) % m
        
        return ans

# Test with example
ob = Solution()
nums = [7, 4, 9]
result = ob.solve(nums)
print(f"Sum of widths: {result}")
Sum of widths: 15

How It Works

After sorting the array, for each element at position i:

  • As maximum: It can be the maximum in 2^i different subsequences (all subsets of elements to its left, plus itself)

  • As minimum: It can be the minimum in 2^(n-i-1) different subsequences (all subsets of elements to its right, plus itself)

The contribution of each element is: (times_as_max - times_as_min) × element_value

Verification with Manual Calculation

For nums = [7, 4, 9], after sorting we get [4, 7, 9] ?

# Manual verification
nums = [7, 4, 9]
print("Original:", nums)

# All subsequences and their widths
subsequences = [
    ([7], 0), ([4], 0), ([9], 0),
    ([7, 4], 3), ([7, 9], 2), ([4, 9], 5),
    ([7, 4, 9], 5)
]

total_width = 0
print("\nSubsequence -> Width:")
for subseq, width in subsequences:
    print(f"{subseq} -> {width}")
    total_width += width

print(f"\nTotal sum of widths: {total_width}")
Original: [7, 4, 9]

Subsequence -> Width:
[7] -> 0
[4] -> 0
[9] -> 0
[7, 4] -> 3
[7, 9] -> 2
[4, 9] -> 5
[7, 4, 9] -> 5

Total sum of widths: 15

Conclusion

This algorithm efficiently calculates the sum of widths by sorting the array and using combinatorics with powers of 2. The time complexity is O(n log n) due to sorting, making it optimal for large inputs.

Updated on: 2026-03-25T13:53:48+05:30

220 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements