-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmax_consecutive_ones.cpp
58 lines (46 loc) · 1017 Bytes
/
max_consecutive_ones.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
using namespace std;
/*
Given a binary array, count max consecutive ones
ip: arr[] = {0, 1, 1, 0, 1, 0}
op: 2 (index 1 and 2)
ip: arr[] = {1, 1, 1, 1, 1, 1}
op: 6 (all indices)
ip: arr[] = {0, 0, 0, 0}
op: 0
ip: arr[] = {1, 0, 1, 1, 1, 1, 0, 1, 1}
op: 4 (index: 2-5)
Approach: Traverse every element, count consecutive ones begining with the element,
Whenver the element is 0, reset the current count
if el is 1, increment current count and compare it with res
if(curr_count > res) => update result with curr_count
Time: O(n)
Space: O(1)
*/
int countConsecutiveOnes(int arr[], int n)
{
int res = 0;
for (int i = 0; i < n; i++)
{
int count = 0;
while(arr[i] == 1)
{
++count;
++i;
}
res = max(res, count);
}
return res;
}
/*
Dry run:
arr[] = {0, 1, 1, 0, 1, 1, 1}
initially, res = 0, curr = 0
i=0, curr=0
i=1, curr=1, res=1
i=2, curr=2, res=2
i=3, curr=0,
i=4, curr=1
i=5, curr=2
i=6, curr=3, res=3
*/