@@ -0,0 +1,2273 @@
+/**%0A * %0A */%0Apackage algorithms.datastructures;%0A%0Aimport java.util.Scanner;%0A%0A/**%0A * %0A * Challenge: https://www.hackerrank.com/challenges/array-left-rotation%0A * %0A * References:%0A * %0A * 1. http://www.geeksforgeeks.org/array-rotation/%0A * %0A * 2.%0A * http://www.geeksforgeeks.org/program-for-array-rotation-continued-reversal-%0A * algorithm/%0A * %0A * @author Sudharsanan Muralidharan%0A */%0Apublic class ArrayLeftRotation %7B%0A%0A  /**%0A   * Brute force approach. Rotate each item for n*d times.%0A   * %0A   * Time Complexity: O(n2), Space Complexity: O(1)%0A   * %0A   * @param arr%0A   * @param n%0A   * @param d%0A   */%0A  private static void rotate(int%5B%5D arr, int n, int d) %7B%0A    for (int i = 0; i %3C d; i++) %7B%0A      int temp = arr%5B0%5D;%0A      for (int j = 0; j %3C n - 1; j++) %7B%0A        arr%5Bj%5D = arr%5Bj + 1%5D;%0A      %7D%0A      arr%5Bn - 1%5D = temp;%0A    %7D%0A    print(arr);%0A  %7D%0A%0A  /**%0A   * Using a buffer array to copy the ranges.%0A   * %0A   * Time Complexity: O(n), Space Complexity: O(n)%0A   * %0A   * @param arr%0A   * @param n%0A   * @param d%0A   */%0A  private static void rotateUsingBufferArray(int%5B%5D arr, int n, int d) %7B%0A    int%5B%5D newArr = new int%5Barr.length%5D;%0A    System.arraycopy(arr, d, newArr, 0, n - d);%0A    System.arraycopy(arr, 0, newArr, (n - d), d);%0A    print(newArr);%0A%0A  %7D%0A%0A  /**%0A   * Reversing 0 to d-1 and then d to n-1 and then reverse the whole array from%0A   * 0 to n-1.%0A   * %0A   * Time Complexity: O(n), Space Complexity: O(1)%0A   * %0A   * @param arr%0A   * @param n%0A   * @param d%0A   */%0A  private static void rotateByReversing(int%5B%5D arr, int n, int d) %7B%0A    reverseRange(arr, 0, d - 1);%0A    reverseRange(arr, d, n - 1);%0A    reverseRange(arr, 0, n - 1);%0A    print(arr);%0A  %7D%0A%0A  private static void reverseRange(int%5B%5D arr, int start, int end) %7B%0A    while (start %3C end) %7B%0A      int temp = arr%5Bstart%5D;%0A      arr%5Bstart%5D = arr%5Bend%5D;%0A      arr%5Bend%5D = temp;%0A      start++;%0A      end--;%0A    %7D%0A  %7D%0A%0A  private static void print(int%5B%5D arr) %7B%0A    for (int i = 0; i %3C arr.length; i++) %7B%0A      System.out.print(arr%5Bi%5D + %22 %22);%0A    %7D%0A  %7D%0A%0A  public static void main(String%5B%5D args) %7B%0A    Scanner in = new Scanner(System.in);%0A    int n = in.nextInt();%0A    int d = in.nextInt();%0A    int%5B%5D arr = new int%5Bn%5D;%0A%0A    for (int i = 0; i %3C n; i++) %7B%0A      arr%5Bi%5D = in.nextInt();%0A    %7D%0A%0A    rotateByReversing(arr, n, d);%0A%0A    in.close();%0A  %7D%0A%7D%0A

Add solution for ArrayLeftRotation