Thursday, July 30, 2015

HackerRank::Algorithms::Implementation::The Grid Search

  This is surely a pure implementation task since a digit by digit brute force check gets AC, but here the main implementation itself might be a little challenging. Check the source code for a good understanding.



HackerRank::Algorithms::Warmup::Time Conversion

 This is a basic implementation task, we need to add 12 to the hour number if necessary and output the result, be sure to implement carefully , take care of hour == 24 and if you are about to output a single digit as a minute, second or hour output it in format 0[digit] , don't forget the 0.



Tuesday, July 28, 2015

HackerRank::Algorithms::Implementation::Encryption

   The best way of doing it will be trying to make a square since it will have the lowest area, the rest is technical.



HackerRank::Algorithms::Search::Ice Cream Parlor

  You can simply map the flavors since the constraints are low (about 10000) which will save you time for searching for a pair of matching flavor for each flavor.



Sunday, July 26, 2015

HackerRank::Algorithms::Greedy::Flowers

 Basically the task says the more a person buys the more expensive the next purchase will be so it's simple enough to understand that each of you should start buying one by one in decreasing order starting from the most expensive flower.



HackerRank::Algorithms::Greedy::Largest Permutation

  We are aiming to build the following sequence, N, N-1, N-2, ... .If we have at least one swap, then we should use it in order to bring the number N to front regardless of the other numbers' positions. Then if we have some swaps left we should bring the Number N-1 to the second position, and then the number N-2 to the third position and so on.



HackerRank::Algorithms::Greedy::Priyanka and Toys

  Actually there is no such a thing as optimal way of choosing the toys here ( more or less). Let's sort them out by the order of their weights. Now for the one which has the lowest weight, we need to buy that in any case, then we might get some bonus items after buying them, then the same goes for the remaining set of toys, we need to buy the one with the lowest weight in any case, and we might get a bonus if we are lucky, then the same thing again until we buy all of them.



Friday, July 24, 2015

HackerRank::Algorithms::Greedy::Two Arrays

   Greedy again, which means we need to be as greedy as we can and in this case greediness would be spending as little "resources" as possible in order to save the best for the last. For every element from A we need to find a pair from B where their sum will be more or equal than K, so fro every I, we need to try to find the smallest B which will satisfy the condition, by picking this way we will find a valid solution for sure (if the solution exists) , if we can't find anything than simply the answer is NO>


HackerRank::Algorithms::Greedy::Mark and Toys

   Just because we care only about the quantity of the items we need to start buying them from the cheapest one, the we should buy the second cheapest and so on until we either buy all of them or run out of money.


HackerRank::Algorithms::Greedy::Jim and the Orders

Simply sort all the orders by the time of their completion and print the corresponding customer for each order from the earliest completion time to the latest.



Wednesday, July 22, 2015

HackerRank::Algorithms::Warmup::Service Lane

  I think understanding this task is a lot harder than the solution itself, so I will not spoil it for you, try to understand it, and trust me it's about finding the minimum number in the given segment ;)



HacerRank::Algorithms::Warmup::Extra long factorials

   For solving this I again used my handy bigInt class for C++ which still lacks the division operation but has all it needs to sovle this task. Although, I encountered another problem, for some reason the submission receives "Abort Called" message when submitted in hackerrank , I don't know the reason, it works fine on my computer, anyway, I made a precalculated list of factorials and got accepted, either way the class itself is included in the source code below, it works fine.



HacerRank::Algorithms::Warmup::Caesar Cipher

  Simply loop over all the characters, check whether the current character is a letter or not and then simply get the K-th next letter. Check the source code for a better understanding.




Tuesday, July 21, 2015

Sunday, July 19, 2015

HackerRank::Algorithms::Warmup::Staircase

   In any case, out output answer has to be an NxN array with either space bars of with '#' symbols. The # sybol has to appear only in the cells where in the coordinate [X,Y] the Y is more or equal than N-X.



HackerRank::Algorithms::Warmup::Plus Minus

   There is not much to explain here, simply count the number of negatives, positives and zeros and divide on N.



HackerRank::Algorithms::Warmup::Diagonal Difference

   For any I from 1 to N , the indexes of the first diagonal's elements are [I,I] and the second diagonal's elements will have the index [I, N-I] ( if numeration starts from 1).



Saturday, July 18, 2015

HackerRank::Algorithms::Warmup::A very big sum

  It's really simple , __int64 or long long :D



HackerRank::Algorithms::Imeplementation::Cut the sticks

   The constraints are so low, that you can implement the process of cutting the trees until you run out of trees.



HackerRank::Algorithms::Implementation::Song of Pi

  There are different approaches to solve this implementation problem. Have a look at my approach below.



HackerRank::Algorithms::Strings::Palindrome Index

   The guarantee is going to help us a lot here, and the guarantee is that it is always possible to make a palindrome by removing only one character. Obviously the most obvious approach is to take out every single character and check for palindrome but that gives us around (10^6)^2 complexity which will not fit in 1 second time limit for sure. Instead we can try a little trickier method, it is guaranteed that we are sure going to be able to form a palindrome, so if initially the first character is not equal to the last character then we can conclude that either we have to remove the first character in order to form a palindrome or the last. If they are equal then we need to check the second character to be equal to the pre-last character and if it is not then the character which must be removed is either the second one or the pre last one , and so on.