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.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given a 0-indexed integer array forts of length n representing the positions of several forts. forts[i] can be -1, 0, or 1 where:
-1 represents there is no fort at the ith position.0 indicates there is an enemy fort at the ith position.1 indicates the fort at the ith the position is under your command.Now you have decided to move your army from one of your forts at position i to an empty position j such that:
0 <= i, j <= n - 1k where min(i,j) < k < max(i,j), forts[k] == 0.While moving the army, all the enemy forts that come in the way are captured.
Return the maximum number of enemy forts that can be captured. In case it is impossible to move your army, or you do not have any fort under your command, return 0.
Example 1:
Input: forts = [1,0,0,-1,0,0,0,0,1] Output: 4 Explanation: - Moving the army from position 0 to position 3 captures 2 enemy forts, at 1 and 2. - Moving the army from position 8 to position 3 captures 4 enemy forts. Since 4 is the maximum number of enemy forts that can be captured, we return 4.
Example 2:
Input: forts = [0,0,1,-1] Output: 0 Explanation: Since no enemy fort can be captured, 0 is returned.
Constraints:
1 <= forts.length <= 1000-1 <= forts[i] <= 1Problem summary: You are given a 0-indexed integer array forts of length n representing the positions of several forts. forts[i] can be -1, 0, or 1 where: -1 represents there is no fort at the ith position. 0 indicates there is an enemy fort at the ith position. 1 indicates the fort at the ith the position is under your command. Now you have decided to move your army from one of your forts at position i to an empty position j such that: 0 <= i, j <= n - 1 The army travels over enemy forts only. Formally, for all k where min(i,j) < k < max(i,j), forts[k] == 0. While moving the army, all the enemy forts that come in the way are captured. Return the maximum number of enemy forts that can be captured. In case it is impossible to move your army, or you do not have any fort under your command, return 0.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Two Pointers
[1,0,0,-1,0,0,0,0,1]
[0,0,1,-1]
max-consecutive-ones)max-consecutive-ones-iii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2511: Maximum Enemy Forts That Can Be Captured
class Solution {
public int captureForts(int[] forts) {
int n = forts.length;
int ans = 0, i = 0;
while (i < n) {
int j = i + 1;
if (forts[i] != 0) {
while (j < n && forts[j] == 0) {
++j;
}
if (j < n && forts[i] + forts[j] == 0) {
ans = Math.max(ans, j - i - 1);
}
}
i = j;
}
return ans;
}
}
// Accepted solution for LeetCode #2511: Maximum Enemy Forts That Can Be Captured
func captureForts(forts []int) (ans int) {
n := len(forts)
i := 0
for i < n {
j := i + 1
if forts[i] != 0 {
for j < n && forts[j] == 0 {
j++
}
if j < n && forts[i]+forts[j] == 0 {
ans = max(ans, j-i-1)
}
}
i = j
}
return
}
# Accepted solution for LeetCode #2511: Maximum Enemy Forts That Can Be Captured
class Solution:
def captureForts(self, forts: List[int]) -> int:
n = len(forts)
i = ans = 0
while i < n:
j = i + 1
if forts[i]:
while j < n and forts[j] == 0:
j += 1
if j < n and forts[i] + forts[j] == 0:
ans = max(ans, j - i - 1)
i = j
return ans
// Accepted solution for LeetCode #2511: Maximum Enemy Forts That Can Be Captured
impl Solution {
pub fn capture_forts(forts: Vec<i32>) -> i32 {
let n = forts.len();
let mut ans = 0;
let mut i = 0;
while i < n {
let mut j = i + 1;
if forts[i] != 0 {
while j < n && forts[j] == 0 {
j += 1;
}
if j < n && forts[i] + forts[j] == 0 {
ans = ans.max(j - i - 1);
}
}
i = j;
}
ans as i32
}
}
// Accepted solution for LeetCode #2511: Maximum Enemy Forts That Can Be Captured
function captureForts(forts: number[]): number {
const n = forts.length;
let ans = 0;
let i = 0;
while (i < n) {
let j = i + 1;
if (forts[i] !== 0) {
while (j < n && forts[j] === 0) {
j++;
}
if (j < n && forts[i] + forts[j] === 0) {
ans = Math.max(ans, j - i - 1);
}
}
i = j;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
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.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.