FastPrepFirst Prefix Reaching a Target Score

First Prefix Reaching a Target Score

Visa logoVisa● EasyFULLTIMEOA
Learn

Problem statement

You are given an integer array scores and an integer target.

Starting from index 0, add the scores in order. Return the first index i for which scores[0] + scores[1] + ... + scores[i] is greater than or equal to target.

If no prefix reaches the target, return -1.

Function

firstPrefixAtLeastTarget(scores: int[], target: int) → int

Examples

Example 1

scores = [2,3,1,4]target = 6return = 2

The prefix sums are 2, 5, and 6. Index 2 is the first index that reaches the target.

Example 2

scores = [-2,5,-1]target = 4return = -1

The prefix sums are -2, 3, and 2, so none reaches 4.

Constraints

  • 1 <= scores.length <= 105
  • -109 <= scores[i], target <= 109
  • Every prefix sum fits in a signed 64-bit integer.

More Visa problems

See Visa hiring insights
public int firstPrefixAtLeastTarget(int[] scores, int target) {
    // Write your code here.
}
scores[2,3,1,4]
target6
expected2
Checking account…