#include <stdio.h>
void reverse_array(int arr[], int i, int j) {
// Swap elements from front and back until the middle is reached
while (i < j) {
// Temporarily hold one element
int temp = arr[i];
// Swap front and back
arr[i] = arr[j];
arr[j] = temp;
// Move indices towards the center
i++;
j--;
}
}
void rotate_left(int arr[], int n, int k) {
if (k > n) {
k = k % n;
}
reverse_array(arr, 0, (k - 1));
reverse_array(arr, k, (n - 1));
reverse_array(arr, 0, (n - 1));
}
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