-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay5.cpp
More file actions
40 lines (33 loc) · 687 Bytes
/
Day5.cpp
File metadata and controls
40 lines (33 loc) · 687 Bytes
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
/*
This problem was asked by Stripe.
Given an integer n, return the length of the longest consecutive run of 1s in its binary representation.
For example, given 156, you should return 3.
*/
#include <bits/stdc++.h>
using namespace std;
int consecutiveRun(int n)
{
int count = 0;
int maxCount = 0;
while (n > 0)
{
if (n & 1)
{
count++;
maxCount = max(count, maxCount);
}
else
{
count = 0;
}
n >>= 1;
}
return maxCount;
}
int main()
{
assert(consecutiveRun(156) == 3);
assert(consecutiveRun(0) == 0);
cout << "All tests passed." << endl;
return 0;
}