## Ice Cream Parlor

Solutions The brute force approach is easy: just run two nested loops and for each value $$x$$ find if it exists a value equals to $$target - x$$. This solution is quadratic. Efficient solutions are based either on sorting or additional storage (e.g. frequency table, hash maps). A sort-based solution Suppose we sort the prices $$p$$ in ascending order. Consider the sum $$p[0] + p[n-1]$$ where $$n$$ is the number of available prices. [Read More]

## Making Anagrams

Solutions Two strings are anagrams of one another if they share the same characters and each character has the same frequency in both strings. Thus, we can easily solve this problem with a frequency table. Basically, we can add 1 for each character in a and subtract 1 for each character in b. Those characters with non-zero frequency must be deleted and then added to the total count. Here is an implementation in C++: [Read More]

## Picking Numbers

Solutions Sorting A C++ solution based on sorting and a sliding window made of two increasing pointers: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #include <numeric> #include <array> #include <vector> #include <iostream> #include <algorithm> #include <iterator> using namespace std; int main() { int n; cin >> n; vector<int> v(n); copy_n(istream_iterator<int>(cin), n, begin(v)); sort(begin(v), end(v)); auto selected = 0; auto tail = begin(v); auto head = next(tail); while (head ! [Read More]

## Poker hand

Solutions The solutions revolve around finding the frequency of the card values. In addition, checking flush requires us to verify all the suits are equal. This check can be done in advance or while counting the frequency of each card. Afterwards, mapping the card frequency to the score is straighforward: find the maximum frequency and that will give the score: 1 is “High Card” 2 is “Pair”, 3, 4 and 5 are “Three of a Kind”). [Read More]

## Secret repetition

Solutions This problem is very easy but, as we’ll see in a minute, it can be solved in a very clever way. The easiest approach uses extra space, a frequency table. The core of the algorithm in C++: 1 2 3 4 5 6 7 8 9 10 11 12 13 int N; cin >> N; vector<int> A(2*N); copy_n(istream_iterator<int>(cin), 2*N, begin(A)); vector<int> freq(1'000'000 + 1); for (auto i : A) { freq[i]++; if (freq[i] > 1) { cout << i; break; } } This solution is linear. [Read More]

## Sherlock and Pairs

Solutions Sort C++ solution using sorting ($$O(N \cdot logN)$$: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 int T, N; cin >> T; while (T--) { cin >> N; vector<int> nums; nums.reserve(N); copy_n(istream_iterator<int>(cin), N, back_inserter(nums)); sort(begin(nums), end(nums)); auto it = begin(nums); unsigned long long pairs = 0; while (it != end(nums)) { auto ub = find_if(it, end(nums), [&](int i){ return i! [Read More]

## Sherlock and the Valid String

Solutions Warning: HackerRank does not fully cover this challenge with tests. You can write a solution which passes all the tests but not this test case: 1 aaavvbbb That should be NO. An example of code that passes all the tests but the one above: 1 2 3 4 5 6 fun isValid(s: String): String { val listLetters = s.groupingBy { it }.eachCount().toList() val dif = listLetters.filter { listLetters.first().second != it. [Read More]

## Smallest Rearrangement Spell

MegaBytus is a young and promising wizard who loves programming and computers. The master GigaBytus gave him a task: crafting a spell to rearrange the digits of a number such that the number obtained is the smallest possible. MegaBytus needs your help to find a way to verify that his spell works. Then you have to write a program that, given an integral number, will find the smallest number obtained by rearranging its digits. [Read More]

## String Construction

Solutions Intuition: we only pay the number of distinct letters in s. The rest can be copied. Thus, the solution consists in simply counting the number of distinct characters in s. Haskell Just use Set to count unique characters. 1 2 3 4 5 6 7 8 9 import qualified Data.Set as Set import Data.List (intercalate) calculateCost = length . Set.fromList main = do _ <- getLine contents <- getContents putStrLn . [Read More]

## The Lucky Employee

Summer is over and people at Gugol are sad and unmotivated. They would prefer going back on vacation instead of working - how to blame them? Ted, head of employee happiness department, has got an idea to cheer people up: he will run a lottery among all the employees and will award the luckiest one with two extra weeks of vacation. The lottery will work this way: Ted will raffle off some ranges of employee ids like $$[120, 200]$$ and $$[150, 180]$$, a sophisticated algorithm will calculate which is the most frequent employee id in all such ranges, if more than one such an id exists, the sophisticated algorithm will select the smallest one - it corresponds to the employee who has been working at Gugol for more time You are a $$\cancel{slave}$$ trainee at Gugol and you have to design and write such a sophisticated algorithm. [Read More]