Skip to content

Practice Predict the Winner

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#486
LeetCode Problem

Predict the Winner

Problem Statement

You are given an integer array nums. Two players are playing a game with this array: player 1 and player 2. Player 1 and player 2 take turns, with player 1 starting first. Both players start the game with a score of 0. At each turn, the player takes one of the numbers from either end of the array (i.e., nums[0] or nums[nums.length - 1]) which reduces the size of the array by 1. The player adds the chosen number to their score. The game ends when there are no more elements in the array. Return true if Player 1 can win the game. If the scores of both players are equal, then player 1 is still the winner, and you should also return true. You may assume that both players are playing optimally.

Source: Predict the Winner on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [1,5,2]
Output
false

# Example 2

Input
nums = [1,5,233,7]
Output
true

Constraints

1 <= nums.length <= 20
0 <= nums[i] <= 10^7

Related LeetCode Problems

Practice more problems similar to Predict the Winner.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.