Related Articles

Related Articles

Maximize the profit by selling at-most M products
  • Difficulty Level : Easy
  • Last Updated : 26 Sep, 2018

Given two lists that contains cost prices CP[] and selling prices SP[] of products respectively. The task is to maximize the profit by selling at-most ‘M’ prodcts.

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:



  1. Store the profit/loss on buying and selling of each product i.e. SP[i]-CP[i] in an array.
  2. Sort that array in descending order.
  3. Add the positive values up to M values as positive values denote profit.
  4. Return Sum.

Below is the implementation of above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of above approach:
#include <bits/stdc++.h>
using namespace std;
  
// Function to find profit
int solve(int N, int M, int cp[], int sp[])
{
    int profit[N];
  
    // Calculating profit for each gadget
    for (int i = 0; i < N; i++)
        profit[i] = sp[i] - cp[i];
  
    // sort the profit array in decending order
    sort(profit, profit + N, greater<int>());
  
    // variable to calculate total profit
    int sum = 0;
  
    // check for best M profits
    for (int i = 0; i < M; i++) {
        if (profit[i] > 0)
            sum += profit[i];
        else
            break;
    }
  
    return sum;
}
  
// Driver Code
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of above approach:
import java.util.*;
import java.lang.*;
import java.io.*;
  
class GFG
{
  
// Function to find profit
static int solve(int N, int M, 
                 int cp[], int sp[])
{
    Integer []profit = new Integer[N];
  
    // Calculating profit for each gadget
    for (int i = 0; i < N; i++)
        profit[i] = sp[i] - cp[i];
  
    // sort the profit array 
    // in decending order
    Arrays.sort(profit, Collections.reverseOrder()); 
  
    // variable to calculate total profit
    int sum = 0;
  
    // check for best M profits
    for (int i = 0; i < M; i++)
    {
        if (profit[i] > 0)
            sum += profit[i];
        else
            break;
    }
  
    return sum;
}
  
// Driver Code
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));
}
}
  
// This code is contributed
// by Subhadeep Gupta

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation 
# of above approach
  
# Function to find profit
def solve(N, M, cp, sp) :
      
    # take empty list
    profit = []
      
    # Calculating profit
    # for each gadget
    for i in range(N) :
        profit.append(sp[i] - cp[i])
  
    # sort the profit array
    # in decending order
    profit.sort(reverse = True)
  
    sum = 0
      
    # check for best M profits
    for i in range(M) :
        if profit[i] > 0 :
            sum += profit[i]
        else :
            break
  
    return sum
  
# Driver Code
if __name__ == "__main__" :
  
    N, M = 5, 3
    CP = [5, 10, 35, 7, 23]
    SP = [11, 10, 0, 9, 19]
      
    # function calling
    print(solve(N, M, CP, SP))
      
# This code is contributed
# by ANKITRAI1 

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of above approach:
using System;
  
class GFG
{
  
// Function to find profit
static int solve(int N, int M, 
                 int[] cp, int[] sp)
{
    int[] profit = new int[N];
  
    // Calculating profit for each gadget
    for (int i = 0; i < N; i++)
        profit[i] = sp[i] - cp[i];
  
    // sort the profit array 
    // in descending order
    Array.Sort(profit);
    Array.Reverse(profit);
  
    // variable to calculate total profit
    int sum = 0;
  
    // check for best M profits
    for (int i = 0; i < M; i++)
    {
        if (profit[i] > 0)
            sum += profit[i];
        else
            break;
    }
  
    return sum;
}
  
// Driver Code
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));
}
}
  
// This code is contributed
// by ChitraNayal

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
// PHP implementation of above approach:
  
// Function to find profit
function solve($N, $M, &$cp, &$sp)
{
    $profit = array_fill(0, $N, NULL);
  
    // Calculating profit for each gadget
    for ($i = 0; $i < $N; $i++)
        $profit[$i] = $sp[$i] - $cp[$i];
  
    // sort the profit array
    // in descending order
    rsort($profit);
  
    // variable to calculate
    // total profit
    $sum = 0;
  
    // check for best M profits
    for ($i = 0; $i < $M; $i++) 
    {
        if ($profit[$i] > 0)
            $sum += $profit[$i];
        else
            break;
    }
  
    return $sum;
}
  
// Driver Code
$N = 5;
$M = 3;
$CP = array( 5, 10, 35, 7, 23 );
$SP = array( 11, 10, 0, 9, 19 );
  
echo solve($N, $M, $CP, $SP);
  
// This code is contributed
// by ChitraNayal
?>

chevron_right


Output:

8

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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :