-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path058RotateAnArrayLeftByNPositions.cpp
59 lines (47 loc) · 1.48 KB
/
058RotateAnArrayLeftByNPositions.cpp
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//program to rotate an array left by N position
// including the required the header file
#include <iostream>
using namespace std;
// function to rotate an array left by n times
void rotateArr(int arr[], int size, int n) {
int tempArr[n];
for (int i = 0; i < n; i++) {
tempArr[i] = arr[i];
}
for (int j = n; j < size; j++) {
arr[j - n] = arr[j];
}
for (int k = 0; k < n; k++) {
arr[size - n + k] = tempArr[k];
}
}
// main function
int main() {
// input length, elements of the array and number of rotations from the user
int arrSize, num;
cout << endl << "Enter the length of the array: ";
cin >> arrSize;
int arr[arrSize];
cout << "Enter the elements of the array " << endl;
for (int i = 0; i < arrSize; ++i) {
cout << "Enter the " << i+1 << "th element of array: ";
cin >> arr[i];
}
cout << "Enter the number of positions to rotate left by: ";
cin >> num;
// calling the rotate function and displaying the final rotated array
if (num >= 0 && num < arrSize) {
rotateArr(arr, arrSize, num);
cout << "Array after rotating left by " << num << " positions: [ ";
for (int i = 0; i < arrSize; ++i) {
if (i < arrSize - 1) {
cout << arr[i] << ", ";
} else {
cout << arr[i] << " ]" << endl;
}
}
} else {
cout << "Invalid number of positions to rotate." << endl;
}
return 0;
}