Trionic Array II

You are given an integer array nums of length n.

A trionic subarray is a contiguous subarray nums[l...r] with 0 <= l < r < n for which there exist indices l < p < q < r such that:

  • nums[l...p] is strictly increasing.
  • nums[p...q] is strictly decreasing.
  • nums[q...r] is strictly increasing.

Return the maximum sum of any trionic subarray in nums.

Example 1
Inputnums = [0,-2,-1,-3,0,2,-1]
Output-4
Choosing l = 1, p = 2, q = 3, and r = 5 gives the trionic subarray [-2, -1, -3, 0, 2] with sum -4.
Example 2
Inputnums = [1,4,2,7]
Output14
Choosing l = 0, p = 1, q = 2, and r = 3 gives the trionic subarray [1, 4, 2, 7] with sum 14.

Constraints

  • 4 <= n = nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • It is guaranteed that at least one trionic subarray exists.

Asked at 6 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