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

UPCOMING CHALLENGES:

CURRENT CHALLENGES:

Cuprum 2018

PAST CHALLENGES

Cutting Complexity

Nickel 2018

Cobaltum 2018

Ferrum 2018

Manganum 2017

Chromium 2017

Vanadium 2016

Titanium 2016

Scandium 2016

Calcium 2015

Kalium 2015

Argon 2015

Chlorum 2014

Sulphur 2014

Phosphorus 2014

Silicium 2014

Aluminium 2014

Magnesium 2014

Natrium 2014

Neon 2014

Fluorum 2014

Oxygenium 2014

Nitrogenium 2013

Carbo 2013

Boron 2013

Beryllium 2013

Lithium 2013

Helium 2013

Hydrogenium 2013

Omega 2013

Psi 2012

Chi 2012

Phi 2012

Upsilon 2012

Tau 2012

Sigma 2012

Rho 2012

Pi 2012

Omicron 2012

Xi 2012

Nu 2011

Mu 2011

Lambda 2011

Kappa 2011

Iota 2011

Theta 2011

Eta 2011

Zeta 2011

Epsilon 2011

Delta 2011

Gamma 2011

Beta 2010

Alpha 2010

Programming language:
Spoken language:

A network consisting of N cities and N − 1 roads is given. Each city is labeled with a distinct integer from 0 to N − 1.

Roads connect cities in such a way that each distinct pair of cities is connected either by a direct road or through a path consisting of direct roads. There is exactly one way to reach any city from any other city.

Each city also has its own *attractiveness level*, which will be denoted by an integer. City P is more attractive than city Q if the attractiveness level of city P is strictly greater than the attractiveness level of city Q.

You are planning a trip to visit some of the most attractive cities. You want to select cities to visit based on the following requirements:

- At most K cities can be included in the trip plan.
- It must be possible to travel among the cities included in the trip plan without having to travel through cities that have been excluded from the trip plan.
- None of the cities included in the trip plan can be less attractive than any of the excluded cities. City attractiveness levels do not have to be unique, though, so it is permissible to visit only a subset of cities that are equally attractive.

The goal is to maximize the number of cities selected while satisfying the above requirements.

The network of cities is described using arrays C and D, each of length N. Array C describes a network of cities as follows: if C[P] = Q and P ≠ Q, then there is a direct road between cities P and Q. Array D describes attractiveness of the cities: D[P] is the attractiveness level of city P.

For example, consider the following network consisting of seven cities (each circle represents a city: the city label appears inside the circle and its attractiveness level outside the circle):

If K = 2, we can select a maximum number of two cities: either 2 and 0 or 2 and 4. In both cases, the attractiveness levels of the selected cities are greater than or equal to 6 and the attractiveness levels of the excluded cities are less than or equal to 6.

If, however, K = 5, the maximum number of cities we can select according to the rules above is four: we must select cities 2, 0, 4 and 5.

Write a function:

int solution(int K, int C[], int D[], int N);

that, given the integer K and non-empty arrays C and D of length N describing a network of cities and their attractiveness, returns the maximum number of cities that can be included in a valid trip plan.

For example, given arrays C and D describing the network above:

and K = 2 the function should return 2, as explained above. Similarly, given the same network but with K = 5, the function should return 4, as explained above.

Assume that:

- N is an integer within the range [1..100,000];
- K is an integer within the range [1..N];
- each element of array C is an integer within the range [0..(N−1)];
- there is exactly one (possibly indirect) connection between any two distinct cities;
- each element of array D is an integer within the range [0..1,000,000].

Complexity:

- expected worst-case time complexity is O(N*log(N));
- expected worst-case space complexity is O(N) (not counting the storage required for input arguments).

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

A network consisting of N cities and N − 1 roads is given. Each city is labeled with a distinct integer from 0 to N − 1.

Roads connect cities in such a way that each distinct pair of cities is connected either by a direct road or through a path consisting of direct roads. There is exactly one way to reach any city from any other city.

Each city also has its own *attractiveness level*, which will be denoted by an integer. City P is more attractive than city Q if the attractiveness level of city P is strictly greater than the attractiveness level of city Q.

You are planning a trip to visit some of the most attractive cities. You want to select cities to visit based on the following requirements:

- At most K cities can be included in the trip plan.
- It must be possible to travel among the cities included in the trip plan without having to travel through cities that have been excluded from the trip plan.
- None of the cities included in the trip plan can be less attractive than any of the excluded cities. City attractiveness levels do not have to be unique, though, so it is permissible to visit only a subset of cities that are equally attractive.

The goal is to maximize the number of cities selected while satisfying the above requirements.

The network of cities is described using arrays C and D, each of length N. Array C describes a network of cities as follows: if C[P] = Q and P ≠ Q, then there is a direct road between cities P and Q. Array D describes attractiveness of the cities: D[P] is the attractiveness level of city P.

