3038. Maximum Number of Operations With the Same Score I LeetCode Solution

In this guide, you will get 3038. Maximum Number of Operations With the Same Score I LeetCode Solution with the best time and space complexity. The solution to Maximum Number of Operations With the Same Score I problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Maximum Number of Operations With the Same Score I solution in C++
  4. Maximum Number of Operations With the Same Score I solution in Java
  5. Maximum Number of Operations With the Same Score I solution in Python
  6. Additional Resources
3038. Maximum Number of Operations With the Same Score I LeetCode Solution image

Problem Statement of Maximum Number of Operations With the Same Score I

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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

3038. Maximum Number of Operations With the Same Score I LeetCode Solution in C++

class Solution {
 public:
  int maxOperations(vector<int>& nums) {
    int ans = 1;
    int sum = nums[0] + nums[1];

    for (int i = 2; i + 1 < nums.size(); i += 2) {
      if (nums[i] + nums[i + 1] == sum)
        ++ans;
      else
        break;
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

3038. Maximum Number of Operations With the Same Score I LeetCode Solution in Java

class Solution {
  public int maxOperations(int[] nums) {
    int ans = 1;
    int sum = nums[0] + nums[1];

    for (int i = 2; i + 1 < nums.length; i += 2) {
      if (nums[i] + nums[i + 1] == sum)
        ++ans;
      else
        break;
    }

    return ans;
  }
}
// code provided by PROGIEZ

3038. Maximum Number of Operations With the Same Score I LeetCode Solution in Python

class Solution:
  def maxOperations(self, nums: list[int]) -> int:
    ans = 1
    summ = nums[0] + nums[1]

    for i in range(2, len(nums) - 1, 2):
      if nums[i] + nums[i + 1] == summ:
        ans += 1
      else:
        break

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.