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

Given a sequence, find the longest subsequence that can be decomposed into at most three monotonic parts.

Programming language:
Spoken language:

You are a skier participating in a giant slalom. The slalom track is located on a ski slope, goes downhill and is fenced by barriers on both sides. The barriers are perpendicular to the starting line located at the top of the slope. There are N slalom gates on the track. Each gate is placed at a distinct distance from the starting line and from the barrier on the right-hand side (looking downhill).

You start from any place on the starting line, ski down the track passing as many gates as possible, and finish the slalom at the bottom of the slope. *Passing* a gate means skiing through the position of the gate.

You can ski downhill in either of two directions: to the left or to the right. When you ski to the left, you pass gates of increasing distances from the right barrier, and when you ski to the right, you pass gates of decreasing distances from the right barrier. You want to ski to the left at the beginning.

Unfortunately, changing direction (left to right or vice versa) is exhausting, so you have decided to change direction *at most two* times during your ride. Because of this, you have allowed yourself to miss some of the gates on the way down the slope. You would like to know the maximum number of gates that you can pass with at most two changes of direction.

The arrangement of the gates is given as an array A consisting of N integers, whose elements specify the positions of the gates: gate K (for 0 ≤ K < N) is at a distance of K+1 from the starting line, and at a distance of A[K] from the right barrier.

For example, consider array A such that:

The picture above illustrates the example track with N = 13 gates and a course that passes eight gates. After starting, you ski to the left (from your own perspective). You pass gates 2, 3, 5, 6 and then change direction to the right. After that you pass gates 7, 8 and then change direction to the left. Finally, you pass gates 10, 11 and finish the slalom. There is no possible way of passing more gates using at most two changes of direction.

Write a function:

int solution(int A[], int N);

that, given an array A consisting of N integers, describing the positions of the gates on the track, returns the maximum number of gates that you can pass during one ski run.

For example, given the above data, the function should return 8, as explained above.

For the following array A consisting of N = 2 elements:

the function should return 2.

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

- 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];
- the elements of A are all distinct.

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

You are a skier participating in a giant slalom. The slalom track is located on a ski slope, goes downhill and is fenced by barriers on both sides. The barriers are perpendicular to the starting line located at the top of the slope. There are N slalom gates on the track. Each gate is placed at a distinct distance from the starting line and from the barrier on the right-hand side (looking downhill).

You start from any place on the starting line, ski down the track passing as many gates as possible, and finish the slalom at the bottom of the slope. *Passing* a gate means skiing through the position of the gate.

You can ski downhill in either of two directions: to the left or to the right. When you ski to the left, you pass gates of increasing distances from the right barrier, and when you ski to the right, you pass gates of decreasing distances from the right barrier. You want to ski to the left at the beginning.

Unfortunately, changing direction (left to right or vice versa) is exhausting, so you have decided to change direction *at most two* times during your ride. Because of this, you have allowed yourself to miss some of the gates on the way down the slope. You would like to know the maximum number of gates that you can pass with at most two changes of direction.

The arrangement of the gates is given as an array A consisting of N integers, whose elements specify the positions of the gates: gate K (for 0 ≤ K < N) is at a distance of K+1 from the starting line, and at a distance of A[K] from the right barrier.

For example, consider array A such that:

The picture above illustrates the example track with N = 13 gates and a course that passes eight gates. After starting, you ski to the left (from your own perspective). You pass gates 2, 3, 5, 6 and then change direction to the right. After that you pass gates 7, 8 and then change direction to the left. Finally, you pass gates 10, 11 and finish the slalom. There is no possible way of passing more gates using at most two changes of direction.

Write a function:

int solution(vector<int> &A);

that, given an array A consisting of N integers, describing the positions of the gates on the track, returns the maximum number of gates that you can pass during one ski run.

For example, given the above data, the function should return 8, as explained above.

For the following array A consisting of N = 2 elements:

the function should return 2.

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

- 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];
- the elements of A are all distinct.

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

You are a skier participating in a giant slalom. The slalom track is located on a ski slope, goes downhill and is fenced by barriers on both sides. The barriers are perpendicular to the starting line located at the top of the slope. There are N slalom gates on the track. Each gate is placed at a distinct distance from the starting line and from the barrier on the right-hand side (looking downhill).

