FunctionRacerProblemsRace

First to Reach the Total

HardBitsPython, JavaScript, TypeScript, Java, C++

Two players take turns picking a whole number from 1 to top. A number can be picked only once in the whole game, by either player. The picks are added to one running total, and the player whose pick brings the total to goal or more wins.

Return True if the first player can win whatever the second player does, otherwise False. If all the numbers together add up to less than goal, nobody wins and the answer is False.

Example 1

top 3
goal 4
returns false

After any first pick, the second player has a number left that brings the total to 4 or more.

Example 2

top 4
goal 6
returns true

The first player picks 1, which leaves 5 to go. Whatever the second player adds, the first player can finish.

Example 3

top 3
goal 7
returns false

1 + 2 + 3 is only 6.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Bits problems