Split Array into Consecutive Subsequences

You are given an integer array nums that is sorted in non-decreasing order.

Determine if it is possible to split nums into one or more subsequences such that both of the following conditions are true:

  • Each subsequence is a consecutive increasing sequence (i.e., each integer is exactly one more than the previous integer).
  • All subsequences have a length of 3 or more.

Return true if you can split nums according to the above conditions, or false otherwise.

A subsequence of an array is a new array that is formed from the original array by deleting some (can be none) of the elements without disturbing the relative positions of the remaining elements. That is, [1, 3, 5] is a subsequence of [1, 2, 3, 4, 5] while [1, 3, 2] is not.

Example 1
Inputnums = [1,2,3,3,4,5]
Outputtrue
nums can be split into the subsequences [1, 2, 3] and [3, 4, 5], both consecutive and of length at least 3.
Example 2
Inputnums = [1,2,3,3,4,4,5,5]
Outputtrue
nums can be split into the subsequences [1, 2, 3, 4, 5] and [3, 4, 5], both consecutive and of length at least 3.

Constraints

  • 1 <= nums.length <= 10^4
  • -1000 <= nums[i] <= 1000
  • nums is sorted in non-decreasing order.

Asked at 2 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