Stock buy and sell
PROBLEM :
The cost of a stock on each day is given in an array, find the max profit that you can make by buying and selling in those days.
Input:
First line contains number of test cases T. Each test case contain the integer value 'N' denoting days followed by an array of stock prices in N days.
Output:
The maximum profit is displayed as shown below. And if there is no profit then print "No Profit".
Constraints:
1 <=T<= 100
2 <=N<= 100
1 <=arr[i]<= 10000
Example
Input:
2
7
100 180 260 310 40 535 695
10
23 13 25 29 33 19 34 45 65 67
Output:
(0 3) (4 6)
(1 4) (5 9)
Notice: Output format is as follows - (buy_day sell_day) (buy_day sell_day)
For each input, output should be in a single line.
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
void StockBuySell(int [],int ) ;
struct stock
{
int buy ;
int sell ;
} ;
int main()
{
int t ;
cin>>t ;
while(t--)
{
int no ;
cin>>no ;
int arr[no] ;
for(int i=0;i<no;i++)
cin>>arr[i] ;
StockBuySell(arr,no) ;
cout<<endl ;
}
return 0;
}
void StockBuySell(int arr[],int no)
{
if(no==1)
return ;
struct stock curr[no/2+1] ;
int count=0 ;
int i=0 ;
while(i<no-1)
{
while(i<no-1 && arr[i]>=arr[i+1])
i++ ;
if(i==no-1)
break ;
curr[count].buy=i++ ;
while(i<no-1 && arr[i]<=arr[i+1])
i++ ;
curr[count].sell=i++ ;
count++ ;
}
if(!count)
{
cout<<"No Profit" ;
return ;
}
for(i=0;i<count;i++)
cout<<"("<<curr[i].buy<<" "<<curr[i].sell<<")"<<" " ;
}
--------------------------------------------------------------------------------
The cost of a stock on each day is given in an array, find the max profit that you can make by buying and selling in those days.
Input:
First line contains number of test cases T. Each test case contain the integer value 'N' denoting days followed by an array of stock prices in N days.
Output:
The maximum profit is displayed as shown below. And if there is no profit then print "No Profit".
Constraints:
1 <=T<= 100
2 <=N<= 100
1 <=arr[i]<= 10000
Example
Input:
2
7
100 180 260 310 40 535 695
10
23 13 25 29 33 19 34 45 65 67
Output:
(0 3) (4 6)
(1 4) (5 9)
Notice: Output format is as follows - (buy_day sell_day) (buy_day sell_day)
For each input, output should be in a single line.
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
void StockBuySell(int [],int ) ;
struct stock
{
int buy ;
int sell ;
} ;
int main()
{
int t ;
cin>>t ;
while(t--)
{
int no ;
cin>>no ;
int arr[no] ;
for(int i=0;i<no;i++)
cin>>arr[i] ;
StockBuySell(arr,no) ;
cout<<endl ;
}
return 0;
}
void StockBuySell(int arr[],int no)
{
if(no==1)
return ;
struct stock curr[no/2+1] ;
int count=0 ;
int i=0 ;
while(i<no-1)
{
while(i<no-1 && arr[i]>=arr[i+1])
i++ ;
if(i==no-1)
break ;
curr[count].buy=i++ ;
while(i<no-1 && arr[i]<=arr[i+1])
i++ ;
curr[count].sell=i++ ;
count++ ;
}
if(!count)
{
cout<<"No Profit" ;
return ;
}
for(i=0;i<count;i++)
cout<<"("<<curr[i].buy<<" "<<curr[i].sell<<")"<<" " ;
}
--------------------------------------------------------------------------------
Shopping on Www.daybyday.in
ReplyDelete