{"id":1788,"date":"2024-07-03T10:00:01","date_gmt":"2024-07-03T14:00:01","guid":{"rendered":"http:\/\/www.mymiller.name\/wordpress\/?p=1788"},"modified":"2024-07-03T10:00:01","modified_gmt":"2024-07-03T14:00:01","slug":"algorithm-max-diff","status":"publish","type":"post","link":"https:\/\/www.mymiller.name\/wordpress\/challenge\/algorithm-max-diff\/","title":{"rendered":"Algorithm: Max Diff between consecutive elements in an ordered array"},"content":{"rendered":"\n<p>So you have an array that is ordered from highest to lowest, and now you need to know what is the maximum difference between consecutive elements in the array. &nbsp;So let&#8217;s take a look at what we are first talking about.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">int[] array = {1,2,3,5,7,11,13,17,19}<\/pre>\n\n\n\n<p>As you can see we have an ordered list. &nbsp;We can easily see that the maximum difference between the elements is 4. &nbsp;11-7 and 17-13 both result in 4. &nbsp;Now what if this array was &nbsp;a size in thousands. &nbsp;What would be the best way to calculate this value?<\/p>\n\n\n\n<p>I have found a divide and conquer approach that works extremely well to reduce the over all number of checks needed.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/*******************************************************************************\n * Copyright (c) 2017 MyMiller Consulting LLC.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\"); you may not\n * use this file except in compliance with the License. You may obtain a copy of\n * the License at\n *\n * http:\/\/www.apache.org\/licenses\/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the\n * License for the specific language governing permissions and limitations under\n * the License.\n *******************************************************************************\/\npackage name.mymiller.algorithms;\n \nimport java.util.ArrayList;\nimport java.util.List;\n \nimport name.mymiller.exceptions.OutOfOrderException;\n \n\/**\n * Class of static methods for various algorithm implementations\n *\n * @author jmiller\n *\n *\/\npublic class Algorithms\n{\n    \/**\n     * Process an sorted array to identify the MAX difference between elements\n     *\n     * @param array\n     *            Array to analyze\n     * @return Maximum difference between two consecutive elements in the array.\n     * @throws OutOfOrderException\n     *             Exception throw to indicate Array is out of order\n     *\/\n    static public int maxDiff(int[] array) throws OutOfOrderException\n    {\n \n        return Algorithms.maxDiff(array, 0, array.length - 1, 0);\n    }\n \n    \/**\n     * Process an sorted array to identify the MAX difference between elements\n     *\n     * @param array\n     *            Array to analyze\n     * @param start\n     *            Position to start analyzing\n     * @param end\n     *            Position to stop analyzing\n     * @return Maximum difference between two consecutive elements in the array.\n     * @throws OutOfOrderException\n     *             Exception throw to indicate Array is out of order\n     *\/\n    static public int maxDiff(int[] array, int start, int end) throws OutOfOrderException\n    {\n        return Algorithms.maxDiff(array, start, end, 0);\n    }\n \n    \/**\n     * Process an sorted array to identify the MAX difference between elements\n     *\n     * @param array\n     *            Array to analyze\n     * @param start\n     *            Position to start analyzing\n     * @param end\n     *            Position to stop analyzing\n     * @param currentMax\n     *            Maximum difference known\n     * @return Maximum difference between two consecutive elements in the array.\n     * @throws OutOfOrderException\n     *             Exception throw to indicate Array is out of order\n     *\/\n    static private int maxDiff(int[] array, int start, int end, int currentMax) throws OutOfOrderException\n    {\n        final int range = Math.abs(array[end] - array[start]);\n        if (array[start] &gt; array[end])\n        {\n            throw new OutOfOrderException(\"Array is not ordered. Index:(\" + start + \") \" + array[start]\n                    + \" is greater than Index:(\" + end + \") \" + array[end]);\n        }\n        if (start &gt;= end)\n        {\n            return 0;\n        }\n        else if (range &lt;= currentMax)\n        {\n            return 0;\n        }\n        else if (((start + 1) == end) || (range &lt;= 1))\n        {\n            return range;\n        }\n        else\n        {\n            final int mid = ((end - start) \/ 2) + start;\n \n            final int maxFront = Algorithms.maxDiff(array, start, mid, currentMax);\n            final int maxBack = Algorithms.maxDiff(array, mid, end, (maxFront &gt; currentMax) ? maxFront : currentMax);\n \n            return (maxFront &gt; maxBack) ? maxFront : maxBack;\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>This works through a divide and conquer algorithm. &nbsp;We split the array, and check each section separately. &nbsp;The key is we pass in what is known as the current maximum. &nbsp;Then we check to determine if the entire array from start to finish is less than the current known maximum, if it is then we continue checking it, otherwise we don&#8217;t need to follow on that array any further. &nbsp;This leads to more checks early while we are finding the maximum, with fewer checks occurring once we get closer to the maximum.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So you have an array that is ordered from highest to lowest, and now you need to know what is the maximum difference between consecutive elements in the array. &nbsp;So let&#8217;s take a look at what we are first talking about. int[] array = {1,2,3,5,7,11,13,17,19} As you can see we have an ordered list. &nbsp;We [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1790,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[273],"tags":[],"series":[],"class_list":["post-1788","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-challenge"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2016\/07\/silhouette-936724_640.jpg?fit=640%2C452&ssl=1","jetpack-related-posts":[{"id":1846,"url":"https:\/\/www.mymiller.name\/wordpress\/challenge\/dividing-game\/","url_meta":{"origin":1788,"position":0},"title":"Dividing Game Interview Coding Sample","author":"Jeffery Miller","date":"August 29, 2016","format":false,"excerpt":"I was recently given the challenge to write an solution for the Dividing Game. \u00a0The objective Player 1 and Player 2 each choose a number. \u00a0The solution should output the number of common divisor's they share in common between the selected numbers. This logic is rather simple, create a list\u2026","rel":"","context":"In &quot;Challenge&quot;","block_context":{"text":"Challenge","link":"https:\/\/www.mymiller.name\/wordpress\/category\/challenge\/"},"img":{"alt_text":"dividing game","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2016\/08\/calculator-1330104_640.jpg?fit=640%2C426&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2016\/08\/calculator-1330104_640.jpg?fit=640%2C426&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2016\/08\/calculator-1330104_640.jpg?fit=640%2C426&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":2632,"url":"https:\/\/www.mymiller.name\/wordpress\/lists\/list-intersection-union-unique\/","url_meta":{"origin":1788,"position":1},"title":"List Intersection, Union, &#038; Unique","author":"Jeffery Miller","date":"December 23, 2025","format":false,"excerpt":"Ever have two lists that you need to pull the unique elements from? Or need to pull the elements in both? Here is a ListUtils class for doing just those steps. package name.mymiller.extensions.utils; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; \/** * * @author jmiller Provide a set of utilities to\u2026","rel":"","context":"In &quot;Lists&quot;","block_context":{"text":"Lists","link":"https:\/\/www.mymiller.name\/wordpress\/category\/lists\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2019\/09\/grocery-list-1670408_640.jpg?fit=640%2C426&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2019\/09\/grocery-list-1670408_640.jpg?fit=640%2C426&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2019\/09\/grocery-list-1670408_640.jpg?fit=640%2C426&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":3340,"url":"https:\/\/www.mymiller.name\/wordpress\/lambda_stream\/java-collectors\/","url_meta":{"origin":1788,"position":2},"title":"Java Collectors","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"Java 8 introduced the Collectors class, which provides a variety of collectors for use with streams. Collectors are used to accumulate the elements of a stream into a single result, such as a list or a map. In this article, we'll take a closer look at the Collectors class and\u2026","rel":"","context":"In &quot;Lambda's and Streams&quot;","block_context":{"text":"Lambda's and Streams","link":"https:\/\/www.mymiller.name\/wordpress\/category\/lambda_stream\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/data-g271bb8554_640.jpg?fit=640%2C285&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/data-g271bb8554_640.jpg?fit=640%2C285&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/06\/data-g271bb8554_640.jpg?fit=640%2C285&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":2988,"url":"https:\/\/www.mymiller.name\/wordpress\/angular\/angular-structural-directive-onsize\/","url_meta":{"origin":1788,"position":3},"title":"Angular Structural Directive onSize","author":"Jeffery Miller","date":"January 18, 2021","format":false,"excerpt":"Creating an angular structural directive to operate on screen resolution.","rel":"","context":"In &quot;Angular&quot;","block_context":{"text":"Angular","link":"https:\/\/www.mymiller.name\/wordpress\/category\/angular\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2021\/01\/plan-2092499_640.jpg?fit=640%2C435&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2021\/01\/plan-2092499_640.jpg?fit=640%2C435&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2021\/01\/plan-2092499_640.jpg?fit=640%2C435&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":1936,"url":"https:\/\/www.mymiller.name\/wordpress\/java\/advancedstring-java-lang-string-steroids\/","url_meta":{"origin":1788,"position":4},"title":"AdvancedString &#8211; java.lang.String on steroids!","author":"Jeffery Miller","date":"April 20, 2026","format":false,"excerpt":"Every need an additional method on the String Class? \u00a0Well I have and it would have made life much easier. \u00a0Unfortunately you can't subclass String as it is Final. \u00a0So what are you to do? \u00a0Well you wrap the String class. \u00a0I have created AdvancedString, which contains additional functionality and\u2026","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java\/"},"img":{"alt_text":"AdvancedString","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2016\/09\/rope-1379561_640.jpg?fit=640%2C425&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2016\/09\/rope-1379561_640.jpg?fit=640%2C425&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2016\/09\/rope-1379561_640.jpg?fit=640%2C425&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":3421,"url":"https:\/\/www.mymiller.name\/wordpress\/java_extra\/convert-csv-to-json-and-json-to-csv-with-csvjsonconverter\/","url_meta":{"origin":1788,"position":5},"title":"Convert CSV to JSON and JSON to CSV with CSVJSONConverter","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"In today's data-driven world, it's common to work with different data formats like CSV (Comma-Separated Values) and JSON (JavaScript Object Notation). Converting data between these formats is a common task in data processing and integration workflows. In this article, we'll explore how to use the CSVJSONConverter class, a versatile utility\u2026","rel":"","context":"In &quot;Java Extras&quot;","block_context":{"text":"Java Extras","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java_extra\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/07\/computer-g610baba23_640.jpg?fit=640%2C360&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/07\/computer-g610baba23_640.jpg?fit=640%2C360&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/07\/computer-g610baba23_640.jpg?fit=640%2C360&ssl=1&resize=525%2C300 1.5x"},"classes":[]}],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/1788","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/comments?post=1788"}],"version-history":[{"count":4,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/1788\/revisions"}],"predecessor-version":[{"id":2498,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/1788\/revisions\/2498"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media\/1790"}],"wp:attachment":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media?parent=1788"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/categories?post=1788"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/tags?post=1788"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/series?post=1788"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}