Sample Program Run
Enter the number of test cases: 1
Enter the size of the array: 5
Enter the number of rotation to perform: 2
Enter the elements of the array: 1 2 3 4 5
Resulting array: 4 5 1 2 3
#include<iostream>
#include<cmath>
using namespace std;
int* Rotation(int* arr, int sz, int numOfrot)
{
int* res=new int[sz];
int begin=abs(sz-numOfrot);
int j=0;
for(int i=begin;i<sz;i++,j++)
{
res[j]=arr[i];
}
for(int i=0;i<begin;i++,j++)
{
res[j]=arr[i];
}
return res;
}
int main()
{
int testCases, sizeOfArray, numOfRot;
cout<<"Please, enter the number of test cases: ";
cin>>testCases;
cout<<"Please, enter the size of array: ";
cin>>sizeOfArray;
int* arr=new int[sizeOfArray];
for(int i=0;i<testCases;i++)
{
cout<<"Please, enter the number of rotation to perform: ";
cin>>numOfRot;
cout<<"Please, enter the elements of the array: ";
for(int j=0;j<sizeOfArray;j++)
{
cin>>arr[j];
}
int* res=Rotation(arr,sizeOfArray,numOfRot);
cout<<"Resulting array is ";
for(int k=0;k<sizeOfArray;k++)
{
cout<<res[k]<<" ";
}
cout<<endl;
}
}
Comments
Leave a comment