Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions DSA/Arrays/Rotate an array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// { Driver Code Starts
#include<bits/stdc++.h>
using namespace std;


// } Driver Code Ends

class Solution{
public:

//Function to rotate an array by d elements in counter-clockwise direction.
void rotateArr(int arr[], int d, int n){

reverse(arr, arr+d);//reversing the first d elements
reverse(arr+d, arr+n);//reversing the last n-d elements
reverse(arr, arr+n);//reversing the whole array

}
};


// { Driver Code Starts
int main() {
int t;
//taking testcases
cin >> t;

while(t--){
int n, d;

//input n and d
cin >> n >> d;

int arr[n];

//inserting elements in the array
for(int i = 0; i < n; i++){
cin >> arr[i];
}
Solution ob;
//calling rotateArr() function
ob.rotateArr(arr, d,n);

//printing the elements of the array
for(int i =0;i<n;i++){
cout << arr[i] << " ";
}
cout << endl;
}
return 0;
} // } Driver Code Ends