Created
August 23, 2019 09:03
-
-
Save netEmmanuel/42994c786a7a90e733abe5409e96bc32 to your computer and use it in GitHub Desktop.
Solution to left rotate problem on hackerrank
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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