Suppose we have an array A. We have to rotate right it k steps. So if the array is A = [5, 7, 3, 6, 8, 1, 5, 4], and k = 3, then the output will be [1,5,4,5,7,3,6,8]. The steps are like
To solve this, we will follow these steps.
Let us see the following implementation to get a better understanding −
class Solution(object): def rotate(self, nums, k): """ :type nums: List[int] :type k: int :rtype: None Do not return anything, modify nums in-place instead. """ n = len(nums) k%=n nums[:] = nums[n-k:]+nums[:n-k] nums = [5,7,3,6,8,1,5,4] ob1 = Solution() ob1.rotate(nums, 3) print(nums)
nums = [5,7,3,6,8,1,5,4] k = 3
[1,5,4,5,7,3,6,8]