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

Rotate an array to the right by a given number of steps.

Spoken language:

An array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is moved to the first place. For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7] (elements are shifted right by one index and 6 is moved to the first place).

The goal is to rotate array A K times; that is, each element of A will be shifted to the right K times.

Assume that the following declarations are given:

struct Results { int * A; int N; // Length of the array };

Write a function:

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

that, given an array A consisting of N integers and an integer K, returns the array A rotated K times.

For example, given

the function should return [9, 7, 6, 3, 8]. Three rotations were made:

For another example, given

the function should return [0, 0, 0]

Given

the function should return [1, 2, 3, 4]

Assume that:

- N and K are integers within the range [0..100];
- each element of array A is an integer within the range [−1,000..1,000].

In your solution, focus on ** correctness**. The performance of your solution will not be the focus of the assessment.

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

An array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is moved to the first place. For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7] (elements are shifted right by one index and 6 is moved to the first place).

The goal is to rotate array A K times; that is, each element of A will be shifted to the right K times.

Write a function:

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

that, given an array A consisting of N integers and an integer K, returns the array A rotated K times.

For example, given

the function should return [9, 7, 6, 3, 8]. Three rotations were made:

For another example, given

the function should return [0, 0, 0]

Given

the function should return [1, 2, 3, 4]

Assume that:

- N and K are integers within the range [0..100];
- each element of array A is an integer within the range [−1,000..1,000].

In your solution, focus on ** correctness**. The performance of your solution will not be the focus of the assessment.

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

An array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is moved to the first place. For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7] (elements are shifted right by one index and 6 is moved to the first place).

The goal is to rotate array A K times; that is, each element of A will be shifted to the right K times.

Write a function:

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

that, given an array A consisting of N integers and an integer K, returns the array A rotated K times.

For example, given

the function should return [9, 7, 6, 3, 8]. Three rotations were made:

For another example, given

the function should return [0, 0, 0]

Given

the function should return [1, 2, 3, 4]

Assume that:

- N and K are integers within the range [0..100];
- each element of array A is an integer within the range [−1,000..1,000].

In your solution, focus on ** correctness**. The performance of your solution will not be the focus of the assessment.

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

Write a function:

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

For example, given

the function should return [9, 7, 6, 3, 8]. Three rotations were made:

For another example, given

the function should return [0, 0, 0]

Given

the function should return [1, 2, 3, 4]

Assume that:

- N and K are integers within the range [0..100];
- each element of array A is an integer within the range [−1,000..1,000].

** correctness**. The performance of your solution will not be the focus of the assessment.

Write a function:

List<int> solution(List<int> A, int K);

For example, given

the function should return [9, 7, 6, 3, 8]. Three rotations were made:

For another example, given

the function should return [0, 0, 0]

Given

the function should return [1, 2, 3, 4]

Assume that:

- N and K are integers within the range [0..100];
- each element of array A is an integer within the range [−1,000..1,000].

** correctness**. The performance of your solution will not be the focus of the assessment.

Write a function:

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

For example, given

the function should return [9, 7, 6, 3, 8]. Three rotations were made:

For another example, given

the function should return [0, 0, 0]

Given

the function should return [1, 2, 3, 4]

Assume that:

- N and K are integers within the range [0..100];
- each element of array A is an integer within the range [−1,000..1,000].

** correctness**. The performance of your solution will not be the focus of the assessment.

Write a function:

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

For example, given

the function should return [9, 7, 6, 3, 8]. Three rotations were made:

For another example, given

the function should return [0, 0, 0]

Given

the function should return [1, 2, 3, 4]

Assume that:

- N and K are integers within the range [0..100];
- each element of array A is an integer within the range [−1,000..1,000].

** correctness**. The performance of your solution will not be the focus of the assessment.

Write a function:

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

For example, given

the function should return [9, 7, 6, 3, 8]. Three rotations were made:

For another example, given

the function should return [0, 0, 0]

Given

the function should return [1, 2, 3, 4]

Assume that:

- N and K are integers within the range [0..100];
- each element of array A is an integer within the range [−1,000..1,000].

** correctness**. The performance of your solution will not be the focus of the assessment.

Write a function:

function solution(A, K);

For example, given

the function should return [9, 7, 6, 3, 8]. Three rotations were made:

For another example, given

the function should return [0, 0, 0]

Given

the function should return [1, 2, 3, 4]

Assume that:

- N and K are integers within the range [0..100];
- each element of array A is an integer within the range [−1,000..1,000].

** correctness**. The performance of your solution will not be the focus of the assessment.

Write a function:

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

For example, given

the function should return [9, 7, 6, 3, 8]. Three rotations were made:

For another example, given

the function should return [0, 0, 0]

