Sort by

recency

|

3111 Discussions

|

  • + 0 comments

    Here is **circular array rotation solution in Python, java, c++, c and javascript **- https://programmingoneonone.com/hackerrank-circular-array-rotation-problem-solution.html

  • + 0 comments

    ts solution:

    function circularArrayRotation(a: number[], k: number, queries: number[]): number[] { for(let i=0; i<k; i++){ let popped = a.pop() ?? 0; a.unshift(popped); } return queries.map(i => a[i]); } 
  • + 0 comments

    List res=new ArrayList<>(); int n=a.size(); k=k%n; for(int q:queries){ int newIndex=(q-k+n)%n; res.add(a.get(newIndex)); } return res;

  • + 0 comments

    Test failed succesfully???

    Test case 4 says success but it's marked red, as if it failed?

  • + 0 comments
    def circularArrayRotation(a, k, queries): # Example: For array [1, 2, 3, 4, 5] (n=5): # k=0: [1, 2, 3, 4, 5] (no rotation) # k=1: [5, 1, 2, 3, 4] (rotated right once) # k=2: [4, 5, 1, 2, 3] (rotated right twice) # ... # k=5: [1, 2, 3, 4, 5] (full rotation, back to original) # To rotate efficiently without multiple single shifts: # 1. Calculate the effective rotation offset using modulo arithmetic # (since rotating by n positions brings array back to original) # 2. Split the array at the offset and swap the two parts # Calculate the rotation point offset = len(a) - k % len(a) # Perform the rotation by concatenating the two slices rotated = a[offset:] + a[:offset] # Return the elements at the requested indices return [rotated[q] for q in queries]