You start from any place on the starting line, ski down the track passing as many gates as possible, and finish the slalom at the bottom of the slope. *Passing* a gate means skiing through the position of the gate.

You can ski downhill in either of two directions: to the left or to the right. When you ski to the left, you pass gates of increasing distances from the right barrier, and when you ski to the right, you pass gates of decreasing distances from the right barrier. You want to ski to the left at the beginning.

Unfortunately, changing direction (left to right or vice versa) is exhausting, so you have decided to change direction *at most two* times during your ride. Because of this, you have allowed yourself to miss some of the gates on the way down the slope. You would like to know the maximum number of gates that you can pass with at most two changes of direction.

The arrangement of the gates is given as an array A consisting of N integers, whose elements specify the positions of the gates: gate K (for 0 ≤ K < N) is at a distance of K+1 from the starting line, and at a distance of A[K] from the right barrier.

For example, consider array A such that:

The picture above illustrates the example track with N = 13 gates and a course that passes eight gates. After starting, you ski to the left (from your own perspective). You pass gates 2, 3, 5, 6 and then change direction to the right. After that you pass gates 7, 8 and then change direction to the left. Finally, you pass gates 10, 11 and finish the slalom. There is no possible way of passing more gates using at most two changes of direction.

Write a function:

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

that, given an array A consisting of N integers, describing the positions of the gates on the track, returns the maximum number of gates that you can pass during one ski run.

For example, given the above data, the function should return 8, as explained above.

For the following array A consisting of N = 2 elements:

the function should return 2.

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

- 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];
- the elements of A are all distinct.

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

*Passing* a gate means skiing through the position of the gate.

*at most two* times during your ride. Because of this, you have allowed yourself to miss some of the gates on the way down the slope. You would like to know the maximum number of gates that you can pass with at most two changes of direction.

For example, consider array A such that:

Write a function:

func Solution(A []int) int

For example, given the above data, the function should return 8, as explained above.

For the following array A consisting of N = 2 elements:

the function should return 2.

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

- 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];
- the elements of A are all distinct.

*Passing* a gate means skiing through the position of the gate.

*at most two* times during your ride. Because of this, you have allowed yourself to miss some of the gates on the way down the slope. You would like to know the maximum number of gates that you can pass with at most two changes of direction.

For example, consider array A such that:

Write a function:

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

For example, given the above data, the function should return 8, as explained above.

For the following array A consisting of N = 2 elements:

the function should return 2.

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

- 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];
- the elements of A are all distinct.

*Passing* a gate means skiing through the position of the gate.

*at most two* times during your ride. Because of this, you have allowed yourself to miss some of the gates on the way down the slope. You would like to know the maximum number of gates that you can pass with at most two changes of direction.

For example, consider array A such that:

Write a function:

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

For example, given the above data, the function should return 8, as explained above.

For the following array A consisting of N = 2 elements:

the function should return 2.

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

- 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];
- the elements of A are all distinct.

*Passing* a gate means skiing through the position of the gate.

*at most two* times during your ride. Because of this, you have allowed yourself to miss some of the gates on the way down the slope. You would like to know the maximum number of gates that you can pass with at most two changes of direction.

For example, consider array A such that:

Write a function:

function solution(A);

For example, given the above data, the function should return 8, as explained above.

For the following array A consisting of N = 2 elements:

the function should return 2.

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

- 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];
- the elements of A are all distinct.

*Passing* a gate means skiing through the position of the gate.

*at most two* times during your ride. Because of this, you have allowed yourself to miss some of the gates on the way down the slope. You would like to know the maximum number of gates that you can pass with at most two changes of direction.

For example, consider array A such that:

Write a function:

fun solution(A: IntArray): Int

For example, given the above data, the function should return 8, as explained above.

For the following array A consisting of N = 2 elements:

the function should return 2.

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

- 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];
- the elements of A are all distinct.

*Passing* a gate means skiing through the position of the gate.

*at most two* times during your ride. Because of this, you have allowed yourself to miss some of the gates on the way down the slope. You would like to know the maximum number of gates that you can pass with at most two changes of direction.

For example, consider array A such that:

Write a function:

function solution(A)

