Maximum Profit

PROBLEM :
In stock market , a person buys a stock and sells it on some future date. Given the stock prices of N days in an form of an array A[ ] and a positive integer K, find out the maximum profit a person can make in atmost K transactions. A transaction is equivalent to (buying + selling) of a stock and new transaction can start only when the previous transaction has been completed.

Input
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow.
The first line of each test case contains a positve integer K, denoting the number of transactions.
The second line of each test case contains a positve integer N, denoting the length of the array A[ ].
The third line of each test case contains a N space separated positive integers, denoting the prices of each day in the array A[ ].

Output
Print out the maximum profit earned by the person.
No profit will be equivalent to 0.

Constraints
1 <= T <= 100
0 <   K <= 10
2 <= N <=30
0 <= A[ ] <= 1000

Examples
Input
3
2
6
10 22 5 75 65 80
3
4
20 580 420 900
1
5
100 90 80 50 25

Output
87
1040
0

Explanation:
Output 1: Trader earns 87 as sum of 12 and 75  i.e. Buy at price 10, sell at 22, buy at  5 and sell at 80
Output 2: Trader earns 1040 as sum of 560 and 480 i.e. Buy at price 20, sell at 580, buy at 420 and sell at 900
Output 3: Trader cannot make any profit as selling price is decreasing day by day.Hence, it is not possible to earn anything.

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;

int Maximum_Profit(int [],int ,int ) ;
int max_of(int ,int ) ;
int main()
 {
    int t,k,no,i ;
    int arr[1000] ;
    cin>>t ;
    while(t--)
    {
        cin>>k ;
        cin>>no ;
        for(i=0;i<no;i++)
            cin>>arr[i] ;
           
        cout<<Maximum_Profit(arr,k,no)<<endl ;
    }
return 0;
}

int Maximum_Profit(int arr[],int k,int no)
{
    int mat[k+1][no] ;
    int i,j,m,max ;
   
    for(i=0;i<=k;i++)
        mat[i][0]=0 ;
    for(i=0;i<no;i++)
        mat[0][i]=0 ;
       
    for(i=1;i<=k;i++)
    {
        for(j=1;j<no;j++)
        {
            mat[i][j]=mat[i][j-1] ;
            for(m=0;m<j;m++)
                mat[i][j]=max_of(mat[i][j],arr[j]-arr[m]+mat[i-1][m]) ;
        }
    }
   
    return mat[k][no-1] ;
}

int max_of(int a,int b)
{
    return a>b?a:b ;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )