 Open in App
Not now

# Find all the queens attacking the king in a chessboard

• Difficulty Level : Medium
• Last Updated : 13 Nov, 2020

Given a 2D array queens[][] consisting of coordinates of N queens in an 8 * 8 chessboard and an array king[] denoting the coordinates of the king, the task is to find the queens that are attacking the king

Examples:

Input: queens[][] = {{0, 1}, {1, 0}, {4, 0}, {0, 4}, {3, 3}, {2, 4}}, king[] = {2, 3}
Output: {{0, 1}, {2, 4}, {3, 3}}

Explanation:The queens at coordinates {0, 1} and {3, 3} are diagonally attacking the king and the queen at {2, 4} is vertically below the king.

Input: queens[][]] = {{4, 1}, {1, 0}, {4, 0}}, king[] = {0, 0}
Output : {{1, 0}}

Approach Follow the steps below to solve the problem:

• Iterate over the array queens[][].
• For every coordinate traversed, check for all possibilities of attacking the king, i.e. horizontally, vertically and diagonally. If found to be attacking the king, check for the following:
• If no other queen is attacking the king from that direction, including the current king as an attacker.
• If an attacker is already present in that direction, check if the current queen is the closest attacker or not. If found to be true, including the cent queen as an attacker. Otherwise, proceed to the next coordinates.
• Finally, print all the coordinates.

Below is the implementation of the above approach:

## C++

 `// C++ Program to implement``// the above approach` `#include ``using` `namespace` `std;` `// Function to find the queen``// closest to king in an``// attacking position``int` `dis(vector<``int``> ans,``        ``vector<``int``> attacker)``{``    ``return` `abs``(ans - attacker)``           ``+ ``abs``(ans - attacker);``}` `// Function to find all the queens``// attacking the king in the chessboard``vector > findQueens(``    ``vector >& queens,``    ``vector<``int``>& king)``{``    ``vector > sol;``    ``vector > attackers(8);` `    ``// Iterating over the coordinates``    ``// of the queens``    ``for` `(``int` `i = 0; i < queens.size(); i++) {` `        ``// If king is horizontally on``        ``// the right of current queen``        ``if` `(king == queens[i]``            ``&& king > queens[i]) {` `            ``// If no attacker is present``            ``// in that direction``            ``if` `((attackers.size() == 0)` `                ``// Or if the current queen is``                ``// closest in that direction``                ``|| (dis(attackers, king)``                    ``> dis(queens[i], king)))` `                ``// Set current queen as``                ``// the attacker``                ``attackers = queens[i];``        ``}` `        ``// If king is horizontally on``        ``// the left of current queen``        ``if` `(king == queens[i]``            ``&& king < queens[i]) {` `            ``// If no attacker is present``            ``// in that direction``            ``if` `((attackers.size() == 0)` `                ``// Or if the current queen is``                ``// closest in that direction``                ``|| (dis(attackers, king)``                    ``> dis(queens[i], king)))` `                ``// Set current queen as``                ``// the attacker``                ``attackers = queens[i];``        ``}` `        ``// If the king is attacked by a``        ``// queen from the left by a queen``        ``// diagonal above``        ``if` `(king - queens[i]``                ``== king - queens[i]``            ``&& king > queens[i]) {` `            ``// If no attacker is present in``            ``// that direction``            ``if` `((attackers.size() == 0)` `                ``// Or the current queen is``                ``// the closest attacker in``                ``// that direction``                ``|| (dis(attackers, king)``                    ``> dis(queens[i], king)))` `                ``// Set current queen as``                ``// the attacker``                ``attackers = queens[i];``        ``}` `        ``// If the king is attacked by a``        ``// queen from the left by a queen``        ``// diagonally below``        ``if` `(king - queens[i]``                ``== king - queens[i]``            ``&& king < queens[i]) {` `            ``// If no attacker is present in``            ``// that direction``            ``if` `((attackers.size() == 0)` `                ``// Or the current queen is``                ``// the closest attacker in``                ``// that direction``                ``|| (dis(attackers, king)``                    ``> dis(queens[i], king)))` `                ``// Set current queen as``                ``// the attacker``                ``attackers = queens[i];``        ``}` `        ``// If the king is attacked by a``        ``// queen from the right by a queen``        ``// diagonally above``        ``if` `(king - queens[i] == 0``            ``&& king > queens[i]) {` `            ``// If no attacker is present in``            ``// that direction``            ``if` `((attackers.size() == 0)` `                ``// Or the current queen is``                ``// the closest attacker in``                ``// that direction``                ``|| (dis(attackers, king)``                    ``> dis(queens[i], king)))` `                ``// Set current queen as``                ``// the attacker``                ``attackers = queens[i];``        ``}` `        ``// If the king is attacked by a``        ``// queen from the right by a queen``        ``// diagonally below``        ``if` `(king - queens[i] == 0``            ``&& king < queens[i]) {` `            ``// If no attacker is present in``            ``// that direction``            ``if` `((attackers.size() == 0)` `                ``// Or the current queen is``                ``// the closest attacker in``                ``// that direction``                ``|| (dis(attackers, king)``                    ``> dis(queens[i], king)))` `                ``// Set current queen as``                ``// the attacker``                ``attackers = queens[i];``        ``}` `        ``// If a king is vertically below``        ``// the current queen``        ``if` `(king - queens[i]``                ``== -(king - queens[i])``            ``&& king > queens[i]) {` `            ``// If no attacker is present in``            ``// that direction``            ``if` `((attackers.size() == 0)` `                ``// Or the current queen is``                ``// the closest attacker in``                ``// that direction``                ``|| (dis(attackers, king)``                    ``> dis(queens[i], king)))` `                ``// Set current queen as``                ``// the attacker``                ``attackers = queens[i];``        ``}` `        ``// If a king is vertically above``        ``// the current queen``        ``if` `(king - queens[i]``                ``== -(king - queens[i])``            ``&& king < queens[i]) {` `            ``// If no attacker is present in``            ``// that direction``            ``if` `((attackers.size() == 0)` `                ``// Or the current queen is``                ``// the closest attacker in``                ``// that direction``                ``|| (dis(attackers, king)``                    ``> dis(queens[i], king)))` `                ``// Set current queen as``                ``// the attacker``                ``attackers = queens[i];``        ``}``    ``}` `    ``for` `(``int` `i = 0; i < 8; i++)``        ``if` `(attackers[i].size())``            ``sol.push_back(attackers[i]);` `    ``// Return the coordinates``    ``return` `sol;``}` `// Print all the coordinates of the``// queens attacking the king``void` `print(vector > ans)``{``    ``for` `(``int` `i = 0; i < ans.size();``         ``i++) {` `        ``for` `(``int` `j = 0; j < 2; j++)``            ``cout << ans[i][j] << ``" "``;` `        ``cout << ``"\n"``;``    ``}``}` `// Driver Code``int` `main()``{``    ``vector<``int``> king = { 2, 3 };` `    ``vector > queens``        ``= { { 0, 1 }, { 1, 0 },``            ``{ 4, 0 }, { 0, 4 },``            ``{ 3, 3 }, { 2, 4 } };` `    ``vector > ans``        ``= findQueens(queens, king);` `    ``print(ans);``}`

