Skip to main content
Back to problems
#3637
Easy Algorithms

Trionic array i

Array
49.5% acceptance
Feb 25, 2026
467
40
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.

Solution

Rust
Time O(n²)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn is_trionic(nums: Vec<i32>) -> bool {
    let n = nums.len();
    // p must be in [1, n-3], q in [p+1, n-2]
    // Try all valid (p, q) pairs
    for p in 1..n - 2 {
      for q in p + 1..n - 1 {
        // Check increasing [0..p]
        let inc1 = (0..p).all(|i| nums[i] < nums[i + 1]);
        // Check decreasing [p..q]
        let dec = (p..q).all(|i| nums[i] > nums[i + 1]);
        // Check increasing [q..n-1]
        let inc2 = (q..n - 1).all(|i| nums[i] < nums[i + 1]);
        if inc1 && dec && inc2 {
          return true;
        }
      }
    }
    false
  }
}