Print all triplets with given sum | C # program to find triplets in a given array whose sum is equal to given sum . ; function to print triplets with given sum ; Find all pairs with sum equals to " sum - arr [ i ] " ; Driver code
using System ; using System . Collections . Generic ; public class GFG { static void findTriplets ( int [ ] arr , int n , int sum ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { HashSet < int > s = new HashSet < int > ( ) ; for ( int j = i + 1 ; j < n ; j ++ ) { int x = sum - ( arr [ i ] + arr [ j ] ) ; if ( s . Contains ( x ) ) Console . Write ( " { 0 } ▁ { 1 } ▁ { 2 } STRNEWLINE " , x , arr [ i ] , arr [ j ] ) ; else s . Add ( arr [ j ] ) ; } } } public static void Main ( String [ ] args ) { int [ ] arr = { 0 , - 1 , 2 , - 3 , 1 } ; int sum = - 2 ; int n = arr . Length ; findTriplets ( arr , n , sum ) ; } }