For example, given the above data, the function should return 8, as explained above.

For the following array A consisting of N = 2 elements:

the function should return 2.

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

- 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];
- the elements of A are all distinct.

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.

*Passing* a gate means skiing through the position of the gate.

*at most two* times during your ride. Because of this, you have allowed yourself to miss some of the gates on the way down the slope. You would like to know the maximum number of gates that you can pass with at most two changes of direction.

For example, consider array A such that:

Write a function:

int solution(NSMutableArray *A);

For example, given the above data, the function should return 8, as explained above.

For the following array A consisting of N = 2 elements:

the function should return 2.

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

- 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];
- the elements of A are all distinct.

*Passing* a gate means skiing through the position of the gate.

*at most two* times during your ride. Because of this, you have allowed yourself to miss some of the gates on the way down the slope. You would like to know the maximum number of gates that you can pass with at most two changes of direction.

For example, consider array A such that:

Write a function:

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

For example, given the above data, the function should return 8, as explained above.

For the following array A consisting of N = 2 elements:

the function should return 2.

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

- 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];
- the elements of A are all distinct.

*Passing* a gate means skiing through the position of the gate.

*at most two* times during your ride. Because of this, you have allowed yourself to miss some of the gates on the way down the slope. You would like to know the maximum number of gates that you can pass with at most two changes of direction.

For example, consider array A such that:

Write a function:

function solution($A);

For example, given the above data, the function should return 8, as explained above.

For the following array A consisting of N = 2 elements:

the function should return 2.

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

- 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];
- the elements of A are all distinct.

*Passing* a gate means skiing through the position of the gate.

*at most two* times during your ride. Because of this, you have allowed yourself to miss some of the gates on the way down the slope. You would like to know the maximum number of gates that you can pass with at most two changes of direction.

For example, consider array A such that:

Write a function:

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

For example, given the above data, the function should return 8, as explained above.

For the following array A consisting of N = 2 elements:

the function should return 2.

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

- 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];
- the elements of A are all distinct.

*Passing* a gate means skiing through the position of the gate.

*at most two* times during your ride. Because of this, you have allowed yourself to miss some of the gates on the way down the slope. You would like to know the maximum number of gates that you can pass with at most two changes of direction.

For example, consider array A such that:

Write a function:

def solution(A)

For example, given the above data, the function should return 8, as explained above.

For the following array A consisting of N = 2 elements:

the function should return 2.

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

- 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];
- the elements of A are all distinct.

*Passing* a gate means skiing through the position of the gate.

*at most two* times during your ride. Because of this, you have allowed yourself to miss some of the gates on the way down the slope. You would like to know the maximum number of gates that you can pass with at most two changes of direction.

For example, consider array A such that:

Write a function:

def solution(a)

For example, given the above data, the function should return 8, as explained above.

For the following array A consisting of N = 2 elements:

the function should return 2.

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

- 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];
- the elements of A are all distinct.

*Passing* a gate means skiing through the position of the gate.

*at most two* times during your ride. Because of this, you have allowed yourself to miss some of the gates on the way down the slope. You would like to know the maximum number of gates that you can pass with at most two changes of direction.

For example, consider array A such that:

Write a function:

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

For example, given the above data, the function should return 8, as explained above.

For the following array A consisting of N = 2 elements:

the function should return 2.

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

- 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];
- the elements of A are all distinct.

*Passing* a gate means skiing through the position of the gate.

*at most two* times during your ride. Because of this, you have allowed yourself to miss some of the gates on the way down the slope. You would like to know the maximum number of gates that you can pass with at most two changes of direction.

For example, consider array A such that:

Write a function:

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

For example, given the above data, the function should return 8, as explained above.

For the following array A consisting of N = 2 elements:

the function should return 2.

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

- 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];
- the elements of A are all distinct.

*Passing* a gate means skiing through the position of the gate.

*at most two* times during your ride. Because of this, you have allowed yourself to miss some of the gates on the way down the slope. You would like to know the maximum number of gates that you can pass with at most two changes of direction.

For example, consider array A such that:

Write a function:

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

For example, given the above data, the function should return 8, as explained above.

For the following array A consisting of N = 2 elements:

the function should return 2.

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

- 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];
- the elements of A are all distinct.

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

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