Circular Array Rotation C++ HackerRank I solved the Circular-Array-Rotation problem on HackerRank.  I conceded defeat and looked at the answers given on HackerRank itself but the ones I saw used things like vectors, which are concepts I'm a noob at as my knowledge of C++ thus far extends only as far as virtual functions and classes and such (junior COE student). So here's the description of the problem:


  John Watson performs an operation called a right circular rotation on an array of integers, \$a[0], a[1], \dotsc, a[n-1]\$. After performing one right circular rotation operation, the array is transformed from \$a[0], a[1], \dotsc, a[n-1]\$ to \$a[n-1], a[0], \dotsc, a[n-2]\$.
  
  Watson performs this operation \$k\$ times. To test Sherlock's ability to identify the current element at a particular position in the rotated array, Watson asks \$q\$ queries, where each query consists of a single integer, \$m\$, for which you must print the element at index \$m\$ in the rotated array (i.e., the value of \$a[m]\$).
  
  Input Format
  
  The first line contains 3 space-separated integers, \$n\$, \$k\$, and \$q\$, respectively. 
  
  The second line contains \$n\$ space-separated integers, where each integer \$i\$ describes array element \$a[i]\$ (where \$0 \le i &lt; n\$). 
  Each of the \$q\$ subsequent lines contains a single integer denoting \$m\$.
  
  Constraints
  
  \begin{align*}
  1 &amp;\le n &amp;&amp;\le 10^5 \\
  1 &amp;\le a[i] &amp;&amp;\le 10^5 \\
  1 &amp;\le k &amp;&amp;\le 10^5 \\
  1 &amp;\le q &amp;&amp;\le 500 \\
  0 &amp;\le m &amp;&amp;\le n - 1
\end{align*}
  
  Output Format
  
  For each query, print the value of the element at index \$m\$ of the rotated array on a new line.
  
  Sample Input

3 2 3
1 2 3
0
1
2

  
  Sample Output

2
3
1

  
  Explanation
  
  After the first rotation, the array becomes \$[3,1,2]\$. 
  After the second (and final) rotation, the array becomes \$[2,3,1]\$.
  
  Let's refer to the array's final state as array \$b\$. For each query, we just have to print the value of \$b[m]\$ on a new line:
  
  
  \$m = 0\$, so we print 0 on a new line.
  \$m = 1\$, so we print 1 on a new line.
  \$m = 2\$, so we print 2 on a new line.
  


This is my code, sadly it only works for a few of the test cases, and times out for the others. Any feedback welcome, including suggestions on learning new methods or anything.

int n;
int k;
int q;
cin &gt;&gt; n &gt;&gt; k &gt;&gt; q;
int *arr = new int[n];
int *query = new int[q];
for (int i = 0; i &lt; n; i++)
{
    cin &gt;&gt; arr[i];
}
for (int j = 0; j &lt; q; j++)
{
    cin &gt;&gt; query[j];
}

int *arr2 = new int[n];
int count = 0;
while (count &lt; k)
{
    int temp = arr[n - 1];
    for (int j = 1; j &lt; n; j++)
    {
        arr2[j] = arr[j - 1];
    }
    arr2[0] = temp;
    for (int j = 0; j &lt; n; j++)
    {
        arr[j] = arr2[j];
    }
    count++;
}

for (int m = 0; m &lt; q; m++)
{
    cout &lt;&lt; arr2[query[m]] &lt;&lt; endl;
}
Rather than performing a rotation k times, perform one rotation of k places.

So, you have an array [5, 4, 3, 2, 1] and want to rotate it 3 places:


Create a new array containing the results
Start iterating through the array, calculating the index in the new array and assigning the values
Starts at i = 0 (value 5). Wants a rotation of 3, so newArray[i + k] = array[i]
Continues this up to i = 2, where we find a hitch: newArray[i + k] = array[i], a.k.a. newArray[5] = array[2], so out of bounds. Uh oh!
We must make the assignment cycle around, so we must use mod with the size of the array to produce a single iteration over the initial array with this: newArray[(i + k) % n] = array[i]