---
# System prepended metadata

title: L2-CyclicRotation
tags: [Codility_lessons]

---

# L2-CyclicRotation
###### tags: `Codility_lessons`
## Question
https://app.codility.com/programmers/lessons/2-arrays/cyclic_rotation/
## Key
最後一個值移到第一個位置，其他值往後移一個位置；就等於從後面最後一個值開始相鄰值兩兩交換
## Reference
## Solution
```cpp=
#include <stdlib.h>
#include <vector>
#include <iostream>


using namespace std;


vector <int> solution(vector<int> &A, int K)
{
    if(!A.empty())
    {
            int swap;
        for ( int i=0; i<K; i++)
        {
            for (unsigned int j=A.size()-1; j>0; j--)
            {
                swap = A[j];
                A[j] = A[j-1];
                A[j-1] = swap;
            }   
        }
        return A;
    }
    
    return {};
}
```