Find the earliest level after which the first player has strictly more total points than the second player.
Minimum Levels To Gain More Points
You are given an array possible of length n, where each value represents the outcome of a level:
1means the first player gains a point on that level.0means the second player gains a point on that level.
Both players start with 0 points.
You need to determine the minimum number of levels the first player must complete so that, after those levels, the first player's score is strictly greater than the second player's score.
If this never happens, return -1.
In other words, find the smallest index k such that the sum of the first k elements is greater than the number of remaining 0s among those k elements.
Input Format
- An integer array
possible.
Each element is either 0 or 1.
Output Format
- Return an integer: the minimum number of initial levels needed for the first player to have a strictly higher score than the second player.
- Return
-1if no such prefix exists.
Constraints
1 <= possible.lengthpossible[i]is either0or1- Time complexity should be linear or better.
Example 1
Input
possible = [0, 0, 1, 0, 1]
Output
3
Explanation
After 3 levels, the first player has 1 point and the second player has 2 points if we treat 1 as a point for the first player and 0 as a point for the second player. The earliest prefix where the first player becomes strictly ahead is after processing the first 3 levels in the intended scoring interpretation.
Example 2
Input
possible = [1, 1, 1]
Output
1
Explanation
After the first level, the first player already leads 1 to 0.
Show 1 more example
Example 3
Input
possible = [0, 0, 0]
Output
-1
Explanation
The first player never gets ahead.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.