filmov
tv
#1 - Right Rotation of array in Data Structures and Algorithms (DSA) | Python | Praveen Kumar

Показать описание
In Right Rotation of Array, the elements of the array moved right as per given kth position.
For Example:
arr = [1,2,3,4,5] is the input array
k=2, is the position from where we want to rotate in right side
output = [4,5,1,2,3]
Steps followed:
1- Handle ‘k’ Greater than ‘n’ (k=k%n)
2- Reverse the entire arrary (Reverse(arr,0,n-1))
3- Reverse the first ‘k’ elements (Reverse(arr,0,k-1))
4- Reverse the remaining ‘n-k’ elements (Reverse(arr,k,n-1))
5- Return the Rotated Array
Time Complexity: O(n), where n is the length of the array. Each reverse operation takes O(n) in total.
Space Complexity: O(1), as no extra space is used except for variables.
Like, Share and Subscribe!!
Keep Learning and Keep Shinning!!
#pythonlearning #python #interview #dsa #dsalgo #learningcoding
For Example:
arr = [1,2,3,4,5] is the input array
k=2, is the position from where we want to rotate in right side
output = [4,5,1,2,3]
Steps followed:
1- Handle ‘k’ Greater than ‘n’ (k=k%n)
2- Reverse the entire arrary (Reverse(arr,0,n-1))
3- Reverse the first ‘k’ elements (Reverse(arr,0,k-1))
4- Reverse the remaining ‘n-k’ elements (Reverse(arr,k,n-1))
5- Return the Rotated Array
Time Complexity: O(n), where n is the length of the array. Each reverse operation takes O(n) in total.
Space Complexity: O(1), as no extra space is used except for variables.
Like, Share and Subscribe!!
Keep Learning and Keep Shinning!!
#pythonlearning #python #interview #dsa #dsalgo #learningcoding