Skip to main content
LeetCode 1508, Medium. Topics: Array, Two Pointers, Binary Search, Sorting, Prefix Sum. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 22 parametrized pytest cases, and a playground notebook:

Problem

<p>You are given the array <code>nums</code> consisting of <code>n</code> positive integers. You computed the sum of all non-empty continuous subarrays from the array and then sorted them in non-decreasing order, creating a new array of <code>n * (n + 1) / 2</code> numbers.</p> <p><em>Return the sum of the numbers from index </em><code>left</code><em> to index </em><code>right</code> (<strong>indexed from 1</strong>)<em>, inclusive, in the new array. </em>Since the answer can be a huge number return it modulo <code>10<sup>9</sup> + 7</code>.</p>

Examples

Constraints

<ul> <li><code>n == nums.length</code></li> <li><code>1 <= nums.length <= 1000</code></li> <li><code>1 <= nums[i] <= 100</code></li> <li><code>1 <= left <= right <= n * (n + 1) / 2</code></li> </ul>

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026