Categories
coding solutions

NumberOfDiscIntersections O(N) Solution

NumberOfDiscIntersections O(N) solution Task The task is to count overlapping discs given an array of radii 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 […]

Categories
coding solutions

Codility: PassingCars Solution

PassingCars Task The task is to count pairs of zeros and ones, where the 1 comes after the 0. 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 cars (P, Q), where […]

Categories
coding solutions

Codility: MinAvgTwoSlice Solution

MinAvgTwoSlice Task The task is to find the slice of an array with the minimum average Write a function: int solution(vector<int> &A); that, given a non-empty array A consisting of N integers, returns the starting position of the slice with the minimal average. If there is more than one slice with a minimal average, you […]

Categories
coding solutions

Codility: GenomicRangeQuery Solution

GenomicRangeQuery Task The task is to find the minimum values in slices of an array. 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 […]

Categories
coding solutions

Codility: CountDiv Solution

CountDiv Task This is a division task. This is classified as ‘Respectable’, but is simple. Write a function: int solution(int A, int B, int K); 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 ≤ […]

Categories
coding solutions

Codility: PermCheck Solution

PermCheck Solution Task The task is to calculate if an array contains all the numbers from 1 to N, where N is the size of the array: 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, […]

Categories
coding solutions

Codility: MaxCounters Solution

MaxCounters Task This task is a functional operation task, involving commands that can operate on all items in an array: 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 […]

Categories
coding solutions

Codility: TapeEquilibrium Solution

Tape Equilibrium Task Task is to find the minimum difference in the sums of the values in an array, before and after a pivot point 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 […]

Categories
coding solutions

Codility: Cyclic Rotation Solution

Cyclic Rotation Task Again, this is a simple task: Write a function: vector<int> solution(vector<int> &A, int K); that, given an array A consisting of N integers and an integer K, returns the array A rotated K times. Solution You just calculate the previous index of each element and populate the answer accordingly #include <iostream> using […]

Categories
coding solutions

Codility:BinaryGap Solution

Binary Gap Task This is a simple task: 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. You simply have to find the length of the maximum run of zeros in the binary representation of […]