# Derive a MultiSet from given Array such that sum is > P and removing any element makes sum < P

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 = 4Output:[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 = 10Output:-1Explanation:

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:

## Python3

`# 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))` |

**Output**

0 1

**Time Complexity:** O(N)**Auxiliary Space:** O(1)