Skip to content

Commit 9ae8a5e

Browse files
authored
Create rotation.py
1 parent d9d2c67 commit 9ae8a5e

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

data_structures/array/rotation.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
def gcd(a, b):
2+
if b == 0:
3+
return a
4+
return gcd(b, a%b)
5+
6+
7+
def rotate(arr, d):
8+
n = len(arr)
9+
g = gcd(n, d)
10+
11+
for i in range(g):
12+
temp = arr[i]
13+
j = i
14+
15+
while 1:
16+
k = j + d
17+
if k >= n:
18+
k = k -n
19+
if k == i:
20+
break
21+
arr[j] = arr[k]
22+
j = k
23+
24+
arr[j] = temp

0 commit comments

Comments
 (0)