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.

It can be optimized in terms of space if you don't store the whole array, but only the rotated elements. If the elements are 1 2 3 ... 100000, and the number of left rotations is 2, you can save the first two elements (1 and 2) and directly print the rest as soon as you read them. Finally, print the saved ones. The difference in space is from 100000 ints to 2 ints.

no no, if you have 1000 rotations in an array of 30, you have 1000/30 complete rotations (rotations of the whole array) and 1000%30 effective left rotations. So you store only the first 1000%30 elements. Sorry for the initial incomplete explanation, my fault.

## Left Rotation

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

It can be optimized in terms of space if you don't store the whole array, but only the rotated elements. If the elements are 1 2 3 ... 100000, and the number of left rotations is 2, you can save the first two elements (1 and 2) and directly print the rest as soon as you read them. Finally, print the saved ones. The difference in space is from 100000 ints to 2 ints.

what if I have thousand rotation??then I have to store thousands of int?

no no, if you have 1000 rotations in an array of 30, you have 1000/30 complete rotations (rotations of the whole array) and 1000%30 effective left rotations. So you store only the first 1000%30 elements. Sorry for the initial incomplete explanation, my fault.

appreciated!!bt still don't you think it will require alot memory??even if u will use 1000%30

I think on the place to store those elements it will b more efficient to jst show those elements.