Hướng dẫn how do you rotate an array of elements in the right in python? - làm thế nào để bạn xoay một mảng các phần tử ở bên phải trong python?

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • 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
    end
    4
    leftRotate(arr[], d, n)
    start
      For i = 0 to i < d
        Left rotate all elements of arr[] by one
    end
    5

        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
    end
    4
    leftRotate(arr[], d, n)
    start
      For i = 0 to i < d
        Left rotate all elements of arr[] by one
    end
    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

        

    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

    def1def2

              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 movement
    3
    [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 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}
    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 movement
    3
    [1, 2, 3, 4, 5, 6]
    Rotated list is
    [3, 4, 5, 6, 1, 2]
    5553333333333333333333333333333333
    Another 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 movement
    3
    [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 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}
    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 movement
    3
    [1, 2, 3, 4, 5, 6]
    Rotated list is
    [3, 4, 5, 6, 1, 2]
    5553333333333333333333333333333333

    def1

    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 movement
    3
    [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 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}
    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 movement
    3
    [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)

    def1

    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}
    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 movement
    3
    [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 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}
    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 movement
    3
    [1, 2, 3, 4, 5, 6]
    Rotated list is
    [3, 4, 5, 6, 1, 2]
    5553333333333333333333333333333333
    list_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;

    Hướng dẫn how do you rotate an array of elements in the right in python? - làm thế nào để bạn xoay một mảng các phần tử ở bên phải trong python?

    Vòng quay của mảng trên bởi 2 sẽ tạo ra mảng

    Hướng dẫn how do you rotate an array of elements in the right in python? - làm thế nào để bạn xoay một mảng các phần tử ở bên phải trong python?

    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 movement
    2

        

    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
    end
    4
    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
    end
    4
    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]
    49output_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 movement
    2

        

    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
    end
    4
    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]
    49output_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
    end
    4
    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]
    49output_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
    end
    4
    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
    end
    4
    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]
    81output_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 movement
    3
    [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 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}
    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 movement
    3
    [1, 2, 3, 4, 5, 6]
    Rotated list is
    [3, 4, 5, 6, 1, 2]
    55533333333333333333333

    def1

    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
    end
    14
    leftRotate(arr[], d, n)
    start
      For i = 0 to i < d
        Left rotate all elements of arr[] by one
    end
    15
    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
    end
    17
    leftRotate(arr[], d, n)
    start
      For i = 0 to i < d
        Left rotate all elements of arr[] by one
    end
    18

    def1

    leftRotate(arr[], d, n)
    start
      For i = 0 to i < d
        Left rotate all elements of arr[] by one
    end
    20
    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
    end
    22
    [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
    end
    24

    Đầ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
    end
    4
    leftRotate(arr[], d, n)
    start
      For i = 0 to i < d
        Left rotate all elements of arr[] by one
    end
    34

    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
    end
    4
    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
    end
    55output_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}
    9def0

        

    leftRotate(arr[], d, n)
    start
      For i = 0 to i < d
        Left rotate all elements of arr[] by one
    end
    60
    leftRotate(arr[], d, n)
    start
      For i = 0 to i < d
        Left rotate all elements of arr[] by one
    end
    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}
    9
    leftRotate(arr[], d, n)
    start
      For i = 0 to i < d
        Left rotate all elements of arr[] by one
    end
    63
    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
    end
    67

        

    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
    end
    73

    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 movement
    3
    [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 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}
    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 movement
    3
    [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 movement
    00
    [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 movement
    3
    leftRotate(arr[], d, n)
    start
      For i = 0 to i < d
        Left rotate all elements of arr[] by one
    end
    10
    leftRotate(arr[], d, n)
    start
      For i = 0 to i < d
        Left rotate all elements of arr[] by one
    end
    18

    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
    05
    leftRotate(arr[], d, n)
    start
      For i = 0 to i < d
        Left rotate all elements of arr[] by one
    end
    10
    leftRotate(arr[], d, n)
    start
      For i = 0 to i < d
        Left rotate all elements of arr[] by one
    end
    18

    Độ 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

    Hướng dẫn how do you rotate an array of elements in the right in python? - làm thế nào để bạn xoay một mảng các phần tử ở bên phải trong python?

              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
    end
    26

        

    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 movement
    15

    leftRotate(arr[], d, n)
    start
      For i = 0 to i < d
        Left rotate all elements of arr[] by one
    end
    4
    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 movement
    19

    leftRotate(arr[], d, n)
    start
      For i = 0 to i < d
        Left rotate all elements of arr[] by one
    end
    4
    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
    21
    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 movement
    23

    leftRotate(arr[], d, n)
    start
      For i = 0 to i < d
        Left rotate all elements of arr[] by one
    end
    4
    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}
    9output_list 4

    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
    28
    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
    29____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 movement
    21output_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 movement
    33

    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
    28
    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 movement
    36
    Array after left rotation is:  [3, 4, 5, 6, 7, 1, 2]
    0     7

    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
    39
    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
    29____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 movement
    29
    leftRotate(arr[], d, n)
    start
      For i = 0 to i < d
        Left rotate all elements of arr[] by one
    end
    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
    44

    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
    28
    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 movement
    29____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 movement
    50

    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
    39
    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
    52

    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
    28
    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
    54
    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 movement
    56

    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
    28
    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
    21
    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 movement
    60

    leftRotate(arr[], d, n)
    start
      For i = 0 to i < d
        Left rotate all elements of arr[] by one
    end
    4
    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
    54
    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 movement
    66

        

    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
    end
    73

    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 movement
    3
    [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 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}
    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 movement
    3
    [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
    end
    26

        

    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 movement
    15

    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 movement
    3
    [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 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}
    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 movement
    3
    [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 movement
    00
    [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 movement
    3
    leftRotate(arr[], d, n)
    start
      For i = 0 to i < d
        Left rotate all elements of arr[] by one
    end
    10
    leftRotate(arr[], d, n)
    start
      For i = 0 to i < d
        Left rotate all elements of arr[] by one
    end
    18

    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
    05
    leftRotate(arr[], d, n)
    start
      For i = 0 to i < d
        Left rotate all elements of arr[] by one
    end
    10
    leftRotate(arr[], d, n)
    start
      For i = 0 to i < d
        Left rotate all elements of arr[] by one
    end
    18

    Độ 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
    end
    26

              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}
    32output_list 1
    leftRotate(arr[], d, n)
    start
      For i = 0 to i < d
        Left rotate all elements of arr[] by one
    end
    40
    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
    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}
    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 movement
    15

    leftRotate(arr[], d, n)
    start
      For i = 0 to i < d
        Left rotate all elements of arr[] by one
    end
    4
    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 movement
    19

    def1

              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

    def1

    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
    end
    18

    def1

              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
    4
    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
    21
    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 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
    end
    4
    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}
    9output_list 4

    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
    28
    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
    29____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 movement
    21output_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 movement
    33

    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
    28
    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 movement
    36
    Array 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 movement
    39
    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
    29____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 movement
    29
    leftRotate(arr[], d, n)
    start
      For i = 0 to i < d
        Left rotate all elements of arr[] by one
    end
    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
    44


    Làm thế nào để bạn xoay một phần tử phù hợp trong một mảng?

    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.loop through the array by shifting each element of the array to its next position. The last element of the array will become the first element of the rotated array.

    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.rotate arr[] of size n by d where d is greater than n. In this case d%n should be calculated and rotate by the result after mod.

    Làm thế nào để bạn rời khỏi một mảng trong Python?

    Bước 3: Mảng có thể được quay lại bằng cách chuyển các phần tử của nó sang một vị trí trước khi chúng có thể được thực hiện bằng cách lặp qua mảng và thực hiện thao tác ARR [j] = mảng [j+1].looping through the array and perform the operation arr[j] = arr[j+1].

    Làm thế nào để bạn rời khỏi hoặc xoay một mảng?

    Xoay bên trái của mảng..
    Mảng ban đầu [1, 2, 3, 4, 5].
    xoay theo chỉ số đầu tiên [2, 3, 4, 5, 1].
    xoay theo chỉ số thứ hai [3, 4, 5, 1, 2].