#2274
Medium Algorithms Maximum consecutive floors without special floors
Array Sorting
52.5% acceptance
Feb 25, 2026
430
41
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.
Solution
Rust
Time O(n log n)
Space O(1)
impl Solution {
pub fn max_consecutive(bottom: i32, top: i32, mut special: Vec<i32>) -> i32 {
special.sort_unstable();
let mut ans = 0;
// Gap before first special
ans = ans.max(special[0] - bottom);
// Gaps between consecutive specials
for i in 1..special.len() {
ans = ans.max(special[i] - special[i-1] - 1);
}
// Gap after last special
ans = ans.max(top - special[special.len()-1]);
ans
}
}