Maximum Consecutive Floors Without Special Floors
MediumUpdated: Aug 2, 2025
Practice on:
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 themaximum number of consecutive floors without a special floor.
Examples
Example 1
Input: bottom = 2, top = 9, special = [4,6]
Output: 3
Explanation: The following are the ranges (inclusive) of consecutive floors without a special floor:
- (2, 3) with a total amount of 2 floors.
- (5, 5) with a total amount of 1 floor.
- (7, 9) with a total amount of 3 floors.
Therefore, we return the maximum number which is 3 floors.
Example 2
Input: bottom = 6, top = 8, special = [7,6,8]
Output: 0
Explanation: Every floor rented is a special floor, so we return 0.
Constraints
1 <= special.length <= 10^51 <= bottom <= special[i] <= top <= 10^9- All the values of
specialare unique.
Solution
Method 1 – Sorting and Gap Calculation
Intuition
The main idea is to find the largest gap between consecutive special floors (including the edges: bottom and top). By sorting the special floors, we can easily compute the maximum number of consecutive non-special floors between them.
Approach
- Sort the
specialarray. - Initialize
ansas 0. - Check the gap before the first special floor: from
bottomtospecial[0] - 1. - For each pair of consecutive special floors, check the gap between them: from
special[i-1] + 1tospecial[i] - 1. - Check the gap after the last special floor: from
special[-1] + 1totop. - The answer is the maximum gap found.
Code
C++
class Solution {
public:
int maxConsecutive(int bottom, int top, vector<int>& special) {
sort(special.begin(), special.end());
int ans = 0;
ans = max(ans, special[0] - bottom);
for (int i = 1; i < special.size(); ++i) {
ans = max(ans, special[i] - special[i-1] - 1);
}
ans = max(ans, top - special.back());
return ans;
}
};
Go
func maxConsecutive(bottom int, top int, special []int) int {
sort.Ints(special)
ans := special[0] - bottom
for i := 1; i < len(special); i++ {
if special[i]-special[i-1]-1 > ans {
ans = special[i] - special[i-1] - 1
}
}
if top-special[len(special)-1] > ans {
ans = top - special[len(special)-1]
}
return ans
}
Java
class Solution {
public int maxConsecutive(int bottom, int top, int[] special) {
Arrays.sort(special);
int ans = special[0] - bottom;
for (int i = 1; i < special.length; i++) {
ans = Math.max(ans, special[i] - special[i-1] - 1);
}
ans = Math.max(ans, top - special[special.length-1]);
return ans;
}
}
Kotlin
class Solution {
fun maxConsecutive(bottom: Int, top: Int, special: IntArray): Int {
special.sort()
var ans = special[0] - bottom
for (i in 1 until special.size) {
ans = maxOf(ans, special[i] - special[i-1] - 1)
}
ans = maxOf(ans, top - special.last())
return ans
}
}
Python
class Solution:
def maxConsecutive(self, bottom: int, top: int, special: list[int]) -> int:
special.sort()
ans = special[0] - bottom
for i in range(1, len(special)):
ans = max(ans, special[i] - special[i-1] - 1)
ans = max(ans, top - special[-1])
return ans
Rust
impl Solution {
pub fn max_consecutive(bottom: i32, top: i32, mut special: Vec<i32>) -> i32 {
special.sort_unstable();
let mut ans = special[0] - bottom;
for i in 1..special.len() {
ans = ans.max(special[i] - special[i-1] - 1);
}
ans = ans.max(top - special[special.len()-1]);
ans
}
}
TypeScript
class Solution {
maxConsecutive(bottom: number, top: number, special: number[]): number {
special.sort((a, b) => a - b);
let ans = special[0] - bottom;
for (let i = 1; i < special.length; i++) {
ans = Math.max(ans, special[i] - special[i-1] - 1);
}
ans = Math.max(ans, top - special[special.length-1]);
return ans;
}
}
Complexity
- ⏰ Time complexity:
O(n log n), wherenis the number of special floors, due to sorting. - 🧺 Space complexity:
O(1)(ignoring sort space), as only a few variables are used.