Skip to content

Commit

Permalink
Merge pull request #521 from Sajal-24-jain/patch-3
Browse files Browse the repository at this point in the history
Create Rotate Array.cpp
  • Loading branch information
tanus786 authored Oct 26, 2022
2 parents 39b7ad9 + 527dfec commit 5bddcaf
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions Rotate Array.cpp
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;
}

0 comments on commit 5bddcaf

Please sign in to comment.