-
Notifications
You must be signed in to change notification settings - Fork 1k
/
rotate_the_array_by_k_elements.py
53 lines (44 loc) · 1.33 KB
/
rotate_the_array_by_k_elements.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'''
Its a simple python program to rotate array by k elements.
Approch : First Select first k elements and puth them in seperate array , then move all the elements after position k to their left by k steps.
After this we just put the elements which are in seperate array at the end in reverse order.
it will rotate the array by k elements.
'''
# function to totate the array A
def rotateArr(A, k, N):
# first k elements
lst = A[0:k]
# shifting all elements to left by k steps
for i in range(0, N - k):
A[i] = A[i + k]
# adding first k elemets to end in rerverse order
A[N - k:] = lst[:]
return A
def main():
print("Enter the value of no of elements in array and K in one line")
nd = [int(x) for x in input().strip().split()]
N = nd[0]
k = nd[1]
print("Enter the array in one line")
A = [int(x) for x in input().strip().split()]
rotateArr(A, k, N)
print("After rotating array by ",k," elements the array is ")
# printing the result
for i in A:
print(i, end=" ")
print()
# Driver Code
if __name__ == "__main__":
main()
'''
Sample Input/Output:
Enter the value of no of elements in array and K in one line
5 2
Enter the array in one line
1 2 3 4 5
Output:
After rotating array by 2 elements the array is
3 4 5 1 2
Time Complexity : O(n)
Space Complexity : O(k)
'''