Sunday, 23 April 2023

Rotate an array in O(n)

Step 1 :
Reverse the first k elements.

Step 2:

Reverse the k+1 to n-1 elements.

Step 3:

Reverse the whole array.


Prefect!!!

Example :

arr=[1,2,3,4,5] , k=2

expected output : [3,4,5,1,2]

step 1:

[2,1,3,4,5]

step 2:

[2,1,5,4,3]

step 3:

[3,4,5,1,2]

Got it!

No comments:

Post a Comment

Count of digits in factorial

 To count the digits in a factorial we must seek some optimization as you might also understand that getting all the product and storing the...