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.

## 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?