
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Rotate Array Left by One Position
Rotating an array means shifting the elements of an array in a specific direction while maintaining their relative order. For example, if the array {6, 12, 18, 24, 30} is rotating it left once will result in {12, 18, 24, 30, 6}.
In this article, we are given an array and need to shift all elements one step to the left, with the first element moving to the last position.
Example
Here is an example of left rotation of elements in array by one place.
Input: array = {5, 10, 15, 20, 25} Output: array = {10, 15, 20, 25, 5}
Here are the approaches to rotate an array left by one position:
Using Temporary Variable
We can use a temporary variable to store the first element, then shift all elements one position to the left, and finally place the first element in the last position.
- We store the first element of the array in a temporary variable.
- Shift all elements from index 1 to n-1 one step to the left.
- Place the first element at the last index.
- Print the modified array.
Example
The following example implements the above mentioned-steps to rotate the given an array to left by one position using temporary variable.
#include <bits/stdc++.h> using namespace std; void rotateLeft(int arr[], int n) { if (n <= 1) return; // No rotation needed for small arrays int first = arr[0]; for (int i = 0; i < n - 1; i++) { arr[i] = arr[i + 1]; } arr[n - 1] = first; } int main() { int array[] = {4, 8, 12, 16, 20}; int n = sizeof(array) / sizeof(array[0]); cout << "Given array is: "; for (int i = 0; i < n; i++) { cout << array[i] << " "; } rotateLeft(array, n); cout << "\nArray after left rotation is: "; for (int i = 0; i < n; i++) { cout << array[i] << " "; } return 0; }
The output of the above code is:
Given array is: 4 8 12 16 20 Array after left rotation is: 8 12 16 20 4
Using STL rotate() Function
In this approach, we use the STL built-in function std::rotate in the library, which can be used to rotate the array efficiently.
- Use std::rotate() to rotate the array.
- Pass arr, arr + 1, arr + n as arguments.
- Print the rotated array.
Example
Here is an example implementing the above mentioned-steps to rotate the given an array to left by one position using STL rotate() function.
#include <bits/stdc++.h> using namespace std; int main() { int arr[] = {2, 4, 6, 8, 10}; int n = sizeof(arr) / sizeof(arr[0]); cout << "Given array is: "; for (int i = 0; i < n; i++) { cout << arr[i] << " "; } rotate(arr, arr + 1, arr + n); cout << "\nArray after left rotation is: "; for (int i = 0; i < n; i++) { cout << arr[i] << " "; } return 0; }
The output of the above code is:
Given array is: 2 4 6 8 10 Array after left rotation is: 4 6 8 10 2
Using STL Deque
This is the most efficient approach for rotating an array left by one position. In this approach, we use a deque from C++ STL, which allows the left rotation of the array easily using push_back() and pop_front().
- Use the dequeue for efficient insertion and deletion.
- Move the first element to the back.
- Print the modified rotated array.
Example
In this example, we have used STL Deque to rotate the given an array to left by one position.
#include <bits/stdc++.h> using namespace std; int main() { deque arr = {2, 4, 6, 8, 10}; cout << "Given array is: "; for (int num : arr) { cout << num << " "; } arr.push_back(arr.front()); arr.pop_front(); cout << "\nArray after left rotation is: "; for (int num : arr) { cout << num << " "; } return 0; }
The output of the above code is:
Given array is: 2 4 6 8 10 Array after left rotation is: 4 6 8 10 2
Complexity Comparison
Here is a comparison of time and space complexity of all the above approaches.
Approach | Time Complexity | Space Complexity |
---|---|---|
Using Temporary Variable | O(n) | O(1) |
Using STL rotate() Function | O(n) | O(1) |
Using STL Deque | O(1) | O(n) |