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] <= 105When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach determines if a list of numbers is monotonic by checking every possible scenario. It involves checking if the list is either entirely non-decreasing or entirely non-increasing. We can determine this by checking all possible pairs of numbers in the list.
Here's how the algorithm would work step-by-step:
def is_monotonic(numbers):
is_non_decreasing = True
# Check if the list is non-decreasing
for index in range(1, len(numbers)):
if numbers[index - 1] > numbers[index]:
is_non_decreasing = False
break
is_non_increasing = True
# Check if the list is non-increasing
for index in range(1, len(numbers)):
if numbers[index - 1] < numbers[index]:
is_non_increasing = False
break
# The list is monotonic if either non-decreasing or non-increasing is true
return is_non_decreasing or is_non_increasingTo check if a sequence is monotonic (either always increasing or always decreasing), we can avoid checking every single pair of numbers. The key is to determine the direction of the sequence early and then confirm that it stays consistent with that direction.
Here's how the algorithm would work step-by-step:
def is_monotonic(sequence):
sequence_length = len(sequence)
if sequence_length <= 2:
return True
index = 0
while index < sequence_length - 1 and sequence[index] == sequence[index + 1]:
index += 1
if index == sequence_length - 1:
return True
is_increasing = sequence[index] < sequence[index + 1]
# Determine overall trend (increasing or decreasing)
for i in range(index + 1, sequence_length - 1):
if is_increasing:
if sequence[i] > sequence[i + 1]:
return False
else:
if sequence[i] < sequence[i + 1]:
return False
# Ensure trend consistency.
return True
def is_monotonic_two(nums):
increasing = True
decreasing = True
# Check both increasing and decreasing
for i in range(len(nums) - 1):
if nums[i] > nums[i+1]:
increasing = False
if nums[i] < nums[i+1]:
decreasing = False
# Return if it is either increasing or decreasing
return increasing or decreasing
def is_monotonic_one_pass(array):
direction = 0
# Need to check the direction for increase or decrease.
for i in range(1, len(array)):
if array[i] - array[i - 1] > 0:
current_direction = 1
elif array[i] - array[i - 1] < 0:
current_direction = -1
else:
current_direction = 0
if direction == 0:
direction = current_direction
elif direction != current_direction and current_direction != 0:
# Ensure same direction
return False
return True| Case | How to Handle |
|---|---|
| Empty or null input array | Return true because an empty array can be considered both monotonically increasing and monotonically decreasing. |
| Array with a single element | Return true, as a single element is always monotonic. |
| Array with all identical elements | The algorithm should correctly identify this as monotonic (both increasing and decreasing). |
| Array with strictly increasing values | Algorithm must correctly identify it as monotonically increasing. |
| Array with strictly decreasing values | Algorithm must correctly identify it as monotonically decreasing. |
| Array with mixed increasing and decreasing segments | The algorithm should correctly identify this as non-monotonic. |
| Array with duplicate consecutive elements in increasing or decreasing order | Algorithm should handle these gracefully, maintaining the monotonic property if the increasing/decreasing order is consistent. |
| Large array sizes to test performance | The algorithm should ideally have O(n) time complexity, performing a single pass through the array efficiently. |