We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

Its easy man i cant figure out the last test case though !!

1.Input an array of size n
2.Input number of rotations as r
3. r=r%n -> it reduces the number of repeatative rotations thus decreasing the complexity of our code . suppose the size of the array is 3 and number of rotations is also 3 then our loop wont run it will simply print the same elements .

take a temperory variable and store the leftmost element in it i.e a[o]

Now run the loop from 0 to second last element of the array
6.a[i]=a[i+1] -> shifts the element towards left for eg. for i=0 a[0]=a[1]; it shifts all the elements towards left
7.now simply store the first element in temp variable to the last position.

steps 4 to 7 for first rotation now while loop will run as per the given number of rotations these steps will be repeated until r becomes 0.

Keep in mind a left rotation of x is equal to (len-x) of righ rotations. Hence, it would be more efficient to the rotation with least amount of "moves".

## Left Rotation

You are viewing a single comment's thread. Return to all comments →

I did solve it! It was proably beacuse of the nested loops! they take more time to run!

which is known as complexity

how did you solve it ?

Its easy man i cant figure out the last test case though !!

1.Input an array of size n 2.Input number of rotations as r 3. r=r%n -> it reduces the number of repeatative rotations thus decreasing the complexity of our code . suppose the size of the array is 3 and number of rotations is also 3 then our loop wont run it will simply print the same elements .

:)

Man i have better version of solution. See my code.

vector array_left_rotation(vector a, int n, int k) { rotate(a.begin(),a.begin()+k,a.end()); return a; }

int main(){ int n; int k; cin >> n >> k; vector a(n); for(int a_i = 0;a_i < n;a_i++){ cin >> a[a_i]; } vector output = array_left_rotation(a, n, k); for(int i = 0; i < n;i++) cout << output[i] << " "; cout << endl; return 0; }

chup.. space dedeta kuch to

Doesn't work for 8th test case

how did u solve??

Keep in mind a left rotation of x is equal to (len-x) of righ rotations. Hence, it would be more efficient to the rotation with least amount of "moves".

Thats true. I got accepted for test case 8 using right rotation. We need to choose :

If #rotations < (size /2) Use Left Rotation If #rotations > (size /2) Use Right Rotation

can you elaborate @crystalcode