Maximum profit such that total stolen value is less than K to get bonus
Last Updated :
19 Apr, 2021
Given an integer K and an array arr[] which denotes the amount that can be stolen, the task is to choose a subset of items such that their total value is less than K to get the bonus amount.
Bonus Amount: The bonus amount will be the maximum value that can be stolen from the set of items for each item stolen.
Bonus Amount = (Max of arr[]) * (No of Items stolen)
Examples:
Input: arr[] = {1, 2, 3, 4, 5}, K = 7
Output: 22
Explanation:
Maximum value that can stolen is – 5.
If the items were stolen are 1, 2, and 4. Then the total sum of stolen value will be less than K.
Therefore, Total Profit
=> Each Item value + Maximum value that can be stolen
=> 1 + 5 + 2 + 5 + 4 + 5 = 22
Input: arr[] = {5, 2, 7, 3}, K = 6
Output: 19
Explanation:
Maximum value that can stolen is – 7
If the items stolen are 2 and 3. Then the total sum of stolen value will be less than K.
Therefore, Total Profit
=> Each Item value + Maximum value that can be stolen
=> 2 + 7 + 3 + 7 = 19
Approach: The idea is to use permutation & combinations to choose the elements such that their total sum is less than K. Therefore, considering every possible will result in the maximum profit that can be possible.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxProfit(vector< int > value,
int N, int K)
{
sort(value.begin(), value.end());
int maxval = value[N - 1];
int maxProfit = 0;
int curr_val;
do {
curr_val = 0;
for ( int i = 0; i < N; i++) {
curr_val += value[i];
if (curr_val <= K) {
maxProfit = max(curr_val +
maxval * (i + 1), maxProfit);
}
}
} while (next_permutation(
value.begin(), value.end()));
return maxProfit;
}
int main()
{
int N = 4, K = 6;
vector< int > values{5, 2, 7, 3};
cout << maxProfit(values, N, K);
}
|
Java
import java.util.*;
class GFG{
static int maxProfit( int []value,
int N, int K)
{
Arrays.sort(value);
int maxval = value[N - 1 ];
int maxProfit = 0 ;
int curr_val;
do {
curr_val = 0 ;
for ( int i = 0 ; i < N; i++) {
curr_val += value[i];
if (curr_val <= K) {
maxProfit = Math.max(curr_val +
maxval * (i + 1 ),
maxProfit);
}
}
} while (next_permutation(value));
return maxProfit;
}
static boolean next_permutation( int [] p) {
for ( int a = p.length - 2 ; a >= 0 ; --a)
if (p[a] < p[a + 1 ])
for ( int b = p.length - 1 ;; --b)
if (p[b] > p[a]) {
int t = p[a];
p[a] = p[b];
p[b] = t;
for (++a, b = p.length - 1 ; a < b; ++a, --b) {
t = p[a];
p[a] = p[b];
p[b] = t;
}
return true ;
}
return false ;
}
public static void main(String[] args)
{
int N = 4 , K = 6 ;
int []values = { 5 , 2 , 7 , 3 };
System.out.print(maxProfit(values, N, K));
}
}
|
Python3
def maxProfit(value, N, K):
value.sort()
maxval = value[N - 1 ]
maxProfit = 0
while True :
curr_val = 0
for i in range (N):
curr_val + = value[i]
if (curr_val < = K):
maxProfit = max (curr_val + maxval *
(i + 1 ), maxProfit)
if not next_permutation(value):
break
return maxProfit
def next_permutation(p):
for a in range ( len (p) - 2 , - 1 , - 1 ):
if p[a] < p[a + 1 ]:
b = len (p) - 1
while True :
if p[b] > p[a]:
t = p[a]
p[a] = p[b]
p[b] = t
a + = 1
b = len (p) - 1
while a < b:
t = p[a]
p[a] = p[b]
p[b] = t
a + = 1
b - = 1
return True
b - = 1
return False
N, K = 4 , 6
values = [ 5 , 2 , 7 , 3 ]
print (maxProfit(values, N, K))
|
C#
using System;
class GFG{
static int maxProfit( int [] value,
int N, int K)
{
Array.Sort(value);
int maxval = value[N - 1];
int maxProfit = 0;
int curr_val;
do
{
curr_val = 0;
for ( int i = 0; i < N; i++)
{
curr_val += value[i];
if (curr_val <= K)
{
maxProfit = Math.Max(curr_val +
maxval * (i + 1),
maxProfit);
}
}
} while (next_permutation(value));
return maxProfit;
}
static bool next_permutation( int [] p)
{
for ( int a = p.Length - 2; a >= 0; --a)
if (p[a] < p[a + 1])
for ( int b = p.Length - 1;; --b)
if (p[b] > p[a])
{
int t = p[a];
p[a] = p[b];
p[b] = t;
for (++a, b = p.Length - 1;
a < b; ++a, --b)
{
t = p[a];
p[a] = p[b];
p[b] = t;
}
return true ;
}
return false ;
}
static void Main()
{
int N = 4, K = 6;
int [] values = { 5, 2, 7, 3 };
Console.WriteLine(maxProfit(values, N, K));
}
}
|
Javascript
<script>
function maxProfit(value, N, K)
{
value.sort();
let maxval = value[N - 1];
let maxProfit = 0;
let curr_val;
do
{
curr_val = 0;
for (let i = 0; i < N; i++)
{
curr_val += value[i];
if (curr_val <= K)
{
maxProfit = Math.max(curr_val +
maxval * (i + 1),
maxProfit);
}
}
} while (next_permutation(value));
return maxProfit;
}
function next_permutation(p)
{
for (let a = p.length - 2; a >= 0; --a)
if (p[a] < p[a + 1])
for (let b = p.length - 1;; --b)
if (p[b] > p[a])
{
let t = p[a];
p[a] = p[b];
p[b] = t;
for (++a, b = p.length - 1;
a < b; ++a, --b)
{
t = p[a];
p[a] = p[b];
p[b] = t;
}
return true ;
}
return false ;
}
let N = 4, K = 6;
let values = [ 5, 2, 7, 3 ];
document.write(maxProfit(values, N, K));
</script>
|
Time Complexity: O(N2)
Auxiliary space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...