0

I would like to know how could I take in a negative value for d in the following code. This means, how could I traverse the array backwards and rotate its elements.

catchArray(int [] arr, int d)
{
    int [] bArr = new int[arr.length];

    for(int i = 0; i < bArr.length; i++)
    {
        bArr[(i + d) % bArr.length] = arr[i];
    }

    return bArr;
}

This is my answer for LeetCode Rotate Array assignment. Thank you in advance.

2
  • What happened when you tried using a negative value for d? Commented Sep 22, 2019 at 1:11
  • I dont get the right index rotation and therefore out of bound. Commented Sep 22, 2019 at 1:15

2 Answers 2

1

Just normalize "d" at the start of the program. If it's negative, then correct it.

Let's say the array length is 10 and d is -1. You'd get the same rotation if length was 10 and d was 9. That's all the adjustment at the start of the function is doing.

catchArray(int [] arr, int d)
{
    d = d % arr.length;
    if (d < 0)
    {
        d += arr.length;
    }

    int [] bArr = new int[arr.length];

    for(int i = 0; i < bArr.length; i++)
    {
        bArr[(i + d) % bArr.length] = arr[i];
    }

    return bArr;
}
Sign up to request clarification or add additional context in comments.

Comments

0

If the array is e.g. size 10, then a -1 rotation is the same as a +9 rotation.

In Java, the % remainder operator will return a negative value, if the quotient is negative, e.g. -12 % 10 and -2 % 10 will both return -2.

So, to get a normalized rotation value, we do this:

MIN_VALUE <= d <= MAX_VALUE

d % size  ->  -size < d < size

+ size    ->  0 < d < size * 2

% size    ->  0 <= d < size  (normalized range)

Combined:

d = (d % arr.length + arr.length) % arr.length

That normalizes d to rotate less than one full round to the right.

static int[] catchArray(int[] arr, int d) {
    d = (d % arr.length + arr.length) % arr.length;
    int[] bArr = new int[arr.length];
    for (int i = 0; i < arr.length; i++) {
        bArr[(i + d) % arr.length] = arr[i];
    }
    return bArr;
}

Tests

System.out.println(Arrays.toString(catchArray(new int[] {1,2,3,4,5}, -8)));
System.out.println(Arrays.toString(catchArray(new int[] {1,2,3,4,5}, -2)));
System.out.println(Arrays.toString(catchArray(new int[] {1,2,3,4,5}, -1)));
System.out.println(Arrays.toString(catchArray(new int[] {1,2,3,4,5}, 0)));
System.out.println(Arrays.toString(catchArray(new int[] {1,2,3,4,5}, 1)));
System.out.println(Arrays.toString(catchArray(new int[] {1,2,3,4,5}, 2)));
System.out.println(Arrays.toString(catchArray(new int[] {1,2,3,4,5}, 8)));

Output

[4, 5, 1, 2, 3]
[3, 4, 5, 1, 2]
[2, 3, 4, 5, 1]
[1, 2, 3, 4, 5]
[5, 1, 2, 3, 4]
[4, 5, 1, 2, 3]
[3, 4, 5, 1, 2]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.