-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshraddha.cpp
More file actions
58 lines (47 loc) · 1.35 KB
/
shraddha.cpp
File metadata and controls
58 lines (47 loc) · 1.35 KB
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
#include <iostream>
#include <vector>
using namespace std;
void rotation(int arr[], int n, int d) //arr,5,2 //arr[5]=[1,2,3,4,5]
{
vector<int> temp(arr, arr + d); //temp(arr=5,7)
for (int i = 0; i < n - d; i++) //i=0 to i<3
arr[i] = arr[i + d]; // arr[0]=1 ==arr[2]=3........arr[1]=2 ==arr[3]=4......arr[2]=3 ==arr[5]=5...
//arr[5]={3,4,5,4,5}
//temp={1,2}
for (int i = 0; i < d; i++) //i=0 to i<2
arr[n - d + i] = temp[i]; //arr[5-2-0]=temp[0]-------> arr[5]={3,4,5,1,5} -------> arr[5]={3,4,5,1,2}
}
void display_array(int arr[], int n)
{
cout << "[";
for (int i = 0; i < n; i++)
{
cout << arr[i];
if (i != n - 1)
{
cout << ", ";
}
}
cout << "]" << endl;
}
int main()
{
int n;
cout << "Enter the number of elements in the Array: ";
cin >> n;
int arr[n];
for (int i = 0; i < n; i++)
{
cout << "Enter the " << i + 1 << "th Element: ";
cin >> arr[i];
}
int rot;
cout << "Enter the number of rotations: ";
cin >> rot;
cout << "Original Array:" << endl;
display_array(arr, n);
rotation(arr, n, rot % n);
cout << "Rotated Array:" << endl;
display_array(arr, n);
return 0;
}