Left Rotate an Array by One

Easy
0/40
Average time to solve is 25m
profile
Contributed by
314 upvotes
Asked in companies
Tekion CorpCredex Technology

Problem statement

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.


Example:
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.
Detailed explanation ( Input/output format, Notes, Images )
Input Format :
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.
Sample input 1:
4
5 7 3 2 
Sample output 1:
7 3 2 5
Explanation of sample input 1:
Move the first element to the last and rest all the elements to the left.
Sample input 2:
5
4 0 3 2 5 
Sample output 2:
0 3 2 5 4
Explanation of sample input 2:
Same as sample input 1, Move the first element to the last and rest all the elements to the left
Expected time complexity:
O( n ), Where ‘n’ is the size of an input array ‘arr’.
Constraints :
1 <= 'n' <= 10^5
1 <= 'arr[i] <= 10^9

Time Limit: 1 sec
Hint

Create a Temporary array.

Approaches (2)
Alternate Approach

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:-

  1. Create a 'rotatedArr' of size 'N', where ‘N’ is the size of ‘ARR’.
  2. Now store ith value (1 <= i <= n-1) of 'Arr' at (i-1)th position in 'rotatedArr'.
  3. Store the 0th value of 'ARR' at (N-1)th position in 'rotatedArr'.
  4. Return the ‘rotatedArr’.
Time Complexity

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 ).

Space Complexity

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 )

Code Solution
(100% EXP penalty)
Left Rotate an Array by One
Full screen
Console