drawing book hackerrank
This file contains 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
/* | |
https://www.hackerrank.com/challenges/drawing-book/problem?isFullScreen=true | |
Given and , find and print the minimum number of pages that must be turned in order to arrive at page . | |
*/ | |
function pageCount(n, p) { | |
let index = 0; | |
const chunks = []; | |
const arr = [...Array(n + 1).keys()]; | |
for (let i = 0; i < arr.length; i += 2) { | |
const slice = arr.slice(i, i + 2); | |
if (slice.includes(p)) index = chunks.length; | |
chunks.push(slice); | |
} | |
return Math.min(index, chunks.length-1-index); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment