FastPrepFastPrep
Problem Brief

Find First Index Where Prefix Sum Becomes Non-Positive

FULLTIMEOA

You are given an array of integers representing inventory stock. Your task is to find the first index where the prefix sum of the array becomes non-positive (i.e., less than or equal to 0). The prefix sum is the cumulative sum of elements from the start of the array to the current element. If no such index exists, return -1.

1Example 1

Input
inventory = [1, 2]
Output
-1
Explanation

Prefix sums are [1, 3], no non-positive sum.

2Example 2

Input
inventory = [2, -4, 1]
Output
2
Explanation

Prefix sums are [2, -2, -1], becomes non-positive at index 2.

3Example 3

Input
inventory = [1, 2, 3, -6]
Output
3
Explanation

Prefix sums are [1, 3, 6, 0], becomes non-positive at index 3.

Constraints

Limits and guarantees your solution can rely on.

  • The array contains at least one element, and its length is between 1 and (10^5).
  • The integers in the array can range from (-10^9) to (10^9).
public int findFirstNonPositivePrefixSumIndex(int[] inventory) {
  // write your code here
}
Input

inventory

[1, 2]

Output

-1

Sign in to submit your solution.