Skip to main content
Admissions guidance, study banks, and career tools — get started on Shalyam

Sum of the Last K Elements

Numbers arrive one at a time. After each arrival print the sum of the most recent k numbers (or of all numbers so far if fewer than k have arrived). Use a ring buffer of size k.

Constraints

1 <= k <= n <= 10^5 |a[i]| <= 10^4

Discussion

No discussions yet.

Rewards · v2.1

Get Exclusive Insights with Every Issue

Join Shalyam Newsletter

Stay ahead in education, research, and innovation—straight to your inbox.