//
// main.cpp
// Reversal Algorithm (Array rotation)
//
// Created by Himanshu on 18/09/21.
//
#include <iostream>
using namespace std;
const int N = 5;
void printArray (int A[]) {
for (int i=0; i<N; i++) {
cout<<A[i]<<" ";
}
cout<<endl;
}
void Reverse (int A[], int p, int q) {
int temp;
for(int i=p, j=q; i<j; i++, j--) {
temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
void Rotate (int A[], int d) {
cout<<"Array:"<<endl;
printArray(A);
Reverse(A, 0, d-1);
Reverse(A, d, N-1);
Reverse(A, 0, N-1);
}
int main() {
int A[N] = {5, 2, 4, 6, 1};
int d = 4;
Rotate(A, d);
cout<<"Array after "<<d<<" rotations:"<<endl;
printArray(A);
return 0;
}
Ly8KLy8gIG1haW4uY3BwCi8vICBSZXZlcnNhbCBBbGdvcml0aG0gKEFycmF5IHJvdGF0aW9uKQovLwovLyAgQ3JlYXRlZCBieSBIaW1hbnNodSBvbiAxOC8wOS8yMS4KLy8KCiNpbmNsdWRlIDxpb3N0cmVhbT4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmNvbnN0IGludCBOID0gNTsKCnZvaWQgcHJpbnRBcnJheSAoaW50IEFbXSkgewogICAgZm9yIChpbnQgaT0wOyBpPE47IGkrKykgewogICAgICAgIGNvdXQ8PEFbaV08PCIgIjsKICAgIH0KICAgIGNvdXQ8PGVuZGw7Cn0KCnZvaWQgUmV2ZXJzZSAoaW50IEFbXSwgaW50IHAsIGludCBxKSB7CiAgICBpbnQgdGVtcDsKICAgIGZvcihpbnQgaT1wLCBqPXE7IGk8ajsgaSsrLCBqLS0pIHsKICAgICAgICB0ZW1wID0gQVtpXTsKICAgICAgICBBW2ldID0gQVtqXTsKICAgICAgICBBW2pdID0gdGVtcDsKICAgIH0KfQoKdm9pZCBSb3RhdGUgKGludCBBW10sIGludCBkKSB7CiAgICBjb3V0PDwiQXJyYXk6Ijw8ZW5kbDsKICAgIHByaW50QXJyYXkoQSk7CiAgICAKICAgIFJldmVyc2UoQSwgMCwgZC0xKTsKICAgIFJldmVyc2UoQSwgZCwgTi0xKTsKICAgIFJldmVyc2UoQSwgMCwgTi0xKTsKICAgIAp9CgppbnQgbWFpbigpIHsKICAgIGludCBBW05dID0gezUsIDIsIDQsIDYsIDF9OwogICAgaW50IGQgPSA0OwogICAgCiAgICBSb3RhdGUoQSwgZCk7CiAgICAKICAgIGNvdXQ8PCJBcnJheSBhZnRlciAiPDxkPDwiIHJvdGF0aW9uczoiPDxlbmRsOwogICAgcHJpbnRBcnJheShBKTsKICAgIAogICAgcmV0dXJuIDA7Cn0=