Skip to content

Instantly share code, notes, and snippets.

@LuisitoRizado
Created April 3, 2023 05:09
Show Gist options
  • Save LuisitoRizado/35f23422076f98669d6b929535988d0d to your computer and use it in GitHub Desktop.
Save LuisitoRizado/35f23422076f98669d6b929535988d0d to your computer and use it in GitHub Desktop.
This is a solution for the 'Drawing book' problem by hackerrank. It was made in Javascript (Node js)
function pageCount(n, p) {
// Write your code here
//Identificar desde que lado empezaremos
let foundPage = false;
let currentPages = 0;
let counts = 0;
let tipoDeNumero = 0;
if(n%2!=0){
//Numero par = 0
//Numero impar = 1
tipoDeNumero = 1;
}
if(p<=n/2){
//left side
currentPages = [0,1];
counts = 0;
while(!foundPage){
//Buscamos en el arreglo si coincide p
if(currentPages.includes(p)){
foundPage = true;
break;
}
//sumamos un count
counts++;
//Actualizamos las paginas
currentPages[0] = currentPages[0] + 2;
currentPages[1] = currentPages[1]+2;
}
}
else{
//right side
//Si el numero es par entonces el arreglo empieza en [n, n+1]
if(tipoDeNumero==0)
{
currentPages = [n,n+1]
}
else
{
currentPages = [n-1,n];
}
counts = 0;
while(!foundPage){
//Buscamos en el arreglo si coincide p
if(currentPages.includes(p)){
foundPage = true;
break;
}
//sumamos un count
counts++;
//Actualizamos las paginas
currentPages[0] = currentPages[0] - 2;
currentPages[1] = currentPages[1] - 2;
}
}
return counts;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment