DIY: Monotonic Array

Solve the interview question "Monotonic Array" in this lesson.

Problem statement

An array is monotonic if it is either monotone increasing or monotone decreasing.

An array, arr, is monotone increasing if arr[i] <= arr[i + 1] for every element at each index i. An array, arr, is monotone decreasing if arr[i] >= arr[i + 1] for every element at each index i. Here, i should be 0 <= i <= n-1, where n is the size of the list.

Return True if and only if the given array, arr, is monotonic.

Input

The input will be an array of integers. The following is an example input:

arr = [1, 2, 3, 3, 3, 3, 3, 3, 5, 5, 5, 5, 9]

Output

The output will be a Boolean representing whether the array is monotonic or not. The following is an example output:

true

The array arr is increasing.

Coding exercise

Implement the is_monotonic(arr) function, where arr is the array of integers. The function returns either true or false depending on whether the array is monotonic or not.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.