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

ambitious

Simulate a cannon shooting and heaps of falling cannonballs

Programming language:
Spoken language:

A new kind of cannon is being tested. The cannon shoots cannonballs in a fixed direction. Each cannonball flies horizontally until it hits the ground, and then it rests there. Cannonballs are shot from different heights, so they hit the ground at different points.

You are given two arrays, A and B, containing M and N integers respectively. Array A describes the landscape in the direction along which the cannon is shooting. Elements of array A represent the height of the ground, going from the cannon outwards. Array B contains levels from which consecutive cannonballs are shot.

Assume that a cannonball is shot at level H.

- Let I be the smallest index, such that 0 < I < M and A[I] ≥ H. The cannonball falls at position I − 1 and increases the ground level A[I−1] by 1.
- If there is no such I, and H > A[I] for all 0 ≤ I < M, then the cannonball flies beyond the horizon and has no effect on the result.
- If H ≤ A[0], then the cannonball ricochets away and has no effect on the result either.

Assume that the following declarations are given:

struct Results { int * A1; int M; // Length of the array };

Write a function:

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

that, given arrays A and B, simulates the flight of the cannonballs and returns the final contents of array A (denoted by A1) representing the final shape of the ground along the line of fire.

For example, given the following arrays A and B, of size M = 9 and N = 11 respectively:

the function should return the following array A1 of M = 9 integers:

Assume that:

- M and N are integers within the range [0..30,000];
- each element of arrays A, B is an integer within the range [0..1,000,000].

Complexity:

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

Notation used:

- H − max level of a cannonball.

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

A new kind of cannon is being tested. The cannon shoots cannonballs in a fixed direction. Each cannonball flies horizontally until it hits the ground, and then it rests there. Cannonballs are shot from different heights, so they hit the ground at different points.

You are given two arrays, A and B, containing M and N integers respectively. Array A describes the landscape in the direction along which the cannon is shooting. Elements of array A represent the height of the ground, going from the cannon outwards. Array B contains levels from which consecutive cannonballs are shot.

Assume that a cannonball is shot at level H.

- Let I be the smallest index, such that 0 < I < M and A[I] ≥ H. The cannonball falls at position I − 1 and increases the ground level A[I−1] by 1.
- If there is no such I, and H > A[I] for all 0 ≤ I < M, then the cannonball flies beyond the horizon and has no effect on the result.
- If H ≤ A[0], then the cannonball ricochets away and has no effect on the result either.

Write a function:

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

that, given arrays A and B, simulates the flight of the cannonballs and returns the final contents of array A (denoted by A1) representing the final shape of the ground along the line of fire.

For example, given the following arrays A and B, of size M = 9 and N = 11 respectively:

the function should return the following array A1 of M = 9 integers:

Assume that:

- M and N are integers within the range [0..30,000];
- each element of arrays A, B is an integer within the range [0..1,000,000].

Complexity:

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

Notation used:

- H − max level of a cannonball.

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

A new kind of cannon is being tested. The cannon shoots cannonballs in a fixed direction. Each cannonball flies horizontally until it hits the ground, and then it rests there. Cannonballs are shot from different heights, so they hit the ground at different points.

You are given two arrays, A and B, containing M and N integers respectively. Array A describes the landscape in the direction along which the cannon is shooting. Elements of array A represent the height of the ground, going from the cannon outwards. Array B contains levels from which consecutive cannonballs are shot.

Assume that a cannonball is shot at level H.

- Let I be the smallest index, such that 0 < I < M and A[I] ≥ H. The cannonball falls at position I − 1 and increases the ground level A[I−1] by 1.
- If there is no such I, and H > A[I] for all 0 ≤ I < M, then the cannonball flies beyond the horizon and has no effect on the result.
- If H ≤ A[0], then the cannonball ricochets away and has no effect on the result either.

Write a function:

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

that, given arrays A and B, simulates the flight of the cannonballs and returns the final contents of array A (denoted by A1) representing the final shape of the ground along the line of fire.

For example, given the following arrays A and B, of size M = 9 and N = 11 respectively:

the function should return the following array A1 of M = 9 integers:

Assume that:

- M and N are integers within the range [0..30,000];
- each element of arrays A, B is an integer within the range [0..1,000,000].

Complexity:

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

Notation used:

- H − max level of a cannonball.

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

Assume that a cannonball is shot at level H.

- If H ≤ A[0], then the cannonball ricochets away and has no effect on the result either.

Write a function:

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

For example, given the following arrays A and B, of size M = 9 and N = 11 respectively:

the function should return the following array A1 of M = 9 integers:

Assume that:

- M and N are integers within the range [0..30,000];
- each element of arrays A, B is an integer within the range [0..1,000,000].

Complexity:

- expected worst-case time complexity is O(H+M+N);

Notation used:

- H − max level of a cannonball.

Assume that a cannonball is shot at level H.

- If H ≤ A[0], then the cannonball ricochets away and has no effect on the result either.

Write a function:

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

For example, given the following arrays A and B, of size M = 9 and N = 11 respectively:

the function should return the following array A1 of M = 9 integers:

Assume that:

- M and N are integers within the range [0..30,000];
- each element of arrays A, B is an integer within the range [0..1,000,000].

Complexity:

- expected worst-case time complexity is O(H+M+N);

Notation used:

- H − max level of a cannonball.

Assume that a cannonball is shot at level H.

- If H ≤ A[0], then the cannonball ricochets away and has no effect on the result either.

Write a function:

function solution(A, B);

For example, given the following arrays A and B, of size M = 9 and N = 11 respectively:

the function should return the following array A1 of M = 9 integers:

Assume that:

- M and N are integers within the range [0..30,000];
- each element of arrays A, B is an integer within the range [0..1,000,000].

Complexity:

- expected worst-case time complexity is O(H+M+N);

Notation used:

- H − max level of a cannonball.

Assume that a cannonball is shot at level H.

- If H ≤ A[0], then the cannonball ricochets away and has no effect on the result either.

Write a function:

function solution(A, B)

For example, given the following arrays A and B, of size M = 9 and N = 11 respectively:

the function should return the following array A1 of M = 9 integers:

Assume that:

- M and N are integers within the range [0..30,000];
- each element of arrays A, B is an integer within the range [0..1,000,000].

Complexity:

- expected worst-case time complexity is O(H+M+N);

Notation used:

- H − max level of a cannonball.

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.

Assume that a cannonball is shot at level H.

- If H ≤ A[0], then the cannonball ricochets away and has no effect on the result either.

Write a function:

NSMutableArray * solution(NSMutableArray *A, NSMutableArray *B);

For example, given the following arrays A and B, of size M = 9 and N = 11 respectively:

the function should return the following array A1 of M = 9 integers:

Assume that:

- M and N are integers within the range [0..30,000];
- each element of arrays A, B is an integer within the range [0..1,000,000].

Complexity:

- expected worst-case time complexity is O(H+M+N);

Notation used:

- H − max level of a cannonball.

Assume that a cannonball is shot at level H.

- If H ≤ A[0], then the cannonball ricochets away and has no effect on the result either.

Assume that the following declarations are given:

Results = record A1 : array of longint; M : longint; {Length of the array} end;

Write a function:

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

For example, given the following arrays A and B, of size M = 9 and N = 11 respectively:

the function should return the following array A1 of M = 9 integers:

Assume that:

- M and N are integers within the range [0..30,000];
- each element of arrays A, B is an integer within the range [0..1,000,000].

Complexity:

- expected worst-case time complexity is O(H+M+N);

Notation used:

- H − max level of a cannonball.

Assume that a cannonball is shot at level H.

- If H ≤ A[0], then the cannonball ricochets away and has no effect on the result either.

Write a function:

function solution($A, $B);

For example, given the following arrays A and B, of size M = 9 and N = 11 respectively:

the function should return the following array A1 of M = 9 integers:

Assume that:

- M and N are integers within the range [0..30,000];
- each element of arrays A, B is an integer within the range [0..1,000,000].

Complexity:

- expected worst-case time complexity is O(H+M+N);

Notation used:

- H − max level of a cannonball.

Assume that a cannonball is shot at level H.

- If H ≤ A[0], then the cannonball ricochets away and has no effect on the result either.

Write a function:

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

For example, given the following arrays A and B, of size M = 9 and N = 11 respectively:

the function should return the following array A1 of M = 9 integers:

Assume that:

- M and N are integers within the range [0..30,000];
- each element of arrays A, B is an integer within the range [0..1,000,000].

Complexity:

- expected worst-case time complexity is O(H+M+N);

Notation used:

- H − max level of a cannonball.

Assume that a cannonball is shot at level H.

- If H ≤ A[0], then the cannonball ricochets away and has no effect on the result either.

Write a function:

def solution(A, B)

For example, given the following arrays A and B, of size M = 9 and N = 11 respectively:

the function should return the following array A1 of M = 9 integers:

Assume that:

- M and N are integers within the range [0..30,000];
- each element of arrays A, B is an integer within the range [0..1,000,000].

Complexity:

- expected worst-case time complexity is O(H+M+N);

Notation used:

- H − max level of a cannonball.

Assume that a cannonball is shot at level H.

- If H ≤ A[0], then the cannonball ricochets away and has no effect on the result either.

Write a function:

def solution(a, b)

For example, given the following arrays A and B, of size M = 9 and N = 11 respectively:

the function should return the following array A1 of M = 9 integers:

Assume that:

- M and N are integers within the range [0..30,000];
- each element of arrays A, B is an integer within the range [0..1,000,000].

Complexity:

- expected worst-case time complexity is O(H+M+N);

Notation used:

- H − max level of a cannonball.

Assume that a cannonball is shot at level H.

- If H ≤ A[0], then the cannonball ricochets away and has no effect on the result either.

Write a function:

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

For example, given the following arrays A and B, of size M = 9 and N = 11 respectively:

the function should return the following array A1 of M = 9 integers:

Assume that:

- M and N are integers within the range [0..30,000];
- each element of arrays A, B is an integer within the range [0..1,000,000].

Complexity:

- expected worst-case time complexity is O(H+M+N);

Notation used:

- H − max level of a cannonball.

Assume that a cannonball is shot at level H.

- If H ≤ A[0], then the cannonball ricochets away and has no effect on the result either.

Write a function:

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

For example, given the following arrays A and B, of size M = 9 and N = 11 respectively:

the function should return the following array A1 of M = 9 integers:

Assume that:

- M and N are integers within the range [0..30,000];
- each element of arrays A, B is an integer within the range [0..1,000,000].

Complexity:

- expected worst-case time complexity is O(H+M+N);

Notation used:

- H − max level of a cannonball.

Assume that a cannonball is shot at level H.

- If H ≤ A[0], then the cannonball ricochets away and has no effect on the result either.

Write a function:

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

For example, given the following arrays A and B, of size M = 9 and N = 11 respectively:

the function should return the following array A1 of M = 9 integers:

Assume that:

- M and N are integers within the range [0..30,000];
- each element of arrays A, B is an integer within the range [0..1,000,000].

Complexity:

- expected worst-case time complexity is O(H+M+N);

Notation used:

- H − max level of a cannonball.

Assume that a cannonball is shot at level H.

- If H ≤ A[0], then the cannonball ricochets away and has no effect on the result either.

Write a function:

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

For example, given the following arrays A and B, of size M = 9 and N = 11 respectively:

the function should return the following array A1 of M = 9 integers:

Assume that:

- M and N are integers within the range [0..30,000];
- each element of arrays A, B is an integer within the range [0..1,000,000].

Complexity:

- expected worst-case time complexity is O(H+M+N);

Notation used:

- H − max level of a cannonball.

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