Check if There is a Valid Partition For The Array

You are given a 0-indexed integer array nums. You have to partition the array into one or more contiguous subarrays.

We call a partition of the array valid if each of the obtained subarrays satisfies one of the following conditions:

  • The subarray consists of exactly 2 equal elements.
  • The subarray consists of exactly 3 equal elements.
  • The subarray consists of exactly 3 consecutive increasing elements, that is, the difference between adjacent elements is 1.

Return true if the array has at least one valid partition. Otherwise, return false.

Example 1
Inputnums = [4,4,4,5,6]
Outputtrue
The array can be partitioned into the subarrays [4,4] and [4,5,6], so this partition is valid.
Example 2
Inputnums = [1,1,1,2]
Outputfalse
There is no valid partition for this array.

Constraints

  • 2 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^6

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate