## среда, 25 декабря 2013 г.

### Codility. Train. Passing-cars ★★

A non-empty zero-indexed 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 cars (P, Q), where 0 ≤ P < Q < N, is passing when P is traveling to the east and Q is traveling to the west.
For example, consider array A such that:
```  A = 0
A = 1
A = 0
A = 1
A = 1```
We have five pairs of passing cars: (0, 1), (0, 3), (0, 4), (2, 3), (2, 4).
Write a function:
def solution(A)
that, given a non-empty zero-indexed array A of N integers, returns the number of passing cars.
The function should return −1 if the number of passing cars exceeds 1,000,000,000.
For example, given:
```  A = 0
A = 1
A = 0
A = 1
A = 1```
the function should return 5, as explained above.
Assume that:
• N is an integer within the range [1..100,000];
• each element of array A is an integer within the range [0..1].
Complexity:
• expected worst-case time complexity is O(N);
• expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments).
Elements of input arrays can be modified.

### Codility. Train. Max-Counters ★★★

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 zero-indexed array A of M integers is given. This array represents consecutive operations:
• if A[K] = X, such that 1 ≤ X ≤ N, then operation K is increase(X),
• if A[K] = N + 1 then operation K is max_counter.
For example, given integer N = 5 and array A such that:
```    A = 3
A = 4
A = 4
A = 6
A = 1
A = 4
A = 4```
the values of the counters after each consecutive operation will be:
```    (0, 0, 1, 0, 0)
(0, 0, 1, 1, 0)
(0, 0, 1, 2, 0)
(2, 2, 2, 2, 2)
(3, 2, 2, 2, 2)
(3, 2, 2, 3, 2)
(3, 2, 2, 4, 2)```
The goal is to calculate the value of every counter after all operations.
Write a function:
def solution(N, A)
that, given an integer N and a non-empty zero-indexed array A consisting of M integers, returns a sequence of integers representing the values of the counters.
The sequence should be returned as:
• a structure Results (in C), or
• a vector of integers (in C++), or
• a record Results (in Pascal), or
• an array of integers (in any other programming language).
For example, given:
```    A = 3
A = 4
A = 4
A = 6
A = 1
A = 4
A = 4```
the function should return [3, 2, 2, 4, 2], as explained above.
Assume that:
• N and M are integers within the range [1..100,000];
• each element of array A is an integer within the range [1..N + 1].
Complexity:
• expected worst-case time complexity is O(N+M);
• expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).
Elements of input arrays can be modified.

### Codility. Train. Frog-River-One ★★

A small frog wants to get to the other side of a river. The frog is currently located at position 0, and wants to get to position X. Leaves fall from a tree onto the surface of the river.
You are given a non-empty zero-indexed array A consisting of N integers representing the falling leaves. A[K] represents the position where one leaf falls at time K, measured in minutes.
The goal is to find the earliest time when the frog can jump to the other side of the river. The frog can cross only when leaves appear at every position across the river from 1 to X.
For example, you are given integer X = 5 and array A such that:
```  A = 1
A = 3
A = 1
A = 4
A = 2
A = 3
A = 5
A = 4```
In minute 6, a leaf falls into position 5. This is the earliest time when leaves appear in every position across the river.
Write a function:
def solution(X, A)
that, given a non-empty zero-indexed array A consisting of N integers and integer X, returns the earliest time when the frog can jump to the other side of the river.
If the frog is never able to jump to the other side of the river, the function should return −1.
For example, given X = 5 and array A such that:
```  A = 1
A = 3
A = 1
A = 4
A = 2
A = 3
A = 5
A = 4```
the function should return 6, as explained above. Assume that:
• N and X are integers within the range [1..100,000];
• each element of array A is an integer within the range [1..X].
Complexity:
• expected worst-case time complexity is O(N);
• expected worst-case space complexity is O(X), beyond input storage (not counting the storage required for input arguments).
Elements of input arrays can be modified.

## вторник, 24 декабря 2013 г.

### Codility. Train. Perm-Check ★

A non-empty zero-indexed array A consisting of N integers is given.
permutation is a sequence containing each element from 1 to N once, and only once.
For example, array A such that:
```    A = 4
A = 1
A = 3
A = 2```
is a permutation, but array A such that:
```    A = 4
A = 1
A = 3```
is not a permutation.
The goal is to check whether array A is a permutation.
Write a function:
def solution(A)
that, given a zero-indexed array A, returns 1 if array A is a permutation and 0 if it is not.
For example, given array A such that:
```    A = 4
A = 1
A = 3
A = 2```
the function should return 1.
Given array A such that:
```    A = 4
A = 1
A = 3```
the function should return 0.
Assume that:
• N is an integer within the range [1..100,000];
• each element of array A is an integer within the range [1..1,000,000,000].
Complexity:
• expected worst-case time complexity is O(N);
• expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).
Elements of input arrays can be modified.

### Codility. Train. Tape-Equilibrium ★★★

A non-empty zero-indexed 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: A, A, ..., A[P − 1] and A[P], A[P + 1], ..., A[N − 1].
The difference between the two parts is the value of: |(A + A + ... + A[P − 1]) − (A[P] + A[P + 1] + ... + A[N − 1])|
In other words, it is the absolute difference between the sum of the first part and the sum of the second part.
For example, consider array A such that:
```  A = 3
A = 1
A = 2
A = 4
A = 3```
We can split this tape in four places:
• P = 1, difference = |3 − 10| = 7
• P = 2, difference = |4 − 9| = 5
• P = 3, difference = |6 − 7| = 1
• P = 4, difference = |10 − 3| = 7
Write a function:
def solution(A)
that, given a non-empty zero-indexed array A of N integers, returns the minimal difference that can be achieved.
For example, given:
```  A = 3
A = 1
A = 2
A = 4
A = 3```
the function should return 1, as explained above.
Assume that:
• N is an integer within the range [2..100,000];
• each element of array A is an integer within the range [−1,000..1,000].
Complexity:
• expected worst-case time complexity is O(N);
• expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).
Elements of input arrays can be modified.

### Codility. Train. Perm-Missing-Elem ★★

A zero-indexed 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:
def solution(A)
that, given a zero-indexed array A, returns the value of the missing element.
For example, given array A such that:
```  A = 2
A = 3
A = 1
A = 5```
the function should return 4, as it is the missing element.
Assume that:
• N is an integer within the range [0..100,000];
• the elements of A are all distinct;
• each element of array A is an integer within the range [1..(N + 1)].
Complexity:
• expected worst-case time complexity is O(N);
• expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments).
Elements of input arrays can be modified.

### Codility. Train. Frog-Jmp ★

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 target.
Write a function:
def solution(X, Y, D)
that, given three integers X, Y and D, returns the minimal number of jumps from position X to a position equal to or greater than Y.
For example, given:
```  X = 10
Y = 85
D = 30```
the function should return 3, because the frog will be positioned as follows:
• after the first jump, at position 10 + 30 = 40
• after the second jump, at position 10 + 30 + 30 = 70
• after the third jump, at position 10 + 30 + 30 + 30 = 100
Assume that:
• X, Y and D are integers within the range [1..1,000,000,000];
• X ≤ Y.
Complexity:
• expected worst-case time complexity is O(1);
• expected worst-case space complexity is O(1).