Skip to main content
Back to problems
Leetcode
Medium
Arrays
Greedy
Find The Score Difference In A Game

Compute the maximum difference in score between two players in a game sequence.

Acceptance 0%
Problem Statement

You are given an array of integers representing points gained or lost during a game. Choose a contiguous segment of the sequence that maximizes the score difference between two competing outcomes derived from that segment.

A common interpretation is to find the largest possible difference between the best and worst scoring parts of the game, which can be reduced to tracking the maximum subarray-style gain over the array. Your task is to return the score difference according to the game’s scoring rule.

Write a function that determines the required difference efficiently for the given sequence.

Input Format

  • The first line contains an integer nn.
  • The second line contains nn integers describing the game sequence.

Output Format

  • Print a single integer: the required score difference.

Constraints

  • 1≤n≤1051 \le n \le 10^5
  • Values may be negative, zero, or positive.
  • An O(n)O(n) solution is expected.
Examples
Sample cases returned by the problem API.

Example 1

Input

5
1 -2 3 4 -1

Output

7

Explanation

The best contiguous gain is achieved by taking the segment [3, 4], which contributes 7 in total.

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.