Problem · Array
Check Monotonic Triples
Learn this problemProblem statement
You are given an integer array arr. Determine whether every sequence of three consecutive elements, arr[i], arr[i + 1], and arr[i + 2], is monotonic.
Three consecutive elements are monotonic when their values are in strictly increasing or strictly decreasing order.
Return an integer array of length arr.length - 2. For every valid index i, set the i-th result to 1 if either arr[i] < arr[i + 1] < arr[i + 2] or arr[i] > arr[i + 1] > arr[i + 2]. Otherwise, set it to 0.
A solution with time complexity no worse than O(arr.length^2) will fit within the execution time limit.
Function
solution(arr: int[]) → int[]Examples
Example 1
arr = [1,2,1,-4,5,10]return = [0,1,0,1][1, 2, 1]is not monotonic.[2, 1, -4]is strictly decreasing.[1, -4, 5]is not monotonic.[-4, 5, 10]is strictly increasing.
Example 2
arr = [10,10,10,10,10]return = [0,0,0]All values are equal, so no sequence of three elements is strictly increasing or strictly decreasing.
More Tiktok problems
- Can Reach the Exit with TeleportsOA · Seen Jul 2026
- Shift Every K-th ConsonantOA · Seen Jul 2026
- Count Access Code PairsOA · Seen Jul 2026
- Count Key ChangesOA · Seen Jul 2026
- Sort Matrix BordersOA · Seen Jul 2026
- Travel Distance on ScootersOA · Seen Jul 2026
- Validate 3x3 Digit WindowsOA · Seen Jul 2026
- Count Skipped Numbers After SubtractionsOA · Seen Jul 2026