values and a number rank.
A *stretch* is a non-empty contiguous part of the array; an array of length n has exactly n·(n+1)/2 of them. Take the sum of each stretch, list all the sums in ascending order (equal sums listed separately) and return the one in position rank (counting from 1).
Listing every sum is O(n²); try to do better.values=[4, 1, 2], rank=3 → 3
The sums of all stretches are 4, 1, 2, 4 + 1 = 5, 1 + 2 = 3 and 4 + 1 + 2 = 7. Sorted: 1, 2, 3, 4, 5, 7, so the third is 3.
values=[2, 2, 2], rank=5 → 4
The sums are 2 three times, 4 twice and 6 once. Position five holds 4: equal sums count separately.
values=[4, 1, 2], rank=3
3
Run checks the first 3 tests in your browser. Submit checks all 69 tests on the server, hidden ones included.