public int[] leftRotate(int[] A)
{
for (int i = 0; i < A.length -1; i++)
{
if (i == 0)
{
int x = A[i];
A[i] = A[A.length - 1];
A[A.length - 1] = x;
}
else
{
int x = A[i];
A[i] = x;
A[i - 1] = x;
}
}
return A;
}
public int[] leftRotate(int[] A, int k)
{
for (int i = 0; i < A.length -k; i++)
{
if (i < k)
{
int x = A[i];
A[i] = A[A.length - (k-i)];
A[A.length - (k-i)] = x;
}
else
{
int x = A[i];
A[i] = A[i - k];
A[i - k] = x;
}
}
return A;
}
I do not know what the return type of the method needs to be, but if it is void, then remove the return A. Otherwise, I would assume it returns an int array, so the code above should work as it modifies the array in place too and has no need of creating a new array as you are just swapping the elements.