304 North Cardinal St.
Dorchester Center, MA 02124

# Max Consecutive Ones LeetCode Solution

## Problem – Max Consecutive Ones LeetCode Solution

Given a binary array `nums`, return the maximum number of consecutive `1`‘s in the array.

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 <= nums.length <= 105`
• `nums[i]` is either `0` or `1`.

### Max Consecutive Ones LeetCode Solution in Java

``````    public int findMaxConsecutiveOnes(int[] nums) {
int maxHere = 0, max = 0;
for (int n : nums)
max = Math.max(max, maxHere = n == 0 ? 0 : maxHere + 1);
return max;
}
``````

### Max Consecutive Ones LeetCode Solution in Python

``````class Solution(object):
def findMaxConsecutiveOnes(self, nums):
cnt = 0
ans = 0
for num in nums:
if num == 1:
cnt += 1
ans = max(ans, cnt)
else:
cnt = 0
return ans
``````

### Max Consecutive Ones LeetCode Solution in C++

``````    int findMaxConsecutiveOnes(vector<int>& nums) {
int max_cnt = 0, cnt = 0;
for (auto n : nums) {
if (n == 1) max_cnt = max(++cnt, max_cnt);
else cnt = 0;
}
return max_cnt;
}
``````
##### Max Consecutive Ones LeetCode Solution Review:

In our experience, we suggest you solve this Max Consecutive Ones LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Max Consecutive Ones LeetCode Solution

Find on LeetCode

##### Conclusion:

I hope this Max Consecutive Ones LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions