Minimum Possible value of | ai + aj | C # program to find number of pairs and minimal possible value ; function for finding pairs and min value ; initialize smallest and count ; iterate over all pairs ; is abs value is smaller than smallest update smallest and reset count to 1 ; if abs value is equal to smallest increment count value ; print result ; Driver Code
using System ; class GFG { static void pairs ( int [ ] arr , int n , int k ) { int smallest = 0 ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) { if ( Math . Abs ( arr [ i ] + arr [ j ] - k ) < smallest ) { smallest = Math . Abs ( arr [ i ] + arr [ j ] - k ) ; count = 1 ; } else if ( Math . Abs ( arr [ i ] + arr [ j ] - k ) == smallest ) count ++ ; } Console . WriteLine ( " Minimal ▁ Value ▁ = ▁ " + smallest ) ; Console . WriteLine ( " Total ▁ Pairs ▁ = ▁ " + count ) ; } public static void Main ( ) { int [ ] arr = { 3 , 5 , 7 , 5 , 1 , 9 , 9 } ; int k = 12 ; int n = arr . Length ; pairs ( arr , n , k ) ; } }