diff --git a/DSA/Arrays/Rotate an array.cpp b/DSA/Arrays/Rotate an array.cpp new file mode 100644 index 0000000..87678d2 --- /dev/null +++ b/DSA/Arrays/Rotate an array.cpp @@ -0,0 +1,51 @@ +// { Driver Code Starts +#include +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