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.

I'm failing to see where it actually does a left rotate of an existing array.

What I'm seeing is (from assumption of ...)
array[0..n] is empty initially.
getArray[0..n] stores input from stdin.
store rotated result into array[0..n].

The existing array is getArray. That array is still in an unrotated order. The array[0..n] isn't considered the exiting array as it is empty.

Challenge to you: get rid of getArray array and store input into array[] array. Rotate that array and then print the end result of that array.

This code is actually quite efficient. So for the following values, n=5 (number of elements in the array), d=2 (number of rotations)and assuming i goes from 0-4.
(i + n - d)%n with the iteration of i from 0-4:
(0+5-2)%5=3
(1+5-2)%5=4
.
.
.
(4+5-2)%5=2
So essentially, you are changing the locations of the elements in the array. How the "%" works is that it takes the remainder. So for example, 1%7=1 since 1 cannot be divided by 7 which means 1 is the remainder.

## Left Rotation

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

I like you code some much.PLEASE can you add some explainatioin.I will be very glad

It just generates an array first from Stdin, then illustrates the way to left-rotate an EXISTING array.

I'm failing to see where it actually does a left rotate of an existing array.

What I'm seeing is (from assumption of ...) array[0..n] is empty initially. getArray[0..n] stores input from stdin. store rotated result into array[0..n].

The existing array is getArray. That array is still in an unrotated order. The array[0..n] isn't considered the exiting array as it is empty.

Challenge to you: get rid of getArray array and store input into array[] array. Rotate that array and then print the end result of that array.

This code is actually quite efficient. So for the following values, n=5 (number of elements in the array), d=2 (number of rotations)and assuming i goes from 0-4. (i + n - d)%n with the iteration of i from 0-4: (0+5-2)%5=3 (1+5-2)%5=4 . . . (4+5-2)%5=2 So essentially, you are changing the locations of the elements in the array. How the "%" works is that it takes the remainder. So for example, 1%7=1 since 1 cannot be divided by 7 which means 1 is the remainder.

that's great, can you provide any mathematical illustration to arrive (i+n-d)%n ?