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.
Examples:
Input: arr[] = [1, 5], P = 4
Output: [0, 1]
Explanation:
Here, if number 1 is taken 0 times, and 5 is taken 1 time, the MultiSet becomes: [5]
Therefore, sum = 5 (>P) and removing 5 makes sum = 0 (<P). Hence, the required MultiSet is [5].
Therefore, the output is [0, 1] as the number of times 1 and 5 are taken respectively.Input: arr[] = [1, 5], P = 10
Output: 1
Explanation:
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.
Approach:
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:
# Python implementation to Derive a # MultiSet from given Array such that # sum is > P and removing any # element makes sum < P # Function to derive the multiset def Multiset (n, p, arr):
c = 0
for j in arr:
# Check if p is indivisible
# by any element in arr
if (p % j ! = 0 ):
c = j
break
# Check if there is no element in
# arr which cannot divide p
if (c = = 0 ):
d = sorted (arr)
# Sort arr in descending order
d = d[::  1 ]
coun = 0
pri = [ 0 ] * n
# Assigning multiples of each element
while (coun ! = n and p % d[coun] = = 0 ):
b = arr.index(d[coun])
pri[b] = ((p / / d[coun])  1 )
p = p  (d[coun] * ((p / / d[coun])  1 ))
coun + = 1
# Check if there is no case
# of getting sum > p
if (coun = = n):
return ( "NO" )
elif (p % d[coun] ! = 0 ):
y = (p / / d[coun]) + 1
k = arr.index(d[coun])
pri[k] = y
s = ""
# Multi set
for j in pri:
s = s + str (j) + " "
return (s)
else :
k = p / / c
b = c * (k + 1 )
m = [ 0 ] * n
q = arr.index(c)
m[q] = b / / c
s = ""
for j in m:
s = s + str (j) + " "
return (s)
# Driver code N, P = 2 , 4
arr = [ 1 , 5 ]
print (Multiset(N, P, arr))

0 1
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 studentfriendly price and become industry ready.
Recommended Posts:
 Reduce array to a single element by repeatedly removing an element from any increasing pair
 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
 Maximum contiguous decreasing sequence obtained by removing any one element
 Find the Minimum length Unsorted Subarray, sorting which makes the complete array sorted
 Largest possible Subset from an Array such that no element is K times any other element in the Subset
 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
 Count of triplets from the given Array such that sum of any two elements is the third element
 Find side of Square which makes minimal area to fit two identical rectangles inside it
 Find the winner of a game of removing any number of stones from the least indexed nonempty pile from given N piles
 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
 multiset::operator= in C++ STL
 multiset::swap() in C++ STL
 multiset::emplace() in C++ STL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.