problemeasyalgorithmsleetcode-2511leetcode 2511leetcode2511

Maximum Enemy Forts That Can Be Captured

EasyUpdated: Aug 2, 2025
Practice on:

Problem

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 themaximum 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 .

Examples

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] <= 1

Solution

Method 1 – Two Pointers

Intuition

We want to find the longest segment of consecutive enemy forts (0s) between two of your forts (1 and -1, or -1 and 1). We can scan the array and, whenever we see a 1 or -1, look for the next occurrence of the opposite fort, counting the zeros in between.

Approach

  1. Initialize ans to 0 and prev to -1.
  2. Iterate through the array:
    • If forts[i] is not 0, check if prev is not -1 and forts[i] and forts[prev] are different (1 and -1 or -1 and 1).
    • If so, count the number of zeros between prev and i and update ans.
    • Set prev = i.
  3. Return ans.

Code

C++
class Solution {
public:
    int captureForts(vector<int>& forts) {
        int ans = 0, prev = -1, n = forts.size();
        for (int i = 0; i < n; ++i) {
            if (forts[i] != 0) {
                if (prev != -1 && forts[i] != forts[prev]) {
                    ans = max(ans, abs(i - prev) - 1);
                }
                prev = i;
            }
        }
        return ans;
    }
};
Go
func captureForts(forts []int) int {
    ans, prev := 0, -1
    for i, v := range forts {
        if v != 0 {
            if prev != -1 && v != forts[prev] {
                if i-prev-1 > ans {
                    ans = i - prev - 1
                }
            }
            prev = i
        }
    }
    return ans
}
Java
class Solution {
    public int captureForts(int[] forts) {
        int ans = 0, prev = -1;
        for (int i = 0; i < forts.length; i++) {
            if (forts[i] != 0) {
                if (prev != -1 && forts[i] != forts[prev]) {
                    ans = Math.max(ans, Math.abs(i - prev) - 1);
                }
                prev = i;
            }
        }
        return ans;
    }
}
Kotlin
class Solution {
    fun captureForts(forts: IntArray): Int {
        var ans = 0
        var prev = -1
        for (i in forts.indices) {
            if (forts[i] != 0) {
                if (prev != -1 && forts[i] != forts[prev]) {
                    ans = maxOf(ans, kotlin.math.abs(i - prev) - 1)
                }
                prev = i
            }
        }
        return ans
    }
}
Python
class Solution:
    def captureForts(self, forts: list[int]) -> int:
        ans = 0
        prev = -1
        for i, v in enumerate(forts):
            if v != 0:
                if prev != -1 and v != forts[prev]:
                    ans = max(ans, abs(i - prev) - 1)
                prev = i
        return ans
Rust
impl Solution {
    pub fn capture_forts(forts: Vec<i32>) -> i32 {
        let mut ans = 0;
        let mut prev = -1;
        for (i, &v) in forts.iter().enumerate() {
            if v != 0 {
                if prev != -1 && v != forts[prev as usize] {
                    ans = ans.max((i as i32 - prev - 1).abs());
                }
                prev = i as i32;
            }
        }
        ans
    }
}
TypeScript
class Solution {
    captureForts(forts: number[]): number {
        let ans = 0, prev = -1;
        for (let i = 0; i < forts.length; i++) {
            if (forts[i] !== 0) {
                if (prev !== -1 && forts[i] !== forts[prev]) {
                    ans = Math.max(ans, Math.abs(i - prev) - 1);
                }
                prev = i;
            }
        }
        return ans;
    }
}

Complexity

  • ⏰ Time complexity: O(n), where n is the length of the array, as we scan the array once.
  • 🧺 Space complexity: O(1), as only a few variables are used.

Comments