Skip to content
RED SAGSkills
hard20 pts · first answer countsarrayssliding-windowdebugging

Bug in a minimum length window

This code should return the shortest length with sum >= target but returns a value that is too large for some inputs. What is wrong?

for (let r = 0; r < a.length; r++) {
  sum += a[r]
  if (sum >= target) {
    best = Math.min(best, r - l + 1)
    sum -= a[l++]
  }
}
Choose one answer

Discussion 0

Join free to post in the discussion.

Related questions