HomeGOUnderstanding Monotonic Arrays: A Complete Guide in Golang

Understanding Monotonic Arrays: A Complete Guide in Golang

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:

  1. Check for Increasing: Iterate through the array and see if each element is greater than or equal to the preceding element.
  2. Check for Decreasing: If it’s not increasing, iterate through and check if each element is less than or equal to the preceding element.
  3. 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

  1. Initialization:
    • isIncreasing and isDecreasing are boolean variables initialized to true. We assume the array is both increasing and decreasing at the start.
    • l stores the length of the input array nums.
  2. 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, so isIncreasing is set to false.
    • 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, so isDecreasing is set to false.
    • if !isIncreasing && !isDecreasing checks if both isIncreasing and isDecreasing are false. If this condition is met, it means the array is neither increasing nor decreasing, so it’s not monotonic, and the function immediately returns false.
  3. Result:
    • After the loop finishes, the function returns isIncreasing || isDecreasing. This means it returns true if the array is either increasing or decreasing (or both, in the case of an array with all equal elements), and false otherwise.

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!

RELATED ARTICLES

Most Popular