{"id":25,"date":"2025-05-08T19:16:42","date_gmt":"2025-05-08T13:46:42","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=25"},"modified":"2025-05-08T19:16:43","modified_gmt":"2025-05-08T13:46:43","slug":"maximum-consecutive-ones","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/maximum-consecutive-ones\/","title":{"rendered":"Maximum Consecutive Ones | Leetcode 485 | Explained with Images"},"content":{"rendered":"\n<p>This article will explain on how to solve Leetcode 485 &#8211; Maximum Consecutive Ones problem [<strong><a href=\"https:\/\/leetcode.com\/problems\/max-consecutive-ones\/description\/\" 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>]. There is only one optimal solution to solve this problem, so let&#8217;s get started.<\/p>\n\n\n<div style=\"max-width: -moz-fit-content; \" class=\"wp-block-ub-table-of-contents-block ub_table-of-contents\" id=\"ub_table-of-contents-32689b0b-df26-4407-9486-df2f0f052a5e\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"true\" data-initiallyhideonmobile=\"false\" data-initiallyshow=\"true\"><div class=\"ub_table-of-contents-header-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-header\" style=\"text-align: left; \">\n\t\t\t\t<div class=\"ub_table-of-contents-title\">Content<\/div>\n\t\t\t\t<div class=\"ub_table-of-contents-header-toggle\">\n\t\t\t<div class=\"ub_table-of-contents-toggle\" style=\"\">\n\t\t\t\u00a0[<a class=\"ub_table-of-contents-toggle-link\" href=\"#\" style=\"\">hide<\/a>]\n\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div><div class=\"ub_table-of-contents-extra-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-container ub_table-of-contents-1-column \">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/maximum-consecutive-ones\/#0-understanding-maximum-consecutive-ones-problem\" style=\"\">Understanding Maximum Consecutive Ones Problem<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/maximum-consecutive-ones\/#1-example-1\" style=\"\">Example 1<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/maximum-consecutive-ones\/#2-example-2\" style=\"\">Example 2<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/maximum-consecutive-ones\/#3-example-3\" style=\"\">Example 3<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/maximum-consecutive-ones\/#4-optimal-solution\" style=\"\">Optimal Solution<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/maximum-consecutive-ones\/#5-how-do-we-solve-\" style=\"\">How do we solve?<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/maximum-consecutive-ones\/#6-code-implementation\" style=\"\">Code Implementation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/maximum-consecutive-ones\/#7-step-by-step-explanation-\" style=\"\">Step-by-Step Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/maximum-consecutive-ones\/#8-dry-run-\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/maximum-consecutive-ones\/#9-potential-edge-cases-\" style=\"\">Potential Edge Cases<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/maximum-consecutive-ones\/#10-time-and-space-complexity-\" style=\"\">Time and Space Complexity<\/a><\/li><\/ul><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"0-understanding-maximum-consecutive-ones-problem\">Understanding Maximum Consecutive Ones Problem<\/h2>\n\n\n\n<p>You are given a binary array, an array consisting only of <code>0<\/code>s and <code>1<\/code>s. Your task is to <strong>find the maximum number of consecutive <code>1<\/code>s<\/strong> present in the array.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-example-1\">Example 1<\/h3>\n\n\n\n<p><strong>Input<\/strong>: <code>nums = [1, 1, 0, 1, 1, 1]<\/code><br>We can see that:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The first two <code>1<\/code>s are followed by a <code>0<\/code>.<\/li>\n\n\n\n<li>After that, there are <strong>three consecutive <code>1<\/code>s<\/strong>.<\/li>\n<\/ul>\n\n\n\n<p><strong>Output<\/strong>: <code>3<\/code><br>The maximum number of continuous <code>1<\/code>s is <strong>3<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-example-2\">Example 2<\/h3>\n\n\n\n<p><strong>Input<\/strong>: <code>nums = [1, 0, 1, 1, 0, 1]<\/code><br>In this case:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>There are no more than <strong>two 1&#8217;s in a row<\/strong> anywhere.<\/li>\n<\/ul>\n\n\n\n<p><strong>Output<\/strong>: <code>2<\/code><br>The best streak of <code>1<\/code>s is <code>[1, 1]<\/code>, so the result is <strong>2<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-example-3\">Example 3<\/h3>\n\n\n\n<p><strong>Input<\/strong>: <code>nums = [0, 0, 0, 0]<\/code><br>Since there are no <code>1<\/code>s at all, there&#8217;s no sequence to count.<\/p>\n\n\n\n<p><strong>Output<\/strong>: <code>0<\/code><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"4-optimal-solution\">Optimal Solution<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-how-do-we-solve-\"><strong>How do we solve?<\/strong><\/h3>\n\n\n\n<p><strong>Core Idea:<\/strong><\/p>\n\n\n\n<p>To efficiently find the maximum number of consecutive 1s, we can <strong>traverse the array once<\/strong>, keeping track of the consecutive 1s and updating the maximum count found so far. This approach ensures optimal time complexity without the need for nested iterations or additional data structures.<\/p>\n\n\n\n<p><strong>Visualization:<\/strong><\/p>\n\n\n\n<p>Imagine walking along a path represented by the array nums. As you move forward:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>When you encounter a <\/strong><strong>1<\/strong><strong>:<\/strong><strong><br><\/strong>You continue walking, counting how many 1s you&#8217;ve passed in a row.<\/li>\n\n\n\n<li><strong>When you encounter a 0:<br><\/strong>Your current streak of 1s ends, and you compare it to the maximum count you&#8217;ve recorded so far. If it&#8217;s longer, you update the maximum count. Then, you reset your current streak count and continue.<\/li>\n<\/ul>\n\n\n\n<p><strong>Analogy:<\/strong><\/p>\n\n\n\n<p>Think of the array as a series of hills and valleys:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>1<\/strong><strong> represents a hill:<\/strong><strong><br><\/strong>Each 1 you step on is like climbing a hill, adding to your current elevation (streak).<\/li>\n\n\n\n<li><strong>0<\/strong><strong> represents a valley:<\/strong><strong><br><\/strong>Stepping into a 0 is like descending into a valley, ending your current climb.<\/li>\n<\/ul>\n\n\n\n<p>The goal is to find the highest elevation (longest streak of 1s) you achieve during your walk.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-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 findMaxConsecutiveOnes(self, nums: List[int]) -&gt; int:\n        cnt = 0\n        maxi = 0\n        for i in range(len(nums)):\n            if nums[i] == 1:\n                cnt += 1\n            else:\n                maxi = max(maxi, cnt)\n                cnt = 0\n        return max(maxi, cnt)\" 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\">findMaxConsecutiveOnes<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">nums<\/span><span style=\"color: #D4D4D4\">: List[<\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">]) -&gt; <\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        cnt = <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        maxi = <\/span><span style=\"color: #B5CEA8\">0<\/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\">(<\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(nums)):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> nums[i] == <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                cnt += <\/span><span style=\"color: #B5CEA8\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">else<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                maxi = <\/span><span style=\"color: #DCDCAA\">max<\/span><span style=\"color: #D4D4D4\">(maxi, cnt)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                cnt = <\/span><span style=\"color: #B5CEA8\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">max<\/span><span style=\"color: #D4D4D4\">(maxi, cnt)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7-step-by-step-explanation-\"><strong>Step-by-Step Explanation<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Initialization:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>cnt (Counter):<br><\/strong>Keeps track of the current number of consecutive <em>1s <\/em>you&#8217;ve encountered.<\/li>\n\n\n\n<li><strong>maxi (Maximum):<br><\/strong>Records the maximum number of consecutive <em>1s <\/em>found so far.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Traversal:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>Iterate through each element in <\/strong><strong>nums<\/strong><strong>:<\/strong>\n<ul class=\"wp-block-list\">\n<li><strong>If the current element is <\/strong><strong>1<\/strong><strong>:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Increment <em>cnt <\/em>by 1, indicating you&#8217;re in a streak of 1s.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>If the current element is <\/strong><strong>0<\/strong><strong>:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Compare <em>cnt <\/em>with maxi. If <em>cnt <\/em>is greater, update <em>maxi<\/em>.<\/li>\n\n\n\n<li>Reset <em>cnt <\/em>to 0, as the streak has been interrupted by a 0.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Final Comparison:<\/strong>\n<ul class=\"wp-block-list\">\n<li>After traversing the entire array, there might be a streak of 1s that wasn&#8217;t followed by a 0. Therefore, perform one final comparison between <em>cnt <\/em>and <em>maxi <\/em>to ensure the longest streak is captured.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Result:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Return the value of <em>maxi<\/em>, which now holds the length of the longest consecutive 1s in the array.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8-dry-run-\"><strong>Dry Run<\/strong><\/h3>\n\n\n\n<p>Let\u2019s go through the code step by step, using images to illustrate the detailed dry run process.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/Maximum-Consecutive-Ones-part1-1024x576.png\" alt=\"Dry run of max consecutive ones optimal approach - step 1\" class=\"wp-image-27\" srcset=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/Maximum-Consecutive-Ones-part1-1024x576.png 1024w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/Maximum-Consecutive-Ones-part1-300x169.png 300w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/Maximum-Consecutive-Ones-part1-768x432.png 768w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/Maximum-Consecutive-Ones-part1-1536x864.png 1536w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/Maximum-Consecutive-Ones-part1-150x84.png 150w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/Maximum-Consecutive-Ones-part1-450x253.png 450w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/Maximum-Consecutive-Ones-part1-1200x675.png 1200w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/Maximum-Consecutive-Ones-part1.png 1920w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"353\" src=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/Maximum-Consecutive-Ones-part2-1024x353.png\" alt=\"Dry run of max consecutive ones optimal approach - step 2\" class=\"wp-image-26\" srcset=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/Maximum-Consecutive-Ones-part2-1024x353.png 1024w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/Maximum-Consecutive-Ones-part2-300x103.png 300w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/Maximum-Consecutive-Ones-part2-768x264.png 768w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/Maximum-Consecutive-Ones-part2-1536x529.png 1536w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/Maximum-Consecutive-Ones-part2-150x52.png 150w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/Maximum-Consecutive-Ones-part2-450x155.png 450w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/Maximum-Consecutive-Ones-part2-1200x413.png 1200w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/Maximum-Consecutive-Ones-part2.png 1920w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"9-potential-edge-cases-\"><strong>Potential Edge Cases<\/strong><\/h3>\n\n\n\n<p><strong>1. All Elements are <\/strong><strong>1<\/strong><strong>:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Input:<\/strong> nums = [1, 1, 1, 1, 1]<\/li>\n\n\n\n<li><strong>Output:<\/strong> 5<\/li>\n\n\n\n<li><strong>Explanation:<\/strong><strong><br><\/strong>The entire array is a streak of 1s. The function correctly identifies the maximum streak as 5.<\/li>\n<\/ul>\n\n\n\n<p><strong>2. No <\/strong><strong>1<\/strong><strong>s in the Array:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Input:<\/strong> nums = [0, 0, 0, 0]<\/li>\n\n\n\n<li><strong>Output:<\/strong> 0<\/li>\n\n\n\n<li><strong>Explanation:<\/strong><strong><br><\/strong>There are no 1s, so the maximum consecutive 1s count remains 0.<\/li>\n<\/ul>\n\n\n\n<p><strong>3. Single Element:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Input:<\/strong> nums = [1]<\/li>\n\n\n\n<li><strong>Output:<\/strong> 1<\/li>\n\n\n\n<li><strong>Input:<\/strong> nums = [0]<\/li>\n\n\n\n<li><strong>Output:<\/strong> 0<\/li>\n\n\n\n<li><strong>Explanation:<\/strong><strong><br><\/strong>The function handles single-element arrays by either counting the single 1 or recognizing the absence of 1s.<\/li>\n<\/ul>\n\n\n\n<p><strong>4. Alternating <\/strong><strong>1<\/strong><strong>s and <\/strong><strong>0<\/strong><strong>s:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Input:<\/strong> nums = [1, 0, 1, 0, 1, 0, 1]<\/li>\n\n\n\n<li><strong>Output:<\/strong> 1<\/li>\n\n\n\n<li><strong>Explanation:<\/strong><strong><br><\/strong>Each 1 is separated by a 0, so the maximum streak of 1s is 1.<\/li>\n<\/ul>\n\n\n\n<p><strong>5. Streak at the End:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Input:<\/strong> nums = [0, 1, 1, 1, 0, 0, 1, 1]<\/li>\n\n\n\n<li><strong>Output:<\/strong> 3<\/li>\n<\/ul>\n\n\n\n<p><strong>Explanation:<\/strong><strong><br><\/strong>The longest streak of 1s occurs in the middle of the array.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"10-time-and-space-complexity-\"><strong>Time and Space Complexity<\/strong><\/h3>\n\n\n\n<p><strong>Time Complexity:<\/strong> <strong><em>O(n)<\/em><\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The function traverses the array nums exactly once.<\/li>\n\n\n\n<li>All operations within the loop (if checks, increments, and max comparisons) are constant-time operations.<\/li>\n\n\n\n<li>Therefore, the time complexity scales linearly with the size of the input array.<\/li>\n<\/ul>\n\n\n\n<p><strong>Space Complexity:<\/strong> <strong><em>O(1)<\/em><\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The function uses a fixed amount of additional space regardless of the input size.<\/li>\n\n\n\n<li>Variables like cnt and maxi consume constant space.<\/li>\n\n\n\n<li>No additional data structures are used that scale with input size.<\/li>\n<\/ul>\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:\/\/www.codeanddebug.in\/course\/master-dsa-with-leetcode\" target=\"_blank\" rel=\"noreferrer noopener\">Join our free 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\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\">+91-9712928220<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article will explain on how to solve Leetcode 485 &#8211; Maximum Consecutive Ones problem [Problem Link]. There is only one optimal solution to solve this problem, so let&#8217;s get started. Understanding Maximum Consecutive Ones Problem You are given a binary array, an array consisting only of 0s and 1s. Your task is to find<\/p>\n","protected":false},"author":1,"featured_media":32,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[7,8],"class_list":{"0":"post-25","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-beginner","9":"tag-array","10":"tag-easy"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/max-consecutive-ones-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\/25","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=25"}],"version-history":[{"count":5,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/25\/revisions"}],"predecessor-version":[{"id":119,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/25\/revisions\/119"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/32"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=25"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=25"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=25"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}