Day 10: Binary Numbers

Day 10: Binary Numbers

Objective

Today, we’re working with binary numbers. Check out the Tutorial tab for learning materials and an instructional video!

Task

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.

Input Format

A single integer, n.

Constraints

  • 1 <= n <= 106

Output Format

Print a single base-10 integer denoting the maximum number of consecutive 1‘s in the binary representation of n.

Sample Input 1

1
5

Sample Output 1

1
1

Sample Input 2

1
13

Sample Output 2

1
2

Explanation

Sample Case 1:
The binary representation of 5 is 101, so the maximum number of consecutive 1‘s is 1.

Sample Case 2:
The binary representation of 13 is 1101, so the maximum number of consecutive 1‘s is 2.




Solutions

Solution 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function main() {
let n = parseInt(readLine(), 10);
let values = [];

while (+n != 0) {
values.push(n % 2);

n = Math.floor(n / 2);
}

let { result } = values.reduce((target, item, index) => {
item
? target['accumulator']++
: (target['accumulator'] = 0);

target['accumulator'] > target['result'] && (target['result'] = target['accumulator']);

return target;
}, { accumulator: 0, result: 0 })

console.log(result);
}

Solution 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function main() {
const n = parseInt(readLine(), 10);
let result = 0;

const temp = n.toString(2).split('').reduce((target, num) => {
let value = Number(num) > 0 ? Number(target) + Number(num) : (
result = target > result ? target : result,
0
)

return value;

}, 0)

console.log(result - temp > 0 ? result : temp)
}

Solution 3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function main() {
const n = parseInt(readLine(), 10);
const array = n.toString(2).split('');
let max = 0;
let cur = 0;

for (let i = 0, itotal = array.length; i < itotal; i++) {
if (array[i] == 1) {
cur++;
} else if (array[i] == 0) {
if (cur > max) {
max = cur;
}
cur = 0;
}
}

if (cur > max) {
max = cur;
}

console.log(max);
}
`
Buy Me A Coffee

Comments

10Days of JS 30Days of Code Algorithm Android Debug Bridge Android Debugging Basic for Web Blog Browsers Chrome으로 Android Debugging 방법 Correctness and the Loop Invariant hackerrank solution in javascript Debug Tools Development Environment in MacOS ES6 Front-End Funny String of Algorithms hackerrank solution in javascript Funny String of Algorithms hackerrank solution in typescript Generator Github Page with Hexo Github Pages HackerRank HackerRank in a String of Algorithms hackerrank solution in javascript HackerRank in a String of Algorithms hackerrank solution in typescript Hexo Hexo Icarus theme Hexo 블로그 만들기 Hexo+Github How Browsers work Insertion Sort - Part 1 hackerrank solution in javascript Insertion Sort - Part 2 hackerrank solution in javascript JS Library JavaScript Level1 Level2 Loops MacOS 개발 환경 설정하기 Mobile web Debugging Node.js Pangrams of Algorithms hackerrank solution in javascript Pangrams of Algorithms hackerrank solution in typescript Problem Solving Programmers Quicksort 1 - Partition hackerrank solution in javascript React RoadMap Running Time of Algorithms hackerrank solution in javascript Safari Debugging Safari Technology Preview Settings Sorting String Strings Strong Password of Algorithms hackerrank solution in javascript TypeScript blog iPhone Safari Debugging 방법 insertion sort 모바일 웹 디버깅 아이폰 사파리를 디버깅하는 방법 안드로이드 디버그 브리지 안드로이드 디버깅하는 방법
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×