- 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

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

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))

[2, 3, 5, 3], 2

6

- Related Questions & Answers
- 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
- Maximum number of groups of size 3 containing two type of items in C++
- Program to find maximum number of balanced groups of parentheses in Python
- 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
- 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
- Python – Sort Matrix by K Sized Subarray Maximum Sum
- Distinct number of specific items in list with MySQL
- Program to find number of possible BSTs can be generated using n distinct nodes in Python
- Program to find minimum number of monotonous string groups in Python
- Python Program to get K length groups with given summation

Advertisements