Monotonic Array Leetcode Solution

Difficulty Level Easy
Frequently asked in Amazon Facebook Microsoft
ArrayViews 2240

Problem Statement:

The Monotonic Array Leetcode Solution – Given an array is monotonic if it is either monotone increasing or monotone decreasing. An array nums is monotone increasing if for all i <= jnums[i] <= nums[j]. An array nums is monotone decreasing if for all i <= jnums[i] >= nums[j].

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

Example:

Example 1:

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

Explanation:

[1, 2, 2, 3] – this array is monotone increasing as 1<2<=2<3 

Example 2:

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

Explanation:

[1, 3, 2] – this array is neither monotone increasing nor monotone decreasing as 1>3<2. Both increasing and decreasing sequences are present in the array.

Approach:

An array is monotonic if it is monotone increasing, or monotone decreasing. Since a <= b and b <= c implies a <= c, we only need to check adjacent elements to determine if the array is monotone increasing (or decreasing, respectively). We can check each of these properties in one pass.

Idea:

Monotonic Array Leetcode Solution

  1. Initialize two variables increase and decrease with 0.
  2. Traverse the given array, compare the pair elements of the array and check if the current element in the array is greater than or less than the next element.
  3. If the current element is greater, then mark the “increase” variable as 1, and if the current element is less than the next element then mark the “decrease” variable as 1.
  4. If in any iteration both the increase and decrease variable become equal to 1 then we return false.
  5. Otherwise, we return true after the loop ends as it is proved that the given array is monotonic.

Code:

C++ Program of  Monotonic Array Leetcode Solution:

class Solution {
public:
    bool isMonotonic(vector<int>& nums) {
        int increase=0, decrease=0;
        int n=nums.size();
        for(int i=0;i<n-1;i++){
            if(nums[i] > nums[i+1]) increase = 1;
            if(nums[i] < nums[i+1]) decrease = 1;
            if(increase == 1 && decrease == 1) return false;
        }
        return true;
    }
};

Java Program of  Monotonic Array Leetcode Solution:

class Solution {
    public boolean isMonotonic(int[] nums) {
        int increase=0, decrease=0;
        int n=nums.length;
        for(int i=0;i<n-1;i++){
            if(nums[i] > nums[i+1]) increase = 1;
            if(nums[i] < nums[i+1]) decrease = 1;
            if(increase == 1 && decrease == 1) return false;
        }
        return true;
    }
}

Complexity Analysis for Monotonic Array Leetcode Solution:

Time Complexity:

The Time Complexity of the code is O(N) as we traverse through the given array only once.

Space Complexity:

The Space Complexity of the code is O(1) because we don’t need any extra space to solve this problem.

A similar type of question to practice:

Check if the Elements of an Array are Consecutive

 

Translate »