Skip to content

Commit 6eb559f

Browse files
committed
Day 10
1 parent d419e82 commit 6eb559f

File tree

5 files changed

+126
-1
lines changed

5 files changed

+126
-1
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.codelite/
22
Competitive.workspace
3-
Makefile
3+
Makefile
4+
.vscode/

Day10-BinaryNumbers/C++/main.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int main()
6+
{
7+
int maxCount = 0;
8+
int count = 0;
9+
int n;
10+
cin >> n;
11+
cin.ignore(numeric_limits<streamsize>::max(), '\n');
12+
vector<int> binary;
13+
vector<int>::iterator itr;
14+
while (n)
15+
{
16+
binary.insert(binary.begin(), n % 2);
17+
n /= 2;
18+
}
19+
for (itr = binary.begin(); itr != binary.end(); itr++)
20+
{
21+
if (*itr == 1)
22+
{
23+
count += 1;
24+
}
25+
else
26+
count = 0;
27+
if (count > maxCount)
28+
{
29+
maxCount = count;
30+
}
31+
}
32+
cout << maxCount;
33+
return 0;
34+
}

Day10-BinaryNumbers/Java/main.java

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import java.util.*;
2+
3+
public class Solution {
4+
public static int mostConsecutiveOnes(int n) {
5+
// convert number->binary string->char array
6+
char[] binary = Integer.toBinaryString(n).toCharArray();
7+
8+
// count of current sequence of consecutive ones
9+
int tmpCount = 0;
10+
11+
// running maximum count of consecutive ones for any section to left of tmpCount
12+
int maxCount = 0;
13+
for(int i = 0; i < binary.length; i++){
14+
15+
// reset to 0 if we hit a '0' char
16+
if(binary[i] == '0') {
17+
18+
// set new max if needed
19+
if(tmpCount > maxCount){
20+
maxCount = tmpCount;
21+
}
22+
23+
tmpCount = 0;
24+
}
25+
else { // current location is a section of consecutive 1's
26+
// increment tmpCount
27+
tmpCount = tmpCount + 1;
28+
}
29+
}
30+
// conditional is necessary here in case the string does not end with a 0
31+
return (tmpCount > maxCount) ? tmpCount : maxCount;
32+
}
33+
34+
public static void main(String[] args) {
35+
Scanner scan = new Scanner(System.in);
36+
int n = scan.nextInt();
37+
scan.close();
38+
39+
System.out.println(mostConsecutiveOnes(n));
40+
}
41+
}

Day10-BinaryNumbers/README.md

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
## Objective
2+
Today, we're working with binary numbers.
3+
4+
## Task
5+
Given a base-10 integer, n, convert it to binary (base-2). Then find and print the base-10 integer denoting the maximum number of consecutive 1's in n's binary representation.
6+
7+
## Input Format
8+
```
9+
A single integer, n.
10+
```
11+
12+
## Constraints
13+
<code>
14+
1<=n<=10<sup>6</sup>
15+
</code>
16+
17+
## Output Format
18+
```
19+
Print a single base-10 integer denoting the maximum number of consecutive 1's in the binary representation of n.
20+
```
21+
22+
## Sample Input 1
23+
```
24+
5
25+
```
26+
27+
## Sample Output 1
28+
```
29+
1
30+
```
31+
32+
## Sample Input 2
33+
```
34+
13
35+
```
36+
37+
## Sample Output 2
38+
```
39+
2
40+
```
41+
42+
## Explanation
43+
44+
Sample Case 1:
45+
The binary representation of 5 is 101, so the maximum number of consecutive 1's is 1.
46+
47+
Sample Case 2:
48+
The binary representation of 13 is 1101, so the maximum number of consecutive 1's is 2.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ The repository covers all the basic topics of programming languages. Try a new c
1616
- [Day 7 - Arrays](https://github.com/geekayush/30daysofcode/tree/master/Day7-Arrays)
1717
- [Day 8 - Dictionaries&Maps](https://github.com/geekayush/30daysofcode/tree/master/Day8-Dictionaries&Maps)
1818
- [Day 9 - Recursion](https://github.com/geekayush/30daysofcode/tree/master/Day9-Recursion)
19+
- [Day 10 - Binary Numbers](https://github.com/geekayush/30daysofcode/tree/master/Day10-BinaryNumbers)
1920

2021
##### How to contribute?
2122

0 commit comments

Comments
 (0)