Given n instance of item A and m instance of item B. Find the maximum number of groups of size 3 that can be formed using these items such that all groups contain items of both types, i.e., a group should not have either all items of type A or all items of type B.
Total number of items of type A in the formed groups must be less than or equal to n.
Total number of items of type B in the formed groups must be less than or equal to m.
Input : n = 2 and m = 6. Output : 2 In group 1, one item of type A and two items of type B. Similarly, in the group 2, one item of type A and two items of type B. We have used 2 (<= n) items of type A and 4 (<= m) items of type B. Input : n = 4 and m = 5. Output : 3 In group 1, one item of type A and two items of type B. In group 2, one item of type B and two items of type A. In group 3, one item of type A and two items of type B. We have used 4 (<= n) items of type A and 5 (<= 5) items of type B.
1. There will be n groups possible if m >= 2n. Or there will be m groups possible, if n >= 2m.
2. Suppose n = 3 and m = 3, so one instance of item A will make a group with the two instance of item B and one instance of item B will make a group with the two instance of item A. So, maximum two groups are possible. So find the total number of such conditions with given n and m by dividing m and m by 3. After this, there can be 0, 1, 2 instances of each type can be left. For finding the number of groups for the left instances:
a) If n = 0 or m = 0, 0 group is possible.
b) If n + m >= 3, only 1 group is possible.
Algorithm for solving this problem:
1. If n >= 2m, maximum number of groups = n.
2. Else if m >= 2n, maximum number of groups = m.
3. Else If (m + n) % 3 == 0, maximum number of group = (m + n)/3;
4. Else maximum number of group = (n + m)/3. And set n = n%3 and m = m%3.
a) If n != 0 && m != 0 && (n + m) >= 3, add one to maximum number of groups.
Below is implementation of the above idea :
This article is contributed by Anuj Chauhan(anuj0503). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Maximum items that can be bought with the given type of coins
- Number of ways to make binary string of length N such that 0s always occur together in groups of size K
- Reverse a singly Linked List in groups of given size | Set 3
- Maximum profit by selling N items at two markets
- Minimum window size containing atleast P primes in every window of given range
- Divide N segments into two non-empty groups such that given condition is satisfied
- Maximum size of square such that all submatrices of that size have sum less than K
- Count of ways to distribute N items among 3 people with one person receiving maximum
- Number of groups of magnets formed from N magnets
- Count the number of ways to divide N in k groups incrementally
- Count the number of ordered sets not containing consecutive numbers
- Minimum number of items to be delivered
- Split an array into groups of 3 such that X3 is divisible by X2 and X2 is divisible by X1
- Minimize the cost of partitioning an array into K groups
- Count of different groups using Graph
- Segregate groups of first N numbers having GCD equals to 1
- Count of groups among N people having only one leader in each group
- Minimum Bipartite Groups
- Generate a string of size N whose each substring of size M has exactly K distinct characters
- Count numbers less than N containing digits from the given set : Digit DP