Thursday, 6 April 2017

Geeksforgeeks Solution For " Stock buy and sell "

GeeksforGeeks Solution For Hard Domain .Below You Can Find The Solution Of  School Basic ,Easy ,Medium . Or Hackerrank Solution You Can Also Direct Submit Your Solution to Geeksforgeeks Same Problem .You Need to login then you can submit you answers 

Problem :- Stock buy and sell

Submit Your Solution :- Click Here 

Solution :- 

#include<bits/stdc++.h>
using namespace std;
int main()
 {
int t;
    cin>>t;
    while(t--)
    {
        int n;
   cin>>n;
   int a[n];
   for(int i=0;i<n;i++)
       cin>>a[i];
   int buy=0,sell=0;
   bool key=false;
   for(int i=1;i<n;i++)
   {
       if(a[i]>a[i-1])
           sell++;
       else
       {
           if(buy!=sell)
           {
               cout<<"("<<buy<<" "<<sell<<") ";
               key=true;
           }
           buy=sell=i;
       }
   }
   if(buy!=sell)
   {
       cout<<"("<<buy<<" "<<sell<<") ";
        key=true;
   }
   if(!key)
        cout<<"No Profit";
   cout<<endl;       
    }
return 0;
}

Output:-



Geeksforgeeks Solution For " Stock buy and sell "

No comments:
Write comments

Recommended Posts × +