We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
functionpageCount(n,p){// Write your code hereletfrontTurn=0;letbackTurn=0;letfrontPage=1;letlastPage;if(n%2==0){lastPage=n;}elseif(n%2!=0){lastPage=n-1;}if(p==1){return0}elseif(p%2!=0&&(lastPage==p||lastPage+1==p)){return0}elseif(p%2==0&&lastPage==p){return0}for(leti=1;i<=n;i++){frontTurn+=1;frontPage+=2;if(frontPage>=p){break}}for(leti=1;i<=n;i++){backTurn+=1;lastPage-=2;if(lastPage<=p){break}}if(frontTurn<backTurn){returnfrontTurn}else{returnbackTurn}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Drawing Book
You are viewing a single comment's thread. Return to all comments →