-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotatingarray.cpp
More file actions
62 lines (51 loc) · 900 Bytes
/
rotatingarray.cpp
File metadata and controls
62 lines (51 loc) · 900 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//Rotating an Array
#include <iostream>
#include <vector>
using namespace std;
void reverse(int i,int j,int a[])
{
while(i<j)
{
swap(a[i],a[j]);
i++;
j--;
}
}
void leftshift(int a[], int r, int n)
{
reverse(0,n-1,a);
reverse(0,n-1-r,a);
reverse(n-1-r+1,n-1,a);
}
void display(int a[],int n)
{
cout<<"{";
for(int i=0;i<n;i++)
{
cout<<a[i];
if(i<n-1)
cout<<",";
}
cout<<"}";
}
int main()
{
int n;
cout << "Enter the number of elements: ";
cin >> n;
int a[n];
for(int i=0;i<n;i++)
{
cout<<"ENTER ELEMENT AT "<<i+1<<" POSITON:";
cin>>a[i];
}
cout<<"ORIGINAL ARRAY:\n\t";
display(a,n);
cout<<"\nENTER THE NUMBER TO ROTATE:";
int r;
cin>>r;
leftshift(a,r,n);
cout<<"ROTATED ARRAY:\n\t";
display(a,n);
return 0;
}