For example, consider the following network consisting of seven cities (each circle represents a city: the city label appears inside the circle and its attractiveness level outside the circle):

If K = 2, we can select a maximum number of two cities: either 2 and 0 or 2 and 4. In both cases, the attractiveness levels of the selected cities are greater than or equal to 6 and the attractiveness levels of the excluded cities are less than or equal to 6.

If, however, K = 5, the maximum number of cities we can select according to the rules above is four: we must select cities 2, 0, 4 and 5.

Write a function:

int solution(int K, vector<int> &C, vector<int> &D);

that, given the integer K and non-empty arrays C and D of length N describing a network of cities and their attractiveness, returns the maximum number of cities that can be included in a valid trip plan.

For example, given arrays C and D describing the network above:

and K = 2 the function should return 2, as explained above. Similarly, given the same network but with K = 5, the function should return 4, as explained above.

Assume that:

- N is an integer within the range [1..100,000];
- K is an integer within the range [1..N];
- each element of array C is an integer within the range [0..(N−1)];
- there is exactly one (possibly indirect) connection between any two distinct cities;
- each element of array D is an integer within the range [0..1,000,000].

Complexity:

- expected worst-case time complexity is O(N*log(N));
- expected worst-case space complexity is O(N) (not counting the storage required for input arguments).

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

A network consisting of N cities and N − 1 roads is given. Each city is labeled with a distinct integer from 0 to N − 1.

Roads connect cities in such a way that each distinct pair of cities is connected either by a direct road or through a path consisting of direct roads. There is exactly one way to reach any city from any other city.

Each city also has its own *attractiveness level*, which will be denoted by an integer. City P is more attractive than city Q if the attractiveness level of city P is strictly greater than the attractiveness level of city Q.

You are planning a trip to visit some of the most attractive cities. You want to select cities to visit based on the following requirements:

- At most K cities can be included in the trip plan.
- It must be possible to travel among the cities included in the trip plan without having to travel through cities that have been excluded from the trip plan.
- None of the cities included in the trip plan can be less attractive than any of the excluded cities. City attractiveness levels do not have to be unique, though, so it is permissible to visit only a subset of cities that are equally attractive.

The goal is to maximize the number of cities selected while satisfying the above requirements.

The network of cities is described using arrays C and D, each of length N. Array C describes a network of cities as follows: if C[P] = Q and P ≠ Q, then there is a direct road between cities P and Q. Array D describes attractiveness of the cities: D[P] is the attractiveness level of city P.

For example, consider the following network consisting of seven cities (each circle represents a city: the city label appears inside the circle and its attractiveness level outside the circle):

If K = 2, we can select a maximum number of two cities: either 2 and 0 or 2 and 4. In both cases, the attractiveness levels of the selected cities are greater than or equal to 6 and the attractiveness levels of the excluded cities are less than or equal to 6.

If, however, K = 5, the maximum number of cities we can select according to the rules above is four: we must select cities 2, 0, 4 and 5.

Write a function:

class Solution { public int solution(int K, int[] C, int[] D); }

that, given the integer K and non-empty arrays C and D of length N describing a network of cities and their attractiveness, returns the maximum number of cities that can be included in a valid trip plan.

For example, given arrays C and D describing the network above:

and K = 2 the function should return 2, as explained above. Similarly, given the same network but with K = 5, the function should return 4, as explained above.

Assume that:

- N is an integer within the range [1..100,000];
- K is an integer within the range [1..N];
- each element of array C is an integer within the range [0..(N−1)];
- there is exactly one (possibly indirect) connection between any two distinct cities;
- each element of array D is an integer within the range [0..1,000,000].

Complexity:

- expected worst-case time complexity is O(N*log(N));
- expected worst-case space complexity is O(N) (not counting the storage required for input arguments).

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

*attractiveness level*, which will be denoted by an integer. City P is more attractive than city Q if the attractiveness level of city P is strictly greater than the attractiveness level of city Q.

- At most K cities can be included in the trip plan.

The goal is to maximize the number of cities selected while satisfying the above requirements.

Write a function:

func Solution(K int, C []int, D []int) int

For example, given arrays C and D describing the network above:

Assume that:

- N is an integer within the range [1..100,000];
- K is an integer within the range [1..N];
- each element of array C is an integer within the range [0..(N−1)];
- there is exactly one (possibly indirect) connection between any two distinct cities;
- each element of array D is an integer within the range [0..1,000,000].

Complexity:

- expected worst-case time complexity is O(N*log(N));

*attractiveness level*, which will be denoted by an integer. City P is more attractive than city Q if the attractiveness level of city P is strictly greater than the attractiveness level of city Q.

- At most K cities can be included in the trip plan.

