logo CodeStepByStep logo

isMonotonic

Language/Type: Java arrays
Related Links:

Write a method named isMonotonic that accepts an array of integers as a parameter and returns true if the array's element values are monotonic; that is, whether the array is entirely in ascending or descending order. For example, if an array called a stores {2, 5, 7, 18} or {9, 6, 2, 2, 0, -4}, then the call of isMonotonic(a) should return true. If the array not in monotonic order, such as {4, 8, 7, 11, 12}, return false. An empty, one-element, or two-element array is always considered to be monotonic.

An optimal solution runs in O(N) time and uses a constant amount of extra space. You should not modify the contents of the array passed to your method.

Method: Write a Java method as described, not a complete program or class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.