raghuvanshraj

456.cpp

Jul 28th, 2021 (edited)
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. /**
  2.  *    author:   vulkan
  3.  *    created:  02.06.2020 05:14:10 PM
  4. **/
  5. #include <bits/stdc++.h>
  6.  
  7. using namespace std;
  8.  
  9. bool find132pattern(vector<int> &arr) {
  10.     int n = arr.size();
  11.     int ak = INT_MIN;
  12.     stack<int> st;
  13.     for (int j = n - 1; j >= 0; --j) {
  14.         if (arr[j] < ak) {
  15.             return true;
  16.         }
  17.  
  18.         while (not st.empty() and arr[j] > arr[st.top()]) {
  19.             ak = max(ak, arr[st.top()]);
  20.             st.pop();
  21.         }
  22.  
  23.         st.push(j);
  24.     }
  25.  
  26.     return false;
  27. }
  28.  
  29. int main(int argc, char const *argv[]) {
  30.     int n;
  31.     cin >> n;
  32.     vector<int> arr(n);
  33.     for (int i = 0; i <= n - 1; ++i) {
  34.         cin >> arr[i];
  35.     }
  36.  
  37.     cout << find132pattern(arr);
  38.  
  39.     return 0;
  40. }
Add Comment
Please, Sign In to add comment