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

Find Merge Point of Two Lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the head nodes of 2 linked lists that merge together at some point, find the node where the two lists merge. The merge point is where both lists point to the same node, i.e. they reference the same memory location. It is guaranteed that the two head nodes will be different, and neither will be NULL. If the lists share

View Solution →

Inserting a Node Into a Sorted Doubly Linked List

Given a reference to the head of a doubly-linked list and an integer ,data , create a new DoublyLinkedListNode object having data value data and insert it at the proper location to maintain the sort. Example head refers to the list 1 <-> 2 <-> 4 - > NULL. data = 3 Return a reference to the new list: 1 <-> 2 <-> 4 - > NULL , Function Description Complete the sortedInsert function

View Solution →

Reverse a doubly linked list

This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.

View Solution →

Tree: Preorder Traversal

Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's

View Solution →

Tree: Postorder Traversal

Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the

View Solution →

Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func

View Solution →