K-Distinct Groups - Google Top Interview Questions


Problem Statement :


You are given a list of integers counts where counts[i] represents the number of items that exist of type i. 

You are also given an integer k. 

Return the maximum number of groups of size k we can have given that each group must have items of distinct types.

Constraints

n ≤ 100,000 where n is the length of counts


k ≤ n

Example 1

Input

counts = [3, 3, 2, 5]

k = 2

Output

6

Explanation

Let's name the four item types [a, b, c, d] respectively. We can have the following groups of two where 
all elements are distinct types:



(a, d)

(b, d)

(a, b)

(a, b)

(c, d)

(c, d)

Example 2

Input

counts = [3, 2, 4]

k = 3

Output

2

Explanation

Let's name the three item types [a, b, c] respectively. We can only have these groups of three where all 
elements are distinct types:




(a, b, c)

(a, b, c)


We cannot use (a, c, c) since there are two cs.


Solution :



title-img



                        Solution in C++ :

bool can(vector<int>& v, int take, int k) {
    long long total = 0;
    for (int val : v) {
        total += min(val, take);
    }
    return total / k >= take;
}

int solve(vector<int>& counts, int k) {
    int lhs = 0;
    int rhs = 2e9;
    while (lhs < rhs) {
        int mid = (lhs + rhs + 1) / 2;
        if (can(counts, mid, k)) {
            lhs = mid;
        } else {
            rhs = mid - 1;
        }
    }
    return lhs;
}
                    

                        Solution in Java :

import java.util.*;

class Solution {
    // returns whether we can make num_groups of size k
    public boolean possible(int[] count, int k, long num_groups) {
        long required = num_groups * k;
        for (int i = 0; i < count.length; i++) {
            long use_this_much = Math.min(count[i], num_groups);
            use_this_much = Math.min(use_this_much, required);
            required -= use_this_much;
            if (required == 0)
                return true;
        }
        return false;
    }
    public int solve(int[] counts, int k) {
        long l = 1;
        long r = 0;
        for (int c : counts) r += c;

        int res = 0;
        while (l <= r) {
            long num_groups = l + (r - l) / 2;

            if (possible(counts, k, num_groups)) {
                res = (int) Math.max(res, num_groups);
                l = num_groups + 1;
            } else {
                r = num_groups - 1;
            }
        }
        return (int) res;
    }
}
                    

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

    def solve(self, counts, k):
        res = 0
        l = 0
        r = sum(counts)
        while l <= r:
            m = l + (r - l) // 2
            if self.possible(counts, m, k):
                l = m + 1
                res = max(res, m)
            else:
                r = m - 1
        return res
                    

View More Similar Problems

Median Updates

The median M of numbers is defined as the middle number after sorting them in order if M is odd. Or it is the average of the middle two numbers if M is even. You start with an empty number list. Then, you can add numbers to the list, or remove existing numbers from it. After each add or remove operation, output the median. Input: The first line is an integer, N , that indicates the number o

View Solution →

Maximum Element

You have an empty sequence, and you will be given N queries. Each query is one of these three types: 1 x -Push the element x into the stack. 2 -Delete the element present at the top of the stack. 3 -Print the maximum element in the stack. Input Format The first line of input contains an integer, N . The next N lines each contain an above mentioned query. (It is guaranteed that each

View Solution →

Balanced Brackets

A bracket is considered to be any one of the following characters: (, ), {, }, [, or ]. Two brackets are considered to be a matched pair if the an opening bracket (i.e., (, [, or {) occurs to the left of a closing bracket (i.e., ), ], or }) of the exact same type. There are three types of matched pairs of brackets: [], {}, and (). A matching pair of brackets is not balanced if the set of bra

View Solution →

Equal Stacks

ou have three stacks of cylinders where each cylinder has the same diameter, but they may vary in height. You can change the height of a stack by removing and discarding its topmost cylinder any number of times. Find the maximum possible height of the stacks such that all of the stacks are exactly the same height. This means you must remove zero or more cylinders from the top of zero or more of

View Solution →

Game of Two Stacks

Alexa has two stacks of non-negative integers, stack A = [a0, a1, . . . , an-1 ] and stack B = [b0, b1, . . . , b m-1] where index 0 denotes the top of the stack. Alexa challenges Nick to play the following game: In each move, Nick can remove one integer from the top of either stack A or stack B. Nick keeps a running sum of the integers he removes from the two stacks. Nick is disqualified f

View Solution →

Largest Rectangle

Skyline Real Estate Developers is planning to demolish a number of old, unoccupied buildings and construct a shopping mall in their place. Your task is to find the largest solid area in which the mall can be constructed. There are a number of buildings in a certain two-dimensional landscape. Each building has a height, given by . If you join adjacent buildings, they will form a solid rectangle

View Solution →