Given two lists that contain cost prices CP[] and selling prices SP[] of products respectively. The task is to maximize the profit by selling at-most ‘M’ products.
Examples:
Input: N = 5, M = 3
CP[]= {5, 10, 35, 7, 23}
SP[] = {11, 10, 0, 9, 19}
Output: 8
Profit on 0th product i.e. 11-5 = 6
Profit on 3rd product i.e. 9-7 = 2
Selling any other product will not give profit.
So, total profit = 6+2 = 8.
Input: N = 4, M = 2
CP[] = {17, 9, 8, 20}
SP[] = {10, 9, 8, 27}
Output: 7
Approach:
- Store the profit/loss on buying and selling of each product i.e. SP[i]-CP[i] in an array.
- Sort that array in descending order.
- Add the positive values up to M values as positive values denote profit.
- Return Sum.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int solve( int N, int M, int cp[], int sp[])
{
int profit[N];
for ( int i = 0; i < N; i++)
profit[i] = sp[i] - cp[i];
sort(profit, profit + N, greater< int >());
int sum = 0;
for ( int i = 0; i < M; i++) {
if (profit[i] > 0)
sum += profit[i];
else
break ;
}
return sum;
}
int main()
{
int N = 5, M = 3;
int CP[] = { 5, 10, 35, 7, 23 };
int SP[] = { 11, 10, 0, 9, 19 };
cout << solve(N, M, CP, SP);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
static int solve( int N, int M,
int cp[], int sp[])
{
Integer []profit = new Integer[N];
for ( int i = 0 ; i < N; i++)
profit[i] = sp[i] - cp[i];
Arrays.sort(profit, Collections.reverseOrder());
int sum = 0 ;
for ( int i = 0 ; i < M; i++)
{
if (profit[i] > 0 )
sum += profit[i];
else
break ;
}
return sum;
}
public static void main(String args[])
{
int N = 5 , M = 3 ;
int CP[] = { 5 , 10 , 35 , 7 , 23 };
int SP[] = { 11 , 10 , 0 , 9 , 19 };
System.out.println(solve(N, M, CP, SP));
}
}
|
Python3
def solve(N, M, cp, sp) :
profit = []
for i in range (N) :
profit.append(sp[i] - cp[i])
profit.sort(reverse = True )
sum = 0
for i in range (M) :
if profit[i] > 0 :
sum + = profit[i]
else :
break
return sum
if __name__ = = "__main__" :
N, M = 5 , 3
CP = [ 5 , 10 , 35 , 7 , 23 ]
SP = [ 11 , 10 , 0 , 9 , 19 ]
print (solve(N, M, CP, SP))
|
C#
using System;
class GFG
{
static int solve( int N, int M,
int [] cp, int [] sp)
{
int [] profit = new int [N];
for ( int i = 0; i < N; i++)
profit[i] = sp[i] - cp[i];
Array.Sort(profit);
Array.Reverse(profit);
int sum = 0;
for ( int i = 0; i < M; i++)
{
if (profit[i] > 0)
sum += profit[i];
else
break ;
}
return sum;
}
public static void Main()
{
int N = 5, M = 3;
int [] CP = { 5, 10, 35, 7, 23 };
int [] SP = { 11, 10, 0, 9, 19 };
Console.Write(solve(N, M, CP, SP));
}
}
|
PHP
<?php
function solve( $N , $M , & $cp , & $sp )
{
$profit = array_fill (0, $N , NULL);
for ( $i = 0; $i < $N ; $i ++)
$profit [ $i ] = $sp [ $i ] - $cp [ $i ];
rsort( $profit );
$sum = 0;
for ( $i = 0; $i < $M ; $i ++)
{
if ( $profit [ $i ] > 0)
$sum += $profit [ $i ];
else
break ;
}
return $sum ;
}
$N = 5;
$M = 3;
$CP = array ( 5, 10, 35, 7, 23 );
$SP = array ( 11, 10, 0, 9, 19 );
echo solve( $N , $M , $CP , $SP );
?>
|
Javascript
<script>
function solve(N, M, cp, sp)
{
let profit = new Array(N);
for (let i = 0; i < N; i++)
profit[i] = sp[i] - cp[i];
profit.sort( function (a, b){ return b - a;});
let sum = 0;
for (let i = 0; i < M; i++)
{
if (profit[i] > 0)
sum += profit[i];
else
break ;
}
return sum;
}
let N = 5, M = 3;
let CP = [ 5, 10, 35, 7, 23 ];
let SP = [ 11, 10, 0, 9, 19 ];
document.write(solve(N, M, CP, SP));
</script>
|
Complexity Analysis:
- Time Complexity: O(n*log(n)+m)
- Auxiliary Space: O(n)
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 Sep, 2022
Like Article
Save Article