Maximum Consecutive Floors Without Special Floors - Problem

Alice manages a company and has rented some floors of a building as office space. Alice has decided some of these floors should be special floors, used for relaxation only.

You are given two integers bottom and top, which denote that Alice has rented all the floors from bottom to top (inclusive). You are also given the integer array special, where special[i] denotes a special floor that Alice has designated for relaxation.

Return the maximum number of consecutive floors without a special floor.

Input & Output

Example 1 — Basic Case
$ Input: bottom = 2, top = 9, special = [4,6]
Output: 3
💡 Note: The floors are 2,3,4,5,6,7,8,9 where 4 and 6 are special. The gaps are: floors 2-3 (2 consecutive), floors 5 (1 consecutive), floors 7-8-9 (3 consecutive). Maximum is 3.
Example 2 — No Special Floors
$ Input: bottom = 6, top = 8, special = []
Output: 3
💡 Note: No special floors exist, so all floors 6,7,8 are consecutive regular floors. Answer is 3.
Example 3 — All Special
$ Input: bottom = 1, top = 3, special = [1,2,3]
Output: 0
💡 Note: All floors are special, so there are 0 consecutive regular floors.

Constraints

  • 1 ≤ special.length ≤ 105
  • 1 ≤ bottom ≤ special[i] ≤ top ≤ 109
  • All the values of special are unique.

Visualization

Tap to expand
Maximum Consecutive Floors Without Special FloorsInput: bottom = 2, top = 9, special = [4, 6]23456789RegularRegularSpecialRegularSpecialRegularRegularRegularGaps between special floors: [2,3] → 2 floors, [5] → 1 floor, [7,8,9] → 3 floorsOutput: 3 (maximum consecutive regular floors)
Understanding the Visualization
1
Input
Range [2,9] with special floors [4,6]
2
Process
Find gaps between special floors
3
Output
Maximum gap is 3 floors
Key Takeaway
🎯 Key Insight: Sort special floors and find the maximum gap between consecutive ones (including boundaries)
Asked in
Google 15 Amazon 12 Microsoft 8
23.0K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen