# Juggling Technique

Juggling technique is an efficient technique for rotating arrays. It is based on the concept of finding the greatest common divisor (GCD) of the array seize and the number os steps to rotate.

## Juggling Technique Code Template

Here is an example Python code that uses the Jugging Technique to rotate an integer array to the right by k steps.

```python

def find_gcd(a,b):
    if b==0:
        return a
    else:
        return find_gcd(b,a%b)

def rotate(nums,k):
    n=len(nums)
    k%=n
    gcd=find_gcd(n,k)

    for i in range(gcd):
        temp=nums[i]
        j=i

        while True:
            d=(j+k)%n

            if d==i:
                break
            nums[j]=nums[d]
            j=d
        nums[j]=temp
```

## Juggling Technique Problems

Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array `[1,2,3,4,5,6,7]` is rotated to `[5,6,7,1,2,3,4]`. This is kind of a sliding window problem.

[Rotate Array](https://leetcode.com/problems/rotate-array/)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://aisuko.gitbook.io/wiki/freesoftware/algorithm/juggling_technique.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
