Problem statement
In the problem, you are given the two arrays, container[] and ball[]. The challenge is to determine whether it is possible to entirely fill every container with the given balls if each container may only hold balls of the same type. The maximum number of balls that the i-th container can hold is stored in array Container[i].
Example 1
Input
Container[] = {1, 2, 2, 4}
Ball[] = {5,10,10,12,12,16,18,18,18,18}
Output
YES
Explanation
Container[0] can hold a maximum of 1 ball, so it will hold ball[0] that is 5
Container[1] can hold a maximum of 2 balls of the same type, so it will hold ball[1] and ball[2] that is 10
Container[2] can hold a maximum of 2 balls of the same type, so it will hold ball[3] and ball[4] that is 12
Container[3] can hold a maximum of 4 balls of the same type, so it will hold ball[6], ball[7], ball[8], and ball[9] that is 18
Hence all the containers are completely occupied from the given list of balls.
Example 2
Input
Container[] = {1, 2, 2, 5}
Ball[] = {5,10,10,12,12,16,18,18,18,18}
Output
NO
Explanation
Container[0] can hold a maximum of 1 ball, so it will hold ball[0] that is 5
Container[1] can hold a maximum of 2 balls of the same type, so it will hold ball[1] and ball[2] that is 10
Container[2] can hold a maximum of 2 balls of the same type, so it will hold ball[3] and ball[4] that is 12
Container[3] can hold a maximum of 5 balls of the same type, so it will hold ball[6], ball[7], ball[8] and ball[9] that is 18 but still is remains left by one ball, and it is not completely filled.
Hence all the containers are not completely occupied from the given list of balls.
Recommended topic about, kth largest element in an array, and Euclid GCD Algorithm
Approach
In such types of problems where we have to explore all the possibilities like which container is best suited for which type of ball the intitution should be toward recursion or Backtracking. To check if it's possible to completely fill every container with the same ball or not, the idea is to use Backtracking. It can be seen that the frequency of each form of a ball is all that is required. Thus we store the frequency of each type of ball in a map.




