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 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();
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Arrays: Left Rotation
You are viewing a single comment's thread. Return to all comments →
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.