Rotate Array


I wrote something to help understanding the third Cyclic Replacements approach:interpretation/proof

Consider the following enhanced version of Approach #3 Using Cyclic Replacements in C++.
It first converts the problem into the equivalent rotation to the left of n  (k % n) positions. Then rotate each cycle directly in the vector, storing in the temp variable the first element only.
class Solution { public: void rotate(vector<int>& nums, int k) { int n = nums.size(); k = n  (k % n); int count = 0; for (int begin = 0; count < n; begin++) { int temp = nums[begin]; int curr = begin; for (int next = (curr + k) % n; next != begin; next = (curr + k) % n) { nums[curr] = nums[next]; curr = next; count++; } nums[curr] = temp; count++; } } };

This solution doesn't work for the following condition
Input: array: [1,2,3,4,5] and k = 4
Output: [5,1,2,3,4]
Could anyone tell me what is the issue with this code?def rotateArray(nums, k): k = k%len(nums) end = len(nums)  1 if k is None or k<= 0: return reverse(nums, 0, end  k) reverse(nums, end  k + 1, end) return reverse(nums, 0, end) def reverse(nums, start, end): while start < end: nums[start], nums[end] = nums[end],nums[start] start += 1 end = 1 return nums print rotateArray([1,2,3,4,5], 4)

//Example for Rotate Array in C [Accepted] void rotate(int* nums, int numsSize, int k) { int *nArry = malloc(numsSize*sizeof(int)); if(numsSize<k) k %= numsSize; for(int i=0; i<numsSize; i++) if(i<(numsSizek)) nArry[i+k] = nums[i]; else nArry[i(numsSizek)] = nums[i]; for(int i=0; i<numsSize; i++) nums[i]=nArry[i]; }

So instead of doing a deep copy at the end, I basically set the reference and it did not work:
class Solution {
public void rotate(int[] nums, int k) {
int[] a = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
a[(i + k) % nums.length] = nums[i];
}
nums = a;
}
}I thought arrays were objects in Java and thus reference types.

@emkk Hey dude, I have the same quesiont with you. Took me some while but check this out https://leetcode.com/playground/new
See how the main class call the solution, it is passing an array to nums, thus any redirect of nums only change what it is points to, we have to make sure the ORIGINAL array object is changed, not redirecting the reference.

@emkk Update, the link I put in the initial reply not valide. You have to click the >_ icon in the question to enter mode
Debug Code in playground
and view the code for main class there.