You are given an array of integers nums. Consider the following operation:
nums 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.
Example 1:
Input: nums = [3,2,1,4,5]
Output: 2
Explanation:
3 + 2 = 5. After this operation, nums = [1,4,5].4 + 1 = 5, the same as the previous operation. After this operation, nums = [5].Example 2:
Input: nums = [1,5,3,3,4,1,3,2,2,3]
Output: 2
Explanation:
1 + 5 = 6. After this operation, nums = [3,3,4,1,3,2,2,3].3 + 3 = 6, the same as the previous operation. After this operation, nums = [4,1,3,2,2,3].4 + 1 = 5, which is different from the previous scores.Example 3:
Input: nums = [5,3]
Output: 1
Constraints:
2 <= nums.length <= 1001 <= nums[i] <= 1000Problem Overview: You are given an integer array nums. Each operation removes the first two elements and produces a score equal to their sum. Every operation must produce the same score. The task is to determine the maximum number of valid operations you can perform before the rule breaks.
Approach 1: Brute Force Simulation (O(n) time, O(1) space)
The direct strategy simulates the process exactly as described. Start by computing the score from the first pair: target = nums[0] + nums[1]. Then repeatedly remove the next two elements and check whether their sum equals target. If the sum differs, the sequence of operations must stop. Because each step processes exactly two elements, the algorithm scans the array once. This approach uses simple iteration and conditional checks, making it easy to implement. It fits naturally with problems categorized under Array and Simulation since you mimic the operations step by step without additional data structures.
Approach 2: Optimized Two-Pointer Scan (O(n) time, O(1) space)
A cleaner implementation uses a pointer that moves across the array in steps of two. First compute the required score using the first pair. Then maintain a pointer i starting at index 2. At each step, check whether nums[i] + nums[i+1] equals the target score. If it matches, increment the operation count and move the pointer forward by two positions. If it does not match, terminate immediately because future operations would violate the constant-score rule. This pattern resembles a simplified Two Pointers traversal where the pointer jumps across fixed-size segments instead of sliding element by element.
The key insight is that the score is fixed by the first operation. After that, the array must naturally split into consecutive pairs producing the same sum. No rearrangement or searching is allowed, so the problem reduces to verifying each pair sequentially. Because every element is visited at most once, the runtime stays linear and memory usage remains constant.
Recommended for interviews: The two-pointer style scan is the approach interviewers typically expect. It demonstrates that you recognized the greedy constraint: the first pair determines the only valid score. The brute force simulation still shows clear understanding of the problem mechanics, but the optimized pointer traversal communicates stronger pattern recognition and cleaner implementation.
This solution iterates over the array, using each possible score from the first pair of numbers as a potential target score. It then attempts to match that score with subsequent pairs, counting the number of successful operations. We return the maximum number of operations achieved with any target score.
Time Complexity: O(n^2), Space Complexity: O(1), where n is the number of elements in nums.
This code iterates with a target starting from the first two elements, checks forward if more such operations exist, and counts them. This eliminates redundant backward checks.
Time Complexity: O(n^2), but optimized and often faster in practice, Space Complexity: O(1), since only constant space is used.
First, we calculate the sum of the first two elements, denoted as s. Then we traverse the array, taking two elements at a time. If their sum is not equal to s, we stop the traversal. Finally, we return the number of operations performed.
The time complexity is O(n), where n is the length of the array nums. The space complexity is O(1).
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Brute Force Approach | Time Complexity: O(n^2), Space Complexity: O(1), where n is the number of elements in |
| Optimized Two-Pointer Approach | Time Complexity: O(n^2), but optimized and often faster in practice, Space Complexity: O(1), since only constant space is used. |
| Traversal | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Simulation | O(n) | O(1) | When implementing the problem exactly as described using simple iteration |
| Two-Pointer Sequential Scan | O(n) | O(1) | Preferred solution for interviews; cleanly checks pair sums while scanning the array once |
Maximum Number of Operations With the Same Score I - Python - Leetcode 3038 • CheatCode Ninja • 163 views views
Watch 5 more video solutions →Practice Maximum Number of Operations With the Same Score I with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor