codility

A collection of 26 post

Max Double Slice Sum

Problem A non-empty array A consisting of N integers is given. A triplet (X, Y, Z), such that 0 ≤ X < Y < Z < N, is called a double slice. The sum of double slice (X, Y, Z) is the total of AX + 1 + AX + 2 + … + AY − 1 + AY + 1 + AY + 2 + … + AZ − 1. For example, array A such that: contains the…

EquiLeader

Problem A non-empty array A consisting of N integers is given. The leader of this array is the value that occurs in more than half of the elements of A. An equi leader is an index S such that 0 ≤ S < N − 1 and two sequences A0, A1, …, AS and AS + 1, AS + 2, …, AN − 1 have leaders of the same value…

Dominator

Problem An array A consisting of N integers is given. The dominator of array A is the value that occurs in more than half of the elements of A. For example, consider array A such that The dominator of A is 3 because it occurs in 5 out of 8 elements of A (namely in those with indices 0, 2, 4, 6 and…

Stone Wall

Problem You are going to build a stone wall. The wall should be straight and N meters long, and its thickness should be constant; however, it should have different heights in different places. The height of the wall is specified by an array H of N positive integers. HI is the height of the wall from…

Nesting

Problem A string S consisting of N characters is called properly nested if: S is empty; S has the form “(U)” where U is a properly nested string; S has the form “VW” where V and W are properly nested strings. For example, string ”(()(())())” is properly nested but string ”())” isn’t. Write a…

Fish

Problem You are given two non-empty arrays A and B consisting of N integers. Arrays A and B represent N voracious fish in a river, ordered downstream along the flow of the river. The fish are numbered from 0 to N − 1. If P and Q are two fish and P < Q, then fish P is initially upstream of fish Q…

Brackets

Problem A string S consisting of N characters is considered to be properly nested if any of the following conditions is true: S is empty; S has the form “(U)” or ”U” or “{U}” where U is a properly nested string; S has the form “VW” where V and W are properly nested strings. For example, the string…

Triangle

Problem An array A consisting of N integers is given. A triplet (P, Q, R) is triangular if 0 ≤ P < Q < R < N and: For example, consider array A such that: Triplet (0, 2, 4) is triangular. Write a function: that, given an array A consisting of N integers, returns 1 if there exists a triangular…

Number of Disc Intersection

Problem We draw N discs on a plane. The discs are numbered from 0 to N − 1. An array A of N non-negative integers, specifying the radiuses of the discs, is given. The J-th disc is drawn with its center at (J, 0) and radius AJ. We say that the J-th disc and K-th disc intersect if J ≠ K and the J-th…

Max Product Of Three

Problem A non-empty array A consisting of N integers is given. The product of triplet (P, Q, R) equates to AP _ AQ _ AR (0 ≤ P < Q < R < N). For example, array A such that: contains the following example triplets: (0, 1, 2), product is −3 _ 1 _ 2 = −6 (1, 2, 4), product is 1 _ 2 _ 5 = 10 (2, 4,…

Distinct

Problem Write a function that, given an array A consisting of N integers, returns the number of distinct values in array A. For example, given array A consisting of six elements such that: the function should return 3, because there are 3 distinct values appearing in array A, namely 1, 2 and…

Passing Cars

Problem A non-empty array A consisting of N integers is given. The consecutive elements of array A represent consecutive cars on a road. Array A contains only 0s and/or 1s: 0 represents a car traveling east, 1 represents a car traveling west. The goal is to count passing cars. We say that a pair of…

Min Avg Two Slice

Problem A non-empty array A consisting of N integers is given. A pair of integers (P, Q), such that 0 ≤ P < Q < N, is called a slice of array A (notice that the slice contains at least two elements). The average of a slice (P, Q) is the sum of AP + AP + 1 + … + AQ divided by the length of the slice…

Genomic Range Query

Problem A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which correspond to the types of successive nucleotides in the sequence. Each nucleotide has an impact factor, which is an integer. Nucleotides of types A, C, G and T have impact factors of 1, 2, 3 and…

Count Div

Problem Write a function: that, given three integers A, B and K, returns the number of integers within the range A..B that are divisible by K, i.e.: { i : A ≤ i ≤ B, i mod K = 0 } For example, for A = 6, B = 11 and K = 2, your function should return 3, because there are three numbers divisible by…

Tape Equilibrium

Problem A non-empty array A consisting of N integers is given. Array A represents numbers on a tape. Any integer P, such that 0 < P < N, splits this tape into two non-empty parts: The difference between the two parts is the value of: In other words, it is the absolute difference between the sum of…

Perm Missing Elem

Problem An array A consisting of N different integers is given. The array contains integers in the range 1..(N + 1), which means that exactly one element is missing. Your goal is to find that missing element. Write a function: that, given an array A, returns the value of the missing element. For…

Perm Check

Problem A non-empty array A consisting of N integers is given. A permutation is a sequence containing each element from 1 to N once, and only once. For example, array A such that: is a permutation, but array A such that: is not a permutation, because value 2 is missing. The goal is to check whether…

Cyclic Rotation

Problem An array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is moved to the first place. For example, the rotation of array A = 3, 8, 9, 7, 6 is 6, 3, 8, 9, 7 (elements are shifted right by one…

Missing Integer

Problem This is a demo task. Write a function: that, given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A. For example, given A = 1, 3, 6, 4, 1, 2, the function should return 5. Given A = 1, 2, 3, the function should return 4. Given A = −…

Max Counters

Problem You are given N counters, initially set to 0, and you have two possible operations on them: increase(X) − counter X is increased by 1, max counter − all counters are set to the maximum value of any counter. A non-empty array A of M integers is given. This array represents consecutive…

Frog River One

Problem A small frog wants to get to the other side of a river. The frog is initially located on one bank of the river (position 0) and wants to get to the opposite bank (position X+1). Leaves fall from a tree onto the surface of the river. You are given an array A consisting of N integers…

Frog Jump

Problem A small frog wants to get to the other side of the road. The frog is currently located at position X and wants to get to a position greater than or equal to Y. The small frog always jumps a fixed distance, D. Count the minimal number of jumps that the small frog must perform to reach its…

Demo Test

Problem function solution(A); that, given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A. For example, given A = 1, 3, 6, 4, 1, 2, the function should return 5. Given A = 1, 2, 3, the function should return 4. Given A = −1, −3, the function…

Odd Occurrences In Array

Problem A non-empty array A consisting of N integers is given. The array contains an odd number of elements, and each element of the array can be paired with another element that has the same value, except for one element that is left unpaired. For example, in array A such that: the elements at…

Binary Gap

Problem A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N. For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary…