Given an array arr of N elements, the task is to derive a MultiSet having the numbers from the given array in possible repetitions, such that the sum of the MultiSet is strictly greater than given number P and if any of the element is removed, then the sum becomes strictly less than P. Print the number of times the corresponding element of given array is derived into the MultiSet. Print -1 if no such MultiSet can be derived.
Input: arr = [1, 5], P = 4
Output: [0, 1]
Here, if number 1 is taken 0 times, and 5 is taken 1 time, the MultiSet becomes: 
Therefore, sum = 5 (>P) and removing 5 makes sum = 0 (<P). Hence, the required MultiSet is .
Therefore, the output is [0, 1] as the number of times 1 and 5 are taken respectively.
Input: arr = [1, 5], P = 10
If we take a multiset as [1, 5, 5], the sum will be > P, but removing 1 will not make sum < P. Hence, no such MultiSet can be derived in this case.
The main observation of the above problem is, if P is indivisible by any of the elements in array arr, then we take all the multiples of that element such that the sum is strictly greater than p. But if there is no such element in the array and the multiset is possible then we sort the array in descending order and take multiples of each element one less than P % arr[i] and keep updating the P. The multi set is not possible if every time the updated P is divisible by arr[i+1] till N.
Below is the implementation of above approach:
Time Complexity: O(N)
Auxiliary Space: O(1)
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.
- Find the element whose multiplication with -1 makes array sum 0
- Replacing an element makes array elements consecutive
- Minimize the sum calculated by repeatedly removing any two elements and inserting their sum to the Array
- Largest possible Subset from an Array such that no element is K times any other element in the Subset
- Maximum contiguous decreasing sequence obtained by removing any one element
- Count of triplets from the given Array such that sum of any two elements is the third element
- Find the Minimum length Unsorted Subarray, sorting which makes the complete array sorted
- Find a pair of elements swapping which makes sum of two arrays same
- Split N into two integers whose addition to A and B makes them equal
- Find the winner of a game of removing any number of stones from the least indexed non-empty pile from given N piles
- Find side of Square which makes minimal area to fit two identical rectangles inside it
- multiset rbegin() and rend() function in C++ STL
- multiset crbegin() and crend() function in C++ STL
- multiset cbegin() and cend() function in C++ STL
- multiset begin() and end() function in C++ STL
- Difference between pair in Multiset and Multimap in C++ STL
- Count of ways to make Array sum even by removing only one element
- Find an element in array such that sum of left array is equal to sum of right array
- Find last two remaining elements after removing median of any 3 consecutive elements repeatedly
- Choose two elements from the given array such that their sum is not present in any of the arrays
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.