C program circular array rotation 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 &lt;math.h&gt;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;assert.h&gt;
#include &lt;limits.h&gt;
#include &lt;stdbool.h&gt;

int main(){
    int n; 
    int k; 
    int q; 
    scanf("%d %d %d",&amp;n,&amp;k,&amp;q);
    int *a = malloc(sizeof(int) * n);
    int *b = malloc(sizeof(int) * n);
    for(int a_i = 0; a_i &lt; n; a_i++){
       scanf("%d",&amp;a[a_i]);
    }
    for(int i = 0; i &lt; k; i++){
        b[0] = a[n-1];
        for(int a_i = 1; a_i &lt; n; a_i++){
        b[a_i] = a[a_i-1];
        }
        for(int a_i = 0; a_i &lt; n; a_i++) a[a_i] = b[a_i];
    }
    for(int a0 = 0; a0 &lt; q; a0++){
    int m; 
    scanf("%d",&amp;m);
    printf("%d\n", b[m]);
    }
    return 0;
}
Do not do actual rotation.

k rotations (from your code I understand that all rotations are to the right by 1) map an i'th element to an (i + k) % n position. At the time of printing, do some math to figure out which element was mapped to the position m.

The math is trivial, and I intentionally do not spell it out.