Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọclist and an integer n, write a Python program to right rotate the list by n position.
Bàn luận
Đưa ra một danh sách và một số nguyên n, viết một chương trình Python để xoay đúng danh sách theo vị trí n.n = 2, List_1 = [1, 2, 3, 4, 5, 6]
Output : List_1 = [5, 6, 1, 2, 3, 4]
Explanation: We get output list after right rotating [clockwise] given list by 2.Ví dụ: & nbsp; n = 3, List_1 = [3, 0, 1, 4, 2, 3]
Output : List_1 = [4, 2, 3, 3, 0, 1]
Đầu vào: n = 2, list_1 = [1, 2, 3, 4, 5, 6] đầu ra: list_1 = [5, 6, 1, 2, 3, 4] Giải thích: Chúng tôi nhận được danh sách đầu ra sau khi xoay đúng [theo chiều kim đồng hồ] Danh sách được đưa ra bởi 2.Traverse the first list one by one and then put the elements at required places in a second list.
Python3
Đầu vào: & nbsp; n = 3, list_1 = [3, 0, 1, 4, 2, 3] đầu ra: list_1 = [4, 2, 3, 3, 0, 1]
Cách tiếp cận số 1: Đi qua danh sách đầu tiên từng người một và sau đó đặt các yếu tố tại các địa điểm cần thiết trong danh sách thứ hai. & NBSP;
def
rightRotate[lists, num]:
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end4
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end5
output_list
____10
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]1
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end4
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end5
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]3
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]4
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]5
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]3
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]4
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]5
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}1
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}2
def
1def
2
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12}
b] Then in second set.
arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12}
c] Finally in third set.
arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}
3Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]
0 arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12}
b] Then in second set.
arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12}
c] Finally in third set.
arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}
5
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12}
b] Then in second set.
arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12}
c] Finally in third set.
arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}
6Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]
0 arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12}
b] Then in second set.
arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12}
c] Finally in third set.
arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}
8 arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12}
b] Then in second set.
arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12}
c] Finally in third set.
arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}
9Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
a] Elements are first moved in first set – [See below diagram for this movement
3[1, 2, 3, 4, 5, 6]
Rotated list is
[3, 4, 5, 6, 1, 2]
1Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
a] Elements are first moved in first set – [See below diagram for this movement
3 arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12}
b] Then in second set.
arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12}
c] Finally in third set.
arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}
5Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
a] Elements are first moved in first set – [See below diagram for this movement
3[1, 2, 3, 4, 5, 6]
Rotated list is
[3, 4, 5, 6, 1, 2]
5553333333333333333333333333333333Another approach to solve this problem by using slicing technique. One way of slicing list is by using len[] method.
Python3
Độ phức tạp về thời gian: O [n]
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}1
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}2
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}3
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]0
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}5
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}6
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]0
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}8
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}9
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement3
[1, 2, 3, 4, 5, 6] Rotated list is [3, 4, 5, 6, 1, 2]1
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement3
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}5
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement3
[1, 2, 3, 4, 5, 6] Rotated list is [3, 4, 5, 6, 1, 2]5553333333333333333333333333333333
def
1
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]00
Độ phức tạp về thời gian: O [n]In the above method, last n elements of list_1 was taken and then remaining elements of list_1. Another way is without using len[] method.
Cách tiếp cận số 2: Một cách tiếp cận khác để giải quyết vấn đề này bằng cách sử dụng kỹ thuật cắt. Một cách của danh sách cắt là bằng cách sử dụng phương thức Len []. & NBSP;
Độ phức tạp về thời gian: O [n]
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}1
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}2
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}3
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]0
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}5
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}6
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]0
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}8
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}9
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement3
[1, 2, 3, 4, 5, 6] Rotated list is [3, 4, 5, 6, 1, 2]1
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement3
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}5
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement3
[1, 2, 3, 4, 5, 6] Rotated list is [3, 4, 5, 6, 1, 2]5553333333333333333333333333333333
Độ phức tạp về thời gian: O [n]
def
1
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]00
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12}
b] Then in second set.
arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12}
c] Finally in third set.
arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}
3Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]
0 arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12}
b] Then in second set.
arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12}
c] Finally in third set.
arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}
5
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12}
b] Then in second set.
arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12}
c] Finally in third set.
arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}
6Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]
0 arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12}
b] Then in second set.
arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12}
c] Finally in third set.
arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}
8 arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12}
b] Then in second set.
arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12}
c] Finally in third set.
arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}
9Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
a] Elements are first moved in first set – [See below diagram for this movement
3[1, 2, 3, 4, 5, 6]
Rotated list is
[3, 4, 5, 6, 1, 2]
1Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
a] Elements are first moved in first set – [See below diagram for this movement
3 arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12}
b] Then in second set.
arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12}
c] Finally in third set.
arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}
5Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
a] Elements are first moved in first set – [See below diagram for this movement
3[1, 2, 3, 4, 5, 6]
Rotated list is
[3, 4, 5, 6, 1, 2]
5553333333333333333333333333333333list_1[:] will return the whole list as the blank space on left of slicing operator refers to start of list i.e 0 and blank space on right refers to ending position of list.
Viết một hàm xoay [AR [], d, n] xoay ARR [] có kích thước n theo các phần tử d. & Nbsp;
Vòng quay của mảng trên bởi 2 sẽ tạo ra mảng
Phương pháp 1 [sử dụng mảng temp]::
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1] Store d elements in a temp array temp[] = [1, 2] 2] Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3] Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]
Python3
def
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]43
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]45
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]0
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]1
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]49____10
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement2
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]53
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]54
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end4
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]56
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end4
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]49____10
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]49
output_list
1 arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}9
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]49____10
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement2
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]53
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]54
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end4
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]49____10
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]49
output_list
1 arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}9
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end4
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]49____10
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]49
output_list
1 arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}9
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]53
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]69
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end4
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]71
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]0
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]73
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end4
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]81
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]0
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]81
output_list
1 arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}9
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]95
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]0
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}8
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}9
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement3
[1, 2, 3, 4, 5, 6] Rotated list is [3, 4, 5, 6, 1, 2]1
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement3
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}5
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement3
[1, 2, 3, 4, 5, 6] Rotated list is [3, 4, 5, 6, 1, 2]55533333333333333333333
def
1
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]7
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end14
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end15
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]0
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end17
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end18
def
1
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end20
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]8
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end22
[1, 2, 3, 4, 5, 6] Rotated list is [3, 4, 5, 6, 1, 2]1
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end24
Đầu ra
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]
Nếu mảng cần được xoay nhiều hơn chiều dài của nó thì nên thực hiện mod. & Nbsp; O[n]
Auxiliary Space: O[d]
Ví dụ: xoay arr [] của kích thước n bởi d trong đó d lớn hơn n. Trong trường hợp này d%n nên được tính toán và xoay theo kết quả sau mod. & Nbsp; :
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end
Độ phức tạp về thời gian: O [n] & nbsp; trong đó n là kích thước của không gian arrayauxiliary đã cho: O [1]
Let us take the same example arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2
Rotate arr[] by one 2 times
We get [2, 3, 4, 5, 6, 7, 1] after first rotation and [ 3, 4, 5, 6, 7, 1, 2] after second rotation.
Python3
Vui lòng tham khảo đầy đủ bài viết về chương trình để xoay mảng để biết thêm chi tiết!
Làm thế nào để bạn xoay một phần tử phù hợp trong một mảng?
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end4
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end34
Một mảng được cho là được xoay đúng nếu tất cả các phần tử của mảng được di chuyển sang phải theo một vị trí. Một cách tiếp cận là lặp qua mảng bằng cách chuyển từng phần tử của mảng sang vị trí tiếp theo. Phần tử cuối cùng của mảng sẽ trở thành phần tử đầu tiên của mảng xoay.
Làm thế nào để bạn xoay các mục trong một mảng trong Python?
Nếu mảng cần được xoay nhiều hơn chiều dài của nó thì nên thực hiện mod. Ví dụ: xoay arr [] của kích thước n bởi d trong đó d lớn hơn n. Trong trường hợp này D%N nên được tính toán và xoay theo kết quả sau mod.
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end4
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]71
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]0
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end55
output_list
1arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}9
def
0
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end60
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end0
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}9
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end63
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]0
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]91
def
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end67
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]3
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]49
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]5
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]6
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end73
Các
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]95
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]0
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}8
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}9
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement3
[1, 2, 3, 4, 5, 6] Rotated list is [3, 4, 5, 6, 1, 2]1
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement3
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}5
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement3
[1, 2, 3, 4, 5, 6] Rotated list is [3, 4, 5, 6, 1, 2]55533333333333333333333
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement00
[1, 2, 3, 4, 5, 6] Rotated list is [3, 4, 5, 6, 1, 2]1
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement3
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end10
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end18
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement05
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end10
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end18
Độ phức tạp về thời gian: o [n * d] & nbsp; không gian phụ trợ: o [1] & nbsp; O[n * d]
Auxiliary Space : O[1]
Phương pháp 3 [thuật toán tung hứng] & nbsp; Đây là phần mở rộng của phương pháp 2. Thay vì di chuyển từng cái một, chia mảng trong các tập hợp khác nhau & nbsp; trong đó số lượng bộ bằng GCD của N và D và di chuyển các phần tử trong các bộ. & nbsp; nếu gcd là 1 như là cho mảng ví dụ trên [n = 7 và d = 2], thì các phần tử sẽ được di chuyển trong một tập d] đến mảng [i] và cuối cùng lưu trữ nhiệt độ ở đúng vị trí. Đây là một ví dụ cho n = 12 và d = 3. gcd là 3 và & nbsp;
This is an extension of method 2. Instead of moving one by one, divide the
array in different sets
where number of sets is equal to GCD of n and d and move the elements within sets.
If GCD is 1 as is for the above example array [n = 7 and d =2], then elements will be moved within one set only, we just start with temp = arr[0] and keep moving arr[I+d] to arr[I] and finally store temp at the right place.
Here is an example for n =12 and d = 3. GCD is 3 and
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}
Python3
def
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end26
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]3
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]49
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]5
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]6
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement15
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end4
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]45
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]0
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement19
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end4
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement21
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]0
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement23
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end4
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]53
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}9
output_list
4Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement28
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement29____10
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement21
output_list
1 Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement33
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement28
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]19
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement36
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]0
7Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement39
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement29____10
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement29
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end0
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement44
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement28
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]19
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement29____1010
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement50
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement39
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement52
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement28
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement54
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]0
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement56
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement28
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement21
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]0
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement60
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end4
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement54
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]0
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]91
def
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement66
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]3
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]49
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]5
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]6
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end73
Các
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]95
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]0
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}8
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}9
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement3
[1, 2, 3, 4, 5, 6] Rotated list is [3, 4, 5, 6, 1, 2]1
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement3
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}5
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement3
[1, 2, 3, 4, 5, 6] Rotated list is [3, 4, 5, 6, 1, 2]55533333333333333333333
Độ phức tạp về thời gian: o [n * d] & nbsp; không gian phụ trợ: o [1] & nbsp;
Phương pháp 3 [thuật toán tung hứng] & nbsp; Đây là phần mở rộng của phương pháp 2. Thay vì di chuyển từng cái một, chia mảng trong các tập hợp khác nhau & nbsp; trong đó số lượng bộ bằng GCD của N và D và di chuyển các phần tử trong các bộ. & nbsp; nếu gcd là 1 như là cho mảng ví dụ trên [n = 7 và d = 2], thì các phần tử sẽ được di chuyển trong một tập d] đến mảng [i] và cuối cùng lưu trữ nhiệt độ ở đúng vị trí. Đây là một ví dụ cho n = 12 và d = 3. gcd là 3 và & nbsp;
def
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end26
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]3
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]49
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]5
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]6
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement15
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]95
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]0
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}8
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}9
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement3
[1, 2, 3, 4, 5, 6] Rotated list is [3, 4, 5, 6, 1, 2]1
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement3
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}5
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement3
[1, 2, 3, 4, 5, 6] Rotated list is [3, 4, 5, 6, 1, 2]55533333333333333333333
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement00
[1, 2, 3, 4, 5, 6] Rotated list is [3, 4, 5, 6, 1, 2]1
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement3
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end10
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end18
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement05
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end10
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end18
Độ phức tạp về thời gian: o [n * d] & nbsp; không gian phụ trợ: o [1] & nbsp; O[n]
Auxiliary Space : O[1]
Phương pháp 3 [thuật toán tung hứng] & nbsp; Đây là phần mở rộng của phương pháp 2. Thay vì di chuyển từng cái một, chia mảng trong các tập hợp khác nhau & nbsp; trong đó số lượng bộ bằng GCD của N và D và di chuyển các phần tử trong các bộ. & nbsp; nếu gcd là 1 như là cho mảng ví dụ trên [n = 7 và d = 2], thì các phần tử sẽ được di chuyển trong một tập d] đến mảng [i] và cuối cùng lưu trữ nhiệt độ ở đúng vị trí. Đây là một ví dụ cho n = 12 và d = 3. gcd là 3 và & nbsp;Using List slicing
Python3
def
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end26
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}29
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}30
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]0
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}32
output_list
1leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end40
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement2
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}36
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]3
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]49
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]5
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]6
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement15
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end4
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]45
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]0
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement19
def
1
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}56
def
1
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]7
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}59
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end18
def
1
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}62
[1, 2, 3, 4, 5, 6] Rotated list is [3, 4, 5, 6, 1, 2]1
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}64
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]8
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}66
leftRotate[arr[], d, n]
start
For i = 0 to i < d
Left rotate all elements of arr[] by one
end
4Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
a] Elements are first moved in first set – [See below diagram for this movement
21Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]
0 Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
a] Elements are first moved in first set – [See below diagram for this movement
23
[1, 2, 3, 4, 5, 6] Rotated list is [3, 4, 5, 6, 1, 2]
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end4
Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]53
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12} b] Then in second set. arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12} c] Finally in third set. arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}9
output_list
4Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement28
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement29____10
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement21
output_list
1 Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement33
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
a] Elements are first moved in first set – [See below diagram for this movement
28Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]
19 Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
a] Elements are first moved in first set – [See below diagram for this movement
36Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]
0
7: O[n] where n is size of given array
Auxiliary Space : O[1]
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement39
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement29____10
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement29
leftRotate[arr[], d, n] start For i = 0 to i < d Left rotate all elements of arr[] by one end0
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} a] Elements are first moved in first set – [See below diagram for this movement44