Skip to main content
Back to problems
Leetcode
Medium
Arrays
Greedy
Minimum Levels To Gain More Points

Find the earliest level after which the first player has strictly more total points than the second player.

Acceptance 0%
Problem Statement

Minimum Levels To Gain More Points

You are given an array possible of length n, where each value represents the outcome of a level:

  • 1 means the first player gains a point on that level.
  • 0 means 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 -1 if no such prefix exists.

Constraints

  • 1 <= possible.length
  • possible[i] is either 0 or 1
  • Time complexity should be linear or better.
Examples
Sample cases returned by the problem API.

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.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.