Maximum Number of Operations With the Same Score I - Problem

You are given an array of integers nums. Consider the following operation:

Delete the first two elements nums[0] and nums[1] and define the score of the operation as the sum of these two elements.

You can perform this operation until nums contains fewer than two elements. Additionally, the same score must be achieved in all operations.

Return the maximum number of operations you can perform.

Input & Output

Example 1 — Basic Case
$ Input: nums = [3,2,1,4,5,2]
Output: 2
💡 Note: First pair: 3+2=5, second pair: 1+4=5, third pair: 5+2=7≠5, so we stop. Total operations: 2.
Example 2 — Single Operation
$ Input: nums = [3,2,6,1]
Output: 1
💡 Note: First pair: 3+2=5, second pair: 6+1=7≠5, so we stop after 1 operation.
Example 3 — All Same Score
$ Input: nums = [1,4,2,3]
Output: 2
💡 Note: First pair: 1+4=5, second pair: 2+3=5. Both pairs have same score, so 2 operations total.

Constraints

  • 2 ≤ nums.length ≤ 1000
  • 1 ≤ nums[i] ≤ 1000

Visualization

Tap to expand
Maximum Operations With Same ScoreInput: [3,2,1,4,5,2]3214523+2=51+4=55+2=7Op 1 ✓Op 2 ✓Stop ✗Target Score: 5 (from first pair)Operations: 2 (stop when score changes)Output: 2
Understanding the Visualization
1
Input
Array of integers [3,2,1,4,5,2]
2
Process
Take consecutive pairs from front with same sum
3
Output
Count of operations performed: 2
Key Takeaway
🎯 Key Insight: The first pair determines the target score, and we count consecutive matching pairs from the front
Asked in
Microsoft 25 Amazon 20 Google 15
30.3K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen