Find Maximum Non-decreasing Array Length

You are given a 0-indexed integer array nums.

You can perform any number of operations, where each operation involves selecting a subarray of the array and replacing it with the sum of its elements. For example, if the given array is [1, 3, 5, 6] and you select subarray [3, 5], the array will convert to [1, 8, 6].

Return the maximum length of a non-decreasing array that can be made after applying operations.

A subarray is a contiguous non-empty sequence of elements within an array.

Example 1
Inputnums = [5,2,2]
Output1
Replacing the entire array with its sum gives [9], and no operation can make a non-decreasing array of length 2 or 3.
Example 2
Inputnums = [1,2,3,4]
Output4
The array is already non-decreasing, so its full length can be kept.

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^5

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate