Given an array of N elements and the task is to print the elements of an array after left rotating array elements by d positions.
Input: N, d and next line containing the n elements of array.
Output: Array elements after d rotation.
Example:
Input:
n = 7, d = 2
array elements: 1 2 3 4 5 6 7
Output:
3 4 5 6 7 1 2
The naïve approach to solve this problem is to shift the all elements d times but this is a time-consuming process.
We can use a small trick here to print the elements of the array after left rotating d elements.
Let, i = ith iteration D = number of elements to rotate N = size of array Then to left rotate array we can use - arr[i] = arr[(i+D)%N]
C++ Implementation:
Output