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 ; Prvar the final result ; Driver code
function countDist ( arr1 , n , arr2 , m , k ) { var count = 0 ; var smallest = arr2 [ 0 ] ; var largest = arr2 [ 0 ] ; for ( i = 0 ; i < m ; i ++ ) { smallest = Math . max ( smallest , arr2 [ i ] ) ; largest = Math . min ( largest , arr1 [ i ] ) ; } for ( i = 0 ; i < n ; i ++ ) { if ( Math . abs ( arr1 [ i ] - smallest ) > k || Math . abs ( arr1 [ i ] - largest ) > k ) count ++ ; } document . write ( count ) ; } var arr1 = [ 3 , 1 , 4 ] ; var n = arr1 . length ; var arr2 = [ 5 , 1 , 2 ] ; var m = arr2 . length ; var k = 2 ; countDist ( arr1 , n , arr2 , m , k ) ;