# Get maximum items when other items of total cost of an item are free

Given a list of prices of ‘N’ items. A person can buy only one item of any price and he can get other items for free such that the total price of the rest of the items doesn’t exceed the price of the bought item. The task is to find the maximum number of items the person can have.

Examples:

Input: n = 5, arr = {5, 3, 1, 5, 6}
Output: 3
The person can buy any item of price 5 or 6 and download items of prices 1 and 3 for free. So, he can get at most 3 items.

Input: n = 2, arr = {7, 7}
Output: 2

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

The person should buy the most expensive item and then start taking the items starting from the least pricing (until the total price is lest than or equal to the bought item) in order to maximize the total number of items.
Thus, we sort the list of prices and choose the last element, then we will iterate from 1st index to n-2 index and check if the total sum is less than or equal to the last element.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of  ` `// the above approach  ` `#include ` `using` `namespace` `std; ` ` `  `// Function to count the  ` `// total number of items  ` `int`  `items(``int` `n, ``int` `a[]){  ` ` `  `    ``// Sort the prices  ` `    ``sort(a,a+n); ` ` `  `    ``// Choose the last element  ` `    ``int` `z = a[n-1];  ` ` `  `    ``// Initial count of item  ` `    ``int` `x = 1; ` ` `  `    ``// Sum to keep track of  ` `    ``// the total price  ` `    ``// of free items  ` `    ``int` `s = 0; ` `    ``for` `(``int` `i=0;i

## Java

 `// Java implementation of  ` `// the above approach ` `import` `java.util.Arrays;  ` `import` `java.io.*; ` ` `  `class` `GFG { ` ` `  `// Function to count the  ` `// total number of items  ` `static` `int` `items(``int` `n, ``int` `a[]){  ` ` `  `    ``// Sort the prices  ` `    ``Arrays.sort(a); ` ` `  `    ``// Choose the last element  ` `    ``int` `z = a[n-``1``];  ` ` `  `    ``// Initial count of item  ` `    ``int` `x = ``1``; ` ` `  `    ``// Sum to keep track of  ` `    ``// the total price  ` `    ``// of free items  ` `    ``int` `s = ``0``; ` `    ``for` `(``int` `i=``0``;i

## Python3

 `# Python3 implementation of  ` `# the above approach ` ` `  `# Function to count the  ` `# total number of items ` `def` `items(n, a): ` ` `  `    ``# Sort the prices ` `    ``a.sort() ` ` `  `    ``# Choose the last element ` `    ``z ``=` `a[n``-``1``] ` ` `  `    ``# Initial count of item ` `    ``x ``=` `1` ` `  `    ``# Sum to keep track of  ` `    ``# the total price  ` `    ``# of free items ` `    ``s ``=` `0` `    ``for` `i ``in` `range``(``0``, n``-``1``): ` ` `  `        ``s ``+``=` `a[i] ` ` `  `        ``# If total is less than  ` `        ``# or equal to z then  ` `        ``# we will add 1 to the answer ` `        ``if` `(s <``=` `z): ` `            ``x``+``=` `1` `        ``else``: ` `            ``break` `    ``return` `x ` ` `  `n ``=` `5` `a ``=` `[``5``, ``3``, ``1``, ``5``, ``6``] ` `print``(items(n, a)) `

## C#

 `// C# implementation of the  ` `// above approach ` `using` `System; ` ` `  `class` `GFG ` `{ ` `// Function to count the  ` `// total number of items  ` `static` `int` `items(``int` `n, ``int` `[]a) ` `{  ` ` `  `    ``// Sort the prices  ` `    ``Array.Sort(a); ` ` `  `    ``// Choose the last element  ` `    ``int` `z = a[n - 1];  ` ` `  `    ``// Initial count of item  ` `    ``int` `x = 1; ` ` `  `    ``// Sum to keep track of  ` `    ``// the total price  ` `    ``// of free items  ` `    ``int` `s = 0; ` `    ``for` `(``int` `i = 0; i < n - 1; i++)  ` `    ``{ ` `        ``s += a[i];  ` ` `  `        ``// If total is less than or equal to z  ` `        ``// then we will add 1 to the answer  ` `        ``if` `(s <= z)  ` `            ``x += 1; ` `        ``else` `            ``break``; ` `    ``} ` `    ``return` `x; ` `} ` ` `  `// Driver code ` `static` `public` `void` `Main () ` `{ ` `    ``int` `n = 5; ` `    ``int` `[]a = {5, 3, 1, 5, 6}; ` `    ``Console.WriteLine(items(n, a));  ` `} ` `} ` ` `  `// This code is contributed  ` `// by akt_mit  `

## PHP

 ` `

Output:

```3
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : andrew1234, jit_t

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.