At first, let’s note that if we rotate the array by K units anticlockwise, it’s the same as the rotation by N-K units clockwise. So, from now on, we will consider only clockwise rotations. In case we have any anticlockwise rotation query, it can be reduced to the clockwise one.
When we rotate the array by K units anticlockwise, its’ first element becomes equal to the K mod N+1-th element of the initial array. The second element of this rotated array will be equal to (K+1) mod N+1-th element of the initial array, the third will be equal to the (K+2) mod N+1-th one, and so on (of course, we use 1-indexation in our considerations). So, using this facts one can simply get the M-th element of the array after a rotation.
What happens when we have several rotations? One can note that the rotations are “additive”, so we only need to maintain is the current “shift” variable that points to the first element of the current array after all the rotations. If this “shift” variable equals to R before some clockwise turn, it will be equal to (R+K) mod N after this turn. Basically, like it is described in the previous paragraph, we can get the M-th element of the current array as A[(R+M-1) mod N], where A is the array that was given initially.
In this solution we don’t actually rotate the array, we only operate with out “shift” variable (R above). So each operation is performed in O(1) time, so we get the total complexity of O(N+Q) where N is the size of the array and Q is the number of queries.
#include
using namespace std;
int main()
{
int M, N;
int A[100001];
int B[100001];
char operation;
int unit;
scanf("%d %d",&N, &M);
int j;
for ( int i = 1; i<= N; i++)
{
scanf("%d", &A[i]);
B[i] = A[i];
}
for ( int k = 0; k<M; k++)
{
fflush(stdin);
scanf("%c %d",&operation, &unit);
if ( operation == ‘R’)
{
printf("%d\n",A[unit]);
}
else if ( operation == ‘C’)
{
j = 1;
//A[0] = A[unit];
for ( int i = unit+1; i<=N; i++)
{
A[j] = B[i];
j++;
}
for ( int i = 1; i<=unit; i++)
{
A[j] = B[i];
j++;
}
for ( int i =1;i<=N;i++)
{
B[i] = A[i];
printf("%d “,B[i]);
}
printf(”\n");
}
else if ( operation == 'A')
{
j = 1;
for ( int i =1;i<unit;i++)
{
A[(N-(unit-i))+1] = B[i];
}
for ( int i=unit;i<=N;i++)
{
A[j] = B[i];
j++;
}
for ( int i =1;i<=N;i++)
{
B[i] = A[i];
printf("%d ",B[i]);
}
printf("\n");
}
}
return 0;
if(index>n)
index-=n;
But what if index=11 and n=5, then index=11-5=6, which is still greater. So instead use % operator, index=11%5=1,the correct ans. Hope this will help.
if(tempIndex>=n) tempIndex = tempIndex-n;
what is tempindex=11, and n=5, tempIndex=11-5=6, which is not a valid array index in this situation… Hope this will help, and happy coding.