<p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>&#39;s in the array</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,1,0,1,1,1]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,0,1,1,0,1]
<strong>Output:</strong> 2
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>
</ul>

class Solution {
public:
    int findMaxConsecutiveOnes(vector<int>& nums) {
        vector<int> places;  // To store positions of zeros
        vector<int> ones;    // To store counts of consecutive ones

        // Step 1: Find the positions of all zeros
        for (int i = 0; i < nums.size(); i++) {
            if (nums[i] == 0) {
                places.push_back(i);
            }
        }

        // Handle edge cases
        if (places.empty()) {  // No zeros in the array
            return nums.size();  // The entire array is made up of ones
        }

        // Step 2: Calculate the lengths of consecutive ones between zeros
        for (int i = 0; i < places.size() - 1; i++) {
            ones.push_back(places[i + 1] - places[i] - 1);
        }

        // Add the number of ones before the first zero and after the last zero
        ones.push_back(places[0]);  // Before the first zero
        ones.push_back(nums.size() - places.back() - 1);  // After the last zero

        // Step 3: Find and return the maximum count of consecutive ones
        return *max_element(ones.begin(), ones.end());
    }
};

40500