Rotate Left
Completed
Read n integers and k. Rotate the array left by k positions in place (one step at a time) and print it.
Input
n, the integers, then k.
Output
One line.
Example
Input
5 1 2 3 4 5 2
Output
3 4 5 1 2
Editor
C
Hints
Save the first element, shift everything left, put it at the end.
Solution explanation
k % n avoids doing full rotations that change nothing.
Show solution
#include <stdio.h>
int main() {
int n, k, a[100];
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
scanf("%d", &k);
k = k % n;
for (int step = 0; step < k; step++) {
int first = a[0];
for (int i = 0; i < n - 1; i++) a[i] = a[i + 1];
a[n - 1] = first;
}
for (int i = 0; i < n; i++) {
printf("%d", a[i]);
if (i < n - 1) printf(" ");
}
printf("\n");
return 0;
}