# Print left rotation of array in O(n) time and O(1) space

Given an array of size n and multiple values around which we need to left rotate the array. How to quickly print multiple left rotations?

Examples :

```Input : arr[] = {1, 3, 5, 7, 9}
k1 = 1
k2 = 3
k3 = 4
k4 = 6
Output : 3 5 7 9 1
7 9 1 3 5
9 1 3 5 7
3 5 7 9 1

Input : arr[] = {1, 3, 5, 7, 9}
k1 = 14
Output : 9 1 3 5 7
```

## Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

We have discussed a solution in below post.
Quickly find multiple left rotations of an array | Set 1

The solution discussed above requires extra space. In this post, an optimized solution is discussed that doesn’t require extra space.

## C++

 `// CPP implementation of left rotation of ` `// an array K number of times ` `#include ` `using` `namespace` `std; ` ` `  `// Function to leftRotate array multiple times ` `void` `leftRotate(``int` `arr[], ``int` `n, ``int` `k) ` `{ ` `    ``/* To get the starting point of rotated array */` `    ``int` `mod = k % n; ` ` `  `    ``// Prints the rotated array from start position ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``cout << (arr[(mod + i) % n]) << ``" "``; ` ` `  `    ``cout << ``"\n"``; ` `} ` ` `  `// Driver program ` `int` `main() ` `{ ` `    ``int` `arr[] = { 1, 3, 5, 7, 9 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ``int` `k = 2; ` `    ``leftRotate(arr, n, k); ` ` `  `    ``k = 3; ` `    ``leftRotate(arr, n, k); ` ` `  `    ``k = 4; ` `    ``leftRotate(arr, n, k); ` ` `  `    ``return` `0; ` `} `

## Java

 `// JAVA implementation of left rotation ` `// of an array K number of times ` `import` `java.util.*; ` `import` `java.lang.*; ` `import` `java.io.*; ` ` `  `class` `arr_rot ` `{    ` `    ``// Function to leftRotate array multiple ` `    ``// times ` `    ``static` `void` `leftRotate(``int` `arr[], ``int` `n, ` `                                     ``int` `k) ` `    ``{ ` `        ``/* To get the starting point of  ` `        ``rotated array */` `        ``int` `mod = k % n; ` `     `  `        ``// Prints the rotated array from  ` `        ``// start position ` `        ``for``(``int` `i = ``0``; i < n; ++i) ` `        ``System.out.print(arr[(i + mod) % n] ` `                          ``+ ``" "``);  ` `         `  `        ``System.out.println(); ` `    ``} ` `     `  `    ``// Driver program ` `    ``public` `static` `void` `main (String[] args)  ` `    ``{ ` `            ``int` `arr[] = { ``1``, ``3``, ``5``, ``7``, ``9` `}; ` `            ``int` `n = arr.length;  ` ` `  `            ``int` `k = ``2``; ` `            ``leftRotate(arr, n, k); ` ` `  `            ``k = ``3``; ` `            ``leftRotate(arr, n, k); ` ` `  `            ``k = ``4``; ` `            ``leftRotate(arr, n, k); ` `    ``} ` `} ` ` `  `// This code is contributed by Sanjal `

## Python

 `# Python implementation of left rotation of ` `# an array K number of times ` ` `  `# Function to leftRotate array multiple times ` `def` `leftRotate(arr, n, k): ` ` `  `    ``# To get the starting point of rotated array ` `    ``mod ``=` `k ``%` `n ` `    ``s ``=` `"" ` ` `  `    ``# Prints the rotated array from start position ` `    ``for` `i ``in` `range``(n): ` `        ``print` `str``(arr[(mod ``+` `i) ``%` `n]), ` `    ``print` `    ``return` ` `  `# Driver program ` `arr ``=` `[ ``1``, ``3``, ``5``, ``7``, ``9` `] ` `n ``=` `len``(arr) ` `k ``=` `2` `leftRotate(arr, n, k) ` ` `  `k ``=` `3` `leftRotate(arr, n, k) ` ` `  `k ``=` `4` `leftRotate(arr, n, k) ` ` `  `#This code is contributed by Sachin Bisht `

## C#

 `// C# implementation of left ` `// rotation of an array K ` `// number of times ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `    ``// Function to leftRotate  ` `    ``// array multiple times ` `    ``static` `void` `leftRotate(``int` `[]arr,  ` `                           ``int` `n, ``int` `k) ` `    ``{ ` `        ``// To get the starting   ` `        ``// point of rotated array  ` `        ``int` `mod = k % n; ` `     `  `        ``// Prints the rotated array   ` `        ``// from start position ` `        ``for``(``int` `i = 0; i < n; ++i) ` `        ``Console.Write(arr[(i + mod) %  ` `                           ``n] + ``" "``);  ` `         `  `        ``Console.WriteLine(); ` `    ``} ` `     `  `    ``// Driver Code ` `    ``static` `public` `void` `Main () ` `    ``{ ` `        ``int` `[]arr = {1, 3, 5, 7, 9}; ` `        ``int` `n = arr.Length;  ` `     `  `        ``int` `k = 2; ` `        ``leftRotate(arr, n, k); ` `     `  `        ``k = 3; ` `        ``leftRotate(arr, n, k); ` `     `  `        ``k = 4; ` `        ``leftRotate(arr, n, k); ` `    ``} ` `} ` ` `  `// This code is contributed by m_kit `

## PHP

 ` `

Output :

```5 7 9 1 3
7 9 1 3 5
9 1 3 5 7
```

This article is contributed by Sridhar Babu. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up

Improved By : jit_t

Article Tags :
Practice Tags :

28

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.