From a2c757a21f8bccd315686fd12a43c5a6a8098679 Mon Sep 17 00:00:00 2001 From: Asif Ahmad <67323237+apexx77@users.noreply.github.com> Date: Thu, 21 Oct 2021 20:11:06 +0530 Subject: [PATCH] Created array_rotation.cpp --- CPP/array_rotation.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 CPP/array_rotation.cpp diff --git a/CPP/array_rotation.cpp b/CPP/array_rotation.cpp new file mode 100644 index 0000000..4fdd7a9 --- /dev/null +++ b/CPP/array_rotation.cpp @@ -0,0 +1,40 @@ +/* + Problem Statement : Given an array, its lenght and k, right rotate the + array by k units and print the resultant array +*/ + +#include +using namespace std; + +// Function to rotate the array + +void rotate(int arr[], int n, int k) { + k %= n; + for (int i = 0; i < n; i++) { + if (i < k) { + cout << arr[n + i - k] << " "; + } + else { + cout << (arr[i - k]) << " "; + } + } + cout << "\n"; +} + +// Main function + +int main() { + + int n, k; + cout << "Enter the size of the array : " << "\n"; + cin >> n; + int arr[n]; + cout << "Enter the array elements : " << "\n"; + for (int i = 0; i < n; i++) cin >> arr[i]; + cout << "Enter the degree of rotation : " << "\n"; + cin >> k; + cout << "The resultant array after rotation is : "; + rotate(arr, n, k); + + return 0; +}