Count Knights that can attack a given pawn in an N * N board
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
- Initialize a variable, say cntKnights, to store the count of knights that are attacking the pawn.
- Traverse the knights[][] array using variable i and for every array element knights[i], check if the array { (knights[i][0] – pawn[0]), (knights[i][1] – pawn[1]) } is equal to either { 1, 2 } or { 2, 1 } or not. If found to be true, then increment the value of cntKnights by 1.
- Finally, print the value of cntKnights.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int cntKnightsAttackPawn( int knights[][2],
int pawn[], int M)
{
int cntKnights = 0;
for ( int i = 0; i < M; i++) {
int X = abs (knights[i][0]
- pawn[0]);
int Y = abs (knights[i][1]
- pawn[1]);
if ((X == 1 && Y == 2)
|| (X == 2 && Y == 1)) {
cntKnights++;
}
}
return cntKnights;
}
int main()
{
int knights[][2] = { { 0, 4 }, { 4, 5 },
{ 1, 4 }, { 3, 1 } };
int pawn[] = { 2, 3 };
int M = sizeof (knights)
/ sizeof (knights[0]);
cout << cntKnightsAttackPawn(
knights, pawn, M);
return 0;
}
|
Java
import java.io.*;
import java.lang.Math;
class GFG{
static int cntKnightsAttackPawn( int knights[][],
int pawn[], int M)
{
int cntKnights = 0 ;
for ( int i = 0 ; i < M; i++)
{
int X = Math.abs(knights[i][ 0 ] - pawn[ 0 ]);
int Y = Math.abs(knights[i][ 1 ] - pawn[ 1 ]);
if ((X == 1 && Y == 2 ) ||
(X == 2 && Y == 1 ))
{
cntKnights++;
}
}
return cntKnights;
}
public static void main(String[] args)
{
int [][] knights = { { 0 , 4 }, { 4 , 5 },
{ 1 , 4 }, { 3 , 1 } };
int [] pawn = new int []{ 2 , 3 };
int M = knights.length;
System.out.println(cntKnightsAttackPawn(
knights, pawn, M));
}
}
|
Python3
def cntKnightsAttackPawn(knights, pawn, M):
cntKnights = 0 ;
for i in range (M):
X = abs (knights[i][ 0 ] - pawn[ 0 ]);
Y = abs (knights[i][ 1 ] - pawn[ 1 ]);
if ((X = = 1 and Y = = 2 ) or (X = = 2 and Y = = 1 )):
cntKnights + = 1 ;
return cntKnights;
if __name__ = = '__main__' :
knights = [[ 0 , 4 ], [ 4 , 5 ], [ 1 , 4 ], [ 3 , 1 ]];
pawn = [ 2 , 3 ];
M = len (knights);
print (cntKnightsAttackPawn(knights, pawn, M));
|
C#
using System;
class GFG
{
static int cntKnightsAttackPawn( int [,] knights, int [] pawn, int M)
{
int cntKnights = 0;
for ( int i = 0; i < M; i++) {
int X = Math.Abs(knights[i, 0] - pawn[0]);
int Y = Math.Abs(knights[i, 1] - pawn[1]);
if ((X == 1 && Y == 2)
|| (X == 2 && Y == 1)) {
cntKnights++;
}
}
return cntKnights;
}
static void Main()
{
int [,] knights = {{ 0, 4 }, { 4, 5 }, { 1, 4 }, { 3, 1 }};
int [] pawn = {2, 3};
int M = knights.GetLength(0);
Console.WriteLine(cntKnightsAttackPawn(knights, pawn, M));
}
}
|
Javascript
<script>
function cntKnightsAttackPawn(knights,
pawn, M)
{
let cntKnights = 0;
for (let i = 0; i < M; i++)
{
let X = Math.abs(knights[i][0] - pawn[0]);
let Y = Math.abs(knights[i][1] - pawn[1]);
if ((X == 1 && Y == 2) ||
(X == 2 && Y == 1))
{
cntKnights++;
}
}
return cntKnights;
}
let knights = [[ 0, 4 ], [ 4, 5 ],
[ 1, 4 ], [ 3, 1 ]];
let pawn = [2, 3];
let M = knights.length;
document.write(cntKnightsAttackPawn(
knights, pawn, M));
</script>
|
Time Complexity: O(M), where M is the total count number of knights
Auxiliary Space: O(1)
Last Updated :
03 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...