title: How to write recursive left-shift array algorithm more efficiently
body: <p>I was completing some practice problems today on HackerRank, and on a problem asking me to write an algorithm that left-shifts all elements in an array n-times (i.e. rotLeft({1, 2, 3, 4, 5}, 1) returns {2, 3, 4, 5, 1}), I ran into the classic timeout error due to my algorithm being inefficient. This isn't the first time I have been dinged for inefficient algo writing by an online coding system. Really I have two questions: 1) How can I specifically rewrite my left-shift algorithm to be more time-efficient? 2) In general, how do I improve performance-wise upon an algorithm which runs inefficiently?</p>
  
  <pre><code>public static final int NULL = -2147483648;
  
  static int[] rotLeft(int[] a, int d) {
      return rotLeftRec(a, d, 0);
  }
  static int[] rotLeftRec(int[] a, int d, int numRot) {
      if (numRot &gt;= d) {
          return a;
      } else {
          int first = a[0];
          int temp1 = NULL;
          int temp2 = NULL;
          for (int i = a.length - 1; i &gt;= 0; i--) {
              if (i == a.length - 1) {
                  temp1 = a[i];
              } else {
                  if (temp1 == NULL) {
                      temp1 = a[i];
                      a[i] = temp2;
                      temp2 = NULL;
                  } else {
                      temp2 = a[i];
                      a[i] = temp1;
                      temp1 = NULL;
                  }
              }
          }
          a[a.length - 1] = first;
      }
      return rotLeftRec(a, d, numRot + 1);
  }
  </code></pre>