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:
- 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 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++
#include <bits/stdc++.h>
using namespace std;
void solve( int ar[], int values[],
int N, int K, int M)
{
sort(values, values + K,
greater< int >());
priority_queue< int , vector< int >,
greater< int > >
pq;
for ( int x = 0; x < N; x++) {
pq.push(ar[x]);
}
bool poss = true ;
for ( int x = 0; x < K; x++) {
int mini = pq.top();
pq.pop();
int val = mini + values[x];
if (val > M) {
poss = false ;
break ;
}
pq.push(val);
}
if (poss) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
}
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;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static void solve(Integer ar[], Integer values[],
int N, int K, int M)
{
Arrays.sort(values, (a, b) -> b - a);
PriorityQueue<Integer> pq = new PriorityQueue<>();
for ( int x = 0 ; x < N; x++)
{
pq.add(ar[x]);
}
boolean poss = true ;
for ( int x = 0 ; x < K; x++)
{
int mini = pq.peek();
pq.poll();
int val = mini + values[x];
if (val > M)
{
poss = false ;
break ;
}
pq.add(val);
}
if (poss)
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
public static void main(String args[])
{
Integer ar[] = { 5 , 9 , 3 , 8 , 7 };
int N = 5 ;
Integer values[] = { 1 , 2 , 3 , 4 };
int K = 4 ;
int M = 9 ;
solve(ar, values, N, K, M);
}
}
|
Python3
from queue import PriorityQueue
def solve(ar, values, N, K, M):
values.sort(reverse = True )
pq = PriorityQueue()
for x in range (N):
pq.put(ar[x]);
poss = True ;
for x in range (K):
mini = pq.get();
val = mini + values[x];
if (val > M):
poss = False ;
break ;
pq.put(val);
if (poss):
print ( "Yes" );
else :
print ( "No" );
if __name__ = = '__main__' :
ar = [ 5 , 9 , 3 , 8 , 7 ]
N = 5 ;
values = [ 1 , 2 , 3 , 4 ]
K = 4 ;
M = 9 ;
solve(ar, values, N, K, M);
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void solve( int [] ar, int [] values,
int N, int K, int M)
{
Array.Sort(values);
Array.Reverse(values);
List< int > pq = new List< int >();
for ( int x = 0; x < N; x++)
{
pq.Add(ar[x]);
}
pq.Sort();
bool poss = true ;
for ( int x = 0; x < K; x++)
{
int mini = pq[0];
pq.RemoveAt(0);
int val = mini + values[x];
if (val > M)
{
poss = false ;
break ;
}
pq.Add(val);
pq.Sort();
}
if (poss)
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
static void 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);
}
}
|
Javascript
<script>
function solve(ar, values, N, K, M)
{
values.sort((a, b) => a - b);
values.reverse();
let pq = new Array();
for (let x = 0; x < N; x++)
{
pq.push(ar[x]);
}
pq.sort((a, b) => a -b);
let poss = true ;
for (let x = 0; x < K; x++)
{
let mini = pq[0];
pq.shift();
let val = mini + values[x];
if (val > M)
{
poss = false ;
break ;
}
pq.push(val);
pq.sort((a, b) => a - b);
}
if (poss)
{
document.write( "Yes" );
}
else
{
document.write( "No" );
}
}
let ar = [ 5, 9, 3, 8, 7 ];
let N = 5;
let values = [ 1, 2, 3, 4 ];
let K = 4;
let M = 9;
solve(ar, values, N, K, M);
</script>
|
Time Complexity: O((N+K)*log(N))
Auxiliary Space: O(N)
Last Updated :
20 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...