Queens That Can Attack the King - Problem
On a 0-indexed 8 × 8 chessboard, there can be multiple black queens and one white king.
You are given a 2D integer array queens where queens[i] = [xQueen_i, yQueen_i] represents the position of the i-th black queen on the chessboard.
You are also given an integer array king of length 2 where king = [xKing, yKing] represents the position of the white king.
Return the coordinates of the black queens that can directly attack the king. You may return the answer in any order.
A queen can attack the king if they are on the same row, column, or diagonal with no other pieces blocking the path.
Input & Output
Example 1 — Multiple Queens
$
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:
Queen at [0,1] attacks king vertically upward. Queen at [1,0] attacks king horizontally. Queen at [3,3] attacks king diagonally. Other queens are either not aligned or blocked by these closer queens.
Example 2 — Some Blocked
$
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 at [2,2] can attack diagonally. Queen at [3,4] attacks horizontally. Queen at [4,4] attacks diagonally. Queen at [0,0] is blocked by [2,2], and [3,5] is blocked by [3,4].
Example 3 — Edge Position
$
Input:
queens = [[5,6],[7,7],[2,1],[0,7],[1,6],[5,1],[3,7],[0,3],[4,0],[1,2],[6,3],[5,0]], king = [0,4]
›
Output:
[[0,3],[0,7],[1,2]]
💡 Note:
From king at [0,4]: Queen at [0,3] attacks vertically down, [0,7] attacks vertically up, and [1,2] attacks diagonally. Other queens are either not aligned with king or blocked.
Constraints
- 1 ≤ queens.length ≤ 63
- queens[i].length == 2
- 0 ≤ queens[i][j] < 8
- king.length == 2
- 0 ≤ king[0], king[1] < 8
- All the given positions are unique
Visualization
Tap to expand
Understanding the Visualization
1
Input
8×8 chessboard with queens and king positions
2
Process
Find queens that can attack king directly (no blocking)
3
Output
Coordinates of attacking queens
Key Takeaway
🎯 Key Insight: Search from king outward in 8 directions - first queen found in each direction can attack
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code