- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions

# Program to find number of sublists we can partition so given list is sorted finally in python

Suppose we have a list of numbers called nums. We can partition the list into some individual sublists then sort each piece. We have to find the maximum number of sublists we can partition to such that nums as a whole is sorted afterwards.

So, if the input is like nums = [4, 3, 2, 1, 7, 5], then the output will be 2, as we can sort the sublists like [4, 3, 2, 1] and [7, 5]

To solve this, we will follow these steps:

- count := 0
- main_sum := 0, sorted_sum := 0
- for each element x from nums and y from sorted form of nums, do
- main_sum := main_sum + x
- sorted_sum := sorted_sum + y
- if main_sum is same as sorted_sum, then
- count := count + 1

- return count

Let us see the following implementation to get better understanding:

## Example

class Solution: def solve(self, nums): count = 0 main_sum = sorted_sum = 0 for x, y in zip(nums, sorted(nums)): main_sum += x sorted_sum += y if main_sum == sorted_sum: count += 1 return count ob = Solution() nums = [4, 3, 2, 1, 7, 5] print(ob.solve(nums))

## Input

[4, 3, 2, 1, 7, 5]

## Output

2

- Related Articles
- Program to find number of sublists whose sum is given target in python
- Program to find index, where we can insert element to keep list sorted in Python
- Program to find number of sublists with sum k in a binary list in Python
- Program to check whether we can split list into consecutive increasing sublists or not in Python
- Program to partition color list in Python
- Program to find the number of unique integers in a sorted list in Python
- Program to find shortest sublist so after sorting that entire list will be sorted in Python
- Python program to print all sublists of a list.
- Program to find all prime factors of a given number in sorted order in Python
- Program to check whether we can partition a list with k-partitions of equal sum in C++
- Program to find maximum number of coins we can collect in Python
- Program to find number of sublists that contains exactly k different words in Python
- Program to find the sum of the lengths of two nonoverlapping sublists whose sum is given in Python
- Program to merge two sorted list to form larger sorted list in Python
- Program to find squared elements list in sorted order in Python

Advertisements