Given

the function should return [1, 2, 3, 4]

Assume that:

- N and K are integers within the range [0..100];
- each element of array A is an integer within the range [−1,000..1,000].

** correctness**. The performance of your solution will not be the focus of the assessment.

Write a function:

function solution(A, K)

For example, given

the function should return [9, 7, 6, 3, 8]. Three rotations were made:

For another example, given

the function should return [0, 0, 0]

Given

the function should return [1, 2, 3, 4]

Assume that:

- N and K are integers within the range [0..100];
- each element of array A is an integer within the range [−1,000..1,000].

** correctness**. The performance of your solution will not be the focus of the assessment.

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.

Write a function:

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

For example, given

the function should return [9, 7, 6, 3, 8]. Three rotations were made:

For another example, given

the function should return [0, 0, 0]

Given

the function should return [1, 2, 3, 4]

Assume that:

- N and K are integers within the range [0..100];
- each element of array A is an integer within the range [−1,000..1,000].

** correctness**. The performance of your solution will not be the focus of the assessment.

Assume that the following declarations are given:

Results = record A : array of longint; N : longint; {Length of the array} end;

Write a function:

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

For example, given

the function should return [9, 7, 6, 3, 8]. Three rotations were made:

For another example, given

the function should return [0, 0, 0]

Given

the function should return [1, 2, 3, 4]

Assume that:

- N and K are integers within the range [0..100];
- each element of array A is an integer within the range [−1,000..1,000].

** correctness**. The performance of your solution will not be the focus of the assessment.

Write a function:

function solution($A, $K);

For example, given

the function should return [9, 7, 6, 3, 8]. Three rotations were made:

For another example, given

the function should return [0, 0, 0]

Given

the function should return [1, 2, 3, 4]

Assume that:

- N and K are integers within the range [0..100];
- each element of array A is an integer within the range [−1,000..1,000].

** correctness**. The performance of your solution will not be the focus of the assessment.

Write a function:

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

For example, given

the function should return [9, 7, 6, 3, 8]. Three rotations were made:

For another example, given

the function should return [0, 0, 0]

Given

the function should return [1, 2, 3, 4]

Assume that:

- N and K are integers within the range [0..100];
- each element of array A is an integer within the range [−1,000..1,000].

** correctness**. The performance of your solution will not be the focus of the assessment.

Write a function:

def solution(A, K)

For example, given

the function should return [9, 7, 6, 3, 8]. Three rotations were made:

For another example, given

the function should return [0, 0, 0]

Given

the function should return [1, 2, 3, 4]

Assume that:

- N and K are integers within the range [0..100];
- each element of array A is an integer within the range [−1,000..1,000].

** correctness**. The performance of your solution will not be the focus of the assessment.

Write a function:

def solution(a, k)

For example, given

the function should return [9, 7, 6, 3, 8]. Three rotations were made:

For another example, given

the function should return [0, 0, 0]

Given

the function should return [1, 2, 3, 4]

Assume that:

- N and K are integers within the range [0..100];
- each element of array A is an integer within the range [−1,000..1,000].

** correctness**. The performance of your solution will not be the focus of the assessment.

Write a function:

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

For example, given

the function should return [9, 7, 6, 3, 8]. Three rotations were made:

For another example, given

the function should return [0, 0, 0]

Given

the function should return [1, 2, 3, 4]

Assume that:

- N and K are integers within the range [0..100];
- each element of array A is an integer within the range [−1,000..1,000].

** correctness**. The performance of your solution will not be the focus of the assessment.

Write a function:

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

For example, given

the function should return [9, 7, 6, 3, 8]. Three rotations were made:

For another example, given

the function should return [0, 0, 0]

Given

the function should return [1, 2, 3, 4]

Assume that:

- N and K are integers within the range [0..100];
- each element of array A is an integer within the range [−1,000..1,000].

** correctness**. The performance of your solution will not be the focus of the assessment.

Write a function:

function solution(A: number[], K: number): number[];

For example, given

the function should return [9, 7, 6, 3, 8]. Three rotations were made:

For another example, given

the function should return [0, 0, 0]

Given

the function should return [1, 2, 3, 4]

Assume that:

- N and K are integers within the range [0..100];
- each element of array A is an integer within the range [−1,000..1,000].

** correctness**. The performance of your solution will not be the focus of the assessment.

Write a function:

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

For example, given

the function should return [9, 7, 6, 3, 8]. Three rotations were made:

For another example, given

the function should return [0, 0, 0]

Given

the function should return [1, 2, 3, 4]

Assume that:

- N and K are integers within the range [0..100];
- each element of array A is an integer within the range [−1,000..1,000].

** correctness**. The performance of your solution will not be the focus of the assessment.