- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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 Questions & Answers
- 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 find shortest sublist so after sorting that entire list will be sorted 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 check whether we can split list into consecutive increasing sublists or not 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 find maximum number of coins we can collect in Python
- Program to find the sum of the lengths of two nonoverlapping sublists whose sum is given in Python
- Program to find maximum number of coins we can get using Python
- Program to find number of sublists that contains exactly k different words in Python
- Program to find number of ways we can decode a message in Python
- Program to find number of ways we can split a palindrome in python

Advertisements