I'm trying to solve a Hackerrank problem where you perform k
array[n]
rotations, and at the end, ask for m
and print array[m]
. My program is probably correct (it runs perfectly for most of tests, but some of them terminate due to timeout), but is inefficient and I don't know how to improve it.
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
int n;
int k;
int q;
scanf("%d %d %d",&n,&k,&q);
int *a = malloc(sizeof(int) * n);
int *b = malloc(sizeof(int) * n);
for(int a_i = 0; a_i < n; a_i++){
scanf("%d",&a[a_i]);
}
for(int i = 0; i < k; i++){
b[0] = a[n-1];
for(int a_i = 1; a_i < n; a_i++){
b[a_i] = a[a_i-1];
}
for(int a_i = 0; a_i < n; a_i++) a[a_i] = b[a_i];
}
for(int a0 = 0; a0 < q; a0++){
int m;
scanf("%d",&m);
printf("%d\n", b[m]);
}
return 0;
}