Solution Using Temporary Array
Input arr[] = [10, 12, 23, 40, 53, 36, 78], d = 2, n =7
1) Store d elements in a temp array
temp[] = [10, 12]
2) Shift rest of the arr[]
arr[] = [23, 40, 53, 36, 78, 36, 78]
3) Store back the d elements
arr[] = [23, 40, 53, 36, 78, 10, 12]
Time complexity O(n)
Auxiliary Space: O(d)
Implementation:
#include <stdio.h>
int main() {
long num, rot;
scanf("%ld %ld", &num, &rot);
unsigned long arr[num];
unsigned temp[rot];
for(long loop = 0; loop < num; loop++) {
scanf("%lu", &arr[loop]);
}
for(long loop = 0; loop < rot; loop++) {
temp[loop] = arr[loop];
}
for(long loop = rot; loop < num; loop++) {
arr[loop - rot] = arr[loop];
}
long index = 0;
for(long loop = (num - rot); loop < num; loop++) {
arr[loop] = temp[index++];
}
for(long loop = 0; loop < num; loop++) {
printf("%lu ", arr[loop]);
}
return 0;
}