Skip to main content

Largest Element in an Array after Merge Operations - Solution & Explanation

MediumArrayGreedy17 min readAsked at: Amazon, Google
Practice this problem

Problem Statement

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:

  • Choose an integer 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 <= 105
  • 1 <= nums[i] <= 106

Approach Overview

Problem 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.

Approach 1: Greedy Approach with Cumulative Merge

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

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.

Try this approach in the editor →

Approach 2: Simulating Merge Process with Stack

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n)
Space Complexity: O(n) in worst-case stack usage.

Try this approach in the editor →

Approach 3: Merge in Reverse Order

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).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
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.
Space Complexity: O(1), as we use a constant amount of additional space.

Simulating Merge Process with Stack

Time Complexity: O(n)
Space Complexity: O(n) in worst-case stack usage.

Merge in Reverse Order—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Greedy Cumulative Merge (Right-to-Left)O(n)O(1)Best general solution; minimal memory and fastest implementation
Stack-based Merge SimulationO(n)O(n)Useful when explicitly simulating merge behavior or explaining the process step-by-step

Video Solution

Leetcode Weekly contest 355 - Medium - Largest Element in an Array after Merge Operations • Prakhar Agrawal • 1,235 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Largest Element in an Array after Merge Operations easy or hard?
The problem is rated Medium because the merge rule appears to require simulation, but the optimal solution relies on a greedy observation. Recognizing that right-to-left accumulation captures all valid merges is the key insight.
Largest Element in an Array after Merge Operations Python/Java solution
Most implementations use the greedy right-to-left scan. In Python or Java, maintain a running sum initialized with the last element and iterate backward through the array. Merge when the current value is less than or equal to the running sum, otherwise reset it.
How to solve Largest Element in an Array after Merge Operations in O(n)?
Traverse the array from right to left and keep a running value representing the current merged segment. If nums[i] is less than or equal to the running value, add it to the running sum. Otherwise, reset the running value to nums[i]. Track the maximum value seen during the traversal.
What is the best approach for Largest Element in an Array after Merge Operations?
The optimal approach is a greedy right-to-left traversal that cumulatively merges values. If the current element is less than or equal to the accumulated value, you merge them by adding it. Otherwise, you reset the accumulator. This runs in O(n) time and O(1) space and avoids explicitly simulating merges.
Is Largest Element in an Array after Merge Operations asked at Google/Amazon/Meta?
This problem reflects patterns commonly seen in Google and Amazon interviews, especially greedy array processing and merge simulation questions. Variants involving cumulative merging or monotonic processing appear in technical screens and online assessments.
What data structure is used in Largest Element in an Array after Merge Operations?
The optimal solution uses only a variable to track the cumulative merged value, making it a greedy array traversal. An alternative implementation uses a stack to simulate the merge process explicitly, which helps visualize how adjacent elements combine.
What is the time complexity of Largest Element in an Array after Merge Operations?
The optimal greedy solution runs in O(n) time because the array is scanned once from right to left. Each element participates in at most one merge decision. Space complexity is O(1) since only a running sum and maximum value are stored.

Ready to solve this problem?

Practice Largest Element in an Array after Merge Operations with our built-in code editor and test cases.

Practice on FleetCode