## Java

 `// Java program to implement``// the above approach``import` `java.io.*;``import` `java.util.*;``import` `java.util.stream.Collectors;` `class` `GFG{` `// Method to find the queen closest``// to king in an attacking position``private` `static` `int` `dis(``int``[] ans, ``int``[] attacker)``{``    ``return` `Math.abs(ans[``0``] - attacker[``0``]) +``           ``Math.abs(ans[``1``] - attacker[``1``]);``}` `// Method to find all the queens``// attacking the king in the chessboard``private` `static` `List> findQueens(``    ``int``[][] queens, ``int``[] king)``{``    ``List> sol = ``new` `ArrayList>();``    ``int``[][] attackers = ``new` `int``[``8``][``2``];` `    ``for``(``int` `i = ``0``; i < ``8``; i++)``    ``{``        ``Arrays.fill(attackers[i], -``1``);``    ``}` `    ``for``(``int` `i = ``0``; i < queens.length; i++)``    ``{``        ` `        ``// If king is horizontally on``        ``// the right of current queen``        ``if` `(king[``0``] == queens[i][``0``] &&``            ``king[``1``] > queens[i][``1``])``        ``{``            ` `            ``// If no attacker is present``            ``// in that direction``            ``if` `((attackers[``3``][``0``] == -``1``) ||` `                ``// Or if the current queen is``                ``// closest in that direction``                ``(dis(attackers[``3``], king) >``                    ``dis(queens[i], king)))` `                ``// Set current queen as``                ``// the attacker``                ``attackers[``3``] = queens[i];``        ``}` `        ``// If king is horizontally on``        ``// the left of current queen``        ``if` `(king[``0``] == queens[i][``0``] &&``            ``king[``1``] < queens[i][``1``])``        ``{``            ` `            ``// If no attacker is present``            ``// in that direction``            ``if` `((attackers[``4``][``0``] == -``1``) ||` `                ``// Or if the current queen is``                ``// closest in that direction``                ``(dis(attackers[``4``], king) >``                    ``dis(queens[i], king)))` `                ``// Set current queen as``                ``// the attacker``                ``attackers[``4``] = queens[i];``        ``}` `        ``// If the king is attacked by a``        ``// queen from the left by a queen``        ``// diagonal above``        ``if` `(king[``0``] - queens[i][``0``] ==``            ``king[``1``] - queens[i][``1``] &&``            ``king[``0``] > queens[i][``0``])``        ``{``            ` `            ``// If no attacker is present in``            ``// that direction``            ``if` `((attackers[``0``][``0``] == -``1``) ||` `                ``// Or the current queen is``                ``// the closest attacker in``                ``// that direction``                ``(dis(attackers[``0``], king) >``                    ``dis(queens[i], king)))` `                ``// Set current queen as``                ``// the attacker``                ``attackers[``0``] = queens[i];``        ``}` `        ``// If the king is attacked by a``        ``// queen from the left by a queen``        ``// diagonally below``        ``if` `(king[``0``] - queens[i][``0``] ==``            ``king[``1``] - queens[i][``1``] &&``            ``king[``0``] < queens[i][``0``])``        ``{``            ` `            ``// If no attacker is present in``            ``// that direction``            ``if` `((attackers[``7``][``0``] == -``1``) ||` `                ``// Or the current queen is``                ``// the closest attacker in``                ``// that direction``               ``(dis(attackers[``7``], king) >``                   ``dis(queens[i], king)))` `                ``// Set current queen as``                ``// the attacker``                ``attackers[``7``] = queens[i];``        ``}` `        ``// If the king is attacked by a``        ``// queen from the right by a queen``        ``// diagonally above``        ``if` `(king[``1``] - queens[i][``1``] == ``0` `&&``            ``king[``0``] > queens[i][``0``])``        ``{``            ` `            ``// If no attacker is present in``            ``// that direction``            ``if` `((attackers[``1``][``0``] == -``1``) ||` `                ``// Or the current queen is``                ``// the closest attacker in``                ``// that direction``                ``(dis(attackers[``1``], king) >``                    ``dis(queens[i], king)))` `                ``// Set current queen as``                ``// the attacker``                ``attackers[``1``] = queens[i];``        ``}` `        ``// If the king is attacked by a``        ``// queen from the right by a queen``        ``// diagonally below``        ``if` `(king[``1``] - queens[i][``1``] == ``0` `&&``            ``king[``0``] < queens[i][``0``])``        ``{``            ` `            ``// If no attacker is present in``            ``// that direction``            ``if` `((attackers[``6``][``0``] == -``1``) ||` `                ``// Or the current queen is``                ``// the closest attacker in``                ``// that direction``                ``(dis(attackers[``6``], king) >``                    ``dis(queens[i], king)))` `                ``// Set current queen as``                ``// the attacker``                ``attackers[``6``] = queens[i];``        ``}` `        ``// If a king is vertically below``        ``// the current queen``        ``if` `(king[``0``] - queens[i][``0``] ==``          ``-(king[``1``] - queens[i][``1``]) &&``            ``king[``0``] > queens[i][``0``])``        ``{``            ` `            ``// If no attacker is present in``            ``// that direction``            ``if` `((attackers[``2``][``0``] == -``1``) ||` `                ``// Or the current queen is``                ``// the closest attacker in``                ``// that direction``                ``(dis(attackers[``2``], king) >``                    ``dis(queens[i], king)))` `                ``// Set current queen as``                ``// the attacker``                ``attackers[``2``] = queens[i];``        ``}` `        ``// If a king is vertically above``        ``// the current queen``        ``if` `(king[``0``] - queens[i][``0``] ==``          ``-(king[``1``] - queens[i][``1``]) &&``            ``king[``0``] < queens[i][``0``])``        ``{``            ` `            ``// If no attacker is present in``            ``// that direction``            ``if` `((attackers[``5``][``0``] == -``1``) ||` `                ``// Or the current queen is``                ``// the closest attacker in``                ``// that direction``                ``(dis(attackers[``5``], king) >``                    ``dis(queens[i], king)))` `                ``// Set current queen as``                ``// the attacker``                ``attackers[``5``] = queens[i];``        ``}``    ``}` `    ``for``(``int` `i = ``0``; i < ``8``; i++)``        ``if` `(attackers[i][``0``] != -``1``)``            ``sol.add(``                ``Arrays.stream(``                    ``attackers[i]).boxed().collect(``                        ``Collectors.toList()));` `    ``// Return the coordinates``    ``return` `sol;``}` `// Print all the coordinates of the``// queens attacking the king``private` `static` `void` `print(List> ans)``{``    ``for``(``int` `i = ``0``; i < ans.size(); i++)``    ``{``        ``for``(``int` `j = ``0``; j < ``2``; j++)``            ``System.out.print(ans.get(i).get(j) + ``" "``);` `        ``System.out.println();``    ``}``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``int``[] king = { ``2``, ``3` `};` `    ``int``[][] queens = { { ``0``, ``1` `}, { ``1``, ``0` `},``                       ``{ ``4``, ``0` `}, { ``0``, ``4` `},``                       ``{ ``3``, ``3` `}, { ``2``, ``4` `} };` `    ``List> ans = findQueens(queens, king);` `    ``print(ans);``}``}` `// This code is contributed by jithin`

