Last active
July 13, 2019 15:43
-
-
Save criskgl/3684a8b214bd639f40f680f7ca6a4678 to your computer and use it in GitHub Desktop.
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
/* | |
*In an array a | |
*figures out the element e | |
*in an index after performing r LEFT rotations | |
*/ | |
public class Tragaperras { | |
static int circularLeftArrayRotation(int[] a, int r, int index) { | |
int l = a.length; | |
int R = r - (r/l*l); | |
//Get rid of over-rotations | |
//E.g | |
//length of array = 5 | |
//rotations = 13 | |
//we will only have to perform 2 rotations. | |
//13 - ((13/2)*5) | |
int e = 0;//element in index after rotations | |
int resultIndex = index + R; | |
if(resultIndex > l - 1){ | |
resultIndex = resultIndex-l; | |
}else{ | |
e = a[resultIndex]; | |
} | |
return e; | |
} | |
public static void main(String[] args) { | |
int[] a = {1, 2, 3}; | |
int r = 2; | |
int index = 0; | |
System.out.println(circularLeftArrayRotation(a, r, index)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment