{"id":475,"date":"2025-06-29T20:41:32","date_gmt":"2025-06-29T15:11:32","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=475"},"modified":"2025-06-29T20:41:33","modified_gmt":"2025-06-29T15:11:33","slug":"array-leaders","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/array-leaders\/","title":{"rendered":"Array Leaders | Optimal Solution in Python"},"content":{"rendered":"\n<p>Given an array <code>arr[]<\/code> of size <code>n<\/code>, an element is called an <strong>Array Leaders<\/strong> if it is <strong>greater than or equal to every element on its right<\/strong>.<br>Your task is to return <strong>all leaders in their original left-to-right order<\/strong>.<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/www.geeksforgeeks.org\/problems\/leaders-in-an-array-1587115620\/1\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a><\/strong>] to begin with.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"0-quick-examples\">Quick Examples<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Input array<\/th><th>Leaders (output)<\/th><th>Why?<\/th><\/tr><\/thead><tbody><tr><td><code>[16, 17, 4, 3, 5, 2]<\/code><\/td><td><code>[17, 5, 2]<\/code><\/td><td><code>17<\/code> \u2265 <code>4\u20262<\/code>, <code>5<\/code> \u2265 <code>2<\/code>, last element <code>2<\/code> is always a leader<\/td><\/tr><tr><td><code>[7, 10, 4, 10, 6, 5, 2]<\/code><\/td><td><code>[10, 10, 6, 5, 2]<\/code><\/td><td>Scan from right: <code>2<\/code> (leader) \u2192 <code>5<\/code> \u2265 <code>2<\/code> \u2192 <code>6<\/code> \u2265 <code>5<\/code>\u2026<\/td><\/tr><tr><td><code>[1, 2, 3]<\/code><\/td><td><code>[3]<\/code><\/td><td>Only the right-most element meets the condition<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The naive idea would be to check every element against everything to its right (<code>O(n\u00b2)<\/code>), but we can do better in one backward pass.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-optimal-solution-right-to-left-scan\">Optimal Solution (Right-to-Left Scan)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-intuition-and-approach\">Intuition and Approach<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Sweeping from the right<\/strong> keeps track of the <strong>maximum value seen so far<\/strong> (<code>maxi<\/code>).<\/li>\n\n\n\n<li>Every time the current element <code>arr[i]<\/code> is <strong>\u2265 <code>maxi<\/code><\/strong>, it means nothing to its right is larger, so <code>arr[i]<\/code> is a leader.<\/li>\n\n\n\n<li>Update <code>maxi<\/code> whenever a new leader is found.<\/li>\n\n\n\n<li>Collect leaders in a list while moving right \u2192 left, then <strong>reverse<\/strong> that list to restore left-to-right order.<\/li>\n<\/ul>\n\n\n\n<p>This achieves the goal in <strong>one linear pass<\/strong> with <strong>constant extra space<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-code-implementation\">Code Implementation<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    def leaders(self, arr):\n        n = len(arr)\n        maxi = float(&quot;-inf&quot;)   # holds the greatest element seen so far (to the right)\n        result = []            # stores leaders found during the scan (right \u279c left)\n\n        # Traverse array from right to left\n        for i in range(n - 1, -1, -1):\n            if arr[i] &gt;= maxi:\n                result.append(arr[i])      # current element is a leader\n                maxi = max(maxi, arr[i])   # update the running maximum\n\n        result.reverse()        # restore original ordering\n        return result\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">leaders<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">arr<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        n = <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(arr)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        maxi = <\/span><span style=\"color: #4EC9B0\">float<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;-inf&quot;<\/span><span style=\"color: #D4D4D4\">)   <\/span><span style=\"color: #6A9955\"># holds the greatest element seen so far (to the right)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        result = []            <\/span><span style=\"color: #6A9955\"># stores leaders found during the scan (right \u279c left)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Traverse array from right to left<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> i <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(n - <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, -<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, -<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> arr[i] &gt;= maxi:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                result.append(arr[i])      <\/span><span style=\"color: #6A9955\"># current element is a leader<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                maxi = <\/span><span style=\"color: #DCDCAA\">max<\/span><span style=\"color: #D4D4D4\">(maxi, arr[i])   <\/span><span style=\"color: #6A9955\"># update the running maximum<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        result.reverse()        <\/span><span style=\"color: #6A9955\"># restore original ordering<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> result<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-code-explanation\">Code Explanation<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Initialization<\/strong>\n<ul class=\"wp-block-list\">\n<li><code>maxi<\/code> starts at negative infinity so that the right-most element is always a leader.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Backward Scan<\/strong>\n<ul class=\"wp-block-list\">\n<li>At each position <code>i<\/code>, compare <code>arr[i]<\/code> with <code>maxi<\/code>.<\/li>\n\n\n\n<li>If <code>arr[i]<\/code> is <strong>\u2265 <code>maxi<\/code><\/strong>, no larger element exists to its right \u2192 it\u2019s a leader.<\/li>\n\n\n\n<li>Push it into <code>result<\/code> and update <code>maxi<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Result Reversal<\/strong>\n<ul class=\"wp-block-list\">\n<li>Leaders were gathered right-to-left; a single <code>reverse()<\/code> restores left-to-right order.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-dry-run-input-16-17-4-3-5-2-\">Dry Run (Input <code>[16, 17, 4, 3, 5, 2]<\/code>)<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>i (index)<\/th><th>arr[i]<\/th><th>maxi (before)<\/th><th>arr[i] \u2265 maxi?<\/th><th>result (after)<\/th><th>maxi (after)<\/th><\/tr><\/thead><tbody><tr><td>5<\/td><td>2<\/td><td>-\u221e<\/td><td>\u2705<\/td><td><code>[2]<\/code><\/td><td>2<\/td><\/tr><tr><td>4<\/td><td>5<\/td><td>2<\/td><td>\u2705<\/td><td><code>[2, 5]<\/code><\/td><td>5<\/td><\/tr><tr><td>3<\/td><td>3<\/td><td>5<\/td><td>\u274c<\/td><td><code>[2, 5]<\/code><\/td><td>5<\/td><\/tr><tr><td>2<\/td><td>4<\/td><td>5<\/td><td>\u274c<\/td><td><code>[2, 5]<\/code><\/td><td>5<\/td><\/tr><tr><td>1<\/td><td>17<\/td><td>5<\/td><td>\u2705<\/td><td><code>[2, 5, 17]<\/code><\/td><td>17<\/td><\/tr><tr><td>0<\/td><td>16<\/td><td>17<\/td><td>\u274c<\/td><td><code>[2, 5, 17]<\/code><\/td><td>17<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>After reversal \u2192 <code>[17, 5, 2]<\/code>, which matches the expected leaders.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-time-amp-space-complexity\">Time &amp; Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time:<\/strong> <code>O(n)<\/code> \u2013 single traversal of the array.<\/li>\n\n\n\n<li><strong>Space:<\/strong> <code>O(1)<\/code> extra (the <code>result<\/code> list holds at most <code>n<\/code> leaders, but no auxiliary arrays proportional to <code>n<\/code> are created aside from output).<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Precise: <code>O(n)<\/code> for scanning + <code>O(n)<\/code> in worst-case leaders list \u2192 effectively still <code>O(n)<\/code> total.<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7-conclusion\">Conclusion<\/h3>\n\n\n\n<p>The right-to-left scan leverages a running maximum to identify leaders in just one pass. This elegant approach highlights a common pattern in array problems: <strong>processing from the end can turn seemingly quadratic checks into linear-time solutions<\/strong>. Master it and similar \u201csuffix maximum\u201d tricks will feel natural in future challenges!<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/zero-to-hero-python-dsa\" target=\"_blank\" rel=\"noreferrer noopener\">Join our Advance DSA COURSE<\/a><\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>For any changes to the article, kindly email at <a href=\"mailto:code@codeanddebug.in\" target=\"_blank\" rel=\"noreferrer noopener\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\" target=\"_blank\" rel=\"noreferrer noopener\">+91-9712928220<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given an array arr[] of size n, an element is called an Array Leaders if it is greater than or equal to every element on its right.Your task is to return all leaders in their original left-to-right order. Here&#8217;s the [Problem Link] to begin with. Quick Examples Input array Leaders (output) Why? [16, 17, 4,<\/p>\n","protected":false},"author":1,"featured_media":476,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ub_ctt_via":"","footnotes":""},"categories":[3,6],"tags":[7,19],"class_list":{"0":"post-475","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-intermediate","9":"tag-array","10":"tag-medium"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/array-leaders-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/475","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=475"}],"version-history":[{"count":1,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/475\/revisions"}],"predecessor-version":[{"id":477,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/475\/revisions\/477"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/476"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=475"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=475"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=475"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}