The cost of stock on each day is given in an array A[]
of size N
. Find all the days on which you buy and sell the stock so that in between those days your profit is maximum.
Following is the pseudo code.
It shows an error in line 8.
int i=1;
int t=0;
int j=n-1;
x=0;
while(i<=j)
{
int s=0;
if(arr[i]>=arr[x]) //line 8
{
s=arr[i]-arr[x]; //s is to calculate the profit
if(s>t)
{
t=s; //to find the max profit
}
else if(s<t)
{
System.out.println("("+i+","+x+")");
x=arr[i];
continue;
}
i++;
}
else if(arr[i]<arr[x])
{
x=arr[i];
i++;
}
}