Monotonic Array


Monotonic Array[Medium]

  • Write a function that takes in an array of integers and returns a boolean representing whether the array is monotonic.
  • An array is said to be monotonic if its elements,from left to right,are entirely non-increasing or entirely non-decreasing.
  • Non-increasing elements aren’t necessarily exclusively decreasing;they simply don’t increase. Similarly, non-decreasing elements aren’t necessarily exclusively increasing;they simply don’t decrease.
  • Note that empty arrays and arrays of one element are monotonic.

Sample Input

array=[-1,-5,-10,-1100,-1100,-1101,-1102,-9001]

Sample Output

true

Hints

Hint 1
You can solve this question by iterating through the input array from left to right once.


Hint 2
Try iterating through the input array from left to right,in search of two adjacent integers that can indicate whether the array is trending upward or downward.Once you've found the tentative trend of the array,at each element thereafter,compare the element to the previous one;if this comparison breaks the previously identified trend,the array isn't monotonic.


Hint 3
Alternatively,you can start by assuming that the array is both entirely non-decreasing and entirely non-increasing.As you iterate through each element,perform a check to see if you can invalidate one or both of your assumptions.


Method 1

【O(n)time∣O(1)space】
import java.util.*;

class Program {
  public static boolean isMonotonic(int[] array) {
    // Write your code here.
    int length = array.length;
    
    if(length <= 1){
      return true;
    }
    boolean isIncreasing = false;
    boolean isDecreasing = false;
    
    for(int i = 1; i < length; i++){
      if(array[i] > array[i-1]){
        isDecreasing = true;
      }

      if(array[i] < array[i-1]){
        isIncreasing = true;
      }

      //if it exists isIncreasing and isDecreasing at the same time, return false 
      if(isIncreasing && isDecreasing){
        return false;
      }
    }
    return true;
  }
}

Method 2

【O(n)time∣O(1)space】
import java.util.*;

class Program {
  public static boolean isMonotonic(int[] array) {
    // Write your code here.
    int length = array.length;
    
    if(length <= 1){
      return true;
    }

    // default increasing or decreasing
    // true  increasing
    // false decreasing
    boolean flag =  array[0] > array[length - 1] ;

    for(int i = 1; i < length; i++){
      // if array is default increasing
      // then if exist 'array[i] > array[i-1]' that means decreasing
      // then return false
      if(flag && array[i] > array[i-1]){
        return false;
      }

      // if array is default decreasing
      // then if exist 'array[i] < array[i-1]' that means increasing
      // then return false
      if(!flag && array[i] < array[i-1]){
        return false;
      }
    }
    return true;
  }
}



Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • 2379. Minimum Recolors to Get K Consecutive Black Blocks
  • 2471. Minimum Number of Operations to Sort a Binary Tree by Level
  • 1387. Sort Integers by The Power Value
  • 2090. K Radius Subarray Averages
  • 2545. Sort the Students by Their Kth Score