🏔️ Beginner-Friendly Guide 'Trionic Array I' - Problem 3637 (C++, Python, JavaScript)
Source: Dev.to

Problem Summary
You’re given:
An array of integers called nums with a length of n.
Your goal:
Determine if the array is “trionic.” The array must be divisible into three specific, contiguous parts:
- A strictly increasing sequence at the start.
- A strictly decreasing sequence in the middle.
- A strictly increasing sequence at the end.
Intuition
Think of a “trionic” array as a mountain followed by a valley. To validate this, we can act like a climber traversing the data from left to right, passing through three distinct phases:
The First Climb:
Start at the first element and keep moving as long as the next number is larger than the current one. If we can’t even take one step up, or if we reach the very end of the array without stopping, it’s not trionic.
The Descent:
From the peak we just found, we must go down. Continue moving while the next number is smaller than the current one. If we don’t move down at all, or if the descent takes us to the very last element (leaving no room for the final climb), the pattern fails.
The Final Climb:
From the bottom of the valley, we must climb again. Move forward as long as the numbers are increasing.
If, after these three phases, we end up at the final index of the array, the array perfectly matches the trionic definition.
Walkthrough: Understanding the Examples
Example 1: nums = [1, 3, 5, 4, 2, 6]
- Phase 1 (Up): 1 → 3 → 5 (stop at index 2, value 5).
- Phase 2 (Down): 5 → 4 → 2 (stop at index 4, value 2).
- Phase 3 (Up): 2 → 6 (reach the end at index 5).
Result: true.
Example 2: nums = [2, 1, 3]
- Phase 1 (Up): Start at 2, next number 1 is not higher → cannot move.
- The first segment is not strictly increasing.
Result: false.
Code Blocks
C++
class Solution {
public:
bool isTrionic(vector& nums) {
int n = nums.size();
int i = 0;
// Phase 1: Strictly Increasing
while (i + 1 nums[i + 1]) {
i++;
}
// Must have moved from the peak, and must not be at the end
if (i == peak || i == n - 1) return false;
// Phase 3: Strictly Increasing
while (i + 1 bool:
n = len(nums)
i = 0
# Phase 1: Strictly Increasing
while i + 1 nums[i + 1]:
i += 1
if i == peak or i == n - 1:
return False
# Phase 3: Strictly Increasing
while i + 1 nums[i + 1]) {
i++;
}
if (i === peak || i === n - 1) return false;
// Phase 3: Strictly Increasing
while (i + 1 < n && nums[i] < nums[i + 1]) {
i++;
}
return i === n - 1;
};
Python
n = len(nums)
i = 0
# Phase 1: Strictly Increasing
while i + 1 nums[i + 1]:
i += 1
if i == peak or i == n - 1:
return False
# Phase 3: Strictly Increasing
while i + 1 nums[i + 1]) {
i++;
}
if (i === peak || i === n - 1) return false;
// Phase 3: Strictly Increasing
while (i + 1 < n && nums[i] < nums[i + 1]) {
i++;
}
return i === n - 1;
JavaScript
while (i + 1 < n && nums[i] < nums[i + 1]) {
i++;
}
if (i === peak || i === n - 1) return false;
// Phase 3: Strictly Increasing
while (i + 1 < n && nums[i] < nums[i + 1]) {
i++;
}
return i === n - 1;
Key Takeaways
- Linear Traversal: The solution runs in O(n) time because each element is visited at most once.
- State Management: A single index
itracks progress through three distinct loops, eliminating the need for complex nested logic. - Boundary Conditions: Checks such as
i == 0ori == n - 1ensure that each segment actually exists and contains at least two numbers to form a slope.
Final Thoughts
This problem is an excellent introduction to “Mountain Array” variations. In real‑world software engineering, similar logic is used in signal processing to identify peaks and valleys in sensor data or in financial analysis to detect specific market trends (e.g., a “head‑and‑shoulders” pattern). Mastering the ability to walk through an array while validating specific conditions is a core skill for any technical interview.