You are viewing a single comment's thread. Return to all comments →
No need to create a new array.
You can calculate the originalIndex of the given newIndex and just print a[originalIndex].
I'm not a java developer. So I don't know the syntax properly. But the answer will most likely be something like this.
static int[] circularArrayRotation(int[] a, int k, int[] queries) { int[] result = new int[queries.length]; int l = a.length; int index = 0; k = k%l; for (int i = 0;i<a.length;i++,index++) { int queryIndex = queries[i]; int originIndex; if (queryIndex<k) { originIndex = l-k+queryIndex; } else { originIndex = queryIndex-k; } result[index] = a[originIndex]; } return result; }
Seems like cookies are disabled on this browser, please enable them to open this website
Circular Array Rotation
You are viewing a single comment's thread. Return to all comments →
No need to create a new array.
You can calculate the originalIndex of the given newIndex and just print a[originalIndex].
I'm not a java developer. So I don't know the syntax properly. But the answer will most likely be something like this.