So I was solving this leetcode problem of rotating the array right by k places and this is the function I wrote in Python:
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
n=len(nums)
k=k%n
nums[:n-k]=nums[:n-k][::-1]
nums[n-k:]=nums[n-k:][::-1]
nums=nums[::-1]
Now the problem is that when the nums array is printed finally, it’s printing the value stored after line no. 6 and not line no.7. But when I change line no.7 to nums[:]=nums[::-1], it prints the correct solution. I can’t understand why.
New contributor
Aditya Raj is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.