# Rotate array

! Based knowledge] [are sweeping array (mp.weixin.qq.com/s/ratfC6vQ6…

The rotation array is divided into left rotation and right rotation. Force button 189 is entitled right rotation, and today’s share is left rotation. Given an array, rotate the elements of the array to the left by * K *, where * K * is non-negative.

! [file] (jeff.spring4all.com/FhQILpGZOv1…).

 

Figure 0-1 ArR left rotation k=2 positions

The original array for ` arr = `,2,3,4,5,6,7 [1], [] will be to rotate the position of the two elements, left ` get array arr [] = `,4,5,6,7,1,2 [3]. I recommend you to do the right rotation array problem 189. Arr [] = [1,2,3,4,5,6,7] ‘, ‘k = 2’, ‘arr[] = [1,2,3,4,5,6,7]’, ‘k = 2’ Then we simply use a temporary array ‘temp[]’ to store the first k elements’ temp[] = [1,2] ‘and move the rest of the array two positions to the left’ arr[] = [3,4,5,6,7,6,7,7] ‘. Finally, the elements of the temporary array temp are saved back to the original array, that is, the rotated array ‘arr[] = [3,4,5,6,7,1,2]’, as shown in figure 1-1. ! [file] (jeff.spring4all.com/FoXe5I-lYOs…).

Figure 1-1 Temporary array method


O ( n ) O(n)


T h e t a ( k ) \\Theta(k)

Jeff.spring4all.com/FmMQ\_MJteX…

Figure 2-1 Step-by-step left rotation method


O ( k n ) O(kn)


T h e t a ( 1 ) \\Theta(1)

Jeff.spring4all.com/Fjn6toYBULC…

Figure 3-1 Greatest Common Divisor method — Round 1

Jeff.spring4all.com/FqsoxBoty9s…

Figure 3-2 Maximum Common Divisor method — Round 2

Jeff.spring4all.com/FmJoFxxyGhh…

Figure 3-3 Greatest Common Divisor method — Round 3

Jeff.spring4all.com/FmstHn61LWC…

Figure 3-4 Changes in subscripts of a rotation array

Jeff.spring4all.com/FgZKpX3v2lV…

O ( n ) O(n)


T h e t a ( 1 ) \\Theta(1)

Figure 4-1 Block switching method

Jeff.spring4all.com/Fl3oCHn5hVS…

Figure 4-2 Block switching method (ABlBr –> BrBlA)

Jeff.spring4all.com/Fr7lVlj3BE3…

Figure 4-3 Block switching method (Recursive processing part B)

Jeff.spring4all.com/Ft0Yu0Ult1M…

Figure 4-4 Block switching method (Step 3)

Jeff.spring4all.com/FlMOdq-6r7e…

Figure 4-5 Block switching method (Recursive processing Part A)


O ( n ) O(n)


T h e t a ( 1 ) \\Theta(1)

Jeff.spring4all.com/FhX9qFuzSF3…
Jeff.spring4all.com/FmblFvcPCLu…

Figure 5-1 Reverse (ARR, 0, n)

Jeff.spring4all.com/FibsciV5XYB…

Figure 5-2 Reverse (ARr, 0, k), Reverse (ARr,k,n)

Jeff.spring4all.com/FqBw8E\_S6Q…

Figure 5-3 Reverse method (three steps)


O ( n ) O(n)


T h e t a ( 1 ) \\Theta(1)

Jeff.spring4all.com/FuIgb78vXvg…