-
Notifications
You must be signed in to change notification settings - Fork 387
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #521 from Sajal-24-jain/patch-3
Create Rotate Array.cpp
- Loading branch information
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#include<iostream> | ||
using namespace std; | ||
|
||
|
||
void leftRotateByD(int *arr,int n,int d) | ||
{ | ||
|
||
int temp[d]; | ||
for (int i=0;i<d;i++) | ||
{ | ||
temp[i]=arr[i]; | ||
} | ||
|
||
|
||
int x=0; | ||
for(int j=d;j<n;j++) | ||
{ | ||
arr[x]=arr[j]; | ||
x++; | ||
} | ||
|
||
|
||
x=0; | ||
for (int k=n-d;k<n;k++) | ||
{ | ||
arr[k]=temp[x]; | ||
x++; | ||
} | ||
|
||
} | ||
|
||
|
||
int main() | ||
{ | ||
int n,d; | ||
|
||
cout<<"Enter the size of array\n"; | ||
cin>>n; | ||
|
||
int arr[n]; | ||
cout<<"Enter the elements of array\n"; | ||
for (int i=0;i<n;i++) | ||
{ | ||
cin>>arr[i]; | ||
} | ||
|
||
cout<<"Enter the value of D\n"; | ||
cin>>d; | ||
|
||
/* function call to rotate the array */ | ||
leftRotateByD(arr,n,d); | ||
|
||
cout<<"Array after left rotation by D places\n"; | ||
for (int i=0;i<n;i++) | ||
{ | ||
cout<<arr[i]<<" "; | ||
} | ||
|
||
|
||
return 0; | ||
} |