-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlongest_binary_one.cpp
More file actions
58 lines (46 loc) · 1006 Bytes
/
longest_binary_one.cpp
File metadata and controls
58 lines (46 loc) · 1006 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//
// Created by Mayank Parasar on 2020-02-26.
//
/*
* Return the longest run of 1s for a given integer n's binary representation.
* Example:
Input: 242
Output: 4
242 in binary is 0b11110010, so the longest run of 1 is 4.
* */
#include <iostream>
#include <deque>
using namespace std;
deque<int> return_binary(int num) {
deque<int> binary;
while( num > 0) {
binary.push_front(num % 2);
num = num / 2;
}
return binary;
}
int longest_run(deque<int> const &binary) {
int cntr = 0;
int max_cntr = 0;
for(auto i : binary) {
if( i == 1) {
cntr++;
if(cntr > max_cntr)
max_cntr = cntr;
}
else {
cntr = 0;
}
}
return max_cntr;
}
int main() {
int num = 242;
deque<int> binary = return_binary(num);
for(auto i : binary)
cout << i;
cout << endl;
// cont the longest run of 1s in this binary
cout << longest_run(binary);
return 0;
}