Given a binary array nums, return the maximum number of consecutive 1s in the array.
A binary array is an array that contains only 0s and 1s.
Input: nums = [1, 1, 0, 0, 1, 1, 1, 0]
Output: 3
Explanation: The maximum consecutive 1s are present from index 4 to index 6, amounting to 3 1s
Input: nums = [0, 0, 0, 0, 0, 0, 0, 0]
Output: 0
Explanation: No 1s are present in nums, thus we return 0
Input: nums = [1, 0, 1, 1, 1, 0, 1, 1, 1]
To find the number of maximum consecutive 1s, the idea is to count the number of 1s each time we encounter them and update the maximum number of 1s. On encountering any 0, reset the count to 0 again so as to count the next consecutive 1s.
count
and max_count
to 0. Traverse the array and if the current element is 1, increment the count
by 1.max_count
if count
is greater than max_count
.count
variable to 0 and at last return max_count
.
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
/* Initialize count and max_count
to track current and maximum consecutive 1s */
int cnt = 0;
int maxi = 0;
// Traverse the vector
for (int i = 0; i < nums.size(); i++) {
/* If the current element is 1,
increment the count*/
if (nums[i] == 1) {
cnt++;
/* Update maxi if current
count is greater than maxi*/
maxi = max(maxi, cnt);
} else {
/* If the current element
is 0, reset the count*/
cnt = 0;
}
}
// Return maximum count of consecutive 1s
return maxi;
}
};
int main() {
vector nums = {1, 1, 0, 1, 1, 1};
// Create an instance of the Solution class
Solution sol;
int ans = sol.findMaxConsecutiveOnes(nums);
cout << "The maximum consecutive 1's are " << ans << endl;
return 0;
}
import java.util.*;
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
/* Initialize count and max_count
to track current and maximum consecutive 1s */
int cnt = 0;
int maxi = 0;
// Traverse the array
for (int i = 0; i < nums.length; i++) {
// If the current element is 1, increment the count
if (nums[i] == 1) {
cnt++;
// Update maxi if current count is greater than maxi
maxi = Math.max(maxi, cnt);
} else {
// If the current element is 0, reset the count
cnt = 0;
}
}
// Return the maximum count of consecutive 1s
return maxi;
}
public static void main(String[] args) {
int[] nums = {1, 1, 0, 1, 1, 1};
// Create an instance of the Solution class
Solution sol = new Solution();
// Find and print the maximum consecutive 1s
int ans = sol.findMaxConsecutiveOnes(nums);
System.out.println("The maximum consecutive 1's are " + ans);
}
}
from typing import List
class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
''' Initialize count and max_count
to track current and maximum consecutive 1s '''
cnt = 0
maxi = 0
# Traverse the array
for num in nums:
# If the current element is 1, increment the count
if num == 1:
cnt += 1
# Update maxi if current count is greater than maxi
maxi = max(maxi, cnt)
else:
# If the current element is 0, reset the count
cnt = 0
# Return the maximum count of consecutive 1s
return maxi
# Main function
if __name__ == "__main__":
nums = [1, 1, 0, 1, 1, 1]
# Create an instance of the Solution class
sol = Solution()
# Find and print the maximum consecutive 1s
ans = sol.findMaxConsecutiveOnes(nums)
print("The maximum consecutive 1's are", ans)
class Solution {
findMaxConsecutiveOnes(nums) {
/* Initialize count and max_count
to track current and maximum consecutive 1s */
let cnt = 0;
let maxi = 0;
// Traverse the array
for (let i = 0; i < nums.length; i++) {
/*If the current element is
1, increment the count*/
if (nums[i] == 1) {
cnt++;
/*Update maxi if current
count is greater than maxi*/
maxi = Math.max(maxi, cnt);
} else {
// If the current element is 0, reset the count
cnt = 0;
}
}
// Return the maximum count of consecutive 1s
return maxi;
}
}
const nums = [1, 1, 0, 1, 1, 1];
// Create an instance of the Solution class
const sol = new Solution();
// Find and print the maximum consecutive 1s
const ans = sol.findMaxConsecutiveOnes(nums);
console.log("The maximum consecutive 1's are " + ans);
Q: What is the time complexity, and can it be optimized further?
A: The time complexity of the solution is O(n), where n is the length of the array. This is optimal as each element must be inspected at least once to determine the number of consecutive 1s. It cannot be optimized further without additional assumptions about the input.
Q: How does the algorithm handle arrays with alternating 1s and 0s?
A: The algorithm correctly identifies the length of each segment of consecutive 1s and tracks the maximum. Alternating 1s and 0s do not affect the algorithm's correctness.
Q: How would you modify the algorithm to return the indices of the maximum segment of consecutive 1s?
A: Track the starting index of each segment of consecutive 1s. Update the start and end indices whenever a new maximum is found. Example: Input: nums = [1, 1, 0, 1, 1, 1, 0] Output: Indices (3, 5)
Q: How would you handle a streaming input (data arriving one bit at a time)?
A: For streaming data, maintain a running count of consecutive 1s and update the maximum whenever a 0 is encountered. Use a single variable to track the current count. Update the maximum count dynamically without storing the full array. Example: Stream: [1, 1, 0, 1, 1, 1, 0] Output after processing: 3