Quickly find multiple left rotations of an array | Set 1 | Fills temp with two copies of arr ; Store arr elements at i and i + n ; Function to left rotate an array k times ; Starting position of array after k rotations in temp will be k % n ; Print array after k rotations ; Driver program
def preprocess ( arr , n ) : NEW_LINE INDENT temp = [ None ] * ( 2 * n ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT temp [ i ] = temp [ i + n ] = arr [ i ] NEW_LINE DEDENT return temp NEW_LINE DEDENT def leftRotate ( arr , n , k , temp ) : NEW_LINE INDENT start = k % n NEW_LINE for i in range ( start , start + n ) : NEW_LINE INDENT print ( temp [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( " " ) NEW_LINE DEDENT arr = [ 1 , 3 , 5 , 7 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE temp = preprocess ( arr , n ) NEW_LINE k = 2 NEW_LINE leftRotate ( arr , n , k , temp ) NEW_LINE k = 3 NEW_LINE leftRotate ( arr , n , k , temp ) NEW_LINE k = 4 NEW_LINE leftRotate ( arr , n , k , temp ) NEW_LINE