Hacker Rank: Array left rotation This code is to solve the Hacker Rank problem array left rotation.

A left rotation operation on an array of size n shifts each of the array's elements 1 unit to the left. For example, if 2 left rotations are performed on array [1,2,3,4,5], then the array would become [3,4,5,1,2].

Given an array of n integers and a number, d, perform d left rotations on the array. Then print the updated array as a single line of space-separated integers.

public static int[] rotLeft(int[] a, int d)
    {
        for (int intI = 0; intI &lt; d; intI++)
        {
            int temp = 0;
            temp = a[0];
            for (int intK = 0; intK &lt; a.Length; intK++)
            {
                a[a.Length - (a.Length - intK)] = a.Length - 1 == intK ? temp : a[a.Length - (a.Length - (intK + 1))];
            }
        }
        return a;
    }
    static void Main(string[] args)
    {
        string[] nd = Console.ReadLine().Split(' ');
        int n = Convert.ToInt32(nd[0]);
        int d = Convert.ToInt32(nd[1]);
        int[] a = Array.ConvertAll(Console.ReadLine().Split(' '), aTemp =&gt; Convert.ToInt32(aTemp));
        int[] result = rotLeft(a, d);
        Console.WriteLine(string.Join(" ", result));
    }


This program works fine, but it takes too long with some examples. How can I improve it?
About naming:

intI and intK: don't include the type in the variable name, it is obvious from the context and intellisense and as a loop index a plain i and k are more understandable.



A first simple optimization is that you can avoid the check if intK has reached the end: 


  a.Length - 1 == intK ?


Instead you can just iterate up to a.Length - 1 and then append temp at the end:

public static int[] rotLeft(int[] a, int d)
{
  for (int intI = 0; intI &lt; d; intI++)
  {
    int temp = a[0];

    for (int intK = 0; intK &lt; a.Length - 1; intK++)
    {
      a[a.Length - (a.Length - intK)] = a[a.Length - (a.Length - (intK + 1))];
    }

    a[a.Length - 1] = temp;
  }

  return a;
}




Next step is to consider the math:

a.Length - (a.Length - intK) = a.Length - a.Length + intK = intK


and in the same way:

a.Length - (a.Length - (intK + 1)) = intK + 1


So you could write:

public static int[] rotLeft(int[] a, int d)
{
  for (int intI = 0; intI &lt; d; intI++)
  {
    int temp = a[0];

    for (int intK = 0; intK &lt; a.Length - 1; intK++)
    {
      a[intK] = a[intK + 1];
    }

    a[a.Length - 1] = temp;
  }

  return a;
}




But the real performance problem is that you move each entry in the array d number of times. You can move each entry just once by moving it d places. A simple way to do that could be:

public static int[] rotLeft(int[] a, int d)
{
  int[] temp = new int[d];

  for (int i = 0; i &lt; d; i++)
    temp[i] = a[i];

  for (int i = d; i &lt; a.Length; i++)
  {
    a[i - d] = a[i];
  }

  for (int i = 0; i &lt; d; i++)
    a[a.Length - d + i] = temp[i];

  return a;
}




Another issue is that you operate on the input array a directly and return it as a return value. In this way both the return value and a contains the shifted values. In a challenge like this it may not be important, but I think I would return a new array with the shifted data leaving a unchanged - in "real world":

public static int[] rotLeft(int[] a, int d)
{
  int[] result = new int[a.Length];

  for (int i = d; i &lt; a.Length; i++)
  {
    result[i - d] = a[i];
  }

  for (int j = 0; j &lt; d; j++)
  {
    result[a.Length - d + j] = a[j];
  }

  return result;
}


If you want to operate on a directly intentionally it would be more consistent returning void to signal that you're operating on the input directly.