Check if There is a Valid Partition For The Array

Medium Topics Company Tags

Partition all of nums into nonempty contiguous groups. Each group must be exactly one of these types:

  • Two equal values.
  • Three equal values.
  • Three consecutive increasing values, with a difference of exactly 1 between neighbors.

Return true if at least one such partition exists, and false otherwise. You may not reorder or omit elements.

Example 1:

Input: nums = [2,2]

Output: true

Explanation: Use the whole array as one group of two equal values.

Example 2:

Input: nums = [1,2,3,4]

Output: false

Explanation: A group of three consecutive values would leave one value ungrouped. Splitting into two groups of size two also fails because their values are unequal.

Example 3:

Input: nums = [7,7,7,2,3,4]

Output: true

Explanation: Split into [7,7,7] and [2,3,4]. The first group has three equal values, and the second has three consecutive increasing values.

Constraints:

  • 2 <= nums.length <= 100000
  • 1 <= nums[i] <= 1000000


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.