## Max Consecutive Ones in a given array(Leetcode)

max consecutive ones ii
485. max consecutive ones
consecutive ones meaning
contiguous array - leetcode
create target array in the given order leetcode
leetcode solutions
find longest sequence of 1s in binary representation with one flip
leetcode probability questions

I'm trying to find the maximum number of consecutive 1s in the array with O(n) time complexity and not utilizing more than one unit of space.(Other than the variables used for iterating through the array). Here's the code I've come up with, but Leetcode submission shows it's exceeding the time limit. I'm struggling to understand how is it exceeding the time limit because I iterate each element of the array just once and run the logic in the same iteration.

```public class MaxConsecutive {

public static void main(String[] args) {

System.out.println(findMaxConsecutiveOnes(new int[] {0,1,0,0,0,0,1,1,1}));
}

public static int findMaxConsecutiveOnes(int[] nums) {

int i=0, j=0, maxLen=0;

while(j<nums.length) {

if(nums[j] == 1) {

i=j;
while (true) {
while (j < nums.length && nums[j] == 1) {
j++;
}

if( j - i > maxLen) {
maxLen = j - i;
j++;
break;
}
}
}
else
j++;
}
return maxLen;
}
}
```
```while (true) {
// ....

if( j - i > maxLen) {
maxLen = j - i;
j++;
break;
}

}
```

In this part, you are not breaking the infinity loop unless the current length is maximum than `maxLen`. This loop can be infinite and the program will give you TLE.

I removed the loop and did some modification here:

```public static int findMaxConsecutiveOnes(int[] nums) {

int i=0, j=0, maxLen=0;

while(j<nums.length) {

if(nums[j] == 1) {

i=j;
while (j + 1 < nums.length && nums[j + 1] == 1) {
j++;
}

if( j - i + 1 > maxLen) {
maxLen = j - i + 1;
}
}

j++;
}

return maxLen;

}
```

Max Consecutive Ones III, Given a binary array and an integer k, find the position of zeroes flipping which creates maximum number of consecutive 1s in array. Example 1: Input: arr = [1, 0,​  I'm trying to find the maximum number of consecutive 1s in the array with O(n) time complexity and not utilizing more than one unit of space.(Other than the variables used for iterating through the array). Here's the code I've come up with, but Leetcode submission shows it's exceeding the time limit.

Three `while` loops. In my opinion your code is too complex. You just need only one loop.

Some simpler solution:

```public static int findMaxConsecutiveOnes(int[] nums) {
int maxCount = 0;
int currentCount = 0;
for (int number : nums) {
if (number == 1) {
currentCount++;
} else {
currentCount = 0;
}
maxCount = Math.max(maxCount, currentCount);
}
return maxCount;
}
```

Amazon, Given a binary array, find the maximum number of consecutive 1s in this array. is a leetcode problem : https://leetcode.com/problems/max-consecutive-ones/. The Max Consecutive Ones can be solved using sliding window technique, steps to solve this problem are as follows :-Traverse the input array from start to end, at each iteration check if the current element is 1 or 0. If the current element is 1, then increase the size of the sliding window by 1 (increase the count variable by 1).

One loop enough. Calling Math.max at min.

```public static int findMaxConsecutiveOnes(int[] nums) {

int reset = 0, run=0, maxLen=0;

for (int i = 0 ; i < nums.length; i++) {

if ( nums[i] == 1 ) {
run++;
} else {
maxLen = Math.max(maxLen, run);
run = 0;
}
}
maxLen = Math.max(maxLen, run);

return maxLen;
}
```

Max Consecutive Ones - SAURAV AGGARWAL, Given a binary array, find the maximum number of consecutive 1s in this array.. “​485. Max Consecutive Ones” is published by Xu LIANG in LeetCode Cracker. Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [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. Note: The input array will only contain 0 and 1.

485. Max Consecutive Ones - LeetCode Cracker, Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1,1,0,1,1,1]. Output: 3. Explanation: The first two digits or the  Given a binary array, find the maximum number of consecutive 1s in this array. Input: [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. Explanation: The first two digits or the last three digits are consecutive 1s.

LeetCode/Max Consecutive Ones.java at master · cherryljr , Patreon - https://www.patreon.com/nick_white?al Problem URL - https://​leetcode.com Duration: 6:32 Posted: Sep 11, 2019 Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [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.

LeetCode Max Consecutive Ones III Solution Explained, Given a binary array, find the maximum number of consecutive 1s in this array. Example 1:. Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [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. Note: The input array will only contain 0 and 1. The length

• Hmm, I can see three `while` loops