C # program to find a pair with given sum using hashing
using System ; using System . Collections . Generic ; class GFG { class Node { public int data ; public Node left , right ; } ; static Node NewNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = null ; temp . right = null ; return temp ; } static Node insert ( Node root , int key ) { if ( root == null ) return NewNode ( key ) ; if ( key < root . data ) root . left = insert ( root . left , key ) ; else root . right = insert ( root . right , key ) ; return root ; } static bool findpairUtil ( Node root , int sum , HashSet < int > set ) { if ( root == null ) return false ; if ( findpairUtil ( root . left , sum , set ) ) return true ; if ( set . Contains ( sum - root . data ) ) { Console . WriteLine ( " Pair ▁ is ▁ found ▁ ( " + ( sum - root . data ) + " , ▁ " + root . data + " ) " ) ; return true ; } else set . Add ( root . data ) ; return findpairUtil ( root . right , sum , set ) ; } static void findPair ( Node root , int sum ) { HashSet < int > set = new HashSet < int > ( ) ; if ( ! findpairUtil ( root , sum , set ) ) Console . Write ( " Pairs ▁ do ▁ not ▁ exit " + " STRNEWLINE " ) ; }