Given an array 'arr' containing 'n' elements, rotate this array left once and return it.
Rotating the array left by one means shifting all elements by one place to the left and moving the first element to the last position in the array.
Input: 'a' = 5, 'arr' = [1, 2, 3, 4, 5]
Output: [2, 3, 4, 5, 1]
Explanation: We moved the 2nd element to the 1st position, and 3rd element to the 2nd position, and 4th element to the 3rd position, and the 5th element to the 4th position, and move the 1st element to the 5th position.
The first line will contain a single integer 'n', the size of the array ‘arr’
The second line will contain ‘n’ integers representing the array elements.
Output format :
The output contains all elements of the rotated array separated by space.
Note :
You don't need to print anything. It has already been taken care of. Just implement the given function.
4
5 7 3 2
7 3 2 5
Move the first element to the last and rest all the elements to the left.
5
4 0 3 2 5
0 3 2 5 4
Same as sample input 1, Move the first element to the last and rest all the elements to the left
O( n ), Where ‘n’ is the size of an input array ‘arr’.
1 <= 'n' <= 10^5
1 <= 'arr[i] <= 10^9
Time Limit: 1 sec
Create a Temporary array.
We can create a temporary array ‘rotatedArr’and store the ith element from 1 to N-1 from ‘ARR’ to i-1 the position in ‘rotatedArr’ and we store 0th element from ‘ARR’ to (n-1)th position in ‘rotatedArr’ and return ‘rotatedArr’.
The steps are as follows:-
O( n ), Where ‘n’ is the size of an input array ‘ARR’.
We are traversing the array ‘ARR’ once and the size of the array is N.
Hence the time complexity is O( n ).
O( n ), Where ‘n’ is the size of an input array ‘ARR’.
We are creating an array of sizes n.
Hence the space complexity is O( n )