{"id":6144,"date":"2025-05-29T17:52:50","date_gmt":"2025-05-29T12:22:50","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=6144"},"modified":"2025-05-29T18:26:11","modified_gmt":"2025-05-29T12:56:11","slug":"max-consecutive-ones","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/max-consecutive-ones\/","title":{"rendered":"Max Consecutive Ones"},"content":{"rendered":"\n<!-- Prism.js CSS and JS -->\n<link href=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/themes\/prism-tomorrow.css\" rel=\"stylesheet\" \/>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/prism.js\"><\/script>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/plugins\/autoloader\/prism-autoloader.min.js\"><\/script>\n\n<style>\n.wp_blog_code-tabs-container {\n    font-family: \"Segoe UI\", sans-serif !important;\n    max-width: 900px !important;\n    margin: 2rem auto !important;\n    border: 1px solid #ddd !important;\n    border-radius: 8px !important;\n    overflow: hidden !important;\n    background-color: white !important;\n}\n\n.wp_blog_code-tabs-header {\n    background: #f7f7f7 !important;\n    display: flex !important;\n    border-bottom: 1px solid #ddd !important;\n}\n\n.wp_blog_code-tab-button {\n    flex: 1 !important;\n    padding: 10px 15px !important;\n    border: none !important;\n    background: transparent !important;\n    cursor: pointer !important;\n    font-weight: bold !important;\n    transition: background 0.2s !important;\n    color: #242B33 !important;\n}\n\n.wp_blog_code-tab-button.active {\n    background: white !important;\n    border-bottom: 3px solid #0073aa !important;\n}\n\n.wp_blog_code-tab-content {\n    display: none !important;\n    padding: 20px !important;\n    background: #242B33 !important;\n}\n\n.wp_blog_code-tab-content > pre {\n    background: #242B33 !important;\n}\n\n.wp_blog_code-tab-content.active {\n    display: block !important;\n}\n\n.wp_blog_code-tab-content pre {\n    margin: 0 !important;\n    overflow-x: auto !important;\n}\n\n.wp_blog_explanation {\n    max-width: 900px !important;\n    margin: 2rem auto !important;\n    font-family: \"Segoe UI\", sans-serif !important;\n    line-height: 1.6 !important;\n    background: white !important;\n    color: black !important;\n    padding: 1rem !important;\n    border-radius: 8px !important;\n}\n\n.wp_blog_explanation h2 {\n    color: #0073aa !important;\n    font-size: 1.5rem !important;\n    margin-bottom: 0.5rem !important;\n}\n\n.wp_blog_explanation code {\n    background: #f1f1f1 !important;\n    padding: 2px 6px !important;\n    border-radius: 4px !important;\n    font-family: monospace !important;\n}\n\n.wp_blog_explanation h1,\n.wp_blog_explanation h2,\n.wp_blog_explanation h3,\n.wp_blog_explanation h4,\n.wp_blog_explanation h5,\n.wp_blog_explanation h6,\n.wp_blog_explanation p {\n    margin-top: 10px !important;\n    margin-bottom: 10px !important;\n}\n<\/style>\n\n<!-- \u2705 START: Blog Explanation -->\n<div class=\"wp_blog_explanation\">\n    <p>Given a binary array <code>nums<\/code>, return the maximum number of consecutive 1&#8217;s in the array.<\/p>\n    <h2>Examples<\/h2>\n    <h3>Example 1:<\/h3>\n    <pre><code>Input: nums = [1,1,0,1,1,1]\n    Output: 3\n    Explanation: The first two digits or the last three digits are consecutive 1s. \nThe maximum number of consecutive 1s is 3.\n    <\/code><\/pre>\n    \n    <h3>Example 2:<\/h3>\n    <pre><code>Input: nums = [1,0,1,1,0,1]\n    Output: 2\n    <\/code><\/pre>\n    \n    <h3>Constraints:<\/h3>\n    <ul>\n      <li>1 &le; nums.length &le; 10<sup>5<\/sup><\/li>\n      <li>nums[i] is either 0 or 1.<\/li>\n    <\/ul>\n    \n    <h2>Optimal Approach &#8211; Single Pass<\/h2>\n    <p>Initialize two variables:<\/p>\n    <ul>\n      <li><code>currentCount<\/code> &rarr; to count current streak of 1s<\/li>\n      <li><code>maxCount<\/code> &rarr; to keep track of the maximum streak seen so far<\/li>\n    <\/ul>\n    \n    <p>Traverse the array:<\/p>\n    <ul>\n      <li>If <code>nums[i] == 1<\/code>, increment <code>currentCount<\/code><\/li>\n      <li>If <code>nums[i] == 0<\/code>, compare <code>currentCount<\/code> with <code>maxCount<\/code>, update <code>maxCount<\/code>, then reset <code>currentCount<\/code> to 0<\/li>\n    <\/ul>\n    \n    <p>After the loop, return the maximum of <code>maxCount<\/code> and <code>currentCount<\/code> (to handle case where array ends in 1s)<\/p>\n    \n    <h2>Dry Run<\/h2>\n    <pre><code>Input: nums = [1, 1, 0, 1, 1, 1]\n    \n    i = 0 \u2192 nums[i] = 1 \u2192 currentCount = 1\n    i = 1 \u2192 nums[i] = 1 \u2192 currentCount = 2\n    i = 2 \u2192 nums[i] = 0 \u2192 maxCount = 2, currentCount = 0\n    i = 3 \u2192 nums[i] = 1 \u2192 currentCount = 1\n    i = 4 \u2192 nums[i] = 1 \u2192 currentCount = 2\n    i = 5 \u2192 nums[i] = 1 \u2192 currentCount = 3\n    \n    Final return: max(2, 3) = 3\n    <\/code><\/pre>\n    \n    <h2>Time and Space Complexity<\/h2>\n    <ul>\n      <li><strong>Time Complexity:<\/strong> O(n) &rarr; One pass through the array of n elements<\/li>\n      <li><strong>Space Complexity:<\/strong> O(1) &rarr; No extra space used beyond a few variables<\/li>\n    <\/ul>\n    \n    \n<\/div>\n<!-- \u2705 END: Blog Explanation -->\n\n<!-- \u2705 START: Code Tabs -->\n<div class=\"wp_blog_code-tabs-container\">\n    <div class=\"wp_blog_code-tabs-header\">\n      <button class=\"wp_blog_code-tab-button active\" data-lang=\"js\">JavaScript<\/button>\n      <button class=\"wp_blog_code-tab-button\" data-lang=\"cpp\">C++<\/button>\n      <button class=\"wp_blog_code-tab-button\" data-lang=\"c\">C<\/button>\n      <button class=\"wp_blog_code-tab-button\" data-lang=\"java\">Java<\/button>\n      <button class=\"wp_blog_code-tab-button\" data-lang=\"py\">Python<\/button>\n    <\/div>\n  \n    <div class=\"wp_blog_code-tab-content active\" data-lang=\"js\">\n      <pre><code class=\"language-javascript\">\n  var findMaxConsecutiveOnes = function(nums) {\n    let currentCount = 0;\n    let maxCount = 0;\n    for (let i = 0; i < nums.length; i++) {\n      if (nums[i] == 1) {\n        currentCount++;\n      } else {\n        maxCount = Math.max(currentCount, maxCount);\n        currentCount = 0;\n      }\n    }\n    return Math.max(maxCount, currentCount);\n  };\n      <\/code><\/pre>\n    <\/div>\n  \n    <div class=\"wp_blog_code-tab-content\" data-lang=\"cpp\">\n      <pre><code class=\"language-cpp\">\n  class Solution {\n    public:\n      int findMaxConsecutiveOnes(vector<int>& nums) {\n        int currentCount = 0, maxCount = 0;\n        for (int num : nums) {\n          if (num == 1) {\n            currentCount++;\n          } else {\n            maxCount = max(maxCount, currentCount);\n            currentCount = 0;\n          }\n        }\n        return max(maxCount, currentCount);\n      }\n  };\n      <\/code><\/pre>\n    <\/div>\n  \n    <div class=\"wp_blog_code-tab-content\" data-lang=\"c\">\n      <pre><code class=\"language-c\">\n  int findMaxConsecutiveOnes(int* nums, int numsSize) {\n    int currentCount = 0, maxCount = 0;\n    for (int i = 0; i < numsSize; i++) {\n      if (nums[i] == 1) {\n        currentCount++;\n      } else {\n        if (currentCount > maxCount)\n          maxCount = currentCount;\n        currentCount = 0;\n      }\n    }\n    return currentCount > maxCount ? currentCount : maxCount;\n  }\n      <\/code><\/pre>\n    <\/div>\n  \n    <div class=\"wp_blog_code-tab-content\" data-lang=\"java\">\n      <pre><code class=\"language-java\">\n  class Solution {\n    public int findMaxConsecutiveOnes(int[] nums) {\n      int currentCount = 0;\n      int maxCount = 0;\n      for (int num : nums) {\n        if (num == 1) {\n          currentCount++;\n        } else {\n          maxCount = Math.max(maxCount, currentCount);\n          currentCount = 0;\n        }\n      }\n      return Math.max(maxCount, currentCount);\n    }\n  }\n      <\/code><\/pre>\n    <\/div>\n  \n    <div class=\"wp_blog_code-tab-content\" data-lang=\"py\">\n      <pre><code class=\"language-python\">\n  class Solution:\n    def findMaxConsecutiveOnes(self, nums):\n      currentCount = 0\n      maxCount = 0\n      for num in nums:\n        if num == 1:\n          currentCount += 1\n        else:\n          maxCount = max(maxCount, currentCount)\n          currentCount = 0\n      return max(maxCount, currentCount)\n      <\/code><\/pre>\n    <\/div>\n  <\/div>\n  \n  \n<!-- \u2705 END: Code Tabs -->\n\n\n<!-- \u2705 JS Tab Switch Logic -->\n<script>\ndocument.addEventListener('DOMContentLoaded', function () {\n    const buttons = document.querySelectorAll('.wp_blog_code-tab-button');\n    const contents = document.querySelectorAll('.wp_blog_code-tab-content');\n    buttons.forEach(button => {\n        button.addEventListener('click', () => {\n            const lang = button.getAttribute('data-lang');\n            buttons.forEach(btn => btn.classList.remove('active'));\n            button.classList.add('active');\n            contents.forEach(content => {\n                content.classList.toggle('active', content.getAttribute('data-lang') === lang);\n            });\n        });\n    });\n});\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>Given a binary array nums, return the maximum number of consecutive 1&#8217;s in the array. Examples Example 1: Input: nums = [1,1,0,1,1,1] Output: 3 Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3. Example 2: Input: nums = [1,0,1,1,0,1] Output: 2 Constraints: 1<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[811,810],"tags":[],"class_list":["post-6144","post","type-post","status-publish","format-standard","category-data-structures-and-algorithms","category-dsa"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6144","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=6144"}],"version-history":[{"count":2,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6144\/revisions"}],"predecessor-version":[{"id":6164,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6144\/revisions\/6164"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=6144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=6144"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=6144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}