LeetCode solutions

2872. Largest Element in an Array after Merge Operations

My accepted Python solution to LeetCode problem 2872, Largest Element in an Array after Merge Operations, running in 39ms.

  • Difficulty: Medium
  • Python
  • Runtime 39ms
  • Memory 31.9MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 39ms, memory 31.9MB, accepted 2025-12-31.

python
class Solution:
    def maxArrayValue(self, nums: List[int]) -> int:
        # Work from right to left, merging when possible
        # If nums[i] <= nums[i+1], merge them (sum goes to position i)
        n = len(nums)
        if n == 1:
            return nums[0]
        
        # Start from the end
        current = nums[-1]
        for i in range(n - 2, -1, -1):
            if nums[i] <= current:
                current = nums[i] + current
            else:
                current = nums[i]
        return current

Source