Suppose we have a list of numbers called nums, we have to find the length of the shortest sublist in num, if the sublist is sorted, then the entire array nums will be sorted in ascending order.
So, if the input is like nums = [1,2,5,4,9,10], then the output will be 2, as Sorting the sublist [4, 3] would get us [0, 1, 3, 4, 8, 9]
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
class Solution: def solve(self, nums): f=-1 l=-1 lst=sorted(nums) for i in range(len(nums)): if nums[i]!=lst[i]: if f == -1: f=i else: l=i if l == -1 and f == -1: return 0 return l-f+1 ob = Solution() print(ob.solve([1,2,5,4,9,10]))