Maximize sum of chosen Array elements with value at most M
Given an array arr[] of N positive numbers and an integer M. The task is to maximize the value of M by adding array elements when arr[i] ≤ M.
Note: Any array element can be added at most once.
Examples:
Input: arr[] = {3, 9, 19, 5, 21}, M = 10
Output: 67
Explanation: One way to getthe value is
M > 3; 3 is added to M and it becomes 10+3 = 13
M > 9; 9 is added to M and it becomes 13+9 = 22
M > 19; 19 is added to M and it becomes 22+19 = 41
M > 5; 5 is added to M and it becomes 41+5 = 46
M > 21; 21 is added to M and it becomes 46+21 = 67
Thus, M = 67 at the end.
Input: arr[] = {2, 13, 4, 19}, M = 6
Output: 12
Explanation: One way to get the value is
M > 4; 4 is added to M and it becomes 6+4 = 10
M > 2; 2 is added to M and it becomes 10+2 = 12
No other value in the array is smaller or equal to M.
Thus, M is 12 at the end.
Approach: The solution is based on the concept of sorting. Follow the steps mentioned below:
- First, sort the array in increasing order.
- For every index i, from 0 to N-1, do the following:
- If M ≥ arr[i], add arr[i] with M.
- If M< arr[i], stop iteration.
- Return the final value of M as the answer.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int IsArrayHungry( int M, vector< int >& arr)
{
sort(arr.begin(), arr.end());
long long sum = M;
int N = arr.size();
for ( int i = 0; i < N; i++) {
if (sum >= arr[i])
sum += arr[i];
else
break ;
}
return sum;
}
int main()
{
vector< int > arr{ 3, 9, 19, 5, 21 };
int M = 10;
int res = IsArrayHungry(M, arr);
cout << res;
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG
{
static int IsArrayHungry( int M, int arr[ ])
{
Arrays.sort(arr);
int sum = M;
int N = arr.length;
for ( int i = 0 ; i < N; i++) {
if (sum >= arr[i])
sum += arr[i];
else
break ;
}
return sum;
}
public static void main (String[] args)
{
int arr[ ] = { 3 , 9 , 19 , 5 , 21 };
int M = 10 ;
int res = IsArrayHungry(M, arr);
System.out.print(res);
}
}
|
Python3
def IsArrayHungry(M, arr):
arr.sort()
sum = M
N = len (arr)
for i in range (N):
if ( sum > = arr[i]):
sum + = arr[i]
else :
break
return sum
if __name__ = = "__main__" :
arr = [ 3 , 9 , 19 , 5 , 21 ]
M = 10
res = IsArrayHungry(M, arr)
print (res)
|
C#
using System;
class GFG
{
static int IsArrayHungry( int M, int []arr)
{
Array.Sort(arr);
int sum = M;
int N = arr.Length;
for ( int i = 0; i < N; i++) {
if (sum >= arr[i])
sum += arr[i];
else
break ;
}
return sum;
}
public static void Main()
{
int []arr = { 3, 9, 19, 5, 21 };
int M = 10;
int res = IsArrayHungry(M, arr);
Console.WriteLine(res);
}
}
|
Javascript
<script>
function IsArrayHungry(M, arr)
{
arr.sort( function (a, b) { return a - b })
let sum = M;
let N = arr.length;
for (let i = 0; i < N; i++) {
if (sum >= arr[i])
sum += arr[i];
else
break ;
}
return sum;
}
let arr = [3, 9, 19, 5, 21];
let M = 10;
let res = IsArrayHungry(M, arr);
document.write(res);
</script>
|
Time Complexity: O(N * logN)
Auxiliary Space: O(1), since no extra space has been added.
Last Updated :
13 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...