# Program to find shortest sublist so after sorting that entire list will be sorted in Python

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 −

• f:= -1, l:= -1
• lst:= sort the list nums
• for i in range 0 to size of nums, do
• if nums[i] is not same as lst[i], then
• if f is same as -1, then
• f := i
• otherwise,
• l := i
• if l is same as -1 and f is same as -1, then
• return 0
• return l - f + 1

Let us see the following implementation to get better understanding −

## Example

Live Demo

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]))

## Input

[1,2,5,4,9,10]

## Output

2