Ctrl K

Twins, Triples and Steps

Medium
You are given an integer array values. Cut it into one or more contiguous pieces so that every piece is "good". A piece is good if it is: - exactly two equal numbers, such as [6, 6]; - exactly three equal numbers, such as [1, 1, 1]; - exactly three numbers where each is 1 more than the previous one, such as [8, 9, 10] (but not [8, 10, 12] or [10, 9, 8]). Return true if such a cut exists, and false otherwise.
Example 1
values=[7, 7, 3, 4, 5] → True

Cut into [7, 7] and [3, 4, 5]: a pair of equal numbers and three consecutive ones.

Example 2
values=[2, 2, 2, 2, 2] → True

[2, 2] and [2, 2, 2] works.

Example 3
values=[5, 6, 7, 9] → False

The final 9 cannot belong to any good piece: no pair or triple around it fits.

Constraints
  • 2 ≤ len(values) ≤ 10⁵
  • 1 ≤ values[i] ≤ 10⁶
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsFewest Jumps to the End MediumMost Profitable Streak MediumReach the Last Cell MediumWarehouse Cart Routes MediumCheapest Corner-to-Corner Route Medium
loading Python…
Input
values=[7, 7, 3, 4, 5]
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 137 tests on the server, hidden ones included.