Skip to content

Instantly share code, notes, and snippets.

@rachidelaid
Created May 3, 2022 07:37
Embed
What would you like to do?
drawing book hackerrank
/*
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