Sorted subsequence of size 3 | Practice | GeeksforGeeks

Given an array A of N integers, find any 3 elements in it such that A[i] < A[j] < A[k] and i < j < k. 
Example 1:
Input:
N = 5
A[] = {1,2,1,1,3}
Output: 1
Explanation: a sub-sequence 1 2 3 exist.

Example 2:
Input:
N = 3
A[] = {1,1,3}
Output: 0
Explanation: no such sub-sequence exist.
Your Task:
Your task is to complete the function find3Numbers which finds any 3 elements in it such that A[i] < A[j] < A[k] and i < j < k. You need to return them as a vector/ArrayList/array (depending on the language cpp/Java/Python), if no such element is present then return the empty vector of size 0.
Note: The output will be 1 if the sub-sequence returned by the function is present in array A else 0. If the sub-sequence returned by the function is not in the format as mentioned then the output will be -1.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 105
1 <= A[i] <= 106, for each valid i
Note: The Input/Output format and Examples given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
+1
jalajyadav9911 hours ago
Bug in the problem
The test case
1533 24 92 74 100 72 72 97 54 77 95 52 11 95 29
seems to be working fine in compare mode still it is throwing error when i try to submit it.My Code:
 vector<int> find3Numbers(vector<int> arr, int N) {
       // Your code here
         if(N<3)return {};
       vector<int> left;
       vector<int> right(N,0);
       left.push_back(arr[0]);
       for(int i=1;i<N;i++){
           if(arr[i]<left[i-1]){
               left[i]=arr[i];
           }else{
               left[i]=left[i-1];
           }
       }
       right[N-1]=arr[N-1];
       for(int j=N-2;j>=0;j--){
           if(arr[j]>right[j+1]){
               right[j]=arr[j];
           }else{
               right[j]=right[j+1];
           }
       }
       
       for(int i=1;i<N-1;i++){
           if(left[i-1]<arr[i] and right[i+1]>arr[i]){
               return{left[i-1],arr[i],right[i+1]};
           }
       }
       return {};
   }
