Maximum Enemy Forts That Can Be Captured - 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 i-th position.
  • 0 indicates there is an enemy fort at the i-th position.
  • 1 indicates the fort at the i-th 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.

Input & Output

Example 1 — Basic Capture
$ Input: forts = [1,0,0,-1,0,0,0,-1]
Output: 2
💡 Note: Move army from position 0 (fort 1) to position 3 (empty -1), capturing 2 enemy forts at positions 1 and 2. This is the only valid path since moving to position 7 would require passing through position 3 which contains -1 (not an enemy fort).
Example 2 — No Valid Path
$ Input: forts = [0,0,1,0]
Output: 0
💡 Note: No valid path exists because there's no empty position (-1) to move to from the friendly fort at position 2.
Example 3 — Multiple Options
$ Input: forts = [1,0,0,-1,1,0,-1]
Output: 2
💡 Note: Can move from position 0 to position 3 capturing 2 enemies, or from position 4 to position 6 capturing 1 enemy. Maximum is 2.

Constraints

  • 1 ≤ forts.length ≤ 1000
  • -1 ≤ forts[i] ≤ 1

Visualization

Tap to expand
Maximum Enemy Forts That Can Be Captured INPUT forts = [1,0,0,-1,0,0,0,-1] 0 1 2 3 4 5 6 7 1 0 0 -1 0 0 0 -1 1 = Your Fort 0 = Enemy Fort -1 = Empty Goal: Move army from your fort to empty position, capturing enemy forts in between Find max enemies captured ALGORITHM STEPS 1 Scan Array Track positions of 1 and -1 2 Count Enemies Between each 1 and -1 pair 3 Both Directions Check 1-->-1 and -1-->1 4 Track Maximum Update max captured count Best Path Found: 1 0 0 0 0 -1 4 enemy forts captured! Time: O(n) | Space: O(1) FINAL RESULT Optimal Move: index 0 --> index 7 1 START 0 0 -1 0 0 0 -1 END From -1 at index 7, go left to 1 at index 0 Enemies Captured: 0 + 0 + 0 + 0 = 4 forts at indices 4,5,6 (index 1,2 blocked by -1 at 3) Output: 4 Maximum forts captured! Key Insight: Use two-pointer approach: track last seen 1 or -1, count consecutive 0s between valid endpoints. Army can move BOTH directions: from your fort (1) to empty (-1) OR from empty (-1) to your fort (1). Path must contain ONLY enemy forts (0s) - any other value breaks the valid path. TutorialsPoint - Maximum Enemy Forts That Can Be Captured | Optimal Solution
Asked in
Amazon 12 Microsoft 8
12.0K Views
Medium Frequency
~15 min Avg. Time
456 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen