Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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] <= 1000Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[1,3,5,4,2,6]
[2,1,3]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3637: Trionic Array I
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;
}
}
// Accepted solution for LeetCode #3637: Trionic Array I
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
}
# Accepted solution for LeetCode #3637: Trionic Array I
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
// Accepted solution for LeetCode #3637: Trionic Array I
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
}
}
// Accepted solution for LeetCode #3637: Trionic Array I
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;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.