Given an integer K and an array arr[] consisting of N integers, where an array element arr[i] represents the price of the ith book. Profit of buying ith book represents max(0, -1 * arr[i]), the task is to find the maximum profit possible by buying at most K books.
Examples:
Input: arr[] = {-10, 20, -30, 50, -19}, K = 2
Output: 49
Explanation:
Maximum profit can be obtained by buying the books arr[2](= -30). Profit = 30 and the book, arr[4](= -19) for the profit of 19.
Therefore, the total maximum profit obtained is, (30+19 = 49).
Input: arr[] = {10, 20, 16, 25}, K = 3
Output: 0
Approach: The problem can be solved using the greedy approach based on the observation that only books with negative prices contribute to the maximum profit. Follow the steps below to solve this problem:
- Sort the array arr[] in ascending order.
- Initialize a variable, say, maxBenefit as 0 to store the maximum profit.
- Iterate in the range [0, N-1] using the variable i and perform the following steps:
- If K is greater than 0 and arr[i] is negative, then add the abs(arr[i]) to maxBenefit and then decrement the value of K by 1.
- Finally, print the value of maxBenefit as the maximum profit obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxProfit( int arr[], int N, int K)
{
sort(arr, arr + N);
int maxBenefit = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] < 0 && K > 0) {
maxBenefit += abs (arr[i]);
K--;
}
}
return maxBenefit;
}
int main()
{
int arr[] = { -10, 20, -30, 50, -19 };
int K = 2;
int N = sizeof (arr) / sizeof ( int );
cout << maxProfit(arr, N, K);
return 0;
}
|
Java
import java.io.*;
import java.util.Arrays;
class GFG
{
public static int maxProfit( int arr[], int N, int K)
{
Arrays.sort(arr);
int maxBenefit = 0 ;
for ( int i = 0 ; i < N; i++) {
if (arr[i] < 0 && K > 0 ) {
maxBenefit += Math.abs(arr[i]);
K--;
}
}
return maxBenefit;
}
public static void main(String[] args)
{
int arr[] = { - 10 , 20 , - 30 , 50 , - 19 };
int K = 2 ;
int N = 5 ;
int res = maxProfit(arr, N, K);
System.out.println(res);
}
}
|
Python3
def maxProfit(arr, N, K):
arr.sort()
maxBenefit = 0
for i in range ( 0 , N, 1 ):
if (arr[i] < 0 and K > 0 ):
maxBenefit + = abs (arr[i])
K - = 1
return maxBenefit
if __name__ = = '__main__' :
arr = [ - 10 , 20 , - 30 , 50 , - 19 ]
K = 2
N = len (arr)
print (maxProfit(arr, N, K))
|
C#
using System;
class GFG {
public static int maxProfit( int [] arr, int N, int K)
{
Array.Sort(arr);
int maxBenefit = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] < 0 && K > 0) {
maxBenefit += Math.Abs(arr[i]);
K--;
}
}
return maxBenefit;
}
public static void Main()
{
int [] arr = { -10, 20, -30, 50, -19 };
int K = 2;
int N = 5;
int res = maxProfit(arr, N, K);
Console.Write(res);
}
}
|
Javascript
<script>
function maxProfit(arr, N, K) {
arr.sort( function (a, b) { return a - b });
var maxBenefit = 0;
for (let i = 0; i < N; i++) {
if (arr[i] < 0 && K > 0) {
maxBenefit += Math.abs(arr[i]);
K--;
}
}
return maxBenefit;
}
var arr = [-10, 20, -30, 50, -19];
var K = 2;
var N = 5;
document.write(maxProfit(arr, N, K));
</script>
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
01 Jul, 2021
Like Article
Save Article