Difficulty: Easy | Acceptance: 52.90% | Paid: No Topics: Array, Simulation
Given an array nums consisting of positive integers.
Return the maximum number of operations you can perform on the array.
In each operation:
- Select two elements from
nums. If their sum equals a target value, remove them from the array and continue. - The target value is the sum of the first two elements removed in the first operation.
You must remove the first two elements and calculate their sum as the target. Then, continue removing pairs of elements from the beginning of the array whose sum equals this target.
- Table of Contents
- Examples
- Constraints
- Simulation with While Loop
- Simulation with For Loop
Examples
Example 1:
Input: nums = [3,2,1,4,5]
Output: 2
Explanation:
- First operation: Remove nums[0] and nums[1]. The sum is 3 + 2 = 5. target = 5.
- Second operation: Remove nums[0] and nums[1]. The sum is 1 + 4 = 5. target = 5.
- No more operations can be performed.
Example 2:
Input: nums = [3,2,6,1,4]
Output: 1
Explanation:
- First operation: Remove nums[0] and nums[1]. The sum is 3 + 2 = 5. target = 5.
- Second operation: Remove nums[0] and nums[1]. The sum is 6 + 1 = 7 ≠ target.
- No more operations can be performed.
Example 3:
Input: nums = [1,1,1,1,1,1]
Output: 3
Explanation:
- First operation: Remove nums[0] and nums[1]. The sum is 1 + 1 = 2. target = 2.
- Second operation: Remove nums[0] and nums[1]. The sum is 1 + 1 = 2. target = 2.
- Third operation: Remove nums[0] and nums[1]. The sum is 1 + 1 = 2. target = 2.
- No more elements remain.
Constraints
2 <= nums.length <= 100
1 <= nums[i] <= 1000
Simulation with While Loop
Intuition We simulate the process by iterating through the array in pairs, checking if each pair’s sum matches the initial target sum.
Steps
- Calculate the target as the sum of the first two elements
- Use a while loop to process pairs from the beginning
- For each pair, check if their sum equals the target
- If yes, increment count and move to the next pair
- If no, break the loop
python
from typing import List
class Solution:
def maxOperations(self, nums: List[int]) -> int:
if len(nums) < 2:
return 0
target = nums[0] + nums[1]
count = 0
i = 0
while i + 1 < len(nums):
if nums[i] + nums[i + 1] == target:
count += 1
i += 2
else:
break
return countComplexity
- Time: O(n) where n is the length of the array
- Space: O(1) constant extra space
- Notes: Simple and efficient approach with early termination
Simulation with For Loop
Intuition Same logic as the while loop approach, but using a for loop with step size of 2 to iterate through pairs.
Steps
- Calculate the target as the sum of the first two elements
- Use a for loop with step 2 to process pairs
- For each pair, check if their sum equals the target
- If yes, increment count; if no, break the loop
python
from typing import List
class Solution:
def maxOperations(self, nums: List[int]) -> int:
if len(nums) < 2:
return 0
target = nums[0] + nums[1]
count = 0
for i in range(0, len(nums) - 1, 2):
if nums[i] + nums[i + 1] == target:
count += 1
else:
break
return countComplexity
- Time: O(n) where n is the length of the array
- Space: O(1) constant extra space
- Notes: More concise loop structure with same efficiency