//
//  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;
}