Get maximum items when other items of total cost of an item are free
Last Updated :
07 Sep, 2022
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 less 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++
#include <bits/stdc++.h>
using namespace std;
int items( int n, int a[]){
sort(a,a+n);
int z = a[n-1];
int x = 1;
int s = 0;
for ( int i=0;i<n-1;i++)
{
s += a[i];
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);
}
|
Java
import java.util.Arrays;
import java.io.*;
class GFG {
static int items( int n, int a[]){
Arrays.sort(a);
int z = a[n- 1 ];
int x = 1 ;
int s = 0 ;
for ( int i= 0 ;i<n- 1 ;i++)
{
s += a[i];
if (s <= z)
x+= 1 ;
else
break ;
}
return x;
}
public static void main (String[] args) {
int n = 5 ;
int a[]= { 5 , 3 , 1 , 5 , 6 };
System.out.println(items(n, a));
}
}
|
Python3
def items(n, a):
a.sort()
z = a[n - 1 ]
x = 1
s = 0
for i in range ( 0 , n - 1 ):
s + = a[i]
if (s < = z):
x + = 1
else :
break
return x
n = 5
a = [ 5 , 3 , 1 , 5 , 6 ]
print (items(n, a))
|
C#
using System;
class GFG
{
static int items( int n, int []a)
{
Array.Sort(a);
int z = a[n - 1];
int x = 1;
int s = 0;
for ( int i = 0; i < n - 1; i++)
{
s += a[i];
if (s <= z)
x += 1;
else
break ;
}
return x;
}
static public void Main ()
{
int n = 5;
int []a = {5, 3, 1, 5, 6};
Console.WriteLine(items(n, a));
}
}
|
PHP
<?php
function items( $n , $a ){
sort( $a );
$z = $a [ $n -1];
$x = 1;
$s = 0;
for ( $i =0; $i < $n -1; $i ++)
{
$s += $a [ $i ];
if ( $s <= $z )
$x += 1;
else
break ;
}
return $x ;
}
$n = 5;
$a = array (5, 3, 1, 5, 6);
echo items( $n , $a );
?>
|
Javascript
<script>
function items(n, a)
{
a.sort( function (a, b){ return a - b});
let z = a[n - 1];
let x = 1;
let s = 0;
for (let i = 0; i < n - 1; i++)
{
s += a[i];
if (s <= z)
x += 1;
else
break ;
}
return x;
}
let n = 5;
let a = [5, 3, 1, 5, 6];
document.write(items(n, a));
</script>
|
Complexity Analysis:
- Time Complexity: O(N*logN), as we are using inbuilt sort function.
- Auxiliary Space: O(1), as we are not using any extra space.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...