Skip to content

Commit c8b666e

Browse files
authored
Add files via upload
1 parent 6e01d61 commit c8b666e

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Arrays/Binary_Numbers.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'''
2+
Aim: Given a base-10 integer, n, convert it to binary (base-2). Then find and print the
3+
base-10 integer denoting the maximum number of consecutive 1's in n's binary representation.
4+
5+
'''
6+
7+
n = int(input()) # getting input
8+
remainder = []
9+
while n > 0:
10+
rm = n % 2
11+
n = n//2 # whenever decimal to binary conversion is done, the number is repeatedly divided by 2 until it could not be divided any further
12+
remainder.append(rm) # appending the remainder that is obtained each time in a list
13+
count,result = 0,0
14+
for i in range(0,len(remainder)):
15+
if remainder[i] == 0:
16+
count = 0 # as soon as '0' is encountered, the counter is set to 0
17+
else:
18+
count += 1 # the counter increases every time consecutive ones are encountered
19+
result = max(result,count) # maximum streak of 1's is chosen
20+
print(result)
21+
22+
'''
23+
24+
Sample Test Case:
25+
Input:
26+
13
27+
Output:
28+
2
29+
Explaination:
30+
The binary representation of 13 is 1101, so the maximum number of consecutive 1's is 2.
31+
32+
'''

0 commit comments

Comments
 (0)