Skip to main content
Back to problems
Leetcode
Medium
Arrays
Hash Maps
Maximum Number Of Operations With The Same Score I

Remove pairs of elements from the ends of an array, keeping the pair sum fixed, and maximize the number of valid operations.

Acceptance 0%
Problem Statement

You are given an integer array nums. In one operation, you may remove exactly two elements from the array, and the removed pair must come from the current ends of the array. More precisely, in each move you may remove:

  • the first two elements, or
  • the last two elements, or
  • the first and last elements.

All chosen pairs must have the same sum. Your task is to determine the maximum number of operations you can perform.

The key idea is to choose a target sum based on the first operation, then simulate or count how many valid removals can be made while the array shrinks.

Input Format

  • nums: an integer array of length at least 2.

The exact platform input is an array of integers.

Output Format

Return a single integer: the maximum number of valid operations with the same pair sum.

Constraints

  • 2 <= nums.length
  • Each operation removes exactly 2 elements.
  • The chosen pair in every operation must sum to the same value.
  • The array size decreases after every operation until no valid move remains.

Exact platform constraints are not provided here; use the standard LeetCode-style interpretation.

Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [3,2,1,2,3,4]

Output

3

Explanation

One optimal sequence is to remove [3,4]? No, that is not from the ends. Instead, choose a valid target sum using end pairs and keep removing valid end pairs until impossible. The maximum number of operations is 3.

Example 2

Input

nums = [1,1,1,1]

Output

2

Explanation

Remove the first two elements twice, each time with sum 2.

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.