Problem · Array

Check Even-Position Monotonicity

Learn this problem
EasyTiktok logoTiktokOA
See Tiktok hiring insights

Problem statement

Given an array of non-negative integers numbers, check whether the elements at even positions are monotonic.

The even-positioned elements are numbers[0], numbers[2], numbers[4], and so on. They are monotonic if they are either strictly increasing or strictly decreasing.

Return "increasing" if the even-positioned elements are strictly increasing, "decreasing" if they are strictly decreasing, or "none" if they are not monotonic.

A solution with time complexity not worse than O(numbers.length^2) fits within the execution time limit.

Function

solution(numbers: int[]) → String

Examples

Example 1

numbers = [12, 65, 15, 72, 19, 72]return = "increasing"

Elements at even positions are numbers[0], numbers[2], and numbers[4], i.e. 12, 15, and 19. Since 12 < 15 < 19, the answer is "increasing".

Example 2

numbers = [12, 1, 54, 5, 19, 14]return = "none"

Elements at even positions are numbers[0], numbers[2], and numbers[4], i.e. 12, 54, and 19. These values are not monotonic, so the answer is "none".

Example 3

numbers = [666, 17, 66, 5, 6, 23]return = "decreasing"

Elements at even positions are numbers[0], numbers[2], and numbers[4], i.e. 666, 66, and 6. Since 666 > 66 > 6, the answer is "decreasing".

Constraints

  • numbers is an array of non-negative integers.

More Tiktok problems

drafts saved locally
public String solution(int[] numbers) {
  // write your code here
}
numbers[12, 65, 15, 72, 19, 72]
expected"increasing"
checking account