SHARE
TWEET

Maximum Unsorted SubArray.cpp

a guest Sep 19th, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // https://www.interviewbit.com/problems/maximum-unsorted-subarray/
  2.  
  3. vector<int> Solution::subUnsort(vector<int> &A) {
  4.     int len = A.size();
  5.     stack<pair<int,int> > s;
  6.    
  7.     int st = INT_MAX;
  8.     int lst = -1;
  9.     int mxIdx = 0;
  10.     int mnIdx = INT_MAX;
  11.     for(int i = 1; i < len; i++){
  12.         int j = min(mxIdx, st);
  13.         while(j >= 0 && A[j] > A[i]){
  14.             st = min(st, j);
  15.             j--;
  16.         }
  17.         if(A[mxIdx] <= A[i]){
  18.             mxIdx = i;
  19.         } else {
  20.             lst = i;
  21.         }
  22.            
  23.     }
  24.    
  25.     vector<int> ret;
  26.     if(st != INT_MAX){
  27.         ret.push_back(st);
  28.         ret.push_back(lst);
  29.     }
  30.     else{
  31.         ret.push_back(-1);
  32.     }
  33.     return ret;
  34. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top