Check Even-Position Monotonicity
Learn this problemProblem 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[]) → StringExamples
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
numbersis an array of non-negative integers.