Hackerrank – Minimum Distances

Hackerrank – Problem Statement A description of the problem can be found on Hackerrank. Solution Create index pairs variations for all indexes that pair are unique – it means when (i, j) exists (j, i) is the same. Filter only this pairs of indexes, which have the same value. Make an absolute difference of them […]

Hackerrank – String Construction

Hackerrank – Problem Statement A description of the problem can be found on Hackerrank. Solution The answer is the count of distinct characters. If the string does not contain a specific character, only then it costs 1 dollar. Because one character is a substring. When we are able to find it in a partially created […]

Hackerrank – Save the Prisoner

Hackerrank – Problem Statement A description of the problem can be found on Hackerrank. Solution (((sweets % prisoners) + startId) % prisoners) – 1 Modulo finds the id of prisoner who gets the last poisoned candy after all circles distribution. Since the starting id S is not from first prisoner we make an offset. Do […]

Hackerrank – Kangaroo

Hackerrank – Problem Statement A description of the problem can be found on Hackerrank. Solution There are two checks: I checked if     is less then     and immediatelly print “NO”. That means, kangaroo 1 would be never able to reach kangaroo 2. I calculated differences between kangaroo positions     and their […]

Hackerrank – Beautiful Triplets

Hackerrank – Problem Statement A description of the problem can be found on Hackerrank. Solution I checked each consequent combination of (i, j, k), if it is valid according to condition. There is a catch. I had to stop an iteration if array[j] – array[i]

Hackerrank – Strange Counter

Hackerrank – Problem Statement A description of the problem can be found on Hackerrank. Solution I calculated time in the cycle when counter value is 1: last_time_next_cycle = actual_cycle_time + 2 * (last_time_actual_cycle – last_time_previous_cycle). The difference in seconds between cycle 0 and cycle 1 is 3, which is always like that. For a given […]

Hackerrank – Divisible Sum Pairs

Hackerrank – Problem Statement A description of the problem can be found on Hackerrank. Solution Create index pairs variations for all indexes that pair is unique – it means when (i, j) exists (j, i) is the same. For each pair calculate the sum of values at corresponding index. If is divisible, count them and […]

Hackerrank – CamelCase

Hackerrank – Problem Statement A description of the problem can be found on Hackerrank. Solution There are 2 options To split the string by regular expression”[A-Z]” and then print the length of an array returned from splitting. To iterate over all characters, count the uppercase and print their count + 1 I created solution in: […]

Hackerrank – Compare The Triplets

Hackerrank – Problem Statement A description of the problem can be found on Hackerrank. Solution Read all ratings and compare it according to the defined conditions: If ai > bi, then Alice is awarded 1 point If ai < bi, then Bob is awarded 1 point If ai = bi, then neither person receives a […]

Hackerrank – The Maximum Subarray

Hackerrank – Problem Statement A description of the problem can be found on Hackerrank. Solution Contiguous sum – using Kadane’s algorithm Non-contiguous sum – filter all positive elements from given array and sum them. If this array is empty. Take the greatest element. I created solution in: Scala Java JavaScript Ruby All solutions are also […]