Make max elements in B [ ] equal to that of A [ ] by adding / subtracting integers in range [ 0 , K ] | Java program for the above approach ; 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 ; Driver Code ; Given array A [ ] and B [ ] ; Given K ; Function call
class GFG { static void countElement ( int A [ ] , int N , int B [ ] , int M , int K ) { int cnt = 0 ; for ( int i = 0 ; i < M ; i ++ ) { int currentElement = B [ i ] ; for ( int j = 0 ; j < N ; j ++ ) { int diff = Math . abs ( currentElement - A [ j ] ) ; if ( diff <= K ) { cnt ++ ; break ; } } } System . out . print ( cnt ) ; } public static void main ( String [ ] args ) { int A [ ] = { 100 , 65 , 35 , 85 , 55 } ; int B [ ] = { 30 , 60 , 75 , 95 } ; int K = 5 ; int N = A . length ; int M = B . length ; countElement ( A , N , B , M , K ) ; } }