Home >>Cpp Programs >C++ program to print the left Rotation of the array
In this example, we will see a C++ program through which we can print the left rotation of the array.
In this program, we can shift all the elements d times but this is a time-consuming process. So we can use a small trick here to print the elements of the array after left rotating d elements.
Program:
#include <iostream>
using namespace std;
int main()
{
int n,d;
//input value of n and d
cout<<"Enter the value of n and d"<<endl;
cin>>n>>d;
int a[n];
//input array elements
cout<<"enter the array elements : ";
for(int i=0;i<n;i++)
{
cin<<a[i];
}
//print the elements of array after rotation
cout<<"array elements after rotation : ";
for(int i=0;i<n;i++)
{
cout<<a[(i+d)%n]<<" ";
}
return 0;
}