- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# 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 f is same as -1, then

- if nums[i] is not same as lst[i], then
- 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

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

- Related Articles
- Program to find shortest subarray to be removed to make array sorted in Python
- Program to find longest equivalent sublist after K increments in Python
- Program to find minimum amplitude after deleting KLength sublist in Python
- Program to find shortest string after removing different adjacent bits in Python
- Program to find length of shortest sublist with maximum frequent element with same frequency in Python
- Program to find number of sublists we can partition so given list is sorted finally in python
- Program to find length of longest contiguously strictly increasing sublist after removal in Python
- Java Program to Find a Sublist in a List
- Program to merge two sorted list to form larger sorted list in Python
- Program to find squared elements list in sorted order in Python
- Program to count minimum k length sublist that can be flipped to make all items of list to 0 in Python
- Program to find length of smallest sublist that can be deleted to make sum divisible by k in Python
- Program to count number of switches that will be on after flipping by n persons in python
- Program to find sum of the minimums of each sublist from a list in Python
- Python program to find Sum of a sublist

Advertisements