|
| 1 | +--- |
| 2 | +id: monotonic-array |
| 3 | +title: Monotonic Array |
| 4 | +sidebar_label: 896-Monotonic-Array |
| 5 | +tags: |
| 6 | + - Array |
| 7 | + |
| 8 | +description: The problem no. is 1351. The Problem is to find monotonic array. |
| 9 | +--- |
| 10 | + |
| 11 | +## Problem Description |
| 12 | +An array is monotonic if it is either monotone increasing or monotone decreasing. |
| 13 | + |
| 14 | +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]`. |
| 15 | + |
| 16 | +Given an integer array `nums`, return `true` if the given array is monotonic, or `false` otherwise. |
| 17 | + |
| 18 | + |
| 19 | +### Example |
| 20 | + |
| 21 | +**Example 1:** |
| 22 | + |
| 23 | + |
| 24 | +``` |
| 25 | +Input: nums = [1,2,2,3] |
| 26 | +Output: true |
| 27 | +``` |
| 28 | +**Example 2:** |
| 29 | +``` |
| 30 | +Input: nums = [6,5,4,4] |
| 31 | +Output: true |
| 32 | +``` |
| 33 | +### Constraints |
| 34 | + |
| 35 | +- `-10^5 <= nums[i] <= 10^5` |
| 36 | + |
| 37 | +## Solution Approach |
| 38 | + |
| 39 | +### Intuition: |
| 40 | + |
| 41 | +To efficiently determine the monotonic array |
| 42 | +## Solution Implementation |
| 43 | + |
| 44 | +### Code In Different Languages: |
| 45 | + |
| 46 | +<Tabs> |
| 47 | + <TabItem value="JavaScript" label="JavaScript" default> |
| 48 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 49 | + ```javascript |
| 50 | + |
| 51 | +class Solution { |
| 52 | + isMonotonic(nums) { |
| 53 | + const n = nums.length; |
| 54 | + let increasing = true; |
| 55 | + let decreasing = true; |
| 56 | + for(let i = 0; i < n - 1; i++){ |
| 57 | + if(nums[i] > nums[i+1]) increasing = false; |
| 58 | + if(nums[i] < nums[i+1]) decreasing = false; |
| 59 | + } |
| 60 | + return increasing || decreasing; |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | + ``` |
| 66 | + |
| 67 | + </TabItem> |
| 68 | + <TabItem value="TypeScript" label="TypeScript"> |
| 69 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 70 | + ```typescript |
| 71 | + class Solution { |
| 72 | + isMonotonic(nums: number[]): boolean { |
| 73 | + const n = nums.length; |
| 74 | + let increasing = true; |
| 75 | + let decreasing = true; |
| 76 | + for(let i = 0; i < n - 1; i++){ |
| 77 | + if(nums[i] > nums[i+1]) increasing = false; |
| 78 | + if(nums[i] < nums[i+1]) decreasing = false; |
| 79 | + } |
| 80 | + return increasing || decreasing; |
| 81 | + } |
| 82 | +} |
| 83 | +
|
| 84 | +
|
| 85 | +
|
| 86 | +
|
| 87 | + ``` |
| 88 | + |
| 89 | + </TabItem> |
| 90 | + <TabItem value="Python" label="Python"> |
| 91 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 92 | + ```python |
| 93 | + class Solution: |
| 94 | + def isMonotonic(self, nums): |
| 95 | + n = len(nums) |
| 96 | + increasing = True |
| 97 | + decreasing = True |
| 98 | + for i in range(n - 1): |
| 99 | + if nums[i] > nums[i+1]: |
| 100 | + increasing = False |
| 101 | + if nums[i] < nums[i+1]: |
| 102 | + decreasing = False |
| 103 | + return increasing or decreasing |
| 104 | +
|
| 105 | +
|
| 106 | +
|
| 107 | + ``` |
| 108 | + |
| 109 | + </TabItem> |
| 110 | + <TabItem value="Java" label="Java"> |
| 111 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 112 | + ```java |
| 113 | + public class Solution { |
| 114 | + public boolean isMonotonic(int[] nums) { |
| 115 | + int n = nums.length; |
| 116 | + boolean increasing = true; |
| 117 | + boolean decreasing = true; |
| 118 | + for(int i = 0; i < n - 1; i++){ |
| 119 | + if(nums[i] > nums[i+1]) increasing = false; |
| 120 | + if(nums[i] < nums[i+1]) decreasing = false; |
| 121 | + } |
| 122 | + return increasing || decreasing; |
| 123 | + } |
| 124 | +} |
| 125 | +
|
| 126 | +
|
| 127 | +
|
| 128 | + ``` |
| 129 | + |
| 130 | + </TabItem> |
| 131 | + <TabItem value="C++" label="C++"> |
| 132 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 133 | + ```cpp |
| 134 | +class Solution { |
| 135 | +public: |
| 136 | + bool isMonotonic(vector<int>& nums) { |
| 137 | + int n = nums.size(); |
| 138 | + bool increasing = true; |
| 139 | + bool decreasing = true; |
| 140 | + |
| 141 | + for(int i=0; i<n-1; i++){ |
| 142 | + if(nums[i] > nums[i+1]) increasing = false; |
| 143 | + if(nums[i] < nums[i+1]) decreasing = false; |
| 144 | + } |
| 145 | + |
| 146 | + return increasing || decreasing; |
| 147 | + } |
| 148 | +}; |
| 149 | +
|
| 150 | +``` |
| 151 | +</TabItem> |
| 152 | +</Tabs> |
| 153 | + |
| 154 | +#### Complexity Analysis |
| 155 | + |
| 156 | +- Time Complexity: $$O(n)$$ |
| 157 | +- Space Complexity: $$O(1)$$ |
| 158 | +- The time complexity is $$O(n)$$ where n is the length of the input array nums. This is because the algorithm iterates through the array once, performing a constant amount of work for each element. |
| 159 | +- The space complexity is $$O(1)$$ because we are not using any extra space. |
0 commit comments