본문 바로가기

Computer Science/Algorithm with Code

[HackerRank] Preparation_Kit (06. Left Rotation) [Array]

A left rotation operation on an array shifts each of the array's elements 1 unit to the left. For example, if 2 left rotations are performed on array [1, 2, 3, 4, 5], then the array would become [3, 4, 5, 6, 7]. Note that the lowest index item moves to the highest index in a rotation. This is called a circular array.

Given an array a of n integers and a number, d, perform d left rotations on the array. Return the updated array to be printed as a single line of space-separated integers.

 

Function Description

Complete the function rotLeft in the editor below.

rotLeft has the following parameter(s):

  • int a[n]: the array to rotate
  • int d: the number of rotations

Returns

  • int a'[n]: the rotated array
func rotLeft(a: [Int], d: Int) -> [Int] {
    // Write your code here
    var arr: [Int] = Array(a[d..<a.count] + a[0..<d])
    return arr
}

 

// 힌트) 1<= d <=n

// 매개 변수 조작 X

// Dynamic Array처럼 이동/Resize 원리


https://www.hackerrank.com/challenges/ctci-array-left-rotation/problem?isFullScreen=true&h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=arrays