Queens That Can Attack the King - Problem

Imagine a chess battle where multiple black queens are strategically positioned on an 8ร—8 chessboard, all eyeing a single white king. Your mission? Identify which queens pose an immediate threat!

You're given:

  • queens - A 2D array where queens[i] = [x, y] represents the position of the i-th black queen
  • king - An array [x, y] representing the white king's position

A queen can attack the king if they share the same row, column, or diagonal with no pieces blocking the path. Return the coordinates of all queens that can directly attack the king.

Example: If the king is at [0,1] and queens are at [[0,0], [1,1], [2,2]], then queens at [0,0] (same row) and [1,1] (diagonal) can attack, but [2,2] cannot because [1,1] blocks the diagonal path.

Input & Output

example_1.py โ€” Basic Attack Scenario
$ Input: queens = [[0,1],[1,0],[4,0],[0,4],[3,3],[2,4]], king = [0,0]
โ€บ Output: [[0,1],[1,0],[3,3]]
๐Ÿ’ก Note: The queen at [0,1] can attack horizontally, [1,0] can attack vertically, and [3,3] can attack diagonally. Other queens are either not aligned or blocked by closer queens.
example_2.py โ€” Blocking Scenario
$ Input: queens = [[0,0],[1,1],[2,2],[3,4],[3,5],[4,4],[5,6]], king = [3,3]
โ€บ Output: [[2,2],[3,4],[4,4]]
๐Ÿ’ก Note: Queen [2,2] attacks diagonally, [3,4] horizontally, [4,4] diagonally. The queen at [0,0] is blocked by [1,1] and [2,2].
example_3.py โ€” Edge Case - Corner King
$ Input: queens = [[5,6],[7,7],[2,1],[0,7],[1,6],[5,1],[1,7],[0,6],[7,6]], king = [0,0]
โ€บ Output: [[2,1],[0,7],[7,7]]
๐Ÿ’ก Note: From corner [0,0], king can be attacked by [2,1] diagonally, [0,7] vertically, and [7,7] diagonally. Other queens are not in direct lines of attack.

Visualization

Tap to expand
โ™”โ™›โ™›โ™›โ™›๐ŸŽฏ Attack AnalysisATTACKING QUEENS:โ€ข (1,1) - Northwest diagonal โฌ‰โ€ข (6,4) - East horizontal โžœโ€ข (4,6) - South vertical โฌ‡NON-ATTACKING:โ€ข (7,1) - Not in attack lineALGORITHM INSIGHT:1. Build queen position hash set O(n)2. Scan 8 directions from king3. First queen in each direction attacks4. Queens behind others are blockedโšก Time: O(n) | Space: O(n)
Understanding the Visualization
1
Position Setup
Place king and queens on 8x8 chessboard
2
Direction Vectors
Define 8 attack directions from king position
3
Beam Scanning
Scan each direction until hitting queen or boundary
4
Attack Detection
First queen in each direction can attack the king
Key Takeaway
๐ŸŽฏ Key Insight: Only the closest queen in each of the 8 directions can attack - scanning from the king naturally finds these attackers while ignoring blocked queens.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยฒ)

For each of n queens, we check all other n queens to see if they block the path

n
2n
โš  Quadratic Growth
Space Complexity
O(1)

Only using variables for iteration and result storage

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค queens.length โ‰ค 63
  • queens[i].length == 2
  • 0 โ‰ค queens[i][j] < 8
  • king.length == 2
  • 0 โ‰ค king[0], king[1] < 8
  • All given positions are unique
  • The king and queens are placed on a standard 8ร—8 chessboard
Asked in
Google 35 Amazon 28 Microsoft 22 Meta 18
23.8K Views
Medium Frequency
~18 min Avg. Time
892 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