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 created a queue of integers from the array, and enqueued its first integer back into it. I got the first integer by dequeuing the queue. I do this times the number of rotations given in the input, k.

In other words:
Dequeue() gives me the first element in the queue, which I then Enqueue(int) into the queue itself.

This goes on until the number of rotations, k, becomes 0. I decrease k by 1 at each while cycle.

I went with an alternative method in C# which has the advantage of not actually carrying out the rotations and instead just calculates and returns the end-state.

static int[] rotLeft(int[] a, int d) {
// If d is greater than the length of the array,
// we can skip one rotation entirely
// therefore we only have to execute the FINAL rotation
// which is the remainder
int remainder = d % a.Length;
// by creating an ArraySegment in C# we do not move any data
// around in memory.
// ArraySegment creates an in-place
// enumerator using references.
var front = new ArraySegment<int>(a, 0, remainder);
var back = new ArraySegment<int>(a, remainder, a.Length - remainder);
// finally we return the two segments joined together
// and convert it to an array.
return back.Concat(front).ToArray();
}

## Arrays: Left Rotation

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

C#:

Well done man. But can you just explain it how it works? Thank you btw.

Thank you Stefan :)

I created a queue of integers from the array, and enqueued its first integer back into it. I got the first integer by dequeuing the queue. I do this times the number of rotations given in the input, k.

In other words:

Dequeue() gives me the first element in the queue, which I then Enqueue(int) into the queue itself.

This goes on until the number of rotations, k, becomes 0. I decrease k by 1 at each while cycle.

while(k-- > 0) { queue.Enqueue(queue.Dequeue()); }

Example:

Array: 1 2 3 4 5

k: 4

k = 4:

Dequeue: get [1]

Array: 2 3 4 5

Enqueue[1]: 2 3 4 5 1

k = 3:

Dequeue: get [2]

Array: 3 4 5 1

Enqueue[2]: 3 4 5 1 2

k = 2:

Dequeue: get [3]

Array: 4 5 1 2

Enqueue[3]: 4 5 1 2 3

k = 1:

Dequeue: get [4]

Array: 5 1 2 3

Enqueue[4]: 5 1 2 3 4

k = 0:

STOP

Hope it is clear :)

Yeah it's clear! I had to draw the queue to understand what you've done :D Thank you again!

I had a very similar solution. Why did you return via a for-loop instead of returning the array?

Too short way for Array Rotation

Left Rotation:def array_left_rotate(n, d): return n[d - 0:len(n) ] + (n[0:d - len(n)]) array_left_rotate([1,2,3,4,5],2)

Right Rotation:def array_right_rotate(n, d): return (n[len(n) - d:len(n)]) + n[0:len(n) -d ] array_right_rotate([1,2,3,4,5,6],2)

Really nice way of using queues, I haven't thought of that.

I went with an alternative method in C# which has the advantage of not actually carrying out the rotations and instead just calculates and returns the end-state.