36
loading...
This website collects cookies to deliver better user experience
d
times (where d
is the rotation factor). We are going to use Reversal algorithm for rotating the array in left direction.d
elements.n-d
elements.n
elements.samparr
, begin
, end
. We use a while
loop and assign the starting value of samparr
to a temporary variable called temp
. samparr
to the ending value of samparr
. And, finally, we assign the ending value of samparr
to the temp
again. We use this temp
variable to dynamically change the starting and ending values of the samparr
. We then increment the start with 1 and decrement the end with 1. This is going to be our main function, which we would call with respect to the above-mentioned 3 steps.d
. For that, we create another function which takes the samparr
, d
and n
as parameters and return the rotated array. We return the function if we have d=0, which means the array is empty. Going good till now, but wait!, What if the d
is greater than n
, step 2 will become broken, to fix that, we just update d
as d % n
. We'll look at an example in a minimum scale to better understand this d % n
Reverse
Logger
, which will console log the rotated array. This function takes two parameters, samparr
and n
. It is a simple function which loops through all elements in the array and log them onto console.