Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 254b7d2

Browse files
Merge pull request knaxus#13 from knaxus/problems
update: new problem added
2 parents cb7c1c3 + ec5d2f6 commit 254b7d2

File tree

1 file changed

+24
-0
lines changed
  • src/_Problems_/max-consecutive-1s

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Find the length of maximum consecutive 1s in an array of o & 1
3+
* Input: [1, 0, 1, 1, 0] O/P - 2
4+
* Input: [0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1] O/P - 5
5+
*
6+
* Solved in O(n)
7+
*/
8+
9+
function findMaxConsecutive1s(arr) {
10+
let count = 0;
11+
let max = 0;
12+
const length = arr.length;
13+
14+
for (let i = 0; i < length; i += 1) {
15+
if (arr[i] === 1) {
16+
count += 1;
17+
} else if (arr[i] === 0) {
18+
if (count > max) max = count;
19+
count = 0;
20+
}
21+
}
22+
if (count > max) max = count;
23+
return max;
24+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /