-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeftRotateArray1Place.cpp
More file actions
41 lines (30 loc) · 910 Bytes
/
LeftRotateArray1Place.cpp
File metadata and controls
41 lines (30 loc) · 910 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/* Problem Name : Left Rotate an Array by One
From : coding Ninjas
Link : https://www.naukri.com/code360/problems/left-rotate-an-array-by-one_5026278 */
#include <bits/stdc++.h>
using namespace std;
vector<int> rotateArray(vector<int>& arr, int n) {
vector<int> temp; // new array to store rotated version
int firstElement = arr[0]; // store the first element
// shift all elements to the left (starting from index 1)
for (int i = 1; i < n; i++) { // 1 2 3 4 5
temp.push_back(arr[i]);
}
// place the first element at the end
temp.push_back(firstElement);
return temp;
}
int main() {
int n;
cin >> n;
vector<int> arr(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
vector<int> ans = rotateArray(arr, n);
// print rotated array
for (int i = 0; i < n; i++) {
cout << ans[i] << " ";
}
return 0;
}