Count Knights that can attack a given pawn in an N * N board | Function to count the knights that are attacking the pawn in an M * M board ; Stores count of knights that are attacking the pawn ; Traverse the knights array ; Stores absolute difference of X co - ordinate of i - th knight and pawn ; Stores absolute difference of Y co - ordinate of i - th knight and pawn ; If X is 1 and Y is 2 or X is 2 and Y is 1 ; Update cntKnights ; Driver code ; Stores total count of knights
def cntKnightsAttackPawn ( knights , pawn , M ) : NEW_LINE INDENT cntKnights = 0 ; NEW_LINE for i in range ( M ) : NEW_LINE INDENT X = abs ( knights [ i ] [ 0 ] - pawn [ 0 ] ) ; NEW_LINE Y = abs ( knights [ i ] [ 1 ] - pawn [ 1 ] ) ; NEW_LINE if ( ( X == 1 and Y == 2 ) or ( X == 2 and Y == 1 ) ) : NEW_LINE INDENT cntKnights += 1 ; NEW_LINE DEDENT DEDENT return cntKnights ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT knights = [ [ 0 , 4 ] , [ 4 , 5 ] , [ 1 , 4 ] , [ 3 , 1 ] ] ; NEW_LINE pawn = [ 2 , 3 ] ; NEW_LINE M = len ( knights ) ; NEW_LINE print ( cntKnightsAttackPawn ( knights , pawn , M ) ) ; NEW_LINE DEDENT