Problem · Array

Check Monotonic Triples

Learn this problem
EasyTiktokNEW GRADINTERNOA
See Tiktok hiring insights

Problem 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

drafts saved locally
public int[] solution(int[] arr) {
    // write your code here
}
arr[1,2,1,-4,5,10]
expected[0,1,0,1]
checking account