#2369

Check if There is a Valid Partition For The Array

specialist · 840 · lc medium +31 · verified · 52.2% accepted · 2,068 likes · top 42%

Description

Given a 0-indexed integer array nums, determine whether it can be split into one or more contiguous subarrays such that every subarray satisfies at least one of:

- Exactly 2 equal elements (e.g., [2,2]).

- Exactly 3 equal elements (e.g., [4,4,4]).

- Exactly 3 consecutive integers with step 1 (e.g., [3,4,5], but not [1,3,5]).

Return true if a valid partition exists, otherwise false.

Example 1:

Input: nums = [4,4,4,5,6]
Output: true
Explanation: The array can be partitioned into the subarrays [4,4] and [4,5,6].
This partition is valid, so we return true.

Example 2:

Input: nums = [1,1,1,2]
Output: false
Explanation: There is no valid partition for this array.

Code

1
2
3