![]() |
VOOZH | about |
Given a 2D array knights[][] of size N * 2, with each row of the form { X, Y } representing the coordinates of knights, and an array pawn[] representing the coordinates of a pawn in an N * N board, the task is to find the count of knights present in the board that is attacking the pawn.
Examples:
Input: knights[][] = { { 0, 4 }, { 4, 5 }, { 1, 4 }, { 3, 1 } }, pawn[] = { 2, 3 }
Output: 2
Explanation:
The knights present at coordinate { { 0, 4 }, { 3, 1 } } are attacking the pawn.
Therefore, the required output is 2.Input: knights[][] = { { 4, 6 }, { 7, 5 }, { 5, 5 } }, pawn[] = { 6, 7 }
Output: 3
Explanation:
The knights present at coordinate { { 4, 6 }, { 7, 5 }, { 5, 5 } } are attacking the pawn.
Therefore, the required output is 3.
Approach: Follow the steps given below to solve the problem
Below is the implementation of the above approach:
2
Time Complexity: O(M), where M is the total count number of knights
Auxiliary Space: O(1)