
You are given an integer ‘N’, denoting the total number of bags. Each bag contains some items, and each item has a value attached to it. Your task is to equalize the total value of all the bags by swapping any two items, and the following rules must be followed :
The number of items in a bag must remain constant before and after swapping.
Any number of swappings is permitted.
It is guaranteed that there is at least one way to make the values of all the bags equal to each other.
The first line contains an integer ‘T’, which denotes the number of test cases or queries to be run. Then, the ‘T’ test cases follow.
The first line of each test case contains an integer ‘N’, denoting the total number of bags. The next 2*’N’ lines contain the following :
The first line contains a single integer ‘M’, denoting the number of items in the bag.
The second line contains ‘M’ space-separated integers, denoting the value of each item in the bag.
Output Format :
For each test case, print in a new line the contents of every bag in non-decreasing order.
Output for each test case will be printed in a separate line.
The contents of all the bags must be printed in separate lines, and the following rules must be followed :
The contents of the bag that contains the item having the least value must be printed on the first line.
The contents of the bag that contains the item having the second least value must be printed on the second line and so on and so forth.
Note :
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N,M <= 10
1 <= X <= 10^4
Where 'X' is the value of any item in any bag
Time Limit: 1sec
1
3
3
9 1 4
2
5 6
3
2 11 7
1 5 9
2 6 7
4 11
There are a total of three bags. After swapping 4 from the first bag and 5 from the second bag, the total sum of the first bag becomes equal to 15, and the total value of the second bag becomes 10. After swapping 6 from the second bag and 11 from the third bag, the total sum of the second bag becomes 15, and the sum of the third bag becomes 15, and thus, the total values of every bag become equal.
Since the least value is 1, we print the contents of the bag, which has the value 1 followed by the bag having the value 2, and finally the bag, which has the value 4.
1
2
3
4 1 2
2
5 6
2
1 2 6
4 5
There are a total of two bags. After swapping 4 from the first bag and 6 from the second bag, the total sum of the first bag becomes equal to 9, and the total value of the second bag becomes 9, and thus, the total values of every bag become equal.
Since the least value is 1, we print the contents of the bag, which has the value 1, and finally the bag, which has the value 4.
Find out the required value for each bag. We need to store the sizes of all the bags and the values of all the items.
Approach:
The approach is to find out the required value for each bag. We need to store the sizes of all the bags and the values of all the items. Then for each size, we can find out these many values that can sum up to the required weight for each bag. We can keep doing this for all bags and finally when we get our result, we can simply sort the contents in the required order and return the values.
Steps :
unordered_map<int, bool> currentBagItems(allItems, bagSize, valuePerBag, &checkItems, idx)
O(2 ^ N), where N is the total number of items in all the bags.
The time complexity is O(2 ^ N) because for each bag size, out of all N values, we are trying to find a subset containing these many values that can sum up to a certain value
O(N), where N is the total number of items in all the bags.
We need extra linear space to maintain the hashmap that stores the contents for each bag.