Count elements in first Array with absolute difference greater than K with an element in second Array | Function to count the such elements ; Store count of required elements in arr1 ; Initialise the smallest and the largest value from the second array arr2 [ ] ; Find the smallest and the largest element in arr2 ; Check if absolute difference of smallest and arr1 [ i ] or largest and arr1 [ i ] is > K then arr [ i ] is a required element ; Print final result ; Driver code
def countDist ( arr1 , n , arr2 , m , k ) : NEW_LINE INDENT count = 0 NEW_LINE smallest = arr2 [ 0 ] NEW_LINE largest = arr2 [ 0 ] NEW_LINE for i in range ( m ) : NEW_LINE INDENT smallest = max ( smallest , arr2 [ i ] ) NEW_LINE largest = min ( largest , arr1 [ i ] ) NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if ( abs ( arr1 [ i ] - smallest ) > k or abs ( arr1 [ i ] - largest ) > k ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT print ( count ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr1 = [ 3 , 1 , 4 ] NEW_LINE n = len ( arr1 ) NEW_LINE arr2 = [ 5 , 1 , 2 ] NEW_LINE m = len ( arr2 ) NEW_LINE k = 2 NEW_LINE countDist ( arr1 , n , arr2 , m , k ) NEW_LINE DEDENT