Introduction
Array rotation is a common operation in computer science that involves shifting the elements of an array to the left or right. One efficient technique to achieve this is the Block Swap Algorithm. This algorithm stands out due to its optimal time complexity and minimal space requirements. By dividing the array into blocks and swapping them strategically, the Block Swap Algorithm ensures a seamless and efficient rotation process.

Problem Statement
We are given an array of elements and r, i.e., a number by which we need to rotate the array and return the final rotated array.
Block Swap Algorithm for Array Rotation is a prevalent and renowned approach for the same.
In this,
1. We divide the given array into two subarrays A and B, where A stores first ‘r’ elements and B stores the following ‘n-r’ elements.
Where n = size of elements in array
r = number of rotations
2. If both subarrays' size is not equal, perform a block swap between A and B, where the block size is the size of the smaller subarray. Reduce the size of the larger subarray by the block size.
- If Block A’s size is smaller than the size of Block B, divide the block B into two parts Bl and Br, where Br is the same size as that of A, perform a block swap between A and Br and the array changes from ABlBr to BrBlA. This places the elements of the smaller block i.e A to their correct rotated positions(After k rotations)
- If Block B’s size is smaller than the size of Block A, divide the block A into two parts Al and Ar, where Al is the same size as that of B, perform a block swap between Al and B and the array changes from AlArB to BArAl. This places the elements of the smaller block i.e B to their correct rotated positions(After k rotations)
Repeat until both the subarrays are of equal size.
3. At last perform block swap between A and B.
Let's follow through an example:
Suppose we are given arr = [1,2,3,4,5] r = 2
Step1: We divide the entire array into two parts: r and n-r. So, subarray A would have 2 elements, and array B would have n-r = 5-2 = 3 elements.
Step2: Compare the size of both the subarrays A and B.
Step3: Since A’s size < B’s size. So, divide B subarray into other 2 parts - Bl and Br.
Br is the same size as subarray A from the end, and Bl is the remaining length.
Step4: Swap elements of subarray A and Br.
So, ABlBr array changes to BrBlA in terms of elements, and A subarray elements come at their final position.
Step5: Now, we again compare the size of the remaining non-final subarrays, i.e., A and B (which has now reduced to Bl).
Step6: Since A’s size > new B’s size. So, divide A subarray into other 2 parts - Al and Ar.
Al is the same size as subarray B from the start, and Ar is the remaining size.
Step7: Swap elements of subarray Al and B.
So, AlArB array changes to BArAl in terms of elements, and B subarray elements come at their final position.
Step8: Now, we again compare the size of the remaining non-final subarrays, i.e., A(which reduces to Ar) and B.
Step9: Since A’s size = B’s size, we come out of the loop.
Step10: At the end Block Swap between the elements of subarrays A and B is performed.
Hence, we obtain our final array after rotation of r.
See more, euclid gcd algorithm