java.lang.Object
g2701_2800.s2789_largest_element_in_an_array_after_merge_operations.Solution

public class Solution extends Object
2789 - Largest Element in an Array after Merge Operations.<p>Medium</p> <p>You are given a <strong>0-indexed</strong> array <code>nums</code> consisting of positive integers.</p> <p>You can do the following operation on the array <strong>any</strong> number of times:</p> <ul> <li>Choose an integer <code>i</code> such that <code>0 <= i < nums.length - 1</code> and <code>nums[i] <= nums[i + 1]</code>. Replace the element <code>nums[i + 1]</code> with <code>nums[i] + nums[i + 1]</code> and delete the element <code>nums[i]</code> from the array.</li> </ul> <p>Return <em>the value of the <strong>largest</strong> element that you can possibly obtain in the final array.</em></p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [2,3,7,9,3]</p> <p><strong>Output:</strong> 21</p> <p><strong>Explanation:</strong></p> <p>We can apply the following operations on the array:</p> <ul> <li> <p>Choose i = 0. The resulting array will be nums = [<ins>5</ins>,7,9,3].</p> </li> <li> <p>Choose i = 1. The resulting array will be nums = [5,<ins>16</ins>,3].</p> </li> <li> <p>Choose i = 0. The resulting array will be nums = [<ins>21</ins>,3].</p> </li> </ul> <p>The largest element in the final array is 21. It can be shown that we cannot obtain a larger element.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [5,3,3]</p> <p><strong>Output:</strong> 11</p> <p><strong>Explanation:</strong></p> <p>We can do the following operations on the array:</p> <ul> <li> <p>Choose i = 1. The resulting array will be nums = [5,<ins>6</ins>].</p> </li> <li> <p>Choose i = 0. The resulting array will be nums = [<ins>11</ins>].</p> </li> </ul> <p>There is only one element in the final array, which is 11.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= nums.length <= 10<sup>5</sup></code></li> <li><code>1 <= nums[i] <= 10<sup>6</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • maxArrayValue

      public long maxArrayValue(int[] nums)