#include <stdio.h>
void rotate_left(int arr[], int n, int k) {
// handle case where rotations are useless
if (n < 2)
return;
// make sure number of rotations not bigger than array
k %= n;
// perform k rotations
while (k > 0) {
// backup first element because about to be lost
int backup = arr[0];
for (int i = 1; i < n; i++) {
// move each element to the previous position
arr[i-1] = arr[i];
}
// put backup element at the end
arr[n-1] = backup;
// prepare next rotation
k--;
}
}
int main() {
int n, k;
scanf("%d %d", &n, &k);
int arr[100];
// Read array elements
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Rotate the array
rotate_left(arr, n, k);
// Print the rotated array
for (int i = 0; i < n; i++) {
printf("%d", arr[i]);
if(i < n-1){
printf(" ");
}
}
return 0;
}
Input
5 2 1 2 3 4 5
Expected Output
3 4 5 1 2