- 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 max chunks to make array sorted in Python

Suppose we have an array nums, we have to split the array into some number of partitions, and individually sort each of them. Now after concatenating them we will get one sorted array. We have to find the maximum number of partitions we could have made?

So, if the input is like [3,2,4,5,5], then the output will be 4, as we can make partitions like [3,2], [4], [5], [5] .

To solve this, we will follow these steps −

real:= sort the list nums

p1 := 0,p2 := 1, c := 0

Do the following infinitely, do

flag:= True

tmp:= sort the sublist of nums[from index p1 to p2-1]

for j in range 0 to size of tmp, do

if tmp[j] is not same as real[p1+j], then

flag:= False

p2 := p2 + 1

come out from loop

if flag is true, then

p1 := p2

p2:= p2+1

c := c + 1

if p1 is same as size of nums or p2 > size of nums, then

return c

## Example

Let us see the following implementation to get better understanding

def solve(nums): real=sorted(nums) p1,p2,c=0,1,0 while True: flag=True tmp=sorted(nums[p1:p2]) for j in range(len(tmp)): if tmp[j]!=real[p1+j]: flag=False p2+=1 break if flag: p1,p2=p2,p2+1 c+=1 if p1==len(nums) or p2>len(nums): return c nums = [3,2,4,5,5] print(solve(nums))

## Input

{3,2,4,5,5}

## Output

4

- Related Questions & Answers
- Max Chunks To Make Sorted in C++
- Max Chunks To Make Sorted II in C++
- Program to find shortest subarray to be removed to make array sorted in Python
- Program to find minimum number of operations to make string sorted in Python
- Program to find ways to make a fair array in Python
- Program to find minimum moves to make array complementary in Python
- Delete Columns to Make Sorted in Python
- Program to find minimum operations to make array equal using Python
- C++ code to count operations to make array sorted
- Program to find sum of absolute differences in a sorted array in Python
- Program to find minimum operations to make the array increasing using Python
- C++ code to find max ornaments to make decoration good
- Program to find max value of an equation in Python
- Check if reversing a sub array make the array sorted in Python
- C++ Program to Implement Sorted Array