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( ^{N}P_{K} )

**Efficient Approach:**

Follow the steps below to solve the problem:

- Sort the elements in
**value[]**array in decreasing order. - Store all the elements of
**arr[]**in the min priority queue. - Now extract the minimum element(say
**X**) from the priority queue and add the elements from the array**value[]**to**X**. - 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. - 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++

`// 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*

*filter_none*

**Output:**

Yes

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

**Auxiliary Space:** *O(N)*

## Recommended Posts:

- Generate an Array such with elements maximized through swapping bits
- Count distinct elements after adding each element of First Array with Second Array
- Maximize the median of the given array after adding K elements to the same array
- Maximize array elements upto given number
- Adding elements of an array until every element becomes greater than or equal to k
- Make all elements of an Array equal by adding or subtracting at most K
- Find the winner by adding Pairwise difference of elements in the array until Possible
- Check if there exist two elements in an array whose sum is equal to the sum of rest of the array
- Count of Array elements greater than or equal to twice the Median of K trailing Array elements
- Check if each element of an Array is the Sum of any two elements of another Array
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Count of Array elements greater than all elements on its left and next K elements on its right
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Check if sum of exactly K elements of the Array can be odd or not
- Check if an array contains all elements of a given range
- Check if Sum and XOR of all elements of array is equal
- Check if elements of array can be arranged in AP, GP or HP
- Check if all elements of the array are palindrome or not
- Check if all array elements are distinct
- Check all the elements in an array are even using library in C++

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.