CodePark/ problems / Largest Sum of k in a Row
START PRACTICINGPRACTICE
Coding problem · Data structures and algorithms › Sliding window

Largest Sum of k in a Row

easyabout 12 minutes in an interview

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.

  • k is a whole number from 1 up to the length of nums, so at least one group of k always exists.
  • Numbers can be negative, so the answer can be negative too.
  • nums has up to 100,000 elements, and k can 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