Tuesday, April 14, 2015

HackerRank::Contests::ProjectEuler::Project Euler #2: Even Fibonacci numbers

  Did you know that the 100th Fibonacci number equals 354224848179261915075 , which is longer than 16 digits,
  Now that you know that, even in the worst case when the number of testcases equals to 10^5 , looping over 100 numbers 10^5 time still won't cause you the Time Limit Exceeded error.





HackerRank::Contests::ProjectEuler::Project Euler #1: Multiples of 3 and 5

   A few things to notice here after which the task will become very easy to solve. Here is a quick question , how many numbers are there from 1 to N that are divisible by 3 ? Simple right? It's N/3 (divide and throw away the remainder). In that case, how to calculate their sum?
3+6+9+...3*M = 3( 1+2+3+...+M). which is equal to (3 * M * (M+1)) /2 (M is the number of numbers in the sequence). The same goes for 5.
   Now we have calculated the sum of all number that are divisible by 3, and we have calculated the sum of all number that are divisible by 5. There are some numbers which are divisible by 15 (both 3 and 5) and we counted them twice , the first time we counted them as a multiple of 3 and the second time we counted it as a multiple of 5. That's why we need to subtract the sum of all number from 1 to N that are divisible by 15 in order to get the final answer.



Sunday, April 12, 2015

HackerRank::Algorithms::Warmup::Sherlock and Squares

  The first look at the constraints is very important. A,B<=10^9. Among this there are sqrt(1000000000) almost 32000 numbers which are squares of natural numbers. Initially before processing the input, we need to write out all the full square number and store them somewhere. Then after reading the input, we need to loop over all the numbers and check if they are in the given range or not ( be attentive, we need to loop over the number which we found, and check whether they are in the range or not, not vice versa ). 

HackerRank::Algorithms::Warmup::Taum and B'day

  We will call the two types as "cheap" and "expensive", the cheap one obviously is the one which is cheaper than the other. If the prices are equal than there is no difference whether we name the first one as the "cheap" one or the second one. We need to buy all the required gifts off the cheap type. Then, if buying another cheap gift and converting it to the other type is still cheaper than the original price of the expensive gift, then we buy all the first with the "cheap" value and convert the necessary amount to the other type, otherwise we buy the expensive ones with their price.



HackerRank::Algorithms::Warmup::ACM ICPC Team

  The constraints are very low which means that by manually checking all the possible 2 member teams, we will compute the answer. 

HackerRank::Algorithms::Warmup::Manasa and Stones

  There are many ways we can change the numbers and simple checking all the ways will take too long, but we need to notice one thing. No matter how we move we make N-1 moves and every time we change the number either by A or B, which means that the final number is going to be a sum of N-1 numbers which are either A or B. Which means in total there are N-1 possible outcomes where there are X As,  ( 1<=X<=N-1) and N-1-X Bs.



HackerRank::Algorithms::Warmup::Cavity Map

We need to loop over all the cells and check for the 4 neighbors of every cell to be smaller than that cell.


Friday, April 10, 2015

HackerRank::Algorithms::Warmup::Chocolate Feast

  We need to implement the process of buying the chocolates. Initially he buys N/C chocolates. Even if some money is left we don't need to consider it anymore, because we can't by more with money, we need to buy with wrappers. Then we need to buy as many chocolates as we can with the current number of wrappers, then eat the chocolates and see if we have enough wrappers to buy at least one more chocolate, if we do, then do this step again, until we can't buy any more.


        

HackerRank::Algorithms::Warmup::Find Digits

We need to loop over all the digits and check for the divisibility of the number of that digit, there are different ways to do that, one way is to take the reminder of division of 10 and then divide to 10 to get to the next digit.


HackerRank::Algorithms::Warmup::Angry Professor

  We just need to input N number and find the number of numbers which are less or equal than 0. And then compare the result to the given integer K.