# Maximum profit that can be obtained by buying at most K books

Given an integer **K** and an array **arr[]** consisting of **N **integers, where an array element **arr[i]** represents the price of the **i ^{th}** book. Profit of buying i

^{th}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 = 2Output:49Explanation:

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 = 3Output: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.**

- If
- Finally, print the value of
**maxBenefit**as the maximum profit obtained.

Below is the implementation of the above approach:

## C++

`// C++ program for above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find the maximum` `// profit that can be obtained` `// by buying at most K books` `int` `maxProfit(` `int` `arr[], ` `int` `N, ` `int` `K)` `{` ` ` `// Sort the array in` ` ` `// ascending order` ` ` `sort(arr, arr + N);` ` ` `// Stores the maximum profit` ` ` `int` `maxBenefit = 0;` ` ` `// Traverse the array arr[]` ` ` `for` `(` `int` `i = 0; i < N; i++) {` ` ` `// If arr[i] is less than 0` ` ` `// and K is greater than 0` ` ` `if` `(arr[i] < 0 && K > 0) {` ` ` `// Increment the maxBenefit` ` ` `// by abs(arr[i])` ` ` `maxBenefit += ` `abs` `(arr[i]);` ` ` `// Decrement K by 1` ` ` `K--;` ` ` `}` ` ` `}` ` ` `// Return the profit` ` ` `return` `maxBenefit;` `}` `// Driver Code` `int` `main()` `{` ` ` `// Given Input` ` ` `int` `arr[] = { -10, 20, -30, 50, -19 };` ` ` `int` `K = 2;` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(` `int` `);` ` ` `// Function call` ` ` `cout << maxProfit(arr, N, K);` ` ` `return` `0;` `}` |

## Java

`// Java program for the above approach` `import` `java.io.*;` `import` `java.util.Arrays;` `class` `GFG` `{` ` ` ` ` `// Function to find the maximum` `// profit that can be obtained` `// by buying at most K books` ` ` `public` `static` `int` `maxProfit(` `int` `arr[], ` `int` `N, ` `int` `K)` ` ` `{` ` ` `// Sort the array in` ` ` `// ascending order` ` ` `Arrays.sort(arr);` ` ` `// Stores the maximum profit` ` ` `int` `maxBenefit = ` `0` `;` ` ` `// Traverse the array arr[]` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++) {` ` ` `// If arr[i] is less than 0` ` ` `// and K is greater than 0` ` ` `if` `(arr[i] < ` `0` `&& K > ` `0` `) {` ` ` `// Increment the maxBenefit` ` ` `// by abs(arr[i])` ` ` `maxBenefit += Math.abs(arr[i]);` ` ` `// Decrement K by 1` ` ` `K--;` ` ` `}` ` ` `}` ` ` `// Return the profit` ` ` `return` `maxBenefit;` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` ` ` `// Given input` ` ` `int` `arr[] = { -` `10` `, ` `20` `, -` `30` `, ` `50` `, -` `19` `};` ` ` `int` `K = ` `2` `;` ` ` `int` `N = ` `5` `;` ` ` `// Function call` ` ` `int` `res = maxProfit(arr, N, K);` ` ` `System.out.println(res);` ` ` `}` `}` `// This code is contributed by lokeshpotta20.` |

## Python3

`# Python3 program for above approach` `# Function to find the maximum` `# profit that can be obtained` `# by buying at most K books` `def` `maxProfit(arr, N, K):` ` ` ` ` `# Sort the array in` ` ` `# ascending order` ` ` `arr.sort()` ` ` `# Stores the maximum profit` ` ` `maxBenefit ` `=` `0` ` ` `# Traverse the array arr[]` ` ` `for` `i ` `in` `range` `(` `0` `, N, ` `1` `):` ` ` ` ` `# If arr[i] is less than 0` ` ` `# and K is greater than 0` ` ` `if` `(arr[i] < ` `0` `and` `K > ` `0` `):` ` ` `# Increment the maxBenefit` ` ` `# by abs(arr[i])` ` ` `maxBenefit ` `+` `=` `abs` `(arr[i])` ` ` `# Decrement K by 1` ` ` `K ` `-` `=` `1` ` ` `# Return the profit` ` ` `return` `maxBenefit` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` ` ` `# Given Input` ` ` `arr ` `=` `[ ` `-` `10` `, ` `20` `, ` `-` `30` `, ` `50` `, ` `-` `19` `]` ` ` `K ` `=` `2` ` ` `N ` `=` `len` `(arr)` ` ` `# Function call` ` ` `print` `(maxProfit(arr, N, K))` ` ` `# This code is contributed by SURENDRA_GANGWAR` |

## C#

`// C# program for the above approach` `using` `System;` `class` `GFG {` ` ` `// Function to find the maximum` ` ` `// profit that can be obtained` ` ` `// by buying at most K books` ` ` `public` `static` `int` `maxProfit(` `int` `[] arr, ` `int` `N, ` `int` `K)` ` ` `{` ` ` `// Sort the array in` ` ` `// ascending order` ` ` `Array.Sort(arr);` ` ` `// Stores the maximum profit` ` ` `int` `maxBenefit = 0;` ` ` `// Traverse the array arr[]` ` ` `for` `(` `int` `i = 0; i < N; i++) {` ` ` `// If arr[i] is less than 0` ` ` `// and K is greater than 0` ` ` `if` `(arr[i] < 0 && K > 0) {` ` ` `// Increment the maxBenefit` ` ` `// by abs(arr[i])` ` ` `maxBenefit += Math.Abs(arr[i]);` ` ` `// Decrement K by 1` ` ` `K--;` ` ` `}` ` ` `}` ` ` `// Return the profit` ` ` `return` `maxBenefit;` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `Main()` ` ` `{` ` ` `// Given input` ` ` `int` `[] arr = { -10, 20, -30, 50, -19 };` ` ` `int` `K = 2;` ` ` `int` `N = 5;` ` ` `// Function call` ` ` `int` `res = maxProfit(arr, N, K);` ` ` `Console.Write(res);` ` ` `}` `}` `// This code is contributed by subhammahato348.` |

## Javascript

`<script>` ` ` `// JavaScript program for above approach` ` ` `// Function to find the maximum` ` ` `// profit that can be obtained` ` ` `// by buying at most K books` ` ` `function` `maxProfit(arr, N, K) {` ` ` `// Sort the array in` ` ` `// ascending order` ` ` `arr.sort(` `function` `(a, b) { ` `return` `a - b });` ` ` `// Stores the maximum profit` ` ` `var` `maxBenefit = 0;` ` ` `// Traverse the array arr[]` ` ` `for` `(let i = 0; i < N; i++) {` ` ` `// If arr[i] is less than 0` ` ` `// and K is greater than 0` ` ` `if` `(arr[i] < 0 && K > 0) {` ` ` `// Increment the maxBenefit` ` ` `// by abs(arr[i])` ` ` `maxBenefit += Math.abs(arr[i]);` ` ` `// Decrement K by 1` ` ` `K--;` ` ` `}` ` ` `}` ` ` `// Return the profit` ` ` `return` `maxBenefit;` ` ` `}` ` ` `// Driver Code` ` ` `// Given Input` ` ` `var` `arr = [-10, 20, -30, 50, -19];` ` ` `var` `K = 2;` ` ` `var` `N = 5;` ` ` `// Function call` ` ` `document.write(maxProfit(arr, N, K));` `// This code is contributed by lokeshpotta20.` ` ` `</script>` |

**Output:**

49

**Time Complexity:** O(N*log(N))**Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Participate in the **Scholorship Test for First-Step-to-DSA Course for Class 9 to 12 students**.