Ctrl K

Nudge Into a Consecutive Run

Hard
You hold tickets numbered tickets (positive integers; numbers may repeat). First you may raise each number by 1 or leave it alone, deciding separately for every ticket. Then you pick some tickets whose numbers, once sorted, are **consecutive with no gaps and no repeats**: [5, 6, 7] works, while [5, 7] and [5, 5, 6] do not. Return the largest number of tickets you can pick.
Example 1
tickets=[4, 2, 7, 2, 5] → 4

Raise one of the 2s to 3: that gives 2, 3, 4, 5, four numbers in a row.

Example 2
tickets=[1, 5, 9] → 1

Every two numbers are more than 2 apart, so only one can be taken.

Example 3
tickets=[3, 3, 3] → 2

Keep one 3 and raise another to 4; the third 3 cannot help, so the answer is 2.

Constraints
  • 1 ≤ len(tickets) ≤ 10⁵
  • 1 ≤ tickets[i] ≤ 10⁶
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBoxes Inside Boxes HardStrict Climb From Spare Parts HardBest-Paying Set of Gigs HardLongest Downhill Route HardBiggest Number Divisible by Three Hard
loading Python…
Input
tickets=[4, 2, 7, 2, 5]
Expected
4

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