Talentd
FleetCode
Make and follow sheets.Sign in

Trionic Array I solution and explanation

EasyArray4 min read49.5% acceptanceUpdated 24 Mar 2026
Trionic Array I | Simple Intuition | Dry Run | Leetcode 3637 | codestorywithMIKcodestorywithMIK · 11:07 · 7K viewsWatch on YouTube
9 more videos on this problem
  1. Trionic Array I | Simple Intuition | Dry Run | Leetcode 3637 | codestorywithMIKcodestorywithMIK · 11:07 · 7K views
  2. 3637. Trionic Array I | Leetcode Daily - PythonLeetcode Daily · 10:00 · 1K views
  3. Leetcode 3637. Trionic Array I | Q1 of Weekly Contest 461ExpertFunda · 5:36 · 670 views
  4. Trionic Array I | LeetCode 3637 | Weekly Contest 461 | Java Code | Developer CoderDeveloper Coder · 10:56 · 667 views
  5. Daily Leetcode #2135: Feb 03, 2026 - 3637. Trionic Array IProgramming Live with Larry · 5:29 · 548 views
  6. 3637. Trionic Array I (Leetcode Easy)Programming Live with Larry · 5:27 · 493 views
  7. Trionic Array I | LeetCode 3637 | Brute Force | Single PasscodeTips · 7:29 · 386 views
  8. Trionic Array I | LeetCode 3637 - Pythonimpoldev · 7:50 · 251 views
  9. Trionic Array I - Leetcode 3637 - PythonTim Huang · 3:50 · 229 views
  10. 3637. Trionic Array I | Leetcode Weekly Contest 461Break and Build · 24:14 · 190 views

The problem

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] <= 1000

Approach overview

Problem Overview: You are given an integer array and must determine whether it forms a trionic pattern. A trionic array has three consecutive phases: strictly increasing, then strictly decreasing, then strictly increasing again. Each phase must contain at least one valid transition.

Approach 1: Single Pass State Tracking (O(n) time, O(1) space)

Traverse the array once while tracking which phase of the pattern you are currently in. Start in the increasing phase and move forward comparing nums[i] with nums[i-1]. When the sequence stops increasing and begins decreasing, transition to the second phase. When the sequence switches from decreasing to increasing again, transition to the third phase. If any comparison violates the expected direction of the current phase, the array cannot be trionic.

The key insight is that the pattern is strictly ordered: increase → decrease → increase. You only move forward through these phases and never go backward. Each element comparison determines whether you remain in the current phase or transition to the next one. If the traversal finishes while successfully reaching the third phase and maintaining strict inequalities, the array satisfies the trionic condition.

This approach works because every element is examined exactly once. No auxiliary structures are required—only a few variables to track the phase and validate transitions. Time complexity is O(n) since the array is scanned once, and space complexity is O(1).

The technique is essentially a lightweight state machine implemented during array traversal. Problems that involve detecting patterns in sequences often rely on similar ideas using simple counters or phase flags. You can explore related traversal patterns in array problems and pattern-detection strategies often used alongside two pointers or simulation techniques.

Recommended for interviews: The single-pass approach is exactly what interviewers expect. It demonstrates that you recognize the pattern structure and can enforce it using constant space. A brute-force segmentation approach would work but adds unnecessary complexity, while the linear scan shows strong control over state transitions.

Solution

We first define a pointer p, initially p = 0, pointing to the first element of the array. We move p to the right until we find the first element that doesn't satisfy strict increasing order, i.e., nums[p] geq nums[p + 1]. If p = 0 at this point, it means the first part of the array doesn't have a strictly increasing section, so we return false directly.

Next, we define another pointer q, initially q = p, pointing to the first element of the second part of the array. We move q to the right until we find the first element that doesn't satisfy strict decreasing order, i.e., nums[q] leq nums[q + 1]. If q = p or q = n - 1 at this point, it means the second part of the array doesn't have a strictly decreasing section or there's no third part, so we return false directly.

If all the above conditions are satisfied, it means the array is trionic, and we return true.

The time complexity is O(n), where n is the length of the array. The space complexity is O(1), using only constant extra space.

class Solution:
    def isTrionic(self, nums: List[int]) -> bool:
        n = len(nums)
        p = 0
        while p < n - 2 and nums[p] < nums[p + 1]:
            p += 1
        if p == 0:
            return False
        q = p
        while q < n - 1 and nums[q] > nums[q + 1]:
            q += 1
        if q == p or q == n - 1:
            return False
        while q < n - 1 and nums[q] < nums[q + 1]:
            q += 1
        return q == n - 1
