criskgl
7/13/2019 - 3:42 PM

Element in array index after LEFT rotations

/*
*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));
  }
}