The goal is to maximize the number of cities selected while satisfying the above requirements.

Write a function:

class Solution { public int solution(int K, int[] C, int[] D); }

For example, given arrays C and D describing the network above:

Assume that:

- N is an integer within the range [1..100,000];
- K is an integer within the range [1..N];
- each element of array C is an integer within the range [0..(N−1)];
- there is exactly one (possibly indirect) connection between any two distinct cities;
- each element of array D is an integer within the range [0..1,000,000].

Complexity:

- expected worst-case time complexity is O(N*log(N));

*attractiveness level*, which will be denoted by an integer. City P is more attractive than city Q if the attractiveness level of city P is strictly greater than the attractiveness level of city Q.

- At most K cities can be included in the trip plan.

The goal is to maximize the number of cities selected while satisfying the above requirements.

Write a function:

function solution(K, C, D);

For example, given arrays C and D describing the network above:

Assume that:

- N is an integer within the range [1..100,000];
- K is an integer within the range [1..N];
- each element of array C is an integer within the range [0..(N−1)];
- there is exactly one (possibly indirect) connection between any two distinct cities;
- each element of array D is an integer within the range [0..1,000,000].

Complexity:

- expected worst-case time complexity is O(N*log(N));

*attractiveness level*, which will be denoted by an integer. City P is more attractive than city Q if the attractiveness level of city P is strictly greater than the attractiveness level of city Q.

- At most K cities can be included in the trip plan.

The goal is to maximize the number of cities selected while satisfying the above requirements.

Write a function:

function solution(K, C, D)

For example, given arrays C and D describing the network above:

Assume that:

- N is an integer within the range [1..100,000];
- K is an integer within the range [1..N];
- each element of array C is an integer within the range [0..(N−1)];
- there is exactly one (possibly indirect) connection between any two distinct cities;
- each element of array D is an integer within the range [0..1,000,000].

Complexity:

- expected worst-case time complexity is O(N*log(N));

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.

*attractiveness level*, which will be denoted by an integer. City P is more attractive than city Q if the attractiveness level of city P is strictly greater than the attractiveness level of city Q.

- At most K cities can be included in the trip plan.

The goal is to maximize the number of cities selected while satisfying the above requirements.

Write a function:

int solution(int K, NSMutableArray *C, NSMutableArray *D);

For example, given arrays C and D describing the network above:

Assume that:

- N is an integer within the range [1..100,000];
- K is an integer within the range [1..N];
- each element of array C is an integer within the range [0..(N−1)];
- there is exactly one (possibly indirect) connection between any two distinct cities;
- each element of array D is an integer within the range [0..1,000,000].

Complexity:

- expected worst-case time complexity is O(N*log(N));

*attractiveness level*, which will be denoted by an integer. City P is more attractive than city Q if the attractiveness level of city P is strictly greater than the attractiveness level of city Q.

- At most K cities can be included in the trip plan.

The goal is to maximize the number of cities selected while satisfying the above requirements.

Write a function:

function solution(K: longint; C: array of longint; D: array of longint; N: longint): longint;

For example, given arrays C and D describing the network above:

Assume that:

- N is an integer within the range [1..100,000];
- K is an integer within the range [1..N];
- each element of array C is an integer within the range [0..(N−1)];
- there is exactly one (possibly indirect) connection between any two distinct cities;
- each element of array D is an integer within the range [0..1,000,000].

Complexity:

- expected worst-case time complexity is O(N*log(N));

*attractiveness level*, which will be denoted by an integer. City P is more attractive than city Q if the attractiveness level of city P is strictly greater than the attractiveness level of city Q.

- At most K cities can be included in the trip plan.

The goal is to maximize the number of cities selected while satisfying the above requirements.

Write a function:

function solution($K, $C, $D);

For example, given arrays C and D describing the network above:

Assume that:

- N is an integer within the range [1..100,000];
- K is an integer within the range [1..N];
- each element of array C is an integer within the range [0..(N−1)];
- there is exactly one (possibly indirect) connection between any two distinct cities;
- each element of array D is an integer within the range [0..1,000,000].

Complexity:

- expected worst-case time complexity is O(N*log(N));

*attractiveness level*, which will be denoted by an integer. City P is more attractive than city Q if the attractiveness level of city P is strictly greater than the attractiveness level of city Q.

- At most K cities can be included in the trip plan.

The goal is to maximize the number of cities selected while satisfying the above requirements.

Write a function:

sub solution { my ($K, $C, $D)=@_; my @C=@$C; my @D=@$D; ... }

For example, given arrays C and D describing the network above:

Assume that:

- N is an integer within the range [1..100,000];
- K is an integer within the range [1..N];
- each element of array C is an integer within the range [0..(N−1)];
- there is exactly one (possibly indirect) connection between any two distinct cities;
- each element of array D is an integer within the range [0..1,000,000].

Complexity:

- expected worst-case time complexity is O(N*log(N));

*attractiveness level*, which will be denoted by an integer. City P is more attractive than city Q if the attractiveness level of city P is strictly greater than the attractiveness level of city Q.

- At most K cities can be included in the trip plan.

The goal is to maximize the number of cities selected while satisfying the above requirements.

Write a function:

def solution(K, C, D)

For example, given arrays C and D describing the network above:

Assume that:

- N is an integer within the range [1..100,000];
- K is an integer within the range [1..N];
- each element of array C is an integer within the range [0..(N−1)];
- there is exactly one (possibly indirect) connection between any two distinct cities;
- each element of array D is an integer within the range [0..1,000,000].

Complexity:

- expected worst-case time complexity is O(N*log(N));

*attractiveness level*, which will be denoted by an integer. City P is more attractive than city Q if the attractiveness level of city P is strictly greater than the attractiveness level of city Q.

- At most K cities can be included in the trip plan.

The goal is to maximize the number of cities selected while satisfying the above requirements.

Write a function:

def solution(k, c, d)

For example, given arrays C and D describing the network above:

Assume that:

- N is an integer within the range [1..100,000];
- K is an integer within the range [1..N];
- each element of array C is an integer within the range [0..(N−1)];
- there is exactly one (possibly indirect) connection between any two distinct cities;
- each element of array D is an integer within the range [0..1,000,000].

Complexity:

- expected worst-case time complexity is O(N*log(N));

*attractiveness level*, which will be denoted by an integer. City P is more attractive than city Q if the attractiveness level of city P is strictly greater than the attractiveness level of city Q.

- At most K cities can be included in the trip plan.

The goal is to maximize the number of cities selected while satisfying the above requirements.

Write a function:

object Solution { def solution(k: Int, c: Array[Int], d: Array[Int]): Int }

For example, given arrays C and D describing the network above:

Assume that:

- N is an integer within the range [1..100,000];
- K is an integer within the range [1..N];
- each element of array C is an integer within the range [0..(N−1)];
- there is exactly one (possibly indirect) connection between any two distinct cities;
- each element of array D is an integer within the range [0..1,000,000].

Complexity:

- expected worst-case time complexity is O(N*log(N));

*attractiveness level*, which will be denoted by an integer. City P is more attractive than city Q if the attractiveness level of city P is strictly greater than the attractiveness level of city Q.

- At most K cities can be included in the trip plan.

The goal is to maximize the number of cities selected while satisfying the above requirements.

Write a function:

public func solution(K : Int, inout _ C : [Int], inout _ D : [Int]) -> Int

For example, given arrays C and D describing the network above:

Assume that:

- N is an integer within the range [1..100,000];
- K is an integer within the range [1..N];
- each element of array C is an integer within the range [0..(N−1)];
- there is exactly one (possibly indirect) connection between any two distinct cities;
- each element of array D is an integer within the range [0..1,000,000].

Complexity:

- expected worst-case time complexity is O(N*log(N));

*attractiveness level*, which will be denoted by an integer. City P is more attractive than city Q if the attractiveness level of city P is strictly greater than the attractiveness level of city Q.

- At most K cities can be included in the trip plan.

The goal is to maximize the number of cities selected while satisfying the above requirements.

Write a function:

public func solution(_ K : Int, _ C : inout [Int], _ D : inout [Int]) -> Int

For example, given arrays C and D describing the network above:

Assume that:

- N is an integer within the range [1..100,000];
- K is an integer within the range [1..N];
- each element of array C is an integer within the range [0..(N−1)];
- there is exactly one (possibly indirect) connection between any two distinct cities;
- each element of array D is an integer within the range [0..1,000,000].

Complexity:

- expected worst-case time complexity is O(N*log(N));

*attractiveness level*, which will be denoted by an integer. City P is more attractive than city Q if the attractiveness level of city P is strictly greater than the attractiveness level of city Q.

- At most K cities can be included in the trip plan.

The goal is to maximize the number of cities selected while satisfying the above requirements.

Write a function:

Private Function solution(K As Integer, C As Integer(), D As Integer()) As Integer

For example, given arrays C and D describing the network above:

Assume that:

- N is an integer within the range [1..100,000];
- K is an integer within the range [1..N];
- each element of array C is an integer within the range [0..(N−1)];
- there is exactly one (possibly indirect) connection between any two distinct cities;
- each element of array D is an integer within the range [0..1,000,000].

Complexity:

- expected worst-case time complexity is O(N*log(N));

Information about upcoming challenges, solutions and lessons directly in your inbox.

© 2009–2018 Codility Ltd., registered in England and Wales (No. 7048726). VAT ID GB981191408. Registered office: 107 Cheapside, London EC2V 6DN