Check if Array elements can be maximized upto M by adding all elements from another array

Given a positive integer M and two arrays arr[] and value[] of N and K positive integers respectively, the task is to add every element in value[] to an element in arr[] such that after all the additions are performed, the maximum element in the array is at most M. If it is possible to do so, then print “Yes”. Otherwise, print “No”.

Examples:

Input: arr[] = {5, 9, 3, 8, 7}, value[] = {1, 2, 3, 4}, M = 9
Output: Yes
Explanation:
Add 1 & 3 to arr[0] maximizing it to 9.
Add 2 & 4 to arr[2] maximizes it to 9.
Hence, the final arr becomes {9, 9, 9, 8, 7}.

Input: arr[] = {5, 8, 3, 8, 7}, value[] = {1, 2, 3, 4}, M = 8
Output: No
Explanation:
Adding 1 to arr[4], 3 to arr[0] and 4 to arr[2], the array is modified to {8, 8, 7, 8, 8}.
The current maximum element in arr[] is 8.
Hence, only 2 needs to be added from value[] to any element of arr[].
But, on adding 2 to any element in arr[], the maximum element in the array exceeds 8.

Naive Approach:
The simplest approach is to choose any K elements from the given array arr[] and add the K values in the value[] array with these K values chosen. These K values can be added to the K chosen numbers of the array arr[] in K! ways (in the worst case).
Time Complexity: O( NPK )



Efficient Approach:
Follow the steps below to solve the problem:

  1. Sort the elements in value[] array in decreasing order.
  2. Store all the elements of arr[] in the min priority queue.
  3. Now extract the minimum element(say X) from the priority queue and add the elements from the array value[] to X.
  4. While adding value from the the array value[] to X exceeds M, then insert the element X into priority queue and repeat the above step for the next minimum value in the priority queue.
  5. If all the elements in value[] are added to some elements in arr[] then “Yes”, else print “No”.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to implement the
// above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function which checks if all
// additions are possible
void solve(int ar[], int values[],
           int N, int K, int M)
{
  
    // Sorting values[] in
    // decreasing order
    sort(values, values + K,
         greater<int>());
  
    // Minimum priority queue which
    // contains all the elements
    // of array arr[]
    priority_queue<int, vector<int>,
                   greater<int> >
        pq;
  
    for (int x = 0; x < N; x++) {
        pq.push(ar[x]);
    }
  
    // poss stores whether all the
    // additions are possible
    bool poss = true;
    for (int x = 0; x < K; x++) {
  
        // Minium value in the
        // priority queue
        int mini = pq.top();
        pq.pop();
        int val = mini + values[x];
  
        // If on addition it exceeds
        // M then not possible
        if (val > M) {
            poss = false;
            break;
        }
        pq.push(val);
    }
  
    // If all elements are added
    if (poss) {
        cout << "Yes" << endl;
    }
    else {
        cout << "No" << endl;
    }
}
  
// Driver Code
int main()
{
    int ar[] = { 5, 9, 3, 8, 7 };
    int N = 5;
  
    int values[] = { 1, 2, 3, 4 };
    int K = 4;
  
    int M = 9;
  
    solve(ar, values, N, K, M);
    return 0;
}

chevron_right


Output:

Yes

Time Complexity: O((N+K)*log(N))
Auxiliary Space: O(N)

competitive-programming-img




My Personal Notes arrow_drop_up

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.