HackerRank Day 10 Binary Numbers 30 days of code solution YASH PAL, 31 July 2024 In this HackerRank Day 10 Binary Numbers 30 days of code problem set, we need to develop a program that can accept integer as an input and then convert it into a binary number and then into in base 10 integer. we need to print the base 10 integer that denotes the maximum number of consecutive 1’s in the binary representation of the input. Problem solution in Python 2 programming. #!/bin/python import sys n = int(raw_input().strip()) binary = str(bin(n)) #print binary tot=0 tmp=0 for i in binary: if i=='1': tmp+=1 else: tot=max(tot,tmp) tmp=0 tot=max(tot,tmp) print tot Problem solution in Python 3 programming. #!/bin/python3 import math import os import random import re import sys if __name__ == '__main__': n = int(input()) rmd = [] while n > 0: rm = n % 2 n = n//2 rmd.append(rm) count,result = 0,0 for i in range(0,len(rmd)): if rmd[i] == 0: count = 0 else: count +=1 result = max(result,count) print(result) Problem solution in java programming. import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); scan.close(); char[] binary = Integer.toBinaryString(n).toCharArray(); int tmpCount = 0; int maxCount = 0; for(int i = 0; i < binary.length; i++){ tmpCount = (binary[i] == '0') ? 0 : tmpCount + 1; if(tmpCount > maxCount){ maxCount = tmpCount; } } System.out.println(maxCount); } } Problem solution in c++ programming. #include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <queue> #include <stack> #include <cstring> #include <bitset> #include <cstdio> #include <limits> #include <vector> #include <climits> #include <cstring> #include <cstdlib> #include <fstream> #include <numeric> #include <sstream> #include <iostream> #include <algorithm> #include <unordered_map> using namespace std; string toBinary(int n) { string r; while(n!=0) {r=(n%2==0 ?"0":"1")+r; n/=2;} return r; } int consOnes(string r, int size){ int max=0 ,p=0; for(int i= 0; i<size;i++){ if(r.substr(i,1)=="1"){ p++; if (p>max) max=p; } else{ p=0; } } return max; } int main(){ int n; cin >> n; string r = toBinary(n); cout << consOnes(r,r.length()); return 0; } Problem solution in c programming. #include <math.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <assert.h> #include <limits.h> #include <stdbool.h> int main(){ int n,count=0,max=0; scanf("%d",&n); while(n>0) { if(n%2==1) count++; else { if(count>max) max=count; count=0; } n/=2; } if(count>max) max=count; printf("%d",max); return 0; } Problem solution in Javascript programming. process.stdin.resume(); process.stdin.setEncoding('ascii'); var input_stdin = ""; var input_stdin_array = ""; var input_currentline = 0; process.stdin.on('data', function (data) { input_stdin += data; }); process.stdin.on('end', function () { input_stdin_array = input_stdin.split("n"); main(); }); function readLine() { return input_stdin_array[input_currentline++]; } /////////////// ignore above this line //////////////////// function main() { var n = parseInt(readLine()).toString(2); var splits = n.split('0'); console.log(splits.map(function(elem){return elem.length;}).reduce(function(a,b){ if (a>b) return a; else return b;})); } 30 days of code coding problems
hi bro , can u say how this code find consecutive numbers .can u explain this bro ? for i in range(0,len(rmd)): if rmd[i] == 0: count = 0 else: count +=1 result = max(result,count)
here we first checking that value of rmd[i] is equal to 0 or not if value of rmd[i] is equal to 0 then we set count to 0 but if not then we increast it by +1 and then we storing the max value among (result,count) like if we have result = max(4,6) then value of result = 6 because we need to find the maximum consecutive 1's means maximum occurance of 1's continuously. i think this help you