# Geeksforgeeks array rotation program

program:
input: testcases
n=size of array d=rotations of array
then n elements of array
input: 2
5 2
1 2 3 4 5
10 3
1 2 3 4 5 6 7 8 9 10
output ;
3 4 5 1 2
4 5 6 7 8 9 10 1 2 3

and my code is getting tle can anyone helppp and iam using stl vector in c++

#include
#include
using namespace std;
int main()
{
int tc,k,i,n,d,val;
scanf("%d",&tc);
//vectorv;
for(k=0;k<tc;k++)
{
scanf("%d",&n);
scanf("%d",&d);
//v.resize(n);
vectorv(n);
for(i=0;i<n;i++)
{
scanf("%d",&v[i]);
}
for(i=0;i<d;i++)
{
v.push_back(v[0]);
v.erase(v.begin());
}
for (i=0;i<v.size();++i)
printf("%d “,v[i]);
printf(”\n");
v.clear();
}
return 0;
}

since you are deleting element from beginning , hence all the element will be shifted to left to fill the empty place, this would be done in O(n) time , to perform d deletion total time would be O(d*n) which will surely result in TLE.

2 Likes

you can solve this problem without reversing at all.
for given d , start printing element from position d+1 to n , after that start printing element from 1 to d.
for input
10 3
1 2 3 4 5 6 7 8 9 10

start printing elements from 4th position (1 based indexing) to the 10th position
4 5 6 7 8 9 10
after that print the remaining elements from position 1 to d
hence print
1 2 3

tnq broo…
can you tell how to declare array of large size at runtime???without using pointers???in c++

just declare a global array of maximum size and in each test case use the same array.
or you can use vectors
or
cin>>n;
int ar[n];

any method would work

okk bro could i get your whatapp number if i get any doubts i will ask you as you are a 4 star coder in codechef

you can write me mail at icodencode@gmail.com , i would surely reply when i get time.
here is my youtube channel where i teach algo/ds : CodeNCode

beside you can post here , there a lot of people who can help you.

in whatapp it is pretty easy to ask doubts thats why iam asking number?i am also subscriber of your channel bro

fine , you can join whatsapp group for CP here :