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
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 <bits/stdc++.h> 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<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; } int main() { int n = 5; int a[]= {5, 3, 1, 5, 6}; cout<<items(n, a); } //contributed by Arnab Kundu |
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<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 public static void main (String[] args) { int n = 5; int a[]= {5, 3, 1, 5, 6}; System.out.println(items(n, a)); } //This code is contributed by ajit } |
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 = 5a = [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
<?php //PHP implementation of // the above approach // Function to count the // total number of items function items($n, $a){ // Sort the prices sort($a); // 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=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; } //Code driven $n = 5; $a= array(5, 3, 1, 5, 6); echo items($n, $a); //This code is contributed by ajit ?> |
3
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Maximum items that can be bought from the cost Array based on given conditions
- Maximum profit such that total stolen value is less than K to get bonus
- Minimum total cost incurred to reach the last station
- Minimize total cost without repeating same task in two consecutive iterations
- Minimize Cost with Replacement with other allowed
- Minimum cost to empty Array where cost of removing an element is 2^(removed_count) * arr[i]
- Find N'th item in a set formed by sum of two arrays
- Number of ways in which an item returns back to its initial position in N swaps in array of size K
- Minimum, maximum and average price values for all the items of given type
- Maximum profit by selling N items at two markets
- Maximum possible sum of a window in an array such that elements of same window in other array are unique
- Maximum in array which is at-least twice of other elements
- Sum of the distances from every node to all other nodes is maximum
- Find the maximum element in the array other than Ai
- Maximum possible remainder when an element is divided by other element in the array
- Maximum number of envelopes that can be put inside other bigger envelopes
- Pair having all other given pairs lying between its minimum and maximum
- Minimum cost to reach end of array array when a maximum jump of K index is allowed
- Find the maximum cost of an array of pairs choosing at most K pairs
- Cost of creating smallest subsequence with sum of difference between adjacent elements maximum
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.

