Skip to main content
Back to problems
#456
Medium Algorithms

132 pattern

Array Binary Search Stack Monotonic Stack Ordered Set
34.5% acceptance
Jan 13, 2026
7599
469
Given an array of n integers nums, a 132 pattern is a subsequence of three integers nums[i], nums[j] and nums[k] such that i < j < k and nums[i] < nums[k] < nums[j]. Return true if there is a 132 pattern in nums, otherwise, return false.

Solution

Rust
Time O(n²)
Space O(n)
LeetCode
solution.rs
impl Solution {
  pub fn find132pattern(nums: Vec<i32>) -> bool {
    let mut stack = Vec::new();
    let mut third = i32::MIN;
    
    for &num in nums.iter().rev() {
      if num < third {
        return true;
      }
      while !stack.is_empty() && *stack.last().unwrap() < num {
        third = stack.pop().unwrap();
      }
      stack.push(num);
    }
    
    false
  }
}