3637. Trionic Array I
Difficulty: Easy
Topics: Mid Level, Array, Weekly Contest 461
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
pandqto form the required three segments.
Constraints:
3 <= n <= 100-1000 <= nums[i] <= 1000
Hint:
- Use brute force
Solution:
We need to check if there exist indices p and q that split the array into three strictly monotonic segments. Let me analyze this carefully.
Understanding the Problem
The array should be:
- Strictly increasing from index 0 to
p(inclusive) - Strictly decreasing from index
ptoq(inclusive) - Strictly increasing from index
qton-1(inclusive)
Constraints: 0 < p < q < n-1
Approach
Since the constraints are small (n ≤ 100), a brute force approach is feasible:
- Try all possible
pvalues (from 1 to n-3) - For each
p, try all possibleqvalues (from p+1 to n-2) - Check if all three segments satisfy the monotonic conditions
Let's implement this solution in PHP: 3637. Trionic Array I
<?php
/**
* @param Integer[] $nums
* @return Boolean
*/
function isTrionic(array $nums): bool
{
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
$nums1 = [1, 3, 5, 4, 2, 6];
echo isTrionic($nums1) ? "true\n" : "false\n"; // Output: true
$nums2 = [2, 1, 3];
echo isTrionic($nums2) ? "true\n" : "false\n"; // Output: false
?>
Explanation:
-
Longest Non-Decreasing Prefix and Suffix:
- The prefix is determined by traversing the array from the start until elements are in non-decreasing order.
- Similarly, the suffix is determined by traversing from the end.
-
Initial Minimum Removal:
- Calculate the removal length by keeping only the prefix or the suffix.
-
Merging Prefix and Suffix:
- Use two pointers (
ifor prefix andjfor suffix) to find the smallest subarray to remove such that the last element of the prefix is less than or equal to the first element of the suffix.
- Use two pointers (
-
Return Result:
- The result is the minimum length of the subarray to remove, calculated as the smaller of the initial removal or the merging of prefix and suffix.
Complexity
- Time Complexity: O(n³) in the worst case, but with n ≤ 100, this is acceptable (100³ = 1,000,000 operations)
- Space Complexity: O(1) - we only use a few variables
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:
Top comments (0)