educative.io

Diagram error, leetcode link and alternative solution

Errata

  1. The first diagram in the problem statement shows the rotation in other way round.
    It is N = -1 rotation and N = 2 rotation (instead of N = 1 and N = -2).

  2. Hint button doesn’t work properly.

Leetcode link
Leetcode - rotate-array
Note the Leetcode problem doesn’t include negative k rotations.

Alternative solution

The provided solution is good, this is a similar approach (Same space & time complexity):

class rotateArray{
  public static void rotate_array(List<Integer> arr, int k) {
k = k % arr.size();
reverse(arr, 0, arr.size());    
if (k < 0) {
  reverse(arr, arr.size() + k, arr.size());
  reverse(arr, 0, arr.size() + k);       

} else {
  reverse(arr, 0, k);
  reverse(arr, k, arr.size());  
}
  }
  
  public static void reverse(List<Integer> nums, int start, int end) {
    for (int i = start; i < start + (end - start) / 2; i++) {
        int temp = nums.get(i);
        int reverseIdx = end - 1 - (i - start);
        nums.set(i, nums.get(reverseIdx));
        nums.set(reverseIdx, temp);
    }
}  
}

Hi Wonjoon_Seol,

Thanks for pointing it out and it’s highly appreciated. I’ve changed the ‘N’ values and you’re right the given values are wrong and as illustrations are given so I’ve removed the hint part. The solution you provided works in same runtime and space complexity, so don’t need to update that.

Regards
Team Educative

Hi, I think the solution 1 for Python doesn’t work.
For array = [1,10,20,0,59,86,32,11,9,40] and N = 2, it’s providing output [0,59,86,32,11,9,40,1,10,20]

Hi Ashutosh,

Thank you for pointing the issue out, we’ve revised the code. Feel free to have a look!

If you have any further queries, please let us know.

Best Regards,
Ayesha Basit | Technical Content Developer