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++
// C++ program to implement// the above approachÂ
#include <bits/stdc++.h>using namespace std;Â
// Function to count the knights that are// attacking the pawn in an M * M boardint cntKnightsAttackPawn(int knights[][2],                         int pawn[], int M){    // Stores count of knights that    // are attacking the pawn    int cntKnights = 0;Â
    // Traverse the knights[][] array    for (int i = 0; i < M; i++) {Â
        // Stores absolute difference of X        // co-ordinate of i-th knight and pawn        int X = abs(knights[i][0]                    - pawn[0]);Â
        // Stores absolute difference of Y        // co-ordinate of i-th knight and pawn        int Y = abs(knights[i][1]                    - pawn[1]);Â
        // If X is 1 and Y is 2 or        // X is 2 and Y is 1        if ((X == 1 && Y == 2)            || (X == 2 && Y == 1)) {Â
            // Update cntKnights            cntKnights++;        }    }Â
    return cntKnights;}Â
// Driver Codeint main(){Â
    int knights[][2] = { { 0, 4 }, { 4, 5 },                          { 1, 4 }, { 3, 1 } };Â
    int pawn[] = { 2, 3 };Â
    // Stores total count of knights    int M = sizeof(knights)            / sizeof(knights[0]);Â
    cout << cntKnightsAttackPawn(        knights, pawn, M);Â
    return 0;} |
Java
// Java program to implement // the above approach import java.io.*;import java.lang.Math;Â
class GFG{Â
// Function to count the knights that are // attacking the pawn in an M * M board static int cntKnightsAttackPawn(int knights[][],                                 int pawn[], int M){         // Stores count of knights that     // are attacking the pawn     int cntKnights = 0;          // Traverse the knights[][] array     for(int i = 0; i < M; i++)     {                  // Stores absolute difference of X         // co-ordinate of i-th knight and pawn         int X = Math.abs(knights[i][0] - pawn[0]);              // Stores absolute difference of Y         // co-ordinate of i-th knight and pawn         int Y = Math.abs(knights[i][1] - pawn[1]);              // If X is 1 and Y is 2 or         // X is 2 and Y is 1         if ((X == 1 && Y == 2) ||            (X == 2 && Y == 1))        {                          // Update cntKnights             cntKnights++;         }     }     return cntKnights; }Â
// Driver codepublic static void main(String[] args){    int[][] knights = { { 0, 4 }, { 4, 5 },                         { 1, 4 }, { 3, 1 } };         int[] pawn = new int[]{2, 3};       // Stores total count of knights     int M = knights.length;         System.out.println(cntKnightsAttackPawn(        knights, pawn, M));}}Â
// This code is contributed by vandanakillari54935 |
Python3
# Python program to implement# the above approachÂ
# Function to count the knights that are# attacking the pawn in an M * M boarddef cntKnightsAttackPawn(knights, pawn, M):       # Stores count of knights that    # are attacking the pawn    cntKnights = 0;Â
    # Traverse the knights array    for i in range(M):Â
        # Stores absolute difference of X        # co-ordinate of i-th knight and pawn        X = abs(knights[i][0] - pawn[0]);Â
        # Stores absolute difference of Y        # co-ordinate of i-th knight and pawn        Y = abs(knights[i][1] - pawn[1]);Â
        # If X is 1 and Y is 2 or        # X is 2 and Y is 1        if ((X == 1 and Y == 2) or (X == 2 and Y == 1)):                       # Update cntKnights            cntKnights += 1;Â
    return cntKnights;Â
# Driver codeif __name__ == '__main__':Â Â Â Â knights = [[0, 4], [4, 5], [1, 4], [3, 1]];Â
    pawn = [2, 3];Â
    # Stores total count of knights    M = len(knights);Â
    print(cntKnightsAttackPawn(knights, pawn, M));Â
# This code is contributed by Amit Katiyar |
C#
// C# program to implement // the above approach using System;class GFG {Â
  // Function to count the knights that are   // attacking the pawn in an M * M board   static int cntKnightsAttackPawn(int[,] knights, int[] pawn, int M)   {     // Stores count of knights that     // are attacking the pawn     int cntKnights = 0; Â
    // Traverse the knights[][] array     for (int i = 0; i < M; i++) { Â
      // Stores absolute difference of X       // co-ordinate of i-th knight and pawn       int X = Math.Abs(knights[i, 0] - pawn[0]); Â
      // Stores absolute difference of Y       // co-ordinate of i-th knight and pawn       int Y = Math.Abs(knights[i, 1] - pawn[1]); Â
      // If X is 1 and Y is 2 or       // X is 2 and Y is 1       if ((X == 1 && Y == 2)           || (X == 2 && Y == 1)) { Â
        // Update cntKnights         cntKnights++;       }     } Â
    return cntKnights;   } Â
  // Driver code  static void Main()   {    int[,] knights = {{ 0, 4 }, { 4, 5 }, { 1, 4 }, { 3, 1 }}; Â
    int[] pawn = {2, 3}; Â
    // Stores total count of knights     int M = knights.GetLength(0); Â
    Console.WriteLine(cntKnightsAttackPawn(knights, pawn, M));  }}Â
// This code is contributed by divyeshrabadiya07 |
Javascript
<script>Â
// javascript program for the above approachÂ
// Function to count the knights that are // attacking the pawn in an M * M board function cntKnightsAttackPawn(knights,                                 pawn, M){         // Stores count of knights that     // are attacking the pawn     let cntKnights = 0;          // Traverse the knights[][] array     for(let i = 0; i < M; i++)     {                  // Stores absolute difference of X         // co-ordinate of i-th knight and pawn         let X = Math.abs(knights[i][0] - pawn[0]);              // Stores absolute difference of Y         // co-ordinate of i-th knight and pawn         let Y = Math.abs(knights[i][1] - pawn[1]);              // If X is 1 and Y is 2 or         // X is 2 and Y is 1         if ((X == 1 && Y == 2) ||            (X == 2 && Y == 1))        {                          // Update cntKnights             cntKnights++;         }     }     return cntKnights; }Â
// Driver Code             let knights = [[ 0, 4 ], [ 4, 5 ],                         [ 1, 4 ], [ 3, 1 ]];         let pawn = [2, 3];       // Stores total count of knights     let M = knights.length;         document.write(cntKnightsAttackPawn(        knights, pawn, M));Â
</script> |
2
Time Complexity: O(M), where M is the total count number of knightsÂ
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
