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 = 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:



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output

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 student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

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.