Open In App

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

Last Updated : 05 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

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 the below post. 
Quickly find multiple left rotations of an array | Set 1

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

Implementation:

C++
// C++ implementation of left rotation of
// an array K number of times
#include <bits/stdc++.h>
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 Code
int main()
{
    int arr[] = { 1, 3, 5, 7, 9 };
    int n = sizeof(arr) / sizeof(arr[0]);

    int k = 2;
  
      // Function Call
    leftRotate(arr, n, k);

    k = 3;
  
      // Function Call
    leftRotate(arr, n, k);

    k = 4;
  
      // Function Call
    leftRotate(arr, n, k);

    return 0;
}
C Java Python C# JavaScript PHP

Output
5 7 9 1 3 
7 9 1 3 5 
9 1 3 5 7 

Time Complexity: O(N), as we are using a loop to traverse N times.
Auxiliary Space: O(1), as we are not using any extra space.

Method II: In the below implementation we will use Standard Template Library (STL) which will be making the solution more optimize and easy to Implement.

Implementation:

C++
// C++ Implementation For Print Left Rotation Of Any Array K
// Times

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
// Function For The k Times Left Rotation
void leftRotate(int arr[], int k, int n)
{

    // Stl function rotates takes three parameters - the
    // beginning,the position by which it should be rotated
    // ,the end address of the array 
      // The below function will be rotating the array left     
    // in linear time (k%arraySize) times
    rotate(arr, arr + (k % n), arr + n);
    
      // Print the rotated array from start position
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
    cout << "\n";
}
// Driver program
int main()
{
    int arr[] = { 1, 3, 5, 7, 9 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 2;
  
      // Function Call
    leftRotate(arr, k, n);


    return 0;
}
C Java Python C# JavaScript

Output
5 7 9 1 3 

Note: the array itself gets updated after the rotation.

Time Complexity: O(n)
Auxiliary Space: O(1), since no extra space has been taken.

Method III(Using Reversal):

To left rotate an array by “k” units we will perform 3 simple reversals-

  • Reverse the first “k” elements
  • Reverse the last “n-k” elements where n is the size of the array
  • Reverse the whole array

Code-

C++
// C++ Implementation For Print Left Rotation Of Any Array K
// Times

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
// Function For The k Times Left Rotation
void leftRotate(int arr[], int k, int n)
{
      // if k>n , k%n will bring k back in range
     k = (k%n);

    reverse(arr,arr+k);
    reverse(arr+k,arr+n);
    reverse(arr,arr+n);
    
      // Print the rotated array from start position
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
    cout << "\n";
}
// Driver program
int main()
{
    int arr[] = { 1, 3, 5, 7, 9 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 2;
  
      // Function Call
    leftRotate(arr, k, n);


    return 0;
}
C Java Python C# JavaScript Kotlin



Output
5 7 9 1 3 

Note: the array itself gets updated after the rotation.

Time Complexity: O(n)
Auxiliary Space: O(1), since no extra space has been taken.

 



Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg