I have made a simple program of rotation of integers in a given array.
#include <iostream>
using namespace std;
int main()
{
int t;
cin>>t;
long i,j,n,k,l,m,x;
long a[1000000];
for(i=0;i<t;i++){
cin>>n;
cin>>k;
for(j=0;j<n;j++){
cin>>a[j];
}
for(m=0;m<k;m++){
x=a[0];
for(j=1;j<n;j++){
x=a[j]+x;
a[j]=x-a[j];
x=x-a[j];
}
a[0]=x;
}
for(j=0;j<n;j++){
cout<<a[j]<<" ";
}
cout<<endl;
}
return 0;
}
The question can be found here. My code processes the small inputs easily, but when the input reaches to the order of thousands it takes well over a second and it fails due to that. Any suggestions on how to solve this problem?