Rotate array
You have been given a random integer array/list(ARR) of size N. Write a function that rotates the given array/list by D elements(towards the left).
Note:
Input format :
Output Format :
Constraints :
Sample Input 1:
Sample Output 1:
Sample Input 2:
Sample Output 2:
#include <iostream>
using namespace std;
#include "solution.h"
void rotate(int *arr, int d, int n)
{
int temp[d];
for(int i=0;i<d;i++){
temp[i]=arr[i];
}
for(int i=0;i<n-d;i++){
arr[i]=arr[i+d];
}
for(int i=n-d;i<n;i++){
arr[i]=temp[i-(n-d)];
}
}
int main()
{
int t;
cin >> t;
while (t--)
{
int size;
cin >> size;
int *input = new int[size];
for (int i = 0; i < size; ++i)
{
cin >> input[i];
}
int d;
cin >> d;
rotate(input, d, size);
for (int i = 0; i < size; ++i)
{
cout << input[i] << " ";
}
delete[] input;
cout << endl;
}
return 0;
}
Comments
Post a Comment