Skip to content

Instantly share code, notes, and snippets.

@netEmmanuel
Created August 23, 2019 09:03
Show Gist options
  • Save netEmmanuel/42994c786a7a90e733abe5409e96bc32 to your computer and use it in GitHub Desktop.
Save netEmmanuel/42994c786a7a90e733abe5409e96bc32 to your computer and use it in GitHub Desktop.
Solution to left rotate problem on hackerrank
public class Solution {
// Complete the rotLeft function below.
static int[] rotLeft(int[] a, int d) {
// get the size of the current array
int size = a.lenght;
// declare a new array and make it the same size as the original array
int[] rotated_array = new int[size];
// initialize a variable to zero
int i = 0;
// the number of times we are to left rotate is d
int rotate_index = d;
// so while the rotate index is still less than the size of the original array
while (rotate_index < size) {
// starting from the 4th index in the array will will insert the value into the
// new array
rotated_array[i] = a[rotate_index];
i++;
rotate_index++;
}
// declare the rotate_index back to zero
rotate_index = 0;
// while 0 is less the times we are to rotate for
while (rotate_index < d) {
// starting from the value at index zero to d we insert into the new array again
rotated_array[i] = a[rotate_index];
i++;
rotate_index++;
}
// we return the array
return rotated_array;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment