Make max elements in B [ ] equal to that of A [ ] by adding / subtracting integers in range [ 0 , K ] | C # 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
using System ; 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 ; } } } Console . Write ( 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 ) ; } }