Make max elements in B [ ] equal to that of A [ ] by adding / subtracting integers in range [ 0 , K ] | Function that count the number of integers from array B [ ] such that subtracting element in the range [ 0 , K ] given any element in A [ ] ; To store the count of element ; Traverse the array B [ ] ; Traverse the array A [ ] ; Find the difference ; If difference is atmost K then increment the cnt ; Print the count ; Given array A [ ] and B [ ] ; Given K ; Function call
function countElement ( A , N , B , M , K ) { let cnt = 0 ; for ( let i = 0 ; i < M ; i ++ ) { let currentElement = B [ i ] ; for ( let j = 0 ; j < N ; j ++ ) { let diff = Math . abs ( currentElement - A [ j ] ) ; if ( diff <= K ) { cnt ++ ; break ; } } } document . write ( cnt ) ; } let A = [ 100 , 65 , 35 , 85 , 55 ] ; let B = [ 30 , 60 , 75 , 95 ] ; let K = 5 ; let N = A . length ; let M = B . length ; countElement ( A , N , B , M , K ) ;