-1
vishutyagi74 days ago
C++ easy boi
vector<int> find3Numbers(vector<int> arr, int N)    {        vector<int> res;            if(arr.size() < 3)          return res;            int smaller[N];      int min = 0;            smaller[0] = -1;      for(int i=1 ; i<N ; i++)      {          if(arr[i] <= arr[min])          {              smaller[i] = -1;              min = i;          }          else              smaller[i] = min;      }            //for(int i=0 ; i<N ; i++)          //cout << smaller[i] << " ";      //cout << endl;            int greater[N];      int max = N-1;            greater[N-1] = -1;      for(int i=N-2 ; i>=0 ; i--)      {          if(arr[i] >= arr[max])          {              greater[i] = -1;              max = i;          }          else              greater[i] = max;      }            //for(int i=0 ; i<N ; i++)          //cout << greater[i] << " ";      //cout << endl;            for(int i=1 ; i<N-1 ; i++)      {          if(smaller[i]!=-1 && greater[i]!=-1)          {              res.push_back(arr[smaller[i]]);              res.push_back(arr[i]);              res.push_back(arr[greater[i]]);              break;          }      }            //for(int i=0 ; i<res.size() ; i++)          //cout << res[i] << " ";      //cout << endl;            return res;   }
+1
y4f4qv03hoeyiu092y7cq6kuq4yyt1u7pnc6rrl02 weeks ago
vector<int> find3Numbers(vector<int> a, int n)
   {
        vector<int> ans;
        int min=a[0],minkebad=INT_MAX;
        int minkostorekarnekeliye;
        for(int i=1;i<n;i++)
        {   
            if(a[i]==min)continue;
            else if(a[i]<min)
             {
                 min=a[i];
                 continue;
             }
             //idhar tabhi pahunchenge jab koi element min se bada ho;
             else if(a[i]<minkebad)
             {
                 minkebad=a[i];
                 minkostorekarnekeliye = min;//store min;
             }
             else if(a[i]>minkebad) //a[j] se bhi bada mila;
             {
                 ans.push_back(minkostorekarnekeliye);//sabsechota apna min;
                 ans.push_back(minkebad);//minkebad wala;
                 ans.push_back(a[i]);//sabse bada jo minkebad wale se bhi bada hai;
                 break;//triplet mil gaya to traversal band karte hain;
             }
        }
        return ans;
0
hanumanmanyam8372 weeks ago
class Solution {
    ArrayList<Integer> find3Numbers(ArrayList<Integer> arr, int n) {
        // add code here.
        ArrayList<Integer>res=new ArrayList<>();
        Stack<Integer>st=new Stack<>();
        for(int i=n-1;i>=0;i--)
        {
            while(!st.isEmpty() && arr.get(i)>=st.peek())
            {
                st.pop();
            }
            st.push(arr.get(i));
            if(st.size()==3)
            {
                break;
            }
        }
        if(st.size()!=3)
        {
            return res;
        }
        else
        {
            while(!st.isEmpty())
            {
                res.add(st.pop());
            }
        }
        return res;
    }
}

+1
harendraseervi1234567893 weeks ago
class Solution{
 public:
   vector<int> find3Numbers(vector<int> arr, int n) {
       int mini=INT_MAX,maxi=INT_MIN;
       int mina[n],maxa[n];
       vector<int>ans;
       for(int i =0;i<n;i++){
           if(arr[i]<mini){
               mini=arr[i];
           }
           mina[i]=mini;
       }
       for(int i=n-1;i>=0;i--){
           if(arr[i]>maxi){
               maxi=arr[i];
           }
           maxa[i]=maxi;
       }
       for(int i=0;i<n;i++){
           if(arr[i]>mina[i] && arr[i]<maxa[i]){
               ans.push_back(mina[i]);
               ans.push_back(arr[i]);
               ans.push_back(maxa[i]);
               return ans;
           }
       }
       return ans;
   }
};
+4
dflag441 month ago
seriously ,its easy problem??
 
0
ikshitmadan711 month ago
class Solution {    ArrayList<Integer> find3Numbers(ArrayList<Integer> arr, int n) {// 0(1) space  extra space       int l1=Integer.MAX_VALUE;       int l2=Integer.MAX_VALUE;       int l3=Integer.MAX_VALUE;
if(n<3){   return new ArrayList<>();}       int idx=-1;for(int i=0;i<arr.size();i++){   if(arr.get(i)>l2)   {       l3=Math.min(l3,arr.get(i));       idx=i;       break;   }   else if(arr.get(i)>l1)   {       l2=Math.min(l2,arr.get(i));   }
   else if(arr.get(i)<l1)   {       l1=arr.get(i);   }
}
if(l3==Integer.MAX_VALUE){   return new ArrayList<>();}int i=idx;
while(arr.get(i)>=l3){  i--;}int prevNo=arr.get(i);int FirstNo=0;while (i>=0){
   if(arr.get(i)>prevNo&&arr.get(i)<l3)   {       prevNo=arr.get(i);   }   else if(arr.get(i)<prevNo&&arr.get(i)<l3)   {       FirstNo=arr.get(i);       break;   }
i--;
}
ArrayList<Integer>ls=new ArrayList<>();ls.add(FirstNo);ls.add(prevNo);ls.add(l3);
return ls;
   }}
0
natzdaz702
This comment was deleted.
0
sumit dutta2 months ago
EASY C++ SOLUTIONvector<int> find3Numbers(vector<int> arr, int N) {       // Your code here       vector<int> res;              if(arr.size() < 3)           return res;              int smaller[N];       int min = 0;              smaller[0] = -1;       for(int i=1 ; i<N ; i++)       {           if(arr[i] <= arr[min])           {               smaller[i] = -1;               min = i;           }           else               smaller[i] = min;       }              //for(int i=0 ; i<N ; i++)           //cout << smaller[i] << " ";       //cout << endl;              int greater[N];       int max = N-1;              greater[N-1] = -1;       for(int i=N-2 ; i>=0 ; i--)       {           if(arr[i] >= arr[max])           {               greater[i] = -1;               max = i;           }           else               greater[i] = max;       }              //for(int i=0 ; i<N ; i++)           //cout << greater[i] << " ";       //cout << endl;              for(int i=1 ; i<N-1 ; i++)       {           if(smaller[i]!=-1 && greater[i]!=-1)           {               res.push_back(arr[smaller[i]]);               res.push_back(arr[i]);               res.push_back(arr[greater[i]]);               break;           }       }              //for(int i=0 ; i<res.size() ; i++)           //cout << res[i] << " ";       //cout << endl;              return res;   }
+1
sanketbhagat3 months ago
SIMPLE JAVA SOULTION
class Solution {
    ArrayList<Integer> find3Numbers(ArrayList<Integer> arr, int n) {
        // add code here.
        int next[] = new int[n];
        Stack<Integer> st = new Stack<>();
        for(int i=n-1;i>=0;i--){
            while(!st.isEmpty() && arr.get(st.peek())<=arr.get(i)) st.pop();
            next[i] = st.isEmpty()?n:st.peek();
            st.push(i);
        }
        ArrayList<Integer> list = new ArrayList<>();
        for(int i=0;i<n;i++){
            if(next[i]<n && next[next[i]]<n){
                list.add(arr.get(i));
                list.add(arr.get(next[i]));
                list.add(arr.get(next[next[i]]));
                return list;
            }
        }
        return list;
    }
}
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.