Holiday Prefix Sum Challenge

Prefix Sums Arrays Subarray Basic Algorithms

Holiday Prefix Sum Challenge

In the spirit of fresh starts that often come with the New Year, here's a problem to warm up your coding muscles. Given an array of integers and a target sum, your task is to determine if there is any continuous subarray whose sum exactly equals the target. If such a subarray exists, return the 0-indexed starting and ending positions; otherwise, return [-1, -1].

Problem Details

  • You are provided with an array of integers, which can include negative numbers.
  • You are also given an integer target sum.
  • Your goal is to find any contiguous subarray whose elements sum to the target, using an efficient approach with prefix sums.

Constraints

  • 1 <= array length <= 10^5
  • Array elements can be negative, zero, or positive.

Example

For the array: [1, 2, 3, 4, 5] and target = 9, one valid answer is [1, 3], since 2 + 3 + 4 = 9.

Starter Code Instructions

Below is some starter code in various languages. Complete the function to solve the problem by leveraging the prefix sum strategy.