class Solution {
    public boolean isTrionic(int[] nums) {
        int n = nums.length;
        int p = 0;
        while (p < n - 2 && nums[p] < nums[p + 1]) {
            p++;
        }
        if (p == 0) {
            return false;
        }
        int q = p;
        while (q < n - 1 && nums[q] > nums[q + 1]) {
            q++;
        }
        if (q == p || q == n - 1) {
            return false;
        }
        while (q < n - 1 && nums[q] < nums[q + 1]) {
            q++;
        }
        return q == n - 1;
    }
}
class Solution {
public:
    bool isTrionic(vector<int>& nums) {
        int n = nums.size();
        int p = 0;
        while (p < n - 2 && nums[p] < nums[p + 1]) {
            p++;
        }
        if (p == 0) {
            return false;
        }
        int q = p;
        while (q < n - 1 && nums[q] > nums[q + 1]) {
            q++;
        }
        if (q == p || q == n - 1) {
            return false;
        }
        while (q < n - 1 && nums[q] < nums[q + 1]) {
            q++;
        }
        return q == n - 1;
    }
};
function isTrionic(nums: number[]): boolean {
    const n = nums.length;
    let p = 0;
    while (p < n - 2 && nums[p] < nums[p + 1]) {
        p++;
    }
    if (p === 0) {
        return false;
    }
    let q = p;
    while (q < n - 1 && nums[q] > nums[q + 1]) {
        q++;
    }
    if (q === p || q === n - 1) {
        return false;
    }
    while (q < n - 1 && nums[q] < nums[q + 1]) {
        q++;
    }
    return q === n - 1;
}
func isTrionic(nums []int) bool {
	n := len(nums)
	p := 0
	for p < n-2 && nums[p] < nums[p+1] {
		p++
	}
	if p == 0 {
		return false
	}
	q := p
	for q < n-1 && nums[q] > nums[q+1] {
		q++
	}
	if q == p || q == n-1 {
		return false
	}
	for q < n-1 && nums[q] < nums[q+1] {
		q++
	}
	return q == n-1
}
impl Solution {
    pub fn is_trionic(nums: Vec<i32>) -> bool {
        let n = nums.len();
        let mut p = 0usize;

        while p + 2 < n && nums[p] < nums[p + 1] {
            p += 1;
        }
        if p == 0 {
            return false;
        }

        let mut q = p;
        while q + 1 < n && nums[q] > nums[q + 1] {
            q += 1;
        }
        if q == p || q + 1 == n {
            return false;
        }

        while q + 1 < n && nums[q] < nums[q + 1] {
            q += 1;
        }

        q + 1 == n
    }
}
Try this approach in the editor

Complexity analysis

ApproachTimeSpaceWhen to Use
Segment Detection (Multiple Scans)O(n)O(1)When explicitly separating increasing and decreasing segments for clarity
Single Pass State TrackingO(n)O(1)Best general solution; minimal logic and optimal traversal

Frequently asked questions

Is Trionic Array I easy or hard?

Trionic Array I is categorized as an Easy problem. The challenge mainly involves recognizing the required sequence pattern and implementing a clean single-pass check without overcomplicating the logic.

Trionic Array I Python/Java solution

The solution is identical across languages: iterate once through the array and track the three phases using a variable or enum. Implementations in Python, Java, C++, Go, TypeScript, and Rust follow the same logic with O(n) time and constant space.

How to solve Trionic Array I in O(n)?

Use a single pass through the array and track the current phase of the sequence. Start with an increasing phase, transition to decreasing when the trend reverses, and finally transition back to increasing. Ensure each phase maintains strict ordering and appears in the correct sequence. If the traversal finishes in the final phase, the array is trionic.

What is the best approach for Trionic Array I?

The most efficient solution uses a single-pass traversal with phase tracking. Iterate through the array while maintaining which phase you are in: increasing, decreasing, or increasing again. Each comparison determines whether you remain in the same phase or transition forward. This approach runs in O(n) time and O(1) space.

Is Trionic Array I asked at Google/Amazon/Meta?

Pattern detection problems on arrays appear frequently in interviews at companies like Google, Amazon, and Meta. While the exact problem name may vary, identifying monotonic segments and validating sequence patterns is a common interview theme. Practicing these problems improves array traversal and state tracking skills.

What data structure is used in Trionic Array I?

The problem primarily uses a basic array traversal. No additional data structures are required because the pattern can be validated using simple comparisons and a variable that tracks the current phase of the sequence.

What is the time complexity of Trionic Array I?

The optimal solution runs in O(n) time because the array is scanned exactly once. Each step compares the current element with the previous one to validate the expected direction. Space complexity is O(1) since only a few variables are used to track the current phase.

Comments

Now write it yourselfClose this page and solve Trionic Array I in the editor, without looking.
Practise Trionic Array I