Challenge: Rotating an Array by k Positions

Let’s rotate an array in this challenge.

Problem

Given an array, rotate its elements k times.

Input

An integer array and an integer k.

Output

A rotated array.

Sample input

array = { 1,2,3,4,5,6 }
k = 2

Sample output

array = { 3,4,5,6,1,2 }

Let’s look at an illustration of this problem to better understand it.

Get hands-on with 1400+ tech skills courses.