Objective : We will be given one array and asked to rotate the array by d no. of time
Approach 2: Rotate by one to left ex- Input - {1,2,3,4,5} d=2
Output - {3,4,5,1,2}
I will Recommend you to please do it by yourself before see the answer.
Approach 1 : Using temp Array
Pseudo code
- We will use one temp array where we will add the element of value d
- then we will copy the d+1th element to 0th place d+2th element to 1th element and so on .
- Now we will merge the temp array elements at the end of the array.
Pseudo Code .
- We will first store the first element to temp variable.
- Move all the element to left by one.
- store the temp variable in last place.
Approach 3 : Reversal Algorithm
Pseudo Code
- Let suppose we need to rotate the array by d index what we will do is as below
- we first reverse the (o,d) element of the array
- in second we will reverse the (d+1 , length) elements of the array .
- In third we will reverse (0, length ) element of the array this will give use the required array.
Program Code:
If you like this article please share it with your friends and colleagues
Thanks for reading
Noeik
0 comments:
Post a Comment