You are given an integer array nums of length n.
An array is trionic if there exist indices 0 < p < q < n − 1 such that:
nums[0...p] is strictly increasing,nums[p...q] is strictly decreasing,nums[q...n − 1] is strictly increasing.Return true if nums is trionic, otherwise return false.
Example 1:
Input: nums = [1,3,5,4,2,6]
Output: true
Explanation:
Pick p = 2, q = 4:
nums[0...2] = [1, 3, 5] is strictly increasing (1 < 3 < 5).nums[2...4] = [5, 4, 2] is strictly decreasing (5 > 4 > 2).nums[4...5] = [2, 6] is strictly increasing (2 < 6).Example 2:
Input: nums = [2,1,3]
Output: false
Explanation:
There is no way to pick p and q to form the required three segments.
Constraints:
3 <= n <= 100-1000 <= nums[i] <= 1000When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The problem asks whether an array can be split into three consecutive strictly monotonic parts: increasing, decreasing, and increasing. We can verify this property by simulating a single pass through the array. If all three phases successfully progress without stalling and end exactly at the final element, the array is trionic.
Here's how the algorithm would work step-by-step:
def is_trionic(nums: list[int]) -> bool:
length = len(nums)
# Minimum length required for three strictly monotonic segments is four.
if length < 4:
return False
index = 0
# Traverse the first strictly increasing segment.
while index + 1 < length and nums[index] < nums[index + 1]:
index += 1
if index == 0:
return False
peak_index = index
# Traverse the intermediate strictly decreasing segment.
while index + 1 < length and nums[index] > nums[index + 1]:
index += 1
if index == peak_index:
return False
valley_index = index
# Traverse the final strictly increasing segment.
while index + 1 < length and nums[index] < nums[index + 1]:
index += 1
return index == length - 1 and valley_index < length - 1
The problem asks whether an array can be split into three consecutive segments: strictly increasing, strictly decreasing, and strictly increasing. This requires identifying two boundary indices corresponding to a peak and a valley such that both boundaries strictly divide the three trends. We can simulate the process in a single pass by greedily advancing along each expected trend and checking if the entire array is consumed.
Here's how the algorithm would work step-by-step:
def is_trionic(nums):
total_elements = len(nums)
# Check that the array has at least four elements for three non-empty segments.
if total_elements < 4:
return False
current_index = 0
# Walk through the first strictly increasing segment.
while current_index + 1 < total_elements and nums[current_index] < nums[current_index + 1]:
current_index += 1
first_peak = current_index
if first_peak == 0:
return False
# Walk through the strictly decreasing middle segment.
while current_index + 1 < total_elements and nums[current_index] > nums[current_index + 1]:
current_index += 1
valley = current_index
if valley == first_peak:
return False
# Walk through the final strictly increasing segment.
while current_index + 1 < total_elements and nums[current_index] < nums[current_index + 1]:
current_index += 1
return current_index == total_elements - 1 and valley < total_elements - 1
| Case | How to Handle |
|---|---|
| Array length less than 3 elements | Return false immediately since the array cannot be partitioned into three non-empty subarrays. |
| Total sum not divisible by 3 | Return false early because an integer sum cannot be split evenly into three equal integer parts. |
| Array with all zeros | Return true as long as length is at least 3 since any valid split point will yield sums of zero. |
| Array containing negative numbers | Ensure cumulative sums are tracked correctly using prefix sum logic where negative values can decrease running totals. |
| Extreme values causing integer overflow | Use 64-bit integers for sum calculations if element values approach standard 32-bit integer limits. |
| Array with highly skewed distributions where valid split is near boundaries | Ensure search logic allows valid partitions while keeping subarrays strictly non-empty by enforcing proper boundary checks. |
| Maximum-sized inputs up to constraints limit | Use an optimal linear time single or double pass approach to avoid time limit exceeded errors. |
| Multiple valid split points exist | Return true upon finding the first valid configuration that satisfies all partition conditions. |