In computer science, a monotonic array is an array where the elements are either entirely non-increasing or non-decreasing. This means the array is either sorted in ascending order (allowing for duplicates) or descending order (also allowing for duplicates).
Properties of Monotonic arrays
- Order: The defining characteristic is the consistent order of elements.
- Equality: Duplicate values are allowed as long as the overall trend increases or decreases.
- Single Direction: An array cannot be monotonic if it has both increasing and decreasing sections. For example, [1, 3, 2] is not monotonic.
How to determine if an array is monotonic:
- Check for Increasing: Iterate through the array and see if each element is greater than or equal to the preceding element.
- Check for Decreasing: If it’s not increasing, iterate through and check if each element is less than or equal to the preceding element.
- Not Monotonic: If neither of the above conditions holds true, the array is not monotonic.
Example:
Let’s take the array [1, 2, 4, 4, 5]
:
- Is it monotonically increasing? Yes, each element is greater than or equal to the one before it.
- Is it monotonically decreasing? No, there are elements that are greater than the ones before them.
Therefore, the array [1, 2, 4, 4, 5]
is monotonic.
Golang code to check for monotonic arrays
func isMonotonic(nums []int) bool {
isIncreasing, isDecreasing := true, true
l := len(nums)
for i := 0; i < l-1; i++ {
if nums[i] > nums[i+1] {
isIncreasing = false
} else if nums[i] < nums[i+1] {
isDecreasing = false
}
if !isIncreasing && !isDecreasing {
return false
}
}
return isIncreasing || isDecreasing
}
Explanation of the code
- Initialization:
isIncreasing
andisDecreasing
are boolean variables initialized totrue
. We assume the array is both increasing and decreasing at the start.l
stores the length of the input arraynums
.
- Iteration:
- The code iterates through the array using a
for
loop, comparing each element with the next one. if nums[i] > nums[i+1]
checks if the current element is greater than the next. If true, it means the array is not increasing, soisIncreasing
is set tofalse
.else if nums[i] < nums[i+1]
checks if the current element is less than the next. If true, it means the array is not decreasing, soisDecreasing
is set tofalse
.if !isIncreasing && !isDecreasing
checks if bothisIncreasing
andisDecreasing
arefalse
. If this condition is met, it means the array is neither increasing nor decreasing, so it’s not monotonic, and the function immediately returnsfalse
.
- The code iterates through the array using a
- Result:
- After the loop finishes, the function returns
isIncreasing || isDecreasing
. This means it returnstrue
if the array is either increasing or decreasing (or both, in the case of an array with all equal elements), andfalse
otherwise.
- After the loop finishes, the function returns
Conclusion
Understanding monotonic arrays can be a valuable tool in your Golang programming toolkit. Their inherent order makes them efficient for certain algorithms and problem-solving scenarios. By mastering the concept and utilizing the code examples provided, you’ll be well-equipped to identify and leverage monotonic arrays effectively in your future Golang projects. Happy coding!