- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# 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 are of type i. We also have another value k. We have to find the maximum number of groups of size k we can find, such that each group must have items of distinct types.

So, if the input is like counts = [2, 3, 5, 3] k = 2, then the output will be 6, because let four types of items are 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)].

To solve this, we will follow these steps −

- Define a function possible(). This will take counts, groups, k
- required := groups * k
- for i in range 0 to size of counts, do
- temp := minimum of counts[i], groups and required
- required := required - temp
- if required is same as 0, then
- return True

- return False
- Define a function solve() . This will take counts, k
- res := 0
- l := 0
- r := sum of all elements present in counts
- while l <= r, do
- m := l + floor of (r - l) / 2
- if possible(counts, m, k) is true, then
- l := m + 1
- res := maximum of res and m

- otherwise,
- r := m - 1

- return res

## Example

Let us see the following implementation to get better understanding −

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 l = 0 r = sum(counts) while l <= r: m = l + (r - l) // 2 if possible(counts, m, k): l = m + 1 res = max(res, m) else: r = m - 1 return res counts = [2, 3, 5, 3] k = 2 print(solve(counts, k))

## Input

[2, 3, 5, 3], 2

## Output

6

- Related Articles
- Python program to find N-sized substrings with K distinct characters
- Python – N sized substrings with K distinct characters
- C++ program to find maximum of each k sized contiguous subarray
- Program to find maximum number of balanced groups of parentheses in Python
- Maximum number of groups of size 3 containing two type of items in C++
- Program to find maximum number of groups getting fresh donuts in Python
- Program to find minimum possible maximum value after k operations in python
- Program to find number of distinct combinations that sum up to k in python
- Python – Sort Matrix by K Sized Subarray Maximum Sum
- Program to find number of possible BSTs can be generated using n distinct nodes in Python
- Program to find number of distinct subsequences in Python
- Program to find how many distinct rotation groups are there for a list of words in Python
- Program to count maximum number of distinct pairs whose differences are larger than target in Python
- Python Program to get K length groups with given summation
- Program to find maximum possible value of smallest group in Python

Advertisements