Maximum Number of Operations With the Same Score I
Source: leetcode
Difficulty: Easy
Tags: array, simulation

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


- Delete the first two elements `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:**


- We can perform the first operation with the score `3 + 2 = 5`. After this operation, `nums = [1,4,5]`.
- We can perform the second operation as its score is `4 + 1 = 5`, the same as the previous operation. After this operation, `nums = [5]`.
- As there are fewer than two elements, we can't perform more operations.



**Example 2:**



**Input:** nums \= \[1,5,3,3,4,1,3,2,2,3]


**Output:** 2


**Explanation:**


- We can perform the first operation with the score `1 + 5 = 6`. After this operation, `nums = [3,3,4,1,3,2,2,3]`.
- We can perform the second operation as its score is `3 + 3 = 6`, the same as the previous operation. After this operation, `nums = [4,1,3,2,2,3]`.
- We cannot perform the next operation as its score is `4 + 1 = 5`, which is different from the previous scores.



**Example 3:**



**Input:** nums \= \[5,3]


**Output:** 1



 


**Constraints:**


- `2 <= nums.length <= 100`
- `1 <= nums[i] <= 1000`




C++ solution:
class Solution {
public:
    int maxOperations(vector<int>& nums) {
        int n=nums.size();
        int check=nums[0]+nums[1];
        int i=3;
        int ans=1;
        
        while(i<n && nums[i]+nums[i-1]==check){
            i+=2;
            ans++;
        }
        
        return ans;
    }
};

Go solution:
func maxOperations(nums []int) int {

 
        target_sum := nums[0] + nums[1]
   
        operations_count := 0
     
        n := len(nums)
      
        for i := 0; i + 1 < n && nums[i] + nums[i + 1] == target_sum; i += 2 {
          
            operations_count++
        }
        
        return operations_count
    }

Java solution:
class Solution {
    public int maxOperations(int[] nums) {
        int prev = nums[0] + nums[1],count = 1;
        for(int i=2;i<nums.length-1;i+=2){
            if(nums[i] + nums[i+1] == prev) count++;
            else break;
        }
        return count;
    }
}

JavaScript solution:
/**
 * @param {number[]} nums
 * @return {number}
 */
var maxOperations = function(nums) {
    const prev = nums[0] + nums[1];
    let count = 1;
    for(let i=2;i<nums.length-1;i+=2){
        if(nums[i] + nums[i+1] == prev) count++;
        else break;
    }
    return count;
};

Python solution:
class Solution:
    def maxOperations(self, nums: List[int]) -> int:
        ans = 0 
        score = nums[0] + nums[1]
        for i in range(0, len(nums)-1, 2):
            if nums[i] + nums[i+1] == score: ans += 1
            else: break 
        return ans

Ruby solution:
# @param {Integer[]} nums
# @return {Integer}
def max_operations(nums)
    answer = 1
    score = nums[0..1].sum
    perm_score = score
    nums = nums[2..-1]
    while perm_score == score and nums.size > 1
        score = nums[0..1].sum
        if score == perm_score
            answer += 1
        end
        nums = nums[2..-1]
    end

    answer
end