132 Pattern in C++



The 132 pattern indicates that, as the digits in "132" in any given value, the middle element should be greater than the last and first elements. Similarly, the last element must be greater than the first element.

To check 132 patterns in C++, we will use the Array data structure. We have given an array nums[] of size S. Our task is to check whether the array elements at indices i, j, and k are such that i < j < k and nums[j] < nums[k] and nums[i] < nums[k].

If any three elements at positions i < j < k satisfy this condition, the array is considered to contain a 132 pattern. The following diagram gives you an idea of the general structure of the 132 pattern:

132 Pattern

Let's see some input and output scenarios to understand the task better -

Scenario 1

Input: nums = [1, 5, 4, 3, 2], S = 5
Output: True or 1
Explanation: [1, 5, 4] satisfying the 132 pattern.

Scenario 2

Input: nums = [10, 5, 1, 6, 2, 0], S = 6
Output: False or 0
Explanation: No 3 element in an array satisfying the 132 pattern.

Checking 132 Pattern in C++

We can check for the 132 patterns in an array. In the following ways:

  • Using Nested for loops (Naive approach)
  • Using Array and Stack

Using Nested for loops

A loop is used to iterate over an array, and a nested loop refers to a loop inside another loop. To verify for a"132" pattern in an array, use the following algorithm -

  • Step 1: Initialize the array with a given set of integers.
  • Step 2: Use three nested loops to iterate through all combinations of triplets (i, j, k) such that i < j < k.
  • Step 3: For each triplet, check if nums[j] > nums[k] and nums[k] > nums[i]. This condition verifies the presence of a 132 pattern, where:
    • The middle element is the largest, and the last element is in between.
    • The first element is the smallest.
  • Step 4: If the condition is satisfied for any triplet, set a boolean flag isTrue to true.
  • Step 5: If not satisfied, repeat step 3.
  • Step 6: After all iterations, check the value of isTrue. If it is true, print:
    • "Yes! the 123 pattern exists in the array".
    • "No! the 123 pattern does not exist in the array".

Example

The following is a very basic technique to check whether the elements of an array satisfy the 132 pattern. We use nested loops and compare each element based on the given condition:

#include <iostream>
using namespace std;
int main() {
   int nums[] = {1, 5, 4, 3, 2};
   cout << "The given nums array: ";
   int s = sizeof(nums) / sizeof(nums[0]);
   for (int i = 0; i < s; i++) {
      cout << nums[i] << " ";
   }
   bool isTrue = false;
   for (int i = 0; i < s; i++) {
      for (int j = i + 1; j < s; j++) {
         for (int k = j + 1; k < s; k++) {
            if (nums[i] < nums[k] && nums[k] < nums[j]) {
               isTrue = true;
            }
         }
      }
   }
   if (isTrue) {
      cout << "\nYes! the 123 pattern present in array: "<<isTrue;
   } else {
      cout << "\nNo! the 123 pattern is not present in array: "<<isTrue;
   }
   return 0;
}

Following is the output of the above program:

The given nums array: 1 5 4 3 2 
Yes! the 123 pattern present in array: 1

Using Array and Stack

An array is a linear data structure that stores elements of the same type in a contiguous memory location. A stack is a class (or abstract data type) that follows the Last In, First Out (LIFO) principle.

Using a stack along with a pre-computed minimum array, we can use the algorithm given below -

  • Step 1: Check if the array is empty. If yes, return false;
  • Step 2: Create an array minVals[] that stores the smallest value from the beginning up to each index.
  • Step 3: Traverse the array starting from the end and move backward (right to left), and treat each number as a possible largest value in the 132 pattern.
  • Step 4: Use a stack to keep track of possible last values (those that could be between the smallest and the largest).
    • Remove values from the stack that are less than or equal to the current minimum (they can't be the last element).
    • If the top of the stack is less than the current number and greater than the min value, then a 132 pattern is found, and return true.
  • Step 5: If not, return false.

Example

The following is another C++ example for checking the 132 pattern using an array and a Stack class. It keeps track of minimum values and uses stack comparisons to identify if the pattern exists:

#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
   bool find132pattern(int nums[], int n) {
      if (n == 0) return false;
      int minVals[n];
      minVals[0] = nums[0];
      for (int i = 1; i < n; i++) {
         minVals[i] = min(minVals[i - 1], nums[i]);
      }
      stack<int> s;
      for (int i = n - 1; i > 0; i--) {
         int minVal = minVals[i - 1];
         int curr = nums[i];
         while (!s.empty() && s.top() <= minVal) s.pop();
         if (!s.empty() && s.top() < curr) return true;
         s.push(nums[i]);
      }

      return false;
   }
};
int main() {
   int nums[] = {-1, 3, 2, 0};
   int n = sizeof(nums) / sizeof(nums[0]);
   Solution ob;
   cout << ob.find132pattern(nums, n);
   return 0;
}

The above program produces the following output:

1

Note: You can also check the 132 pattern in other data structures, such as Vector, not just in the Array data structure.

Conclusion

The 132 pattern problem determines whether a given array of integers or any other data structure contains a subsequence of three numbers such that i < j > k, nums[i] < nums[k] < nums[j], and nums[k] > nums[i].

Updated on: 2025-08-28T14:10:10+05:30

438 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements