LeetCode-in-Java

3637. Trionic Array I

Easy

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:

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:

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:

Solution

public class Solution {
    public boolean isTrionic(int[] nums) {
        int i = 1;
        int n = nums.length;
        while (i < n && nums[i] > nums[i - 1]) {
            i++;
        }
        if (i == n || i == 1) {
            return false;
        }
        while (i < n && nums[i] < nums[i - 1]) {
            i++;
        }
        if (i == n) {
            return false;
        }
        while (i < n && nums[i] > nums[i - 1]) {
            i++;
        }
        return i == n;
    }
}