Maximum neighbor element in a matrix within distance K | Function to print the matrix ; Loop to iterate over the matrix ; Function to find the maximum neighbor within the distance of less than equal to K ; Loop to find the maximum element within the distance of less than K ; Driver Code
def printMatrix ( A ) : NEW_LINE INDENT for i in range ( len ( A ) ) : NEW_LINE INDENT for j in range ( len ( A [ 0 ] ) ) : NEW_LINE INDENT print ( A [ i ] [ j ] , end = ' ▁ ' ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT def getMaxNeighbour ( A , K ) : NEW_LINE INDENT ans = A ; NEW_LINE for q in range ( 1 , K + 1 ) : NEW_LINE INDENT for i in range ( len ( A ) ) : NEW_LINE INDENT for j in range ( len ( A [ 0 ] ) ) : NEW_LINE INDENT maxi = ans [ i ] [ j ] ; NEW_LINE if ( i > 0 ) : NEW_LINE INDENT maxi = max ( maxi , ans [ i - 1 ] [ j ] ) ; NEW_LINE DEDENT if ( j > 0 ) : NEW_LINE INDENT maxi = max ( maxi , ans [ i ] [ j - 1 ] ) ; NEW_LINE DEDENT if ( i < len ( A ) - 1 ) : NEW_LINE INDENT maxi = max ( maxi , ans [ i + 1 ] [ j ] ) ; NEW_LINE DEDENT if ( j < len ( A [ 0 ] ) - 1 ) : NEW_LINE INDENT maxi = max ( maxi , ans [ i ] [ j + 1 ] ) ; NEW_LINE DEDENT A [ i ] [ j ] = maxi ; NEW_LINE DEDENT DEDENT ans = A ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT B = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] NEW_LINE printMatrix ( getMaxNeighbour ( B , 2 ) ) ; NEW_LINE DEDENT