Algorithm


Problem Name: 896. Monotonic Array

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

An array nums is monotone increasing if for all i <= j, nums[i] <= nums[j]. An array nums is monotone decreasing if for all i <= j, nums[i] >= nums[j].

Given an integer array nums, return true if the given array is monotonic, or false otherwise.

 

Example 1:

Input: nums = [1,2,2,3]
Output: true

Example 2:

Input: nums = [6,5,4,4]
Output: true

Example 3:

Input: nums = [1,3,2]
Output: false

 

Constraints:

  • 1 <= nums.length <= 105
  • -105 <= nums[i] <= 105
 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


class Solution {
public:
    bool isMonotonic(vector<int>& A) {
        bool a(true), b(true);
        int n = A.size();
        for(int i = 0; i  <  n - 1 && (a || b); ++i){
            if(A[i] > A[i + 1]) a = false;
            if(A[i] < A[i + 1]> b = false;
        }
        return a || b;
    }
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [1,2,2,3]

Output

x
+
cmd
true

#2 Code Example with Java Programming

Code - Java Programming


class Solution {
  public boolean isMonotonic(int[] nums) {
    int idx = 0;
    int sign = 0;
    while (idx  <  nums.length - 1 && sign == 0) {
      if (nums[idx] < nums[idx + 1]) {
        sign = 1;
      } else if (nums[idx] > nums[idx + 1]) {
        sign = -1;
      }
      idx++;
    }
    while (idx  <  nums.length - 1) {
      if ((sign == 1 && nums[idx] > nums[idx + 1]) || (sign == -1 && nums[idx] < nums[idx + 1])) {
        return false;
      }
      idx++;
    }
    return true;
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [1,2,2,3]

Output

x
+
cmd
true

#3 Code Example with Javascript Programming

Code - Javascript Programming


const isMonotonic = function(nums) {
  return inc(nums) || dec(nums)
};

function inc(nums) {
  if(nums == null || nums.length <= 1) return true
  for(let i = 1, n = nums.length; i  <  n; i++) {
    if(nums[i] < nums[i - 1]) return false
  }
  return true 
}
function dec(nums) {
  if(nums == null || nums.length <= 1) return true
  for(let i = 1, n = nums.length; i  <  n; i++> {
    if(nums[i] > nums[i - 1]) return false
  }
  return true 
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [6,5,4,4]

Output

x
+
cmd
true

#4 Code Example with Python Programming

Code - Python Programming


class Solution:
    def isMonotonic(self, A):
        return all(A[i] <= A[i - 1] for i in range(1, len(A))) or all(A[i] >= A[i - 1] for i in range(1, len(A)))
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [6,5,4,4]

Output

x
+
cmd
true

#5 Code Example with C# Programming

Code - C# Programming


namespace LeetCode
{
    public class _0896_MonotonicArray
    {
        public bool IsMonotonic(int[] A)
        {
            if (A.Length <= 2) return true;

            var i = 0;
            while (i  <  A.Length - 1 && A[i] == A[i + 1])
                i++;

            if (i == A.Length - 1) return true;

            var isIncreasing = A[i]  <  A[i + 1];
            i++;
            if (isIncreasing)
            {
                for (int k = i; k  <  A.Length - 1; k++)
                    if (A[k] > A[k + 1]) return false;
            }
            else
            {
                for (int k = i; k  <  A.Length - 1; k++)
                    if (A[k] < A[k + 1]) return false;
            }
            return true;
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [1,3,2]

Output

x
+
cmd
false
Advertisements

Demonstration


Previous
#895 Leetcode Maximum Frequency Stack Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#897 Leetcode Increasing Order Search Tree Solution in C, C++, Java, JavaScript, Python, C# Leetcode