Your browser (Unknown 0) is no longer supported. Some parts of the website may not work correctly. Please update your browser.

AVAILABLE LESSONS:

Lesson 1

Iterations

Lesson 2

Arrays

Lesson 3

Time Complexity

Lesson 4

Counting Elements

Lesson 5

Prefix Sums

Lesson 6

Sorting

Lesson 7

Stacks and Queues

Lesson 8

Leader

Lesson 9

Maximum slice problem

Lesson 10

Prime and composite numbers

Lesson 11

Sieve of Eratosthenes

Lesson 12

Euclidean algorithm

Lesson 13

Fibonacci numbers

Lesson 14

Binary search algorithm

Lesson 15

Caterpillar method

Lesson 16

Greedy algorithms

Lesson 17

Dynamic programming

Lesson 90

Tasks from Indeed Prime 2015 challenge

Lesson 91

Tasks from Indeed Prime 2016 challenge

Lesson 92

Tasks from Indeed Prime 2016 College Coders challenge

Lesson 99

Future training

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

Spoken language:

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 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:

the values of the counters after each consecutive operation will be:

The goal is to calculate the value of every counter after all operations.

Assume that the following declarations are given:

struct Results { int * C; int L; // Length of the array };

Write a function:

struct Results solution(int N, int A[], int M);

that, given an integer N and a non-empty array A consisting of M integers, returns a sequence of integers representing the values of the counters.

Result array should be returned as a structure `Results`.

For example, given:

the function should return [3, 2, 2, 4, 2], as explained above.

Write an ** efficient** algorithm for the following assumptions:

- 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].

Copyright 2009–2021 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.

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 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:

the values of the counters after each consecutive operation will be:

The goal is to calculate the value of every counter after all operations.

Write a function:

vector<int> solution(int N, vector<int> &A);

that, given an integer N and a non-empty array A consisting of M integers, returns a sequence of integers representing the values of the counters.

Result array should be returned as a vector of integers.

For example, given:

the function should return [3, 2, 2, 4, 2], as explained above.

Write an ** efficient** algorithm for the following assumptions:

- 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].

Copyright 2009–2021 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.

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 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:

the values of the counters after each consecutive operation will be:

The goal is to calculate the value of every counter after all operations.

Write a function:

class Solution { public int[] solution(int N, int[] A); }

that, given an integer N and a non-empty array A consisting of M integers, returns a sequence of integers representing the values of the counters.

Result array should be returned as an array of integers.

For example, given:

the function should return [3, 2, 2, 4, 2], as explained above.

Write an ** efficient** algorithm for the following assumptions:

- 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].

Copyright 2009–2021 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.

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 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:

the values of the counters after each consecutive operation will be:

The goal is to calculate the value of every counter after all operations.

Write a function:

func Solution(N int, A []int) []int

Result array should be returned as an array of integers.

For example, given:

the function should return [3, 2, 2, 4, 2], as explained above.

Write an ** efficient** algorithm for the following assumptions:

- 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].

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 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:

the values of the counters after each consecutive operation will be:

The goal is to calculate the value of every counter after all operations.

Write a function:

class Solution { public int[] solution(int N, int[] A); }

Result array should be returned as an array of integers.

For example, given:

the function should return [3, 2, 2, 4, 2], as explained above.

Write an ** efficient** algorithm for the following assumptions:

- 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].

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 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:

the values of the counters after each consecutive operation will be:

The goal is to calculate the value of every counter after all operations.

Write a function:

class Solution { public int[] solution(int N, int[] A); }

Result array should be returned as an array of integers.

For example, given:

the function should return [3, 2, 2, 4, 2], as explained above.

Write an ** efficient** algorithm for the following assumptions:

- 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].

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 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:

the values of the counters after each consecutive operation will be:

The goal is to calculate the value of every counter after all operations.

Write a function:

function solution(N, A);

Result array should be returned as an array of integers.

For example, given:

the function should return [3, 2, 2, 4, 2], as explained above.

Write an ** efficient** algorithm for the following assumptions:

- 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].

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 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:

the values of the counters after each consecutive operation will be:

