Created
July 18, 2022 18:21
-
-
Save alwaisy/913448546cdb04dcbfd32a89c016d4ce to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// problem | |
/** | |
Objective | |
Today, we're working with binary numbers. Check out the Tutorial tab for learning materials and an instructional video! | |
Task | |
Given a base- integer, , convert it to binary (base-). Then find and print the base- integer denoting the maximum number of consecutive 's in 's binary representation. When working with different bases, it is common to show the base as a subscript. | |
Example | |
The binary representation of is . In base , there are and consecutive ones in two groups. Print the maximum, . | |
Input Format | |
A single integer, . | |
Constraints | |
Output Format | |
Print a single base- integer that denotes the maximum number of consecutive 's in the binary representation of . | |
Sample Input 1 | |
5 | |
Sample Output 1 | |
1 | |
Sample Input 2 | |
13 | |
Sample Output 2 | |
2 | |
Explanation | |
Sample Case 1: | |
The binary representation of is , so the maximum number of consecutive 's is . | |
Sample Case 2: | |
The binary representation of is , so the maximum number of consecutive 's is . | |
*/ | |
// solution => all tests are passed | |
'use strict'; | |
process.stdin.resume(); | |
process.stdin.setEncoding('utf-8'); | |
let inputString = ''; | |
let currentLine = 0; | |
process.stdin.on('data', function (inputStdin) { | |
inputString += inputStdin; | |
}); | |
process.stdin.on('end', function () { | |
inputString = inputString.split('\n'); | |
main(); | |
}); | |
function readLine() { | |
return inputString[currentLine++]; | |
} | |
function main() { | |
let n = parseInt(readLine().trim(), 10); | |
// sol1=> console.log(Math.max.apply(null, n.toString(2).split("0").map(x => x.length))); | |
let bin = ""; | |
let currentValue; | |
let max = 0; | |
let count = 0; | |
while (n > 0) { | |
currentValue = (n / 2) % 1 === 0 ? "0" : "1"; | |
if (currentValue === "1") { | |
count++; | |
} else { | |
count = 0; | |
} | |
if (count > max) { | |
max = count; | |
} | |
bin = currentValue + bin; | |
n = Math.floor(n / 2); | |
} | |
console.log(max); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment