Hackerrank – Beautiful Binary String

Hackerrank – Problem Statement A description of the problem can be found on Hackerrank. Solution I replaced all occurences of “010” with an arbitrary placeholder. Then I counted the placeholders. I created solution in: Scala Java JavaScript Ruby All solutions are also available on my GitHub. Java

JavaScript

Scala

Ruby

Hackerrank – Circular Array Rotation

Hackerrank – Problem Statement A description of the problem can be found on Hackerrank. Solution We rotate the array in circles. That why it is enough to calculate a position after all rotations using modulo. We split the array into two halves at position array_length – calculated_position. We change halves and concat them – the […]

Hackerrank – Jumping on the Clouds: Revisited

Hackerrank – Problem Statement A description of the problem can be found on Hackerrank. Solution I jumped for k positions and lowered the energy. If I reached position 0 I printed the remaining enenergy. I created solution in: Scala Java JavaScript Ruby All solutions are also available on my GitHub. Java

JavaScript

Scala […]

Hackerrank – Mars Exploration

Hackerrank – Problem Statement A description of the problem can be found on Hackerrank. Solution I divided the whole string into to 3-character parts. Then I checked for each triplet if first character is equal to S, second to O and third to S. And counted the changed letters. I created solution in: Scala Java […]

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 […]