Codility Challenges

Still in my quest to master Problem Solving, I have compiled the solutions to the challenges under each lesson on Codility.

LEARN . SHARE

I have added links to the lessons and reading materials, and created JS files for each solution to every challenge. The aim is to drop more than one optimized solution for each challenge.

Lessons & Challenges

Read Iterations.pdf

BinaryGap _P: Find the longest sequence of zeros in binary representation of an integer. (note: Recruiters are advised to swap for MinDistinct in a real test situation)

Read Arrays.pdf

CyclicRotation _P: Rotate an array to the right by a given number of steps.

OldOccurencesInArray _P: Find value that occurs in odd number of elements.

Read TimeComplexity.pdf

FrogJmp _P: Count the minimal number of jumps from position X to Y.

PermMissingElem _P: Find the missing element in a given permutation.

TapeEquilibrium _P: Minimize the value | (A[0] + ... + A[P-1]) - (A[P] + ... + A(N-1)).

Read CountingElements.pdf

FrogRiverOne _P: Find the earliest time when a frog can jump to the other side of a river.

MaxCounters _R: Calculate the values of counters after applying all alternating operations: increase counter by 1; set value of all counters to current maximum.

MissingInteger _R: Find the smallest positive integer that does not occur in a given sequence.

PermCheck _P: Check whether an array is a permutation.

Read PrefixSums.pdf

CountDiv _R: Compute the number of integers divisible by k in range[a..b].

GenomicRangeQuery _R: Find the minimal nucleotide from a range of sequence DNA.

MinAvgTwoSlice _R: Find the minimum average of any slice containing at least two elements.

PassingCars _P: Count the number of passing cars on the road.

Read Sorting.pdf

Distinct _P: Compute the number of distinct values in an array.

MaxProductOfThree _P: Maximize A[P] * A[Q] * A[R] for any triplet (P, Q, R).

NumberOfDiscIntersections _R: Compute the number of intersections in a sequence of discs.

Triangle _P: Determine whether a triangle can be built from a given set of edges.

Read Stacks.pdf

Brackets _P: Determine whether a given string of parenthesis (multiple types) is properly nested.

Fish _P: N voracious fish are moving along a river. Calculate how many fish are alive.

Nesting _P: Determine whether a given string of parentheses (single type) is properly nested.

StoneWall _P: Cover "Manhattan skyline" using the minimum number of rectangles.

Read Leader.pdf

Dominator _P: Find an index of an array such that its value occurs at more than half of indices in the array.

EquiLeader _P: Find the index S such that the leaders of the sequence A[0], A[1], ..., A[S] and A[S + 1], A[S + 2], ..., A[N - 1] are the same.

Read MaxSlice.pdf

MaxDoubleSliceSum _R: Find the maximal sum of any double slice.

MaxProfit _P: Given a log of stock prices compute the maximum possible earning.

MaxSliceSum _P: Find a maximum sum of a compact subsequence of array elements.

Read PrimeNumbers.pdf

CountFactors _P: Count factors of a given number n.

Flags _R: Find the maximum number of flags that can be set on mountain peaks.

MinPerimeterRectangle _P: Find the minimal perimeter of any rectangle whose area equals N.

Peaks _R: Divide an array into the maximum number of same-sized blocks, each which should contain an index P such that A[P - 1] < A[P] > A[P + 1].