This is a C++ Program that Solves Stock Maximize Problem using Dynamic Programming technique.

Your algorithms have become so good at predicting the market that you now know what the share price of Wooden Orange Toothpicks Inc. (WOT) will be for the next N days.

Each day, you can either buy one share of WOT, sell any number of shares of WOT that you own, or not make any transaction at all. What is the maximum profit you can obtain with an optimum trading strategy?

Our approach will be to attain maximum profit either by selling a stock at the highest price possible or by selling more than one stock in the same duration. We will proceed in a bottom up manner to compute our results.

Case-1:

Number of days=3 Value of shares on each day={1, 2, 100} Maximum attainable profit = 197

Here is source code of the C++ Program to Solve Stock Maximize Problem. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

`#include<bits/stdc++.h>`

using namespace std;

int main()

`{`

int i,j,k;

int t,n;

long long int profit;

int mx;

cout<<"Enter the number of days ";

cin>>n;

vector<int> a(n);

cout<<"Enter the value of shares on each day "<<endl;

for(i=0;i<n;i++)

cin>>a[i];

mx=a[n-1];

profit=0;

for(i=n-2;i>=0;i--)

`{`

mx=max(mx,a[i+1]);

if(a[i]<mx)

`{`

profit+=(mx-a[i]);

`}`

`}`

cout<<"Maximum attainable profit "<<endl;

cout<<profit<<endl;

return 0;

`}`

In the main function, we ask the user to input the values of stock on each day. Then, we will procees these arrays to find out the maximum attainable profit and display the result on standard output.

Case-1: $ g++ stock_maximize.cpp $ ./a.out Enter the number of days 3 Enter the value of shares on each day 1 2 100 Maximum attainable profit 197

**Sanfoundry Global Education & Learning Series – Dynamic Programming Problems.**

To practice all Dynamic Programming Problems, __here is complete set of 100+ Problems and Solutions__.