Count elements in first Array with absolute difference greater than K with an element in second Array | C # program to 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 the readonly result ; Driver code
using System ; class GFG { static void countDist ( int [ ] arr1 , int n , int [ ] arr2 , int m , int k ) { int count = 0 ; int smallest = arr2 [ 0 ] ; int largest = arr2 [ 0 ] ; for ( int i = 0 ; i < m ; i ++ ) { smallest = Math . Max ( smallest , arr2 [ i ] ) ; largest = Math . Min ( largest , arr1 [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { if ( Math . Abs ( arr1 [ i ] - smallest ) > k || Math . Abs ( arr1 [ i ] - largest ) > k ) count ++ ; } Console . Write ( count ) ; } public static void Main ( String [ ] args ) { int [ ] arr1 = { 3 , 1 , 4 } ; int n = arr1 . Length ; int [ ] arr2 = { 5 , 1 , 2 } ; int m = arr2 . Length ; int k = 2 ; countDist ( arr1 , n , arr2 , m , k ) ; } }