Today we will see the HackerRank Day 10 Solution in Python. The problem is named Binary Numbers which is part of 30 Days of code on HackerRank. Let’s get started!
Day 10: Binary Numbers Problem statement
We are given a base-10 integer(n), and we have to convert it to binary (base-2). And then we have to find and print the base-10 integer denoting the maximum number of consecutive 1’s in n’s binary representation.
Sample Case 1:
Input: 5
Output: 1
Explanation: The binary representation of 5 is 101, so the maximum number of consecutive 1’s is 1.
Sample Case 2:
Input: 13
Output: 1101
Explanation: The binary representation of 13 is 1101, so the maximum number of consecutive 1’s is 2.
You can solve the problem here.
HackerRank Day 10 Solution in Python
#!/bin/python3 import math import os import random import re import sys if __name__ == '__main__': n = int(input().strip()) #List to store the binary format lst = [] #Convert the number to binary and store in list while n > 0: remainder = n % 2 n = n//2 lst.append(remainder) count = 0 max_count = 0 for bit in lst: #If bit is 1 count value is incremented if bit == 1: count += 1 #If count is greater than max_count, count is assigned to max_count if max_count < count: max_count = count #If bit is not 1 then count becomes 0 else: count = 0 print(max_count)
Code Explanation:
- We get the input number as n
- We create a list lst to store the binary representation of the number.
- Then, we run a loop, till n becomes zero to get the last digits and append them in the list lst.
- We create two variables count and max_count to store the current count of one and the maximum count of one respectively.
- Then for every number in the list, we check whether it is equal to one.
- If it is equal to one, then we increment the count value and check if the count is greater than max_count. If the count is greater than max-count then we assign the count value to max_count
- Else we will assign the count value as 0.
- Finally, we print the max_count value which gives us the maximum count of consecutive ones present in the binary representation of the number.
Also Read:
- Download 1000+ Projects, All B.Tech & Programming Notes, Job, Resume & Interview Guide, and More – Get Your Ultimate Programming Bundle!
- HackerRank Day 8 Solution in Python: Dictionaries and Maps
- HackerRank Day 7 Solution in Python: Arrays
- HackerRank Day 6 Solution in Python: Let’s review
- HackerRank Day 5 Solution in Python: Loops
- HackerRank Day 4 Solution in Python: Class vs Instance
- HackerRank Day 3 Solution in Python: Intro to Conditional Statements
- HackerRank Day 2 Solution in Python: Operators
- HackerRank Day 1 Solution in Python: Data Types
- HackerRank Day 0 Solution in Python: Hello World
- HackerRank Day 29 Solution in Python: Bitwise AND
- HackerRank Day 28 Solution in Python: RegEx, Patterns, and Intro to databases
- HackerRank Day 27 Solution in Python: Testing
- HackerRank Day 26 Solution in Python: Nested Logic
- HackerRank Day 25 Solution in Python: Running Time and Complexity
- HackerRank Day 24 Solution in Python: More Linked Lists
- HackerRank Day 23 Solution in Python: BST Level Order Traversal
- HackerRank Day 22 Solution in Python: Binary Search Trees
- Find Peak Element LeetCode 162
- HackerRank Day 20 Solution in Python: Sorting
- HackerRank Day 19 Solution in Python: Interfaces
- HackerRank Day 18 Solution in Python: Queues and Stacks
- HackerRank Day 17 Solution in Python: More Exceptions
- HackerRank Day 16 Solution: Exceptions – String to Integer
- Explained: Allocate minimum number of pages
- HackerRank Day 15 Solution in Python: Linked List
- Search a 2D matrix: leetcode 74
- Maximum Subarray Sum: Kadane’s Algorithm
- HackerRank Day 13 Solution in Python: Abstract Classes
- HackerRank Day 14 Solution in Python: Scope