Ctrl K

Self-Sized Lookback Sums

Easy
You are given an array values. Each element values[i] marks out its own window: it starts values[i] positions to the left (but never before the start of the array, so at index max(0, i - values[i])) and ends at element i itself, inclusive. Add up each of these windows and return the grand total.
Example 1
values=[1, 4, 2] → 13

Index 0: window [1] = 1. Index 1: the value 4 reaches past the start, so the window is [1, 4] = 5. Index 2: two steps back, window [1, 4, 2] = 7. Total 13.

Example 2
values=[2, 1, 1, 3] → 14

The windows are [2] = 2, [2, 1] = 3, [1, 1] = 2 and [2, 1, 1, 3] = 7, for 14 in total.

Constraints
  • 1 ≤ len(values) ≤ 100
  • 1 ≤ values[i] ≤ 1000
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBalance Point Index EasyThe Battery Must Not Run Dry EasyBalance After Each Day EasyTotals of Odd-Length Slices EasyThe Hiker's Highest Point Easy
loading Python…
Input
values=[1, 4, 2]
Expected
13

Run checks the first 3 tests in your browser. Submit checks all 92 tests on the server, hidden ones included.