An energy company monitors a long pipeline using n flow sensors mounted one after another, from the source end of the pipeline out to the far end. Each sensor reports a signed integer reading (flow can briefly reverse, giving a negative value). To understand how much net fluid has moved through the pipeline up to any given point, the engineers want, for every sensor position, the running total of every reading from the source-side sensor up through that sensor.
n, the number of sensors.n space-separated integers measurements[0], measurements[1], ..., measurements[n-1], the reading at each sensor in order from the source end.Print n space-separated integers ans[0], ans[1], ..., ans[n-1], where ans[i] equals the sum of measurements[0] through measurements[i] inclusive.
Example 1
Input
5 1 2 3 4 5
Expected
1 3 6 10 15
Explanation
The cumulative sums are 1, 1+2=3, 1+2+3=6, 1+2+3+4=10, and 1+2+3+4+5=15, so the output is "1 3 6 10 15".
Example 2
Input
4 1 1 1 1
Expected
1 2 3 4
Explanation
Each cumulative sum grows by 1 from the previous one: 1, 2, 3, 4, so the output is "1 2 3 4".
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →