Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
132 Pattern (medium)
On this page

Problem Statement

Examples

Try it yourself

Problem Statement

Given an array nums, containing N integers.

A 132 pattern consists of three numbers, say x, y, and z, where x < z and z < y. This is often referred to as a '132' pattern because if we represent x, y, and z as 1, 3, and 2, respectively, it mimics the positional pattern in '132'.

Return true if such a pattern exists within any sequence of given numbers nums. Otherwise, return false.

Examples

  1. Example 1:

    • Input: nums = [3, 5, 0, 3, 4]
    • Expected Output: True
    • Justification: Here, 3 < 4 and 4 < 5, forming a '132' pattern with the numbers 3, 5, and 4.
  2. Example 2:

    • Input: nums = [1, 2, 3, 4]
    • Expected Output: False
    • Justification: The sequence is in ascending order, and no '132' pattern is present.
  3. Example 3:

    • Input: nums = [9, 11, 8, 9, 10, 7, 9]
    • Expected Output: True
    • Justification: The pattern is formed with 8 < 9 and 9 < 10 in sequence 8, 10, 9.

Constraints:

  • n == nums.length
  • 1 <= n <= 2 * 10<sup>5</sup>
  • -10<sup>9</sup> <= nums[i] <= 10<sup>9</sup>

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Like the course? Get enrolled and start learning!

On this page

Problem Statement

Examples

Try it yourself