Print all pairs with given sum | C # program to find triplets in a given array whose sum is equal to given sum . ; Driver code
using System ; public class SumOfPairs { public void pairedElements ( int [ ] arr , int sum ) { int low = 0 ; int high = arr . Length - 1 ; while ( low < high ) { if ( arr [ low ] + arr [ high ] == sum ) { Console . WriteLine ( " The ▁ pair ▁ is ▁ : ▁ ( " + arr [ low ] + " , ▁ " + arr [ high ] + " ) " ) ; } if ( arr [ low ] + arr [ high ] > sum ) { high -- ; } else { low ++ ; } } } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 4 , - 2 , 6 , 8 , 9 , 11 } ; Array . Sort ( arr ) ; SumOfPairs sp = new SumOfPairs ( ) ; sp . pairedElements ( arr , 6 ) ; } }