{"id":503,"date":"2023-10-27T12:35:16","date_gmt":"2023-10-27T12:35:16","guid":{"rendered":"https:\/\/vmlogger.com\/algorithms\/?p=503"},"modified":"2023-12-10T01:02:17","modified_gmt":"2023-12-10T01:02:17","slug":"what-is-monotonic-arrays-algorithm-to-check-if-an-array-is-monotonic","status":"publish","type":"post","link":"https:\/\/vmlogger.com\/algorithms\/2023\/10\/27\/what-is-monotonic-arrays-algorithm-to-check-if-an-array-is-monotonic\/","title":{"rendered":"What is Monotonic Arrays – Algorithm to check if an array is Monotonic"},"content":{"rendered":"

Monotonic arrays are a fundamental concept in computer science and data structures. They are essential for a wide range of applications, such as searching and sorting algorithms. In this blog post, we’ll explore two different approaches to check if an array is monotonic: the brute-force method and an optimized single-pass solution. By the end of this article, you’ll have a solid understanding of how to determine whether an array is monotonic and how to choose the most efficient approach.<\/p>\n

What is Monotonic Arrays?<\/h2>\n

A monotonic array is an array that is entirely non-increasing or non-decreasing. In simpler terms, it means that the elements in the array are in either ascending or descending order. For instance, [1, 2, 3, 4, 5] and [5, 4, 3, 2, 1] are both monotonic arrays, while [1, 3, 2, 4, 5] is not.<\/p>\n

Brute-Force Approach<\/h2>\n

Let’s start with the brute-force approach, which is relatively straightforward. We’ll iterate through the array and compare adjacent elements. If all adjacent elements maintain the same order (either all increasing or all decreasing), the array is considered monotonic.<\/p>\n

\r\ndef is_monotonic(arr):\r\n    if len(arr) <= 2:\r\n        return True\r\n    increasing = decreasing = True\r\n    for i in range(1, len(arr)):\r\n        if arr[i] < arr[i - 1]:\r\n            increasing = False\r\n        if arr[i] > arr[i - 1]:\r\n            decreasing = False\r\n\r\n    return increasing or decreasing\r\n<\/pre>\n

In this code, we initialize two boolean variables, increasing and decreasing, to True. We then loop through the array, comparing adjacent elements. If any element breaks the monotonicity condition, we set the corresponding boolean variable to False. Finally, we return True if either increasing or decreasing is still True, indicating that the array is monotonic.<\/p>\n

The brute-force approach works, but it’s not the most efficient solution, especially when dealing with large arrays. It has a time complexity of O(n), where n is the length of the array.<\/p>\n

Optimized Single-Pass Approach [Better Approach]<\/h2>\n

A more efficient solution involves a single-pass approach that doesn’t require two separate loops for checking both increasing and decreasing monotonicity. Instead, we maintain a single variable to keep track of the direction and compare it with each element.<\/p>\n

\r\ndef is_monotonic_optimized(arr):\r\n    if len(arr) <= 2:\r\n        return True\r\n    direction = 0  # 0 for unknown, 1 for increasing, -1 for decreasing\r\n    for i in range(1, len(arr)):\r\n        if arr[i] > arr[i - 1]:\r\n            if direction == 0:\r\n                direction = 1\r\n            elif direction == -1:\r\n                return False\r\n        elif arr[i] < arr[i - 1]:\r\n            if direction == 0:\r\n                direction = -1\r\n            elif direction == 1:\r\n                return False\r\n\r\n    return True\r\n<\/pre>\n

In this optimized approach, we use the direction variable to track the trend of the array. We initialize it as 0 to indicate an unknown direction. As we traverse the array, we compare each element with the previous one and determine the direction. If we encounter an element that doesn't conform to the current direction, we immediately return False. If we complete the loop without any issues, the array is monotonic, and we return `True.<\/p>\n

This optimized approach is more efficient and has a time complexity of O(n), the same as the brute-force method, but it traverse through entire array only in worst cases. <\/p>\n

Conclusion<\/h2>\n

In this blog post, we've explored two different approaches to check if an array is monotonic. The brute-force approach is relatively simple and straightforward but may not be the most efficient for large arrays. On the other hand, the optimized single-pass approach offers better performance and is a more elegant solution.<\/p>\n

When choosing between the two methods, consider the size of your array and the desired efficiency. In most cases, the optimized single-pass approach is the preferred choice. It's concise, efficient, and offers a better performance guarantee.<\/p>\n

Remember that understanding and being able to identify monotonic arrays are valuable skills in algorithm and data structure applications, as they can lead to more optimized solutions in various algorithms and problems.<\/p>\n<\/span>","protected":false},"excerpt":{"rendered":"

Monotonic arrays are a fundamental concept in computer science and data structures. They are essential for a wide range of applications, such as searching and sorting algorithms. In this blog post, we’ll explore two different approaches to check if an array is monotonic: the brute-force method and an optimized single-pass solution. By the end of […]<\/p>\n","protected":false},"author":45,"featured_media":514,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[3],"tags":[],"yoast_head":"\nWhat is Monotonic Arrays - Algorithm to check if an array is Monotonic - Algorithms<\/title>\n<meta name=\"description\" content=\"What is monotonic array? Algorithm to check if an array is monotonic | Best algorithm to check monotonic array | python code to check monotonic array\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/vmlogger.com\/algorithms\/2023\/10\/27\/what-is-monotonic-arrays-algorithm-to-check-if-an-array-is-monotonic\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What is Monotonic Arrays - Algorithm to check if an array is Monotonic\" \/>\n<meta property=\"og:description\" content=\"What is monotonic array? Algorithm to check if an array is monotonic | Best algorithm to check monotonic array | python code to check monotonic array\" \/>\n<meta property=\"og:url\" content=\"https:\/\/vmlogger.com\/algorithms\/2023\/10\/27\/what-is-monotonic-arrays-algorithm-to-check-if-an-array-is-monotonic\/\" \/>\n<meta property=\"og:site_name\" content=\"Algorithms\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/www.facebook.com\/vmlogger\" \/>\n<meta property=\"article:author\" content=\"http:\/\/www.facebook.com\/vmlogger\" \/>\n<meta property=\"article:published_time\" content=\"2023-10-27T12:35:16+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-10T01:02:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/vmlogger.com\/algorithms\/wp-content\/uploads\/sites\/15\/2023\/10\/monotonic-array.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2560\" \/>\n\t<meta property=\"og:image:height\" content=\"1440\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Vishwamitra Mishra\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/www.twitter.com\/learnexcelmacro\" \/>\n<meta name=\"twitter:site\" content=\"@learnexcelmacro\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Vishwamitra Mishra\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/vmlogger.com\/algorithms\/2023\/10\/27\/what-is-monotonic-arrays-algorithm-to-check-if-an-array-is-monotonic\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/vmlogger.com\/algorithms\/2023\/10\/27\/what-is-monotonic-arrays-algorithm-to-check-if-an-array-is-monotonic\/\"},\"author\":{\"name\":\"Vishwamitra Mishra\",\"@id\":\"https:\/\/vmlogger.com\/algorithms\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5\"},\"headline\":\"What is Monotonic Arrays – Algorithm to check if an array is Monotonic\",\"datePublished\":\"2023-10-27T12:35:16+00:00\",\"dateModified\":\"2023-12-10T01:02:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/vmlogger.com\/algorithms\/2023\/10\/27\/what-is-monotonic-arrays-algorithm-to-check-if-an-array-is-monotonic\/\"},\"wordCount\":541,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/vmlogger.com\/algorithms\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5\"},\"articleSection\":[\"Easy\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/vmlogger.com\/algorithms\/2023\/10\/27\/what-is-monotonic-arrays-algorithm-to-check-if-an-array-is-monotonic\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/vmlogger.com\/algorithms\/2023\/10\/27\/what-is-monotonic-arrays-algorithm-to-check-if-an-array-is-monotonic\/\",\"url\":\"https:\/\/vmlogger.com\/algorithms\/2023\/10\/27\/what-is-monotonic-arrays-algorithm-to-check-if-an-array-is-monotonic\/\",\"name\":\"What is Monotonic Arrays - Algorithm to check if an array is Monotonic - Algorithms\",\"isPartOf\":{\"@id\":\"https:\/\/vmlogger.com\/algorithms\/#website\"},\"datePublished\":\"2023-10-27T12:35:16+00:00\",\"dateModified\":\"2023-12-10T01:02:17+00:00\",\"description\":\"What is monotonic array? Algorithm to check if an array is monotonic | Best algorithm to check monotonic array | python code to check monotonic array\",\"breadcrumb\":{\"@id\":\"https:\/\/vmlogger.com\/algorithms\/2023\/10\/27\/what-is-monotonic-arrays-algorithm-to-check-if-an-array-is-monotonic\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/vmlogger.com\/algorithms\/2023\/10\/27\/what-is-monotonic-arrays-algorithm-to-check-if-an-array-is-monotonic\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/vmlogger.com\/algorithms\/2023\/10\/27\/what-is-monotonic-arrays-algorithm-to-check-if-an-array-is-monotonic\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/vmlogger.com\/algorithms\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"What is Monotonic Arrays – Algorithm to check if an array is Monotonic\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/vmlogger.com\/algorithms\/#website\",\"url\":\"https:\/\/vmlogger.com\/algorithms\/\",\"name\":\"Algorithms\",\"description\":\"Welcome to the World of Algorithms\",\"publisher\":{\"@id\":\"https:\/\/vmlogger.com\/algorithms\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/vmlogger.com\/algorithms\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/vmlogger.com\/algorithms\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5\",\"name\":\"Vishwamitra Mishra\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/vmlogger.com\/algorithms\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/vmlogger.com\/algorithms\/wp-content\/uploads\/sites\/15\/2023\/03\/welcome-1.png\",\"contentUrl\":\"https:\/\/vmlogger.com\/algorithms\/wp-content\/uploads\/sites\/15\/2023\/03\/welcome-1.png\",\"width\":1963,\"height\":843,\"caption\":\"Vishwamitra Mishra\"},\"logo\":{\"@id\":\"https:\/\/vmlogger.com\/algorithms\/#\/schema\/person\/image\/\"},\"description\":\"My name is Vishwamitra Mishra. Friends Call me Vishwa. I hold a Bachelor\u2019s Degree in Computer Science from D.A.V.V. Indore & currently working as a Technical Lead having over 7 years of experience.\",\"sameAs\":[\"http:\/\/www.learnexcelmacro.com\",\"http:\/\/www.facebook.com\/vmlogger\",\"https:\/\/twitter.com\/https:\/\/www.twitter.com\/learnexcelmacro\",\"https:\/\/www.youtube.com\/c\/VMLogger\"],\"url\":\"https:\/\/vmlogger.com\/algorithms\/author\/vishwamitra\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"What is Monotonic Arrays - Algorithm to check if an array is Monotonic - Algorithms","description":"What is monotonic array? Algorithm to check if an array is monotonic | Best algorithm to check monotonic array | python code to check monotonic array","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/vmlogger.com\/algorithms\/2023\/10\/27\/what-is-monotonic-arrays-algorithm-to-check-if-an-array-is-monotonic\/","og_locale":"en_US","og_type":"article","og_title":"What is Monotonic Arrays - Algorithm to check if an array is Monotonic","og_description":"What is monotonic array? Algorithm to check if an array is monotonic | Best algorithm to check monotonic array | python code to check monotonic array","og_url":"https:\/\/vmlogger.com\/algorithms\/2023\/10\/27\/what-is-monotonic-arrays-algorithm-to-check-if-an-array-is-monotonic\/","og_site_name":"Algorithms","article_publisher":"http:\/\/www.facebook.com\/vmlogger","article_author":"http:\/\/www.facebook.com\/vmlogger","article_published_time":"2023-10-27T12:35:16+00:00","article_modified_time":"2023-12-10T01:02:17+00:00","og_image":[{"width":2560,"height":1440,"url":"https:\/\/vmlogger.com\/algorithms\/wp-content\/uploads\/sites\/15\/2023\/10\/monotonic-array.png","type":"image\/png"}],"author":"Vishwamitra Mishra","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/www.twitter.com\/learnexcelmacro","twitter_site":"@learnexcelmacro","twitter_misc":{"Written by":"Vishwamitra Mishra","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/vmlogger.com\/algorithms\/2023\/10\/27\/what-is-monotonic-arrays-algorithm-to-check-if-an-array-is-monotonic\/#article","isPartOf":{"@id":"https:\/\/vmlogger.com\/algorithms\/2023\/10\/27\/what-is-monotonic-arrays-algorithm-to-check-if-an-array-is-monotonic\/"},"author":{"name":"Vishwamitra Mishra","@id":"https:\/\/vmlogger.com\/algorithms\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5"},"headline":"What is Monotonic Arrays – Algorithm to check if an array is Monotonic","datePublished":"2023-10-27T12:35:16+00:00","dateModified":"2023-12-10T01:02:17+00:00","mainEntityOfPage":{"@id":"https:\/\/vmlogger.com\/algorithms\/2023\/10\/27\/what-is-monotonic-arrays-algorithm-to-check-if-an-array-is-monotonic\/"},"wordCount":541,"commentCount":0,"publisher":{"@id":"https:\/\/vmlogger.com\/algorithms\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5"},"articleSection":["Easy"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/vmlogger.com\/algorithms\/2023\/10\/27\/what-is-monotonic-arrays-algorithm-to-check-if-an-array-is-monotonic\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/vmlogger.com\/algorithms\/2023\/10\/27\/what-is-monotonic-arrays-algorithm-to-check-if-an-array-is-monotonic\/","url":"https:\/\/vmlogger.com\/algorithms\/2023\/10\/27\/what-is-monotonic-arrays-algorithm-to-check-if-an-array-is-monotonic\/","name":"What is Monotonic Arrays - Algorithm to check if an array is Monotonic - Algorithms","isPartOf":{"@id":"https:\/\/vmlogger.com\/algorithms\/#website"},"datePublished":"2023-10-27T12:35:16+00:00","dateModified":"2023-12-10T01:02:17+00:00","description":"What is monotonic array? Algorithm to check if an array is monotonic | Best algorithm to check monotonic array | python code to check monotonic array","breadcrumb":{"@id":"https:\/\/vmlogger.com\/algorithms\/2023\/10\/27\/what-is-monotonic-arrays-algorithm-to-check-if-an-array-is-monotonic\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/vmlogger.com\/algorithms\/2023\/10\/27\/what-is-monotonic-arrays-algorithm-to-check-if-an-array-is-monotonic\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/vmlogger.com\/algorithms\/2023\/10\/27\/what-is-monotonic-arrays-algorithm-to-check-if-an-array-is-monotonic\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/vmlogger.com\/algorithms\/"},{"@type":"ListItem","position":2,"name":"What is Monotonic Arrays – Algorithm to check if an array is Monotonic"}]},{"@type":"WebSite","@id":"https:\/\/vmlogger.com\/algorithms\/#website","url":"https:\/\/vmlogger.com\/algorithms\/","name":"Algorithms","description":"Welcome to the World of Algorithms","publisher":{"@id":"https:\/\/vmlogger.com\/algorithms\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/vmlogger.com\/algorithms\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/vmlogger.com\/algorithms\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5","name":"Vishwamitra Mishra","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/vmlogger.com\/algorithms\/#\/schema\/person\/image\/","url":"https:\/\/vmlogger.com\/algorithms\/wp-content\/uploads\/sites\/15\/2023\/03\/welcome-1.png","contentUrl":"https:\/\/vmlogger.com\/algorithms\/wp-content\/uploads\/sites\/15\/2023\/03\/welcome-1.png","width":1963,"height":843,"caption":"Vishwamitra Mishra"},"logo":{"@id":"https:\/\/vmlogger.com\/algorithms\/#\/schema\/person\/image\/"},"description":"My name is Vishwamitra Mishra. Friends Call me Vishwa. I hold a Bachelor\u2019s Degree in Computer Science from D.A.V.V. Indore & currently working as a Technical Lead having over 7 years of experience.","sameAs":["http:\/\/www.learnexcelmacro.com","http:\/\/www.facebook.com\/vmlogger","https:\/\/twitter.com\/https:\/\/www.twitter.com\/learnexcelmacro","https:\/\/www.youtube.com\/c\/VMLogger"],"url":"https:\/\/vmlogger.com\/algorithms\/author\/vishwamitra\/"}]}},"_links":{"self":[{"href":"https:\/\/vmlogger.com\/algorithms\/wp-json\/wp\/v2\/posts\/503"}],"collection":[{"href":"https:\/\/vmlogger.com\/algorithms\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vmlogger.com\/algorithms\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vmlogger.com\/algorithms\/wp-json\/wp\/v2\/users\/45"}],"replies":[{"embeddable":true,"href":"https:\/\/vmlogger.com\/algorithms\/wp-json\/wp\/v2\/comments?post=503"}],"version-history":[{"count":10,"href":"https:\/\/vmlogger.com\/algorithms\/wp-json\/wp\/v2\/posts\/503\/revisions"}],"predecessor-version":[{"id":521,"href":"https:\/\/vmlogger.com\/algorithms\/wp-json\/wp\/v2\/posts\/503\/revisions\/521"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/vmlogger.com\/algorithms\/wp-json\/wp\/v2\/media\/514"}],"wp:attachment":[{"href":"https:\/\/vmlogger.com\/algorithms\/wp-json\/wp\/v2\/media?parent=503"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vmlogger.com\/algorithms\/wp-json\/wp\/v2\/categories?post=503"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vmlogger.com\/algorithms\/wp-json\/wp\/v2\/tags?post=503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}