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:
Input: nums = [0,-2,-1,-3,0,2,-1]
Output: -4
Explanation:
Pick l = 1, p = 2, q = 3, r = 5:
nums[l...p] = nums[1...2] = [-2, -1] is strictly increasing (-2 < -1).nums[p...q] = nums[2...3] = [-1, -3] is strictly decreasing (-1 > -3)nums[q...r] = nums[3...5] = [-3, 0, 2] is strictly increasing (-3 < 0 < 2).(-2) + (-1) + (-3) + 0 + 2 = -4.Example 2:
Input: nums = [1,4,2,7]
Output: 14
Explanation:
Pick l = 0, p = 1, q = 2, r = 3:
nums[l...p] = nums[0...1] = [1, 4] is strictly increasing (1 < 4).nums[p...q] = nums[1...2] = [4, 2] is strictly decreasing (4 > 2).nums[q...r] = nums[2...3] = [2, 7] is strictly increasing (2 < 7).1 + 4 + 2 + 7 = 14.
Constraints:
4 <= n = nums.length <= 105-109 <= nums[i] <= 109Problem Overview: Trionic Array II asks you to validate or compute properties of a sequence that follows a three-phase pattern. The array transitions through three ordered segments (a "trionic" structure), and your task is to identify whether the pattern exists or compute the valid length/count depending on constraints. The challenge is detecting the three transitions efficiently without repeatedly scanning the array.
Approach 1: Brute Force Partition Check (O(n^3) time, O(1) space)
The most direct method is to try every possible pair of split points that divide the array into three sections. For each pair (i, j), verify whether the segments satisfy the trionic ordering constraints by scanning the ranges individually. This approach works as a baseline because it explicitly checks every possible structure, but it repeatedly rescans large portions of the array. With three nested loops and repeated validation passes, the runtime quickly grows to O(n^3), which is not practical for large inputs.
Approach 2: Prefix/Suffix Dynamic Programming (O(n^2) time, O(n) space)
You can reduce redundant checks using dynamic programming. Precompute prefix information describing how far a valid pattern can extend up to each index, and suffix information describing valid structure from the right side. Then iterate over possible middle split points and combine prefix and suffix states. This removes repeated scans and converts the verification into constant-time lookups. The algorithm still evaluates many candidate boundaries, so the total runtime remains around O(n^2), but it demonstrates how caching structural information simplifies the validation.
Approach 3: Grouped Loop State Tracking (O(n) time, O(1) space)
The optimal solution processes the array in a single pass using a grouped loop that tracks which phase of the trionic pattern you are currently in. Instead of explicitly choosing split points, the algorithm advances through the array while updating state transitions when the relationship between adjacent values changes. Each group of monotonic movement represents one phase of the pattern. Once three valid segments are detected, the algorithm verifies they appear in the required order and cover the needed elements.
This works because the pattern boundaries are determined entirely by local comparisons between consecutive elements. By collapsing runs of similar behavior into groups, you avoid revisiting elements and maintain only a few counters or state flags. The array is scanned once, giving O(n) time with O(1) extra space. The technique is closely related to problems involving monotonic runs in array processing.
Recommended for interviews: Start by describing the brute force partition idea to show you understand the structure of the trionic pattern. Then move quickly to the grouped loop solution. Interviewers usually expect the linear scan because it demonstrates pattern recognition, state transitions, and efficient dynamic programming-style thinking without heavy memory usage.
We can traverse the array to find all possible maximal trionic subarrays, calculate their sums, and update the maximum value.
We define a pointer i, initially i = 0, representing the current position pointing to the first element of the array. We move i to the right until we find the first element that does not satisfy strict increase, i.e., nums[i-1] geq nums[i]. If at this point i = l + 1, it means this segment has only one element and cannot form an increasing sequence, so we continue to the next iteration.
Next, we define pointer p, representing the end position of the current increasing segment. Then we find the second strictly decreasing part. If this segment has only one element, or reaches the end of the array, or encounters equal elements, we continue to the next iteration.
Then we define pointer q, representing the end position of the current decreasing segment. Next, we find the third strictly increasing part. At this point, we have found a maximal trionic subarray. The maximum sum of this trionic subarray consists of the following parts:
[p-2,..,q+1]p-3, or 0 if it doesn't existq+2, or 0 if it doesn't exist.After calculating the sum of this trionic subarray, we update the answer. Then we move pointer i to position q, because the increasing part of the third segment can serve as the first increasing segment of the next iteration.
After the traversal is complete, we return the answer.
The time complexity is O(n), where n is the length of the array. The space complexity is O(1), using only constant-level extra space.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Partition Check | O(n^3) | O(1) | Useful for understanding the structure of the three segments during initial reasoning |
| Prefix/Suffix Dynamic Programming | O(n^2) | O(n) | When precomputing structural properties helps avoid repeated scans |
| Grouped Loop State Tracking | O(n) | O(1) | Best general solution; single pass with constant memory |
Trionic Array II | Simplest Approach | Detailed Intuition | Leetcode 3640 | codestorywithMIK • codestorywithMIK • 13,379 views views
Watch 9 more video solutions →Practice Trionic Array II with our built-in code editor and test cases.
Practice on FleetCode