Program to find maximum possible value of smallest group in Python

We need to split a list of numbers into k contiguous groups and find the maximum possible value of the smallest group's sum. This is a classic binary search optimization problem.

For example, given nums = [2, 6, 4, 5, 8] and k = 3, we can split into groups [2, 6], [4, 5], [8] with sums 8, 9, 8. The smallest sum is 8, which is our answer.

Algorithm Approach

We use binary search on the answer space. For each potential minimum sum, we check if it's possible to split the array into exactly k groups where each group sum is at least that value.

Helper Function

The is_divisible(target) function checks if we can split the array into k groups where each group sum is at least target ?

def is_divisible(nums, k, target):
    if target <= 0:
        return True
    
    num_groups = 0
    current_sum = 0
    
    for num in nums:
        current_sum += num
        if current_sum >= target:
            current_sum = 0
            num_groups += 1
            if num_groups == k:
                return True
    
    return False

# Test the helper function
nums = [2, 6, 4, 5, 8]
k = 3
print(is_divisible(nums, k, 8))  # Can we make 3 groups with min sum 8?
print(is_divisible(nums, k, 10)) # Can we make 3 groups with min sum 10?
True
False

Complete Solution

We binary search between 1 and the average sum of all elements. The search space contains all possible minimum sums ?

def max_smallest_group(nums, k):
    def is_divisible(target):
        if target <= 0:
            return True
        
        num_groups = 0
        current_sum = 0
        
        for num in nums:
            current_sum += num
            if current_sum >= target:
                current_sum = 0
                num_groups += 1
                if num_groups == k:
                    return True
        
        return False
    
    left = 1
    right = sum(nums) // k + 1
    
    while left < right - 1:
        mid = (left + right) // 2
        if is_divisible(mid):
            left = mid
        else:
            right = mid
    
    return left

# Test with examples
nums1 = [2, 6, 4, 5, 8]
k1 = 3
result1 = max_smallest_group(nums1, k1)
print(f"nums = {nums1}, k = {k1}")
print(f"Maximum smallest group sum: {result1}")

nums2 = [1, 2, 3, 4, 5, 6]
k2 = 2
result2 = max_smallest_group(nums2, k2)
print(f"\nnums = {nums2}, k = {k2}")
print(f"Maximum smallest group sum: {result2}")
nums = [2, 6, 4, 5, 8], k = 3
Maximum smallest group sum: 8

nums = [1, 2, 3, 4, 5, 6], k = 2
Maximum smallest group sum: 10

How It Works

The algorithm works in two phases:

  1. Binary Search: We search for the maximum possible minimum sum between 1 and the average sum
  2. Feasibility Check: For each candidate sum, we greedily try to form k groups

The greedy approach works because if we can form k groups with a certain minimum sum, we should do so as early as possible to maximize our chances.

Time Complexity

Operation Complexity Explanation
Binary Search O(log(sum/k)) Search space is sum/k
Feasibility Check O(n) Single pass through array
Overall O(n × log(sum/k)) n checks × log iterations

Conclusion

This problem demonstrates the power of binary search on answer spaces. By checking feasibility for each candidate minimum sum, we efficiently find the maximum possible value of the smallest group in O(n log(sum/k)) time.

Updated on: 2026-03-25T13:42:39+05:30

336 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements