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 so 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].

Algorithm

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

How It Works

The key insight is that if we can partition the list at position i, then the sum of elements from start to position i in the original list must equal the sum of elements from start to position i in the sorted list. This ensures that sorting each partition individually will result in the same final sorted array.

Example

Let us see the following implementation to get better understanding ?

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

# Test the solution
ob = Solution()
nums = [4, 3, 2, 1, 7, 5]
print("Number of sublists:", ob.solve(nums))
Number of sublists: 2

Step-by-Step Execution

For nums = [4, 3, 2, 1, 7, 5] and sorted_nums = [1, 2, 3, 4, 5, 7] ?

nums = [4, 3, 2, 1, 7, 5]
sorted_nums = sorted(nums)
print("Original:", nums)
print("Sorted:  ", sorted_nums)

main_sum = sorted_sum = 0
count = 0

for i, (x, y) in enumerate(zip(nums, sorted_nums)):
    main_sum += x
    sorted_sum += y
    print(f"Position {i}: main_sum={main_sum}, sorted_sum={sorted_sum}")
    
    if main_sum == sorted_sum:
        count += 1
        print(f"  ? Partition found! Count = {count}")

print(f"\nTotal partitions: {count}")
Original: [4, 3, 2, 1, 7, 5]
Sorted:   [1, 2, 3, 4, 5, 7]
Position 0: main_sum=4, sorted_sum=1
Position 1: main_sum=7, sorted_sum=3
Position 2: main_sum=9, sorted_sum=6
Position 3: main_sum=10, sorted_sum=10
  ? Partition found! Count = 1
Position 4: main_sum=17, sorted_sum=15
Position 5: main_sum=22, sorted_sum=22
  ? Partition found! Count = 2

Total partitions: 2

Alternative Implementation

Here's a more concise version without using a class ?

def count_partitions(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

# Test with different examples
test_cases = [
    [4, 3, 2, 1, 7, 5],
    [1, 2, 3, 4, 5],
    [5, 4, 3, 2, 1],
    [1, 3, 2, 4]
]

for nums in test_cases:
    result = count_partitions(nums)
    print(f"Input: {nums} ? Partitions: {result}")
Input: [4, 3, 2, 1, 7, 5] ? Partitions: 2
Input: [1, 2, 3, 4, 5] ? Partitions: 5
Input: [5, 4, 3, 2, 1] ? Partitions: 1
Input: [1, 3, 2, 4] ? Partitions: 2

Conclusion

This algorithm efficiently finds the maximum number of sublists by comparing cumulative sums of the original and sorted arrays. The time complexity is O(n log n) due to sorting, and space complexity is O(n) for the sorted array.

Updated on: 2026-03-25T12:46:29+05:30

243 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements