You are given an array of n integers, nums, where there are at most 50 unique values in the array. You are also given an array of m customer order quantities, quantity, where quantity[i] is the amount of integers the ith customer ordered. Determine if it is possible to distribute nums such that:


	The ith customer gets exactly quantity[i] integers,
	The integers the ith customer gets are all equal, and
	Every customer is satisfied.


Return true if it is possible to distribute nums according to the above conditions.

&nbsp;
Example 1:

Input: nums = [1,2,3,4], quantity = [2]
Output: false
Explanation: The 0th customer cannot be given two different integers.


Example 2:

Input: nums = [1,2,3,3], quantity = [2]
Output: true
Explanation: The 0th customer is given [3,3]. The integers [1,2] are not used.


Example 3:

Input: nums = [1,1,2,2], quantity = [2,2]
Output: true
Explanation: The 0th customer is given [1,1], and the 1st customer is given [2,2].


&nbsp;
Constraints:


	n == nums.length
	1 &lt;= n &lt;= 105
	1 &lt;= nums[i] &lt;= 1000
	m == quantity.length
	1 &lt;= m &lt;= 10
	1 &lt;= quantity[i] &lt;= 105
	There are at most 50 unique values in nums.


class Solution:
    def canDistribute(self, nums: List[int], quantity: List[int]) -> bool:
        freq = {}
        for x in nums: freq[x] = 1 + freq.get(x, 0)
        
        vals = sorted(freq.values(), reverse=True)
        quantity.sort(reverse=True) # pruning - large values first  
        
        def fn(i): 
            """Return True if possible to distribute quantity[i:] to remaining."""
            if i == len(quantity): return True 
            seen = set()
            for k in range(len(vals)): 
                if vals[k] >= quantity[i] and vals[k] not in seen: 
                    seen.add(vals[k]) # pruning - unqiue values 
                    vals[k] -= quantity[i]
                    if fn(i+1): return True 
                    vals[k] += quantity[i] # backtracking
                    
        return fn(0)
class Solution {
    public boolean canDistribute(int[] nums, int[] quantity) {
        
        // Use a map to count the numbers, ex: nums:[5,7,4,7,4,7] -> {5:1, 7:3, 4:2}
        Map<Integer, Integer> freq = new HashMap<>();
        for (int num : nums)
            freq.put(num, freq.getOrDefault(num, 0)+1);
        
        // Turn values of the map into array, ex: {5:1, 7:3, 4:2} -> [1, 3, 2]
        int[] dist = new int[freq.size()];
        int idx = 0;
        for (int f : freq.values())
            dist[idx++] = f;
        
		// Fullfill the quantities from the biggest quantity to the smallest.
        // If the bigger quantity can't be filled, the program will stop as early as possible.
        Arrays.sort(quantity);
        return rec(dist, quantity, quantity.length-1);
    }
    
    // try to fullfill the j-th order quantity
    private boolean rec(int[] dist, int[] quantity, int j) {
        
        // stop condition. We've fulfilled all the order quantities.
        if (j == -1)
            return true;
        
        Set<Integer> used = new HashSet<>();
        for (int i = 0 ; i < dist.length ; ++i) {
		
			// Use a set to make sure that 
			//   we don't distribute from the same amount to this j-th order for more than once.
            // With this check, the program reduces from 97ms to 25 ms.
            if (dist[i] >= quantity[j] && used.add(dist[i])) {
                dist[i] -= quantity[j];
                if (rec(dist, quantity, j-1))
                    return true;
                dist[i] += quantity[j];
            }
        }
        return false;
    }
}
class Solution {
public:
    bool solve(vector<int>&q, map<int,int>&count, int idx){
        if(idx==q.size()){
            return true;
        }
        for(auto it=count.begin();it!=count.end();it++){
            if(it->second>=q[idx]){
                count[it->first]-=q[idx];
                if(solve(q,count,idx+1))
                    return true;
                count[it->first]+=q[idx];
            }
        }
        return false;
    }
    bool canDistribute(vector<int>& nums, vector<int>& quantity) {
        map<int,int>count;
        int n=nums.size();
        for(int i=0;i<n;i++){
            count[nums[i]]++;
        }
        sort(quantity.begin(),quantity.end(),greater<int>());
        return solve(quantity,count,0);

    }
};
var canDistribute = function(nums, quantity) {
    quantity.sort((a,b)=>b-a)
    let freq={},flag=false
    for(let num of nums)
        freq[num]=(freq[num]||0) +1
    let set=Object.keys(freq),n=set.length,k=quantity.length
    let rec=(prefix)=>{
        if(prefix.length==k || flag==true)  
            return flag=true
        for(let i=0;i<n;i++){
            freq[set[i]]-=quantity[prefix.length]
			prefix.push(set[i])
            if( freq[set[i]]>=0)
                rec(prefix)
			prefix.pop()
            freq[set[i]]+=quantity[prefix.length]
        }
    }
    rec([])
    return flag
};
Distribute Repeating Integers