## Python3

 `# Python3 program to implement``# the above approach` `# Function to find the queen``# closest to king in an``# attacking position``def` `dis(ans, attacker):` `    ``return` `(``abs``(ans[``0``] ``-` `attacker[``0``]) ``+``            ``abs``(ans[``1``] ``-` `attacker[``1``]))` `# Function to find all the``# queens attacking the king``# in the chessboard``def` `findQueens(queens, king):` `    ``sol ``=` `[]``    ``attackers ``=` `[[``0` `for` `x ``in` `range``(``8``)]``                    ``for` `y ``in` `range``(``8``)]` `    ``# Iterating over the coordinates``    ``# of the queens``    ``for` `i ``in` `range``(``len``(queens)):` `        ``# If king is horizontally on``        ``# the right of current queen``        ``if` `(king[``0``] ``=``=` `queens[i][``0``] ``and``            ``king[``1``] > queens[i][``1``]):` `            ``# If no attacker is present``            ``# in that direction``            ``if` `((``len``(attackers[``3``]) ``=``=` `0``)``                ` `                ``# Or if the current queen is``                ``# closest in that direction``                ``or` `((dis(attackers[``3``], king) >``                     ``dis(queens[i], king)))):               ` `                ``# Set current queen as``                ``# the attacker``                ``attackers[``3``] ``=` `queens[i];` `        ``# If king is horizontally on``        ``# the left of current queen``        ``if` `(king[``0``] ``=``=` `queens[i][``0``] ``and``            ``king[``1``] < queens[i][``1``]):` `            ``# If no attacker is present``            ``# in that direction``            ``if` `((``len``(attackers[``4``]) ``=``=` `0``)` `                ``# Or if the current queen is``                ``# closest in that direction``                ``or` `(dis(attackers[``4``], king) >``                    ``dis(queens[i], king))):` `                ``# Set current queen as``                ``# the attacker``                ``attackers[``4``] ``=` `queens[i];` `        ``# If the king is attacked by a``        ``# queen from the left by a queen``        ``# diagonal above``        ``if` `(king[``0``] ``-` `queens[i][``0``] ``=``=``            ``king[``1``] ``-` `queens[i][``1``] ``and``            ``king[``0``] > queens[i][``0``]):` `            ``# If no attacker is present in``            ``# that direction``            ``if` `((``len``(attackers[``0``]) ``=``=` `0``)` `                ``# Or the current queen is``                ``# the closest attacker in``                ``# that direction``                ``or` `(dis(attackers[``0``], king) >``                    ``dis(queens[i], king))):` `                ``# Set current queen as``                ``# the attacker``                ``attackers[``0``] ``=` `queens[i]` `        ``# If the king is attacked by a``        ``# queen from the left by a queen``        ``# diagonally below``        ``if` `(king[``0``] ``-` `queens[i][``0``] ``=``=``            ``king[``1``] ``-` `queens[i][``1``] ``and``            ``king[``0``] < queens[i][``0``]):` `            ``# If no attacker is present in``            ``# that direction``            ``if` `((``len``(attackers[``7``]) ``=``=` `0``)` `                ``# Or the current queen is``                ``# the closest attacker in``                ``# that direction``                ``or` `(dis(attackers[``7``], king) >``                    ``dis(queens[i], king))):` `                ``# Set current queen as``                ``# the attacker``                ``attackers[``7``] ``=` `queens[i]` `        ``# If the king is attacked by a``        ``# queen from the right by a queen``        ``# diagonally above``        ``if` `(king[``1``] ``-` `queens[i][``1``] ``=``=` `0` `and``            ``king[``0``] > queens[i][``0``]):` `            ``# If no attacker is present in``            ``# that direction``            ``if` `((``len``(attackers[``1``]) ``=``=` `0``)` `                ``# Or the current queen is``                ``# the closest attacker in``                ``# that direction``                ``or` `(dis(attackers[``1``], king) >``                    ``dis(queens[i], king))):` `                ``# Set current queen as``                ``# the attacker``                ``attackers[``1``] ``=` `queens[i]` `        ``# If the king is attacked by a``        ``# queen from the right by a queen``        ``# diagonally below``        ``if` `(king[``1``] ``-` `queens[i][``1``] ``=``=` `0` `and``            ``king[``0``] < queens[i][``0``]):` `            ``# If no attacker is present in``            ``# that direction``            ``if` `((``len``(attackers[``6``]) ``=``=` `0``)` `                ``# Or the current queen is``                ``# the closest attacker in``                ``# that direction``                ``or` `(dis(attackers[``6``], king) >``                    ``dis(queens[i], king))):` `                ``# Set current queen as``                ``# the attacker``                ``attackers[``6``] ``=` `queens[i];` `        ``# If a king is vertically below``        ``# the current queen``        ``if` `(king[``0``] ``-` `queens[i][``0``] ``=``=``            ``-``(king[``1``] ``-` `queens[i][``1``]) ``and``            ``king[``0``] > queens[i][``0``]):` `            ``# If no attacker is present in``            ``# that direction``            ``if` `((``len``(attackers[``2``]) ``=``=` `0``)` `                ``# Or the current queen is``                ``# the closest attacker in``                ``# that direction``                ``or` `(dis(attackers[``2``], king) >``                    ``dis(queens[i], king))):` `                ``# Set current queen as``                ``# the attacker``                ``attackers[``2``] ``=` `queens[i]` `        ``# If a king is vertically above``        ``# the current queen``        ``if` `(king[``0``] ``-` `queens[i][``0``] ``=``=``          ``-``(king[``1``] ``-` `queens[i][``1``]) ``and``            ``king[``0``] < queens[i][``0``]):` `            ``# If no attacker is present in``            ``# that direction``            ``if` `((``len``(attackers[``5``]) ``=``=` `0``)` `                ``# Or the current queen is``                ``# the closest attacker in``                ``# that direction``                ``or` `(dis(attackers[``5``], king) >``                    ``dis(queens[i], king))):` `                ``# Set current queen as``                ``# the attacker``                ``attackers[``5``] ``=` `queens[i]``                    ` `    ``for` `i ``in` `range``(``8``):``        ``f ``=` `1``        ``for` `x ``in` `attackers[i]:``          ``if` `x !``=` `0``:``            ``f ``=` `0``            ``break``        ``if` `f ``=``=` `0``:``          ``sol.append(attackers[i])` `    ``# Return the coordinates``    ``return` `sol` `# Print all the coordinates of the``# queens attacking the king``def` `print_board(ans):` `    ``for` `i ``in` `range``(``len``(ans)):``        ``for` `j ``in` `range``(``2``):``            ``print``(ans[i][j],``                  ``end ``=` `" "``)``            ` `        ``print``()` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:` `    ``king ``=` `[``2``, ``3``]``    ``queens ``=` `[[``0``, ``1``], [``1``, ``0``],``              ``[``4``, ``0``], [``0``, ``4``],``              ``[``3``, ``3``], [``2``, ``4``]]``    ``ans ``=` `findQueens(queens, king);``    ``print_board(ans);` `# This code is contributed by Chitranayal`

Output:

```0 1
2 4
3 3

```

Time Complexity: O(N), where N is the number of queens
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up