Stock buy and sell | Practice | GeeksforGeeks

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.
Note: There may be multiple possible solutions. Return any one of them. Any correct solution will result in an output of 1, whereas wrong solutions will result in an output of 0.

Example 1:
Input:
N = 7
A[] = {100,180,260,310,40,535,695}
Output:
1
Explanation:
One possible solution is (0 3) (4 6)
We can buy stock on day 0,
and sell it on 3rd day, which will 
give us maximum profit. Now, we buy 
stock on day 4 and sell it on day 6.

Example 2:
Input:
N = 5
A[] = {4,2,2,2,4}
Output:
1
Explanation:
There are multiple possible solutions.
one of them is (3 4)
We can buy stock on day 3,
and sell it on 4th day, which will 
give us maximum profit.

Your Task:
The task is to complete the function stockBuySell() which takes an array A[] and N as input parameters and finds the days of buying and selling stock. The function must return a 2D list of integers containing all the buy-sell pairs i.e. first value of pair will represent the day on which you buy the stock and second value represent the day on which you sell that stock. If there is No Profit, return an empty list. 
 
Note: Since there can be multiple solutions, the driver code will return 1 if your answer is correct, otherwise, it will return 0. In case there's no profit the driver code will return the string "No Profit" for a correct solution.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)

Constraints:
2 ≤ N ≤ 106
0 ≤ A[i] ≤ 106
0
adityatis211 day ago
Simple CPP solution 
 
class Solution{public:    vector<vector<int> > stockBuySell(vector<int> A, int n){              vector<vector<int>>v;       vector<int> a;       int i = 0 ;             while(i<n-1)      {          a.clear();                    while((i < n -1 ) && A[i] >= A[ i + 1])          i++;                    if(i == n -1 )          break;                    a.push_back(i);          i++;                    while((i < n) && A[i] >= A[i - 1])          i++;                    a.push_back(i - 1);                    v.push_back(a);      }              return v;   }};
0
likhithsasank2 days ago
class Solution{   //Function to find the days of buying and selling stock for max profit.   ArrayList<ArrayList<Integer> > stockBuySell(int A[], int n) {       ArrayList<ArrayList<Integer>> ans=new ArrayList<>();       for(int i=1;i<n;i++){           if(A[i]>A[i-1]){               ans.add(new ArrayList<>(Arrays.asList(i-1,i)));           }       }       return ans;                }}
0
avanshul00012 days ago
Here's my solutionclass Solution{   ArrayList<ArrayList<Integer>> stockBuySell(int A[], int n) {             ArrayList<ArrayList<Integer>> list = new ArrayList<>();             if(n == 0){          return list;       }             int idx = 0;       int min = A[idx];       int max = 0;              for(int i=1; i<n; i++){                      if(A[i] < min){               min = A[i];               idx = i;           }                      if(A[i] > min){               ArrayList<Integer> al = new ArrayList<>();               al.add(idx);               al.add(i);               list.add(al);               min = A[i];               idx = i;           }       }       return list;   }}
+1
ruchikajamwal03192 days ago
//User function template for C++
class Solution{public:   //Function to find the days of buying and selling stock for max profit.   vector<vector<int> > stockBuySell(vector<int> A, int n){       // code here       vector<vector<int>> temp;              for(int i = 0; i < n-1; i++){           if(A[i] < A[i+1]){               temp.push_back({i,i+1});           }       }              return temp;   }};
0
tarun3143 days ago
Either change the question or give more clarity .It's so confusing  
0
pradhanrahul33464 days ago
 ArrayList<ArrayList<Integer> > stockBuySell(int arr[], int n) {       // code here      ArrayList<ArrayList<Integer> > array_list = new ArrayList<ArrayList<Integer>>();              int j=0;       for(int i=1;i<n;i++){                      if(arr[i-1]<arr[i]){               array_list.add(new ArrayList<Integer>());               array_list.get(j).add(0, i-1);               array_list.get(j).add(1, i);               j++;           }       }
       return array_list;   }
0
shaikhamaan37864 days ago
Easiest Solution Ever.
 
C++
class Solution{
public:
    //Function to find the days of buying and selling stock for max profit.
    vector<vector<int> > stockBuySell(vector<int> A, int n){
        vector<vector<int>> temp;
        
        for(int i = 0; i < n-1; i++){
            if(A[i] < A[i+1]){
                temp.push_back({i,i+1});
            }
        }
        
        return temp;
    }
};
 
Python
def stockBuySell(self, A, n):
 temp = []
 for i in range(n-1):
     if A[i] < A[i+1]:
         temp.append([i,i+1])
         
 return temp
+6
bsyed1766 days ago
please make the question clear...@admin
+1
proudtobe7271 week ago
class Solution{
public:
   //Function to find the days of buying and selling stock for max profit.
   vector<vector<int> > stockBuySell(vector<int> A, int n)
   {
       vector<vector<int>> v;
       vector<int> temp;
       int low=0,high=0;
       
       for(int i=0;i<(n-1);i++)
       {
           low=i;
           high=i;
           while(A[i+1]>A[i] && i<(n-1))
           {
               ++high;
               ++i;
           }
           if(low!=high)
               v.push_back({low,high});
       }
       return v;
   }
};
0
abhinayabhi1411 week ago
class Solution{public:   //Function to find the days of buying and selling stock for max profit.   vector<vector<int> > stockBuySell(vector<int> A, int n){       // code here         vector<vector<int>>arr;      vector<int>vec;      for(int i=0; i<n-1; i++)      {        int low=i;        int high=i;        while(A[i+1]>A[i] && i<n-1)        {            high++;            i++;        }        if(low!=high)        {               vec.push_back(low);            vec.push_back(high);            arr.push_back(vec);            vec.pop_back();            vec.pop_back();        }      }      return arr;   }};
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.