The goal is to calculate the value of every counter after all operations.

Write a function:

fun solution(N: Int, A: IntArray): IntArray

Result array should be returned as an array of integers.

For example, given:

the function should return [3, 2, 2, 4, 2], as explained above.

Write an ** efficient** algorithm for the following assumptions:

- 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].

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 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:

the values of the counters after each consecutive operation will be:

The goal is to calculate the value of every counter after all operations.

Write a function:

function solution(N, A)

Result array should be returned as an array of integers.

For example, given:

the function should return [3, 2, 2, 4, 2], as explained above.

Write an ** efficient** algorithm for the following assumptions:

- 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].

Note: All arrays in this task are zero-indexed, unlike the common Lua convention. You can use `#A` to get the length of the array A.

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 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:

the values of the counters after each consecutive operation will be:

The goal is to calculate the value of every counter after all operations.

Write a function:

NSMutableArray * solution(int N, NSMutableArray *A);

Result array should be returned as an array of integers.

For example, given:

the function should return [3, 2, 2, 4, 2], as explained above.

Write an ** efficient** algorithm for the following assumptions:

- 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].

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 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:

the values of the counters after each consecutive operation will be:

The goal is to calculate the value of every counter after all operations.

Assume that the following declarations are given:

Results = record C : array of longint; L : longint; {Length of the array} end;

Write a function:

function solution(N: longint; A: array of longint; M: longint): Results;

Result array should be returned as a record `Results`.

For example, given:

the function should return [3, 2, 2, 4, 2], as explained above.

Write an ** efficient** algorithm for the following assumptions:

- 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].

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 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:

the values of the counters after each consecutive operation will be:

The goal is to calculate the value of every counter after all operations.

Write a function:

function solution($N, $A);

Result array should be returned as an array of integers.

For example, given:

the function should return [3, 2, 2, 4, 2], as explained above.

Write an ** efficient** algorithm for the following assumptions:

- 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].

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 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:

the values of the counters after each consecutive operation will be:

The goal is to calculate the value of every counter after all operations.

Write a function:

sub solution { my ($N, @A)=@_; ... }

Result array should be returned as an array of integers.

For example, given:

the function should return [3, 2, 2, 4, 2], as explained above.

Write an ** efficient** algorithm for the following assumptions:

- 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].

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 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:

the values of the counters after each consecutive operation will be:

The goal is to calculate the value of every counter after all operations.

Write a function:

def solution(N, A)

Result array should be returned as an array of integers.

For example, given:

the function should return [3, 2, 2, 4, 2], as explained above.

Write an ** efficient** algorithm for the following assumptions:

- 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].

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 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:

the values of the counters after each consecutive operation will be:

The goal is to calculate the value of every counter after all operations.

Write a function:

def solution(n, a)

Result array should be returned as an array of integers.

For example, given:

the function should return [3, 2, 2, 4, 2], as explained above.

Write an ** efficient** algorithm for the following assumptions:

- 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].

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 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:

the values of the counters after each consecutive operation will be:

The goal is to calculate the value of every counter after all operations.

Write a function:

object Solution { def solution(n: Int, a: Array[Int]): Array[Int] }

Result array should be returned as an array of integers.

For example, given:

the function should return [3, 2, 2, 4, 2], as explained above.

Write an ** efficient** algorithm for the following assumptions:

- 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].

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 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:

the values of the counters after each consecutive operation will be:

The goal is to calculate the value of every counter after all operations.

Write a function:

public func solution(_ N : Int, _ A : inout [Int]) -> [Int]

Result array should be returned as an array of integers.

For example, given:

the function should return [3, 2, 2, 4, 2], as explained above.

Write an ** efficient** algorithm for the following assumptions:

- 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].

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 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:

the values of the counters after each consecutive operation will be:

The goal is to calculate the value of every counter after all operations.

Write a function:

Private Function solution(N As Integer, A As Integer()) As Integer()

Result array should be returned as an array of integers.

For example, given:

the function should return [3, 2, 2, 4, 2], as explained above.

Write an ** efficient** algorithm for the following assumptions:

- 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].