Program to find maximum number of K-sized groups with distinct type items are possible in Python

Suppose we have a list of numbers called counts where counts[i] represents the number of items of type i. We also have another value k. We have to find the maximum number of groups of size k we can form, such that each group must have items of distinct types.

So, if the input is like counts = [2, 3, 5, 3] and k = 2, then the output will be 6. Let four types of items be represented by a, b, c, d respectively. We can have the following groups of k = 2, where all elements are of distinct types: [(c, a), (b, a), (c, b), (c, b), (d, a), (d, a)].

Approach

We use binary search to find the maximum number of groups possible. For each candidate number of groups, we check if it's possible to form that many groups using a greedy approach ?

Algorithm Steps

  • Define a function possible() to check if we can form a given number of groups
  • Use binary search on the range [0, sum(counts)] to find maximum groups
  • For each candidate, greedily assign items to minimize waste

Implementation

def possible(counts, groups, k):
    required = groups * k
    for i in range(len(counts)):
        temp = min(counts[i], groups, required)
        required -= temp
        if required == 0:
            return True
    return False

def solve(counts, k):
    res = 0
    left = 0
    right = sum(counts)
    
    while left <= right:
        mid = left + (right - left) // 2
        if possible(counts, mid, k):
            left = mid + 1
            res = max(res, mid)
        else:
            right = mid - 1
    return res

# Example usage
counts = [2, 3, 5, 3]
k = 2
result = solve(counts, k)
print(f"Maximum groups possible: {result}")

The output of the above code is ?

Maximum groups possible: 6

How It Works

The possible() function checks if we can form exactly groups number of groups of size k. It calculates total items needed (groups * k) and greedily uses available items from each type.

For each item type, we can use at most min(counts[i], groups, required) items:

  • counts[i] − available items of this type
  • groups − we can use at most one item of each type per group
  • required − remaining items needed

Example Walkthrough

For counts = [2, 3, 5, 3] and k = 2, checking if 6 groups are possible ?

def example_walkthrough():
    counts = [2, 3, 5, 3]
    groups = 6
    k = 2
    required = groups * k  # 12 items needed
    
    print(f"Need {required} items total for {groups} groups of size {k}")
    
    for i, count in enumerate(counts):
        can_use = min(count, groups, required)
        required -= can_use
        print(f"Type {i}: have {count}, use {can_use}, remaining needed: {required}")
        
        if required == 0:
            print("Success! Can form all groups.")
            return True
    
    print("Cannot form all groups.")
    return False

example_walkthrough()
Need 12 items total for 6 groups of size 2
Type 0: have 2, use 2, remaining needed: 10
Type 1: have 3, use 3, remaining needed: 7
Type 2: have 5, use 5, remaining needed: 2
Type 3: have 3, use 2, remaining needed: 0
Success! Can form all groups.

Conclusion

This solution uses binary search combined with a greedy verification function to efficiently find the maximum number of distinct-type groups possible. The time complexity is O(n log(sum)) where n is the number of item types.

---
Updated on: 2026-03-26T17:29:54+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements