Skip to content

Instantly share code, notes, and snippets.

@rachidelaid
Created May 3, 2022 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rachidelaid/12612d10518bb3e25d38651d5b84841b to your computer and use it in GitHub Desktop.
Save rachidelaid/12612d10518bb3e25d38651d5b84841b to your computer and use it in GitHub Desktop.
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