Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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.
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 <= 1051 <= bottom <= special[i] <= top <= 109special are unique.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
2 9 [4,6]
6 8 [7,6,8]
longest-consecutive-sequence)maximum-gap)widest-vertical-area-between-two-points-containing-no-points)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2274: Maximum Consecutive Floors Without Special Floors
class Solution {
public int maxConsecutive(int bottom, int top, int[] special) {
Arrays.sort(special);
int n = special.length;
int ans = Math.max(special[0] - bottom, top - special[n - 1]);
for (int i = 1; i < n; ++i) {
ans = Math.max(ans, special[i] - special[i - 1] - 1);
}
return ans;
}
}
// Accepted solution for LeetCode #2274: Maximum Consecutive Floors Without Special Floors
func maxConsecutive(bottom int, top int, special []int) int {
sort.Ints(special)
ans := max(special[0]-bottom, top-special[len(special)-1])
for i, x := range special[1:] {
ans = max(ans, x-special[i]-1)
}
return ans
}
# Accepted solution for LeetCode #2274: Maximum Consecutive Floors Without Special Floors
class Solution:
def maxConsecutive(self, bottom: int, top: int, special: List[int]) -> int:
special.sort()
ans = max(special[0] - bottom, top - special[-1])
for x, y in pairwise(special):
ans = max(ans, y - x - 1)
return ans
// Accepted solution for LeetCode #2274: Maximum Consecutive Floors Without Special Floors
/**
* [2274] Maximum Consecutive Floors Without Special Floors
*
* 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.
*
* 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^5
* 1 <= bottom <= special[i] <= top <= 10^9
* All the values of special are unique.
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/maximum-consecutive-floors-without-special-floors/
// discuss: https://leetcode.com/problems/maximum-consecutive-floors-without-special-floors/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn max_consecutive(bottom: i32, top: i32, special: Vec<i32>) -> i32 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2274_example_1() {
let bottom = 2;
let top = 9;
let special = vec![4, 6];
let result = 3;
assert_eq!(Solution::max_consecutive(bottom, top, special), result);
}
#[test]
#[ignore]
fn test_2274_example_2() {
let bottom = 6;
let top = 8;
let special = vec![7, 6, 8];
let result = 0;
assert_eq!(Solution::max_consecutive(bottom, top, special), result);
}
}
// Accepted solution for LeetCode #2274: Maximum Consecutive Floors Without Special Floors
function maxConsecutive(bottom: number, top: number, special: number[]): number {
special.sort((a, b) => a - b);
const n = special.length;
let ans = Math.max(special[0] - bottom, top - special[n - 1]);
for (let i = 1; i < n; ++i) {
ans = Math.max(ans, special[i] - special[i - 1] - 1);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.