There is a task with hackerrank in short, you need to make a shift to the right by K, a certain number of times N (it is better to read the condition on hr because I very stupidly explained: C)

I got something like that but this is understandably wrong

var massiv = [1,2,3] func shift(var arr: [Int], much: Int) -> [Int] { for _ in 0..<much { var z = arr.removeFirst() arr.append(z) } return arr } shift(massiv, much: 2) 

Task text:

John Watson wants to check out Sherlock Holmes. He gave him an array of A0, A1 ... AN-1. Performed some array transformation, and then asked Sherlock Q questions. Sherlock feels that the transformation that John applied is called a cyclic right shift by K. A cyclic right shift by 1 converts the array A0, A1 ... AN-1 to AN-1, A0 ... AN-2. John applied the unit shift K times.

Help Sherlock answer the questions. Each question is described by an integer X, in response to a question, Sherlock must write out the element AX of the transformed array.

Input The first line contains three integers, space-separated, N, K, and Q. The next line contains N integers, space-separated, array A. Each of the following Q lines contains an integer X, a description of the current question.

Output For each question, output the corresponding element of the converted array.

Restrictions
1 <= N <= 105
1 <= A [i] <= 105
1 <= K <= 105
1 <= Q <= 500
0 <= X <= N-1

Sample Input # 00

3 2 3
1 2 3
0
one
2

Sample Output # 00
2
3
one
Explanation of examples
After the first cyclic shift by 1, the array will become: 3 1 2.
After the second, it will be equal to: 2 3 1.
The 0th element of the array is 2.
The 1st element of the array is 3.
The 2nd element of the array is 1.

  • But can the condition be copied to the question? - Max Mikheyenko

2 answers 2

It is a working solution for you. I can offer a couple more:

1 Classic solution

 func moveRight(arr: [Int], step: Int) -> [Int] { if step % arr.count == 0 { return arr } var array = arr var first: Int for _ in 0..<step % arr.count { first = array[0] for i in 0..<array.count-1 { array[i] = array[i+1] } array[array.count-1] = first } return array } 

2. I like the most:

 func moveRight(arr: [Int], step: Int) -> [Int] { if step % arr.count == 0 { return arr } return Array(arr.suffixFrom(step % arr.count) + arr.prefix(step % arr.count)) } 

3 Your optimized version:

 func moveRight(arr: [Int], step: Int) -> [Int] { if step % arr.count == 0 { return arr } var array = arr for _ in 0..<step % arr.count { let a = array.removeFirst() array.append(a) } return array } 

Well, about the right-wrong, you can dilute the discussion on almost any issue.

Average lead time:

 Количество элементов в массиве 10 Смещение 0..<10000 1 вариант 2 вариант 3 вариант 5.91185092926025e-06 1.23924016952515e-05 2.00728416442871e-05 Количество элементов в массиве 100 Смещение 0..<10000 1 вариант 2 вариант 3 вариант 0.000460050988197327 4.05452013015747e-05 0.000183036613464355 Количество элементов в массиве 1000 Смещение 0..<1000 1 вариант 2 вариант 3 вариант 0.00442803382873535 4.55737113952637e-05 0.000202417373657227 
  • one
    usually still put at the beginning if(step == arr.count) return arr; - Max Mikheyenko 6:01 pm
  • @MaxMikheyenko, exactly, thanks, corrected. - VAndrJ

It makes no sense to move objects or do something with an array, you just need to calculate the desired index. Here is my solution to the problem:

 let nkq = readLine()!.characters.split(" ").map{Int(String($0))!} let n = nkq[0] let k = n - nkq[1] % n let q = nkq[2] var arr = readLine()!.characters.split(" ").map{Int(String($0))!} for _ in 1...q { let i = (k + Int(readLine()!)!) % n print(arr[i]) }