Array Rotation

#include <iostream> #include <new> using namespace std; void rotarray(int *arr, int sz) { int temp= *(arr+sz-1); for(int i=sz-2;i>=0;i--) { *(arr+i+1)=*(arr+i); } *arr=temp; } void rotarrayt(int *arr, int sz, int t) { int *temp = new int [sz]; for(int i=0;i<t;i++) { *(temp+i)=*(arr+sz-t+i); } for(int i=t;i<sz;i++) { *(temp+i)=*(arr+i-t); } for(int i=0;i<sz;i++) { *(arr+i)=*(temp+i); } } void disparr(int *arr, int sz) { for(int i=0;i<sz;i++) { cout << *(arr+i); } } int main() { int n,q,k; cin >> n >> k >> q; int *arr = new int[n]; int *query = new int[q]; for(int i=0;i<n;i++) { cin >> arr[i]; } for(int i=0;i<q;i++) { cin >> query[i]; } rotarrayt(arr,n,k%n); for(int i=0;i<q;i++) { cout << arr[query[i]] << endl; } delete(arr); delete(query); /* int arr[5]= {1,2,3,4,5}; rotarrayt(arr,5,3); disparr(arr,5); */ return 0; }
Array rotation code

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.