nums holds whole numbers. Add up k consecutive numbers, meaning numbers that sit next to each other in the array. Return the largest such sum.
kis a whole number from 1 up to the length ofnums, so at least one group ofkalways exists.- Numbers can be negative, so the answer can be negative too.
numshas up to 100,000 elements, andkcan be large. So aim for one pass over the array.
Input: nums = [3, 1, 4, 1, 5, 2], k = 3
Output: 10The groups sum to 8, 6, 10 and 8. The best is 4 + 1 + 5.
Input: nums = [-5, -2, -8], k = 2
Output: -7
Input: nums = [6], k = 1
Output: 6