Skip to content

Instantly share code, notes, and snippets.

@tatsuyax25
Created February 3, 2026 20:27
Show Gist options
  • Select an option

  • Save tatsuyax25/a88cdc8edd2f53ef393573c6847ce94e to your computer and use it in GitHub Desktop.

Select an option

Save tatsuyax25/a88cdc8edd2f53ef393573c6847ce94e to your computer and use it in GitHub Desktop.
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
/**
* @param {number[]} nums
* @return {boolean}
*/
var isTrionic = function(nums) {
const n = nums.length;
// Need at least 4 elements to form inc → dec → inc
if (n < 4) return false;
let i = 0;
// -------------------------------
// Phase 1: strictly increasing
// -------------------------------
while (i + 1 < n && nums[i] < nums[i + 1]) {
i++;
}
// If we never increased or we stopped at the last element,
// we cannot form the required pattern.
if (i === 0 || i === n - 1) return false;
// -------------------------------
// Phase 2: strictly decreasing
// -------------------------------
let peak = i; // p index
while (i + 1 < n && nums[i] > nums[i + 1]) {
i++;
}
// If we never decreased or we stopped at the last element,
// we cannot form the required pattern.
if (i === peak || i === n - 1) return false;
// -------------------------------
// Phase 3: strictly increasing again
// -------------------------------
let valley = i; // q index
while (i + 1 < n && nums[i] < nums[i + 1]) {
i++;
}
// If we never increased in this final phase, it's invalid.
if (i === valley) return false;
// If we consumed the entire array successfully, it's trionic.
return i === n - 1;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment