Smallest Difference pair of values between two unsorted Arrays | C # Code to find Smallest Difference between two Arrays ; function to calculate Small result between two arrays ; Sort both arrays using sort function ; Initialize result as max value ; Scan Both Arrays upto sizeof of the Arrays ; Move Smaller Value ; return final sma result ; Driver Code ; Input given array A ; Input given array B ; Calculate size of Both arrays ; Call function to print smallest result
using System ; class GFG { static int findSmallestDifference ( int [ ] A , int [ ] B , int m , int n ) { Array . Sort ( A ) ; Array . Sort ( B ) ; int a = 0 , b = 0 ; int result = int . MaxValue ; while ( a < m && b < n ) { if ( Math . Abs ( A [ a ] - B [ b ] ) < result ) result = Math . Abs ( A [ a ] - B [ b ] ) ; if ( A [ a ] < B [ b ] ) a ++ ; else b ++ ; } return result ; } public static void Main ( ) { int [ ] A = { 1 , 2 , 11 , 5 } ; int [ ] B = { 4 , 12 , 19 , 23 , 127 , 235 } ; int m = A . Length ; int n = B . Length ; Console . Write ( findSmallestDifference ( A , B , m , n ) ) ; } }