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.
Here is my simple algorithm to solve the problem.
I won't tell the code as so many people here have already done without explaining anything.
I created a function which takes int n input and returns total count of prime numbers form 1 to n including.
Now, if n is even, answer is "Bob" else it is "Alice".
Now this method passes three test cases but time limit exceeds in two other test case.
What I observed is that, on single test case, multiple inputs are being taken, so calculating everytime the count of prime numbers is redundant. So, I created some global arrays to store the count and when it repeats, I supply it from there.
Reply if you need more explanation or code.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Alice and Bob's Silly Game
You are viewing a single comment's thread. Return to all comments →
Here is my simple algorithm to solve the problem. I won't tell the code as so many people here have already done without explaining anything. I created a function which takes int n input and returns total count of prime numbers form 1 to n including. Now, if n is even, answer is "Bob" else it is "Alice". Now this method passes three test cases but time limit exceeds in two other test case. What I observed is that, on single test case, multiple inputs are being taken, so calculating everytime the count of prime numbers is redundant. So, I created some global arrays to store the count and when it repeats, I supply it from there. Reply if you need more explanation or code.