You are given a 0-indexed array nums consisting of positive integers.
You can do the following operation on the array any number of times:
i such that 0 <= i < nums.length - 1 and nums[i] <= nums[i + 1]. Replace the element nums[i + 1] with nums[i] + nums[i + 1] and delete the element nums[i] from the array.Return the value of the largest element that you can possibly obtain in the final array.
Example 1:
Input: nums = [2,3,7,9,3] Output: 21 Explanation: We can apply the following operations on the array: - Choose i = 0. The resulting array will be nums = [5,7,9,3]. - Choose i = 1. The resulting array will be nums = [5,16,3]. - Choose i = 0. The resulting array will be nums = [21,3]. The largest element in the final array is 21. It can be shown that we cannot obtain a larger element.
Example 2:
Input: nums = [5,3,3] Output: 11 Explanation: We can do the following operations on the array: - Choose i = 1. The resulting array will be nums = [5,6]. - Choose i = 0. The resulting array will be nums = [11]. There is only one element in the final array, which is 11.
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 106Problem Overview: You are given an array where you can repeatedly merge two adjacent elements if the left value is less than or equal to the right value. The merge replaces them with their sum. The goal is to determine the largest possible element that can appear after performing any sequence of valid merges.
Approach 1: Greedy Approach with Cumulative Merge (O(n) time, O(1) space)
This approach scans the array from right to left and greedily accumulates values when merging is valid. Maintain a running value current initialized to the last element. For each element moving left, check if nums[i] ≤ current. If true, merge them by adding the value to current. Otherwise, reset current to nums[i]. Track the maximum value seen during this process. The key insight: merging from the right guarantees that every valid chain of merges is captured without explicitly modifying the array. Time complexity is O(n) and space complexity is O(1). This solution relies on a simple greedy observation about how merges propagate. See related concepts in greedy algorithms and array problems.
Approach 2: Simulating Merge Process with Stack (O(n) time, O(n) space)
This approach explicitly simulates the merge rule using a stack. Iterate through the array and push elements onto the stack. Whenever the top two elements satisfy the condition left ≤ right, pop them and push their sum back onto the stack. Continue merging while the condition holds. The stack represents the current merged structure of the array. After processing all elements, scan the stack to find the maximum value produced. Time complexity remains O(n) because each element is pushed and popped at most once, while space complexity is O(n) due to the stack storage. This method is useful when you want a clearer simulation of the merging rules using a stack.
Recommended for interviews: The greedy right-to-left accumulation is the expected optimal solution. It reduces the entire merge process to a single pass and constant memory. Showing the stack simulation first demonstrates you understand the merge mechanics, but implementing the greedy insight proves stronger algorithmic intuition.
This approach focuses on sequentially merging elements in the array by iterating from left to right. If a current element is less than or equal to the successor, they are merged. This operation continues until the end of the array is reached. This approach ensures that the maximum value is obtained by accumulating the largest possible total from each consecutive merge.
The function findLargestElement takes an array and its size as arguments. It iterates through the array, merging elements whenever a lesser or equal element is found. It keeps track of the largest element found so far and returns it. The merge happens by adding the smaller or equal element to the larger, modifying the array in place.
Time Complexity: O(n), where n is the length of the array. We make a single pass through the array.
Space Complexity: O(1), as we use a constant amount of additional space.
This approach uses a stack to simulate merging operations. By utilizing stack operations (push and pop), we can easily manage merges and keep track of the cumulative sum of merged elements. This method is more intuitive for some as it reflects the explicit merges rather than relying on in-place modifications.
This implementation uses a stack approach to consolidate the merging process, where each element is effectively reduced or accumulated onto preceding elements whenever possible using a while loop until the stack satisfies the push condition. The stack will hold partially merged results and will finally be checked for the highest element.
Time Complexity: O(n)
Space Complexity: O(n) in worst-case stack usage.
According to the problem description, in order to maximize the maximum element in the merged array, we should merge the elements on the right first, making the elements on the right as large as possible, so as to perform as many merge operations as possible and finally get the maximum element.
Therefore, we can traverse the array from right to left. For each position i, where i \in [0, n - 2], if nums[i] leq nums[i + 1], we update nums[i] to nums[i] + nums[i + 1]. Doing so is equivalent to merging nums[i] and nums[i + 1] and deleting nums[i].
In the end, the maximum element in the array is the maximum element in the merged array.
The time complexity is O(n), where n is the length of the array. The space complexity is O(1).
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Greedy Approach with Cumulative Merge | Time Complexity: O(n), where n is the length of the array. We make a single pass through the array. |
| Simulating Merge Process with Stack | Time Complexity: O(n) |
| Merge in Reverse Order | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Greedy Cumulative Merge (Right-to-Left) | O(n) | O(1) | Best general solution; minimal memory and fastest implementation |
| Stack-based Merge Simulation | O(n) | O(n) | Useful when explicitly simulating merge behavior or explaining the process step-by-step |
Leetcode Weekly contest 355 - Medium - Largest Element in an Array after Merge Operations • Prakhar Agrawal • 1,224 views views
Watch 9 more video solutions →Practice Largest Element in an Array after Merge Operations with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor