codeparrot/xlcost-text-to-code huggingface dataset:
First negative integer in every window of size k | C # implementation to find the first negative integer in every window of size k ; function to find the first negative integer in every window of size k ; A Double Ended Queue , Di that will store indexes of useful array elements for the current window of size k . The useful elements are all negative integers . ; Process first k ( or first window ) elements of array ; Add current element at the rear of Di if it is a negative integer ; Process rest of the elements , i . e . , from arr [ k ] to arr [ n - 1 ] ; if Di is not empty then the element at the front of the queue is the first negative integer of the previous window ; else the window does not have a negative integer ; Remove the elements which are out of this window ; Remove from front of queue ; Add current element at the rear of Di if it is a negative integer ; Print the first negative integer of last window ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void printFirstNegativeint ( int [ ] arr , int n , int k ) { List < int > Di = new List < int > ( ) ; int i ; for ( i = 0 ; i < k ; i ++ ) if ( arr [ i ] < 0 ) Di . Add ( i ) ; for ( ; i < n ; i ++ ) { if ( Di . Count != 0 ) Console . Write ( arr [ Di [ 0 ] ] + " ▁ " ) ; else Console . Write ( "0" + " ▁ " ) ; while ( ( Di . Count != 0 ) && Di [ 0 ] < ( i - k + 1 ) ) Di . RemoveAt ( 0 ) ; if ( arr [ i ] < 0 ) Di . Add ( i ) ; } if ( Di . Count != 0 ) Console . Write ( arr [ Di [ 0 ] ] + " ▁ " ) ; else Console . Write ( "0" + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 12 , - 1 , - 7 , 8 , - 15 , 30 , 16 , 28 } ; int n = arr . Length ; int k = 3 ; printFirstNegativeint ( arr , n , k ) ; } }
First negative integer in every window of size k | C # code for First negative integer in every window of size k ; Skip out of window and positive elements ; Check if a negative element is found , otherwise use 0 ; Driver code
using System ; class GFG { static void printFirstNegativeInteger ( int [ ] arr , int k , int n ) { int firstNegativeIndex = 0 ; int firstNegativeElement ; for ( int i = k - 1 ; i < n ; i ++ ) { while ( ( firstNegativeIndex < i ) && ( firstNegativeIndex <= i - k arr [ firstNegativeIndex ] > 0 ) ) { firstNegativeIndex ++ ; } if ( arr [ firstNegativeIndex ] < 0 ) { firstNegativeElement = arr [ firstNegativeIndex ] ; } else { firstNegativeElement = 0 ; } Console . Write ( firstNegativeElement + " ▁ " ) ; } } static public void Main ( ) { int [ ] arr = { 12 , - 1 , - 7 , 8 , - 15 , 30 , 16 , 28 } ; int n = arr . Length ; int k = 3 ; printFirstNegativeInteger ( arr , k , n ) ; } }
Check if all levels of two trees are anagrams or not | Iterative program to check if two trees are level by level anagram . ; A Binary Tree Node ; Returns true if trees with root1 and root2 are level by level anagram , else returns false . ; Base Cases ; start level order traversal of two trees using two queues . ; n1 ( queue size ) indicates number of Nodes at current level in first tree and n2 indicates number of nodes in current level of second tree . ; If n1 and n2 are different ; If level order traversal is over ; Dequeue all Nodes of current level and Enqueue all Nodes of next level ; Check if nodes of current levels are anagrams or not . ; Driver Code ; Constructing both the trees .
using System ; using System . Collections . Generic ; class GFG { public class Node { public Node left , right ; public int data ; public Node ( int data ) { this . data = data ; left = null ; right = null ; } } static Boolean areAnagrams ( Node root1 , Node root2 ) { if ( root1 == null && root2 == null ) return true ; if ( root1 == null root2 == null ) return false ; Queue < Node > q1 = new Queue < Node > ( ) ; Queue < Node > q2 = new Queue < Node > ( ) ; q1 . Enqueue ( root1 ) ; q2 . Enqueue ( root2 ) ; while ( true ) { int n1 = q1 . Count , n2 = q2 . Count ; if ( n1 != n2 ) return false ; if ( n1 == 0 ) break ; List < int > curr_level1 = new List < int > ( ) ; List < int > curr_level2 = new List < int > ( ) ; while ( n1 > 0 ) { Node node1 = q1 . Peek ( ) ; q1 . Dequeue ( ) ; if ( node1 . left != null ) q1 . Enqueue ( node1 . left ) ; if ( node1 . right != null ) q1 . Enqueue ( node1 . right ) ; n1 -- ; Node node2 = q2 . Peek ( ) ; q2 . Dequeue ( ) ; if ( node2 . left != null ) q2 . Enqueue ( node2 . left ) ; if ( node2 . right != null ) q2 . Enqueue ( node2 . right ) ; curr_level1 . Add ( node1 . data ) ; curr_level2 . Add ( node2 . data ) ; } curr_level1 . Sort ( ) ; curr_level2 . Sort ( ) ; for ( int i = 0 ; i < curr_level1 . Count ; i ++ ) if ( curr_level1 [ i ] != curr_level2 [ i ] ) return false ; } return true ; } public static void Main ( String [ ] args ) { Node root1 = new Node ( 1 ) ; root1 . left = new Node ( 3 ) ; root1 . right = new Node ( 2 ) ; root1 . right . left = new Node ( 5 ) ; root1 . right . right = new Node ( 4 ) ; Node root2 = new Node ( 1 ) ; root2 . left = new Node ( 2 ) ; root2 . right = new Node ( 3 ) ; root2 . left . left = new Node ( 4 ) ; root2 . left . right = new Node ( 5 ) ; Console . WriteLine ( areAnagrams ( root1 , root2 ) ? " Yes " : " No " ) ; } }
Check if given Preorder , Inorder and Postorder traversals are of same tree | C # program to check if all three given traversals are of the same tree ; A Binary Tree Node ; Utility function to create a new tree node ; Function to find index of value in arr [ start ... end ] The function assumes that value is present in in [ ] ; Recursive function to construct binary tree of size len from Inorder traversal in [ ] and Preorder traversal pre [ ] . Initial values of inStrt and inEnd should be 0 and len - 1. The function doesn 't do any error checking for  cases where inorder and preorder do not form a  tree  ; Pick current node from Preorder traversal using preIndex and increment preIndex ; If this node has no children then return ; Else find the index of this node in Inorder traversal ; Using index in Inorder traversal , construct left and right subtress ; function to compare Postorder traversal on constructed tree and given Postorder ; first recur on left child ; now recur on right child ; Compare if data at current index in both Postorder traversals are same ; Driver code ; build tree from given Inorder and Preorder traversals ; compare postorder traversal on constructed tree with given Postorder traversal ; If both postorder traversals are same
using System ; public class GfG { static int preIndex = 0 ; 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 int search ( int [ ] arr , int strt , int end , int value ) { for ( int i = strt ; i <= end ; i ++ ) { if ( arr [ i ] == value ) return i ; } return - 1 ; } static Node buildTree ( int [ ] In , int [ ] pre , int inStrt , int inEnd ) { if ( inStrt > inEnd ) return null ; Node tNode = newNode ( pre [ preIndex ++ ] ) ; if ( inStrt == inEnd ) return tNode ; int inIndex = search ( In , inStrt , inEnd , tNode . data ) ; tNode . left = buildTree ( In , pre , inStrt , inIndex - 1 ) ; tNode . right = buildTree ( In , pre , inIndex + 1 , inEnd ) ; return tNode ; } static int checkPostorder ( Node node , int [ ] postOrder , int index ) { if ( node == null ) return index ; index = checkPostorder ( node . left , postOrder , index ) ; index = checkPostorder ( node . right , postOrder , index ) ; if ( node . data == postOrder [ index ] ) index ++ ; else return - 1 ; return index ; } public static void Main ( ) { int [ ] inOrder = { 4 , 2 , 5 , 1 , 3 } ; int [ ] preOrder = { 1 , 2 , 4 , 5 , 3 } ; int [ ] postOrder = { 4 , 5 , 2 , 3 , 1 } ; int len = inOrder . Length ; Node root = buildTree ( inOrder , preOrder , 0 , len - 1 ) ; int index = checkPostorder ( root , postOrder , 0 ) ; if ( index == len ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Check if X can give change to every person in the Queue | C # program to check whether X can give change to every person in the Queue ; Function to check if every person will get the change from X ; To count the 5 $ and 10 & notes ; Serve the customer in order ; Increase the number of 5 $ note by one ; decrease the number of note 5 $ and increase 10 $ note by one ; decrease 5 $ and 10 $ note by one ; decrease 5 $ note by three ; Driver Code ; queue of customers with available notes . ; Calling function
using System ; class GFG { static int isChangeable ( int [ ] notes , int n ) { int fiveCount = 0 ; int tenCount = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( notes [ i ] == 5 ) fiveCount ++ ; else if ( notes [ i ] == 10 ) { if ( fiveCount > 0 ) { fiveCount -- ; tenCount ++ ; } else return 0 ; } else { if ( fiveCount > 0 && tenCount > 0 ) { fiveCount -- ; tenCount -- ; } else if ( fiveCount >= 3 ) { fiveCount -= 3 ; } else return 0 ; } } return 1 ; } public static void Main ( ) { int [ ] a = { 5 , 5 , 5 , 10 , 20 } ; int n = a . Length ; if ( isChangeable ( a , n ) > 0 ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }
Index Mapping ( or Trivial Hashing ) with negatives allowed | C # program to implement direct index mapping with negative values allowed . ; Since array is global , it is initialized as 0. ; searching if X is Present in the given array or not . ; if X is negative take the absolute value of X . ; Driver code
using System ; class GFG { static int MAX = 1000 ; static bool [ , ] has = new bool [ MAX + 1 , 2 ] ; static bool search ( int X ) { if ( X >= 0 ) { if ( has [ X , 0 ] == true ) { return true ; } else { return false ; } } X = Math . Abs ( X ) ; if ( has [ X , 1 ] == true ) { return true ; } return false ; } static void insert ( int [ ] a , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] >= 0 ) { has [ a [ i ] , 0 ] = true ; } else { has [ Math . Abs ( a [ i ] ) , 1 ] = true ; } } } public static void Main ( ) { int [ ] a = { - 1 , 9 , - 5 , - 8 , - 5 , - 2 } ; int n = a . Length ; insert ( a , n ) ; int X = - 5 ; if ( search ( X ) == true ) { Console . WriteLine ( " Present " ) ; } else { Console . WriteLine ( " Not ▁ Present " ) ; } } }
Given level order traversal of a Binary Tree , check if the Tree is a Min | C # program to check if a given tree is Binary Heap or not ; Returns true if given level order traversal is Min Heap . ; First non leaf node is at index ( n / 2 - 1 ) . Check whether each parent is greater than child ; Left child will be at index 2 * i + 1 Right child will be at index 2 * i + 2 ; If parent is greater than right child ; Driver code
using System ; class GFG { public static bool isMinHeap ( int [ ] level ) { int n = level . Length - 1 ; for ( int i = ( n / 2 - 1 ) ; i >= 0 ; i -- ) { if ( level [ i ] > level [ 2 * i + 1 ] ) { return false ; } if ( 2 * i + 2 < n ) { if ( level [ i ] > level [ 2 * i + 2 ] ) { return false ; } } } return true ; } public static void Main ( string [ ] args ) { int [ ] level = new int [ ] { 10 , 15 , 14 , 25 , 30 } ; if ( isMinHeap ( level ) ) { Console . WriteLine ( " True " ) ; } else { Console . WriteLine ( " False " ) ; } } }
Minimum delete operations to make all elements of array same | C # program to find minimum number of deletes required to make all elements same . ; Function to get minimum number of elements to be deleted from array to make array elements equal ; Create an hash map and store frequencies of all array elements in it using element as key and frequency as value ; Find maximum frequency among all frequencies . ; To minimize delete operations , we remove all elements but the most frequent element . ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int minDelete ( int [ ] arr , int n ) { Dictionary < int , int > freq = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( freq . ContainsKey ( arr [ i ] ) ) { freq [ arr [ i ] ] = freq [ arr [ i ] ] + 1 ; } else { freq . Add ( arr [ i ] , 1 ) ; } int max_freq = int . MinValue ; foreach ( KeyValuePair < int , int > entry in freq ) max_freq = Math . Max ( max_freq , entry . Value ) ; return n - max_freq + 1 ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 3 , 4 , 4 , 2 , 4 } ; int n = arr . Length ; Console . Write ( minDelete ( arr , n ) ) ; } }
Minimum operation to make all elements equal in array | C # Code For Minimum operation to make all elements equal in array ; function for min operation ; Insert all elements in hash . ; find the max frequency ; return result ; Driver code
using System ; using System . Collections . Generic ; class GFG { public static int minOperation ( int [ ] arr , int n ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( m . ContainsKey ( arr [ i ] ) ) { var val = m [ arr [ i ] ] ; m . Remove ( arr [ i ] ) ; m . Add ( arr [ i ] , val + 1 ) ; } else { m . Add ( arr [ i ] , 1 ) ; } } int max_count = 0 ; HashSet < int > s = new HashSet < int > ( m . Keys ) ; foreach ( int i in s ) if ( max_count < m [ i ] ) max_count = m [ i ] ; return ( n - max_count ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 5 , 2 , 1 , 3 , 2 , 1 } ; int n = arr . Length ; Console . Write ( minOperation ( arr , n ) ) ; } }
Maximum distance between two occurrences of same element in array | C # program to find maximum distance between two same occurrences of a number . ; Function to find maximum distance between equal elements ; Used to store element to first index mapping ; Traverse elements and find maximum distance between same occurrences with the help of map . ; If this is first occurrence of element , insert its index in map ; Else update max distance ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int maxDistance ( int [ ] arr , int n ) { Dictionary < int , int > map = new Dictionary < int , int > ( ) ; int max_dist = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! map . ContainsKey ( arr [ i ] ) ) map . Add ( arr [ i ] , i ) ; else max_dist = Math . Max ( max_dist , i - map [ arr [ i ] ] ) ; } return max_dist ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 1 , 2 , 1 , 4 , 5 , 8 , 6 , 7 , 4 , 2 } ; int n = arr . Length ; Console . WriteLine ( maxDistance ( arr , n ) ) ; } }
Check if a given array contains duplicate elements within k distance from each other | C # program to Check if a given array contains duplicate elements within k distance from each other ; Creates an empty hashset ; Traverse the input array ; If already present n hash , then we found a duplicate within k distance ; Add this item to hashset ; Remove the k + 1 distant item ; Driver code
using System ; using System . Collections . Generic ; class GFG { static bool checkDuplicatesWithinK ( int [ ] arr , int k ) { HashSet < int > set = new HashSet < int > ( ) ; for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( set . Contains ( arr [ i ] ) ) return true ; set . Add ( arr [ i ] ) ; if ( i >= k ) set . Remove ( arr [ i - k ] ) ; } return false ; } public static void Main ( String [ ] args ) { int [ ] arr = { 10 , 5 , 3 , 4 , 3 , 5 , 6 } ; if ( checkDuplicatesWithinK ( arr , 3 ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Find duplicates in a given array when elements are not limited to a range | C # implementation of the above approach ; Function to find the Duplicates , if duplicate occurs 2 times or more than 2 times in array so , it will print duplicate value only once at output ; Initialize ifPresent as false ; ArrayList to store the output ; Checking if element is present in the ArrayList or not if present then break ; If element is not present in the ArrayList then add it to ArrayList and make ifPresent at true ; If duplicates is present then print ArrayList ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void findDuplicates ( int [ ] arr , int len ) { bool ifPresent = false ; List < int > al = new List < int > ( ) ; for ( int i = 0 ; i < len - 1 ; i ++ ) { for ( int j = i + 1 ; j < len ; j ++ ) { if ( arr [ i ] == arr [ j ] ) { if ( al . Contains ( arr [ i ] ) ) { break ; } else { al . Add ( arr [ i ] ) ; ifPresent = true ; } } } } if ( ifPresent == true ) { Console . Write ( " [ " + al [ 0 ] + " , ▁ " ) ; for ( int i = 1 ; i < al . Count - 1 ; i ++ ) { Console . Write ( al [ i ] + " , ▁ " ) ; } Console . Write ( al [ al . Count - 1 ] + " ] " ) ; } else { Console . Write ( " No ▁ duplicates ▁ present ▁ in ▁ arrays " ) ; } } static void Main ( ) { int [ ] arr = { 12 , 11 , 40 , 12 , 5 , 6 , 5 , 12 , 11 } ; int n = arr . Length ; findDuplicates ( arr , n ) ; } }
Check if leaf traversal of two Binary Trees is same ? | C # program to check if two Leaf Traversal of Two Binary Trees is same or not ; Binary Tree node ; checks if a given node is leaf or not . ; Returns true of leaf traversal of two trees is same , else false ; Create empty stacks . These stacks are going to be used for iterative traversals . ; Loop until either of two stacks is not empty ; If one of the stacks is empty means other stack has extra leaves so return false ; Push right and left children of temp1 . Note that right child is inserted before left ; same for tree2 ; If one is null and other is not , then return false ; If both are not null and data is not same return false ; If control reaches this point , all leaves are matched ; Driver Code ; Let us create trees in above example 1
using System ; using System . Collections . Generic ; public class Node { public int data ; public Node left , right ; public Node ( int x ) { data = x ; left = right = null ; } public virtual bool Leaf { get { return ( left == null && right == null ) ; } } } class GFG { public static bool isSame ( Node root1 , Node root2 ) { Stack < Node > s1 = new Stack < Node > ( ) ; Stack < Node > s2 = new Stack < Node > ( ) ; s1 . Push ( root1 ) ; s2 . Push ( root2 ) ; while ( s1 . Count > 0 s2 . Count > 0 ) { if ( s1 . Count == 0 s2 . Count == 0 ) { return false ; } Node temp1 = s1 . Pop ( ) ; while ( temp1 != null && ! temp1 . Leaf ) { if ( temp1 . right != null ) { s1 . Push ( temp1 . right ) ; } if ( temp1 . left != null ) { s1 . Push ( temp1 . left ) ; } temp1 = s1 . Pop ( ) ; } Node temp2 = s2 . Pop ( ) ; while ( temp2 != null && ! temp2 . Leaf ) { if ( temp2 . right != null ) { s2 . Push ( temp2 . right ) ; } if ( temp2 . left != null ) { s2 . Push ( temp2 . left ) ; } temp2 = s2 . Pop ( ) ; } if ( temp1 == null && temp2 != null ) { return false ; } if ( temp1 != null && temp2 == null ) { return false ; } if ( temp1 != null && temp2 != null ) { if ( temp1 . data != temp2 . data ) { return false ; } } } return true ; } public static void Main ( string [ ] args ) { Node root1 = new Node ( 1 ) ; root1 . left = new Node ( 2 ) ; root1 . right = new Node ( 3 ) ; root1 . left . left = new Node ( 4 ) ; root1 . right . left = new Node ( 6 ) ; root1 . right . right = new Node ( 7 ) ; Node root2 = new Node ( 0 ) ; root2 . left = new Node ( 1 ) ; root2 . right = new Node ( 5 ) ; root2 . left . right = new Node ( 4 ) ; root2 . right . left = new Node ( 6 ) ; root2 . right . right = new Node ( 7 ) ; if ( isSame ( root1 , root2 ) ) { Console . WriteLine ( " Same " ) ; } else { Console . WriteLine ( " Not ▁ Same " ) ; } } }
Most frequent element in an array | C # program to find the most frequent element in an array ; Sort the array ; find the max frequency using linear traversal ; If last element is most frequent ; Driver code
using System ; class GFG { static int mostFrequent ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int max_count = 1 , res = arr [ 0 ] ; int curr_count = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == arr [ i - 1 ] ) curr_count ++ ; else { if ( curr_count > max_count ) { max_count = curr_count ; res = arr [ i - 1 ] ; } curr_count = 1 ; } } if ( curr_count > max_count ) { max_count = curr_count ; res = arr [ n - 1 ] ; } return res ; } public static void Main ( ) { int [ ] arr = { 1 , 5 , 2 , 1 , 3 , 2 , 1 } ; int n = arr . Length ; Console . WriteLine ( mostFrequent ( arr , n ) ) ; } }
Most frequent element in an array | C # program to find the most frequent element in an array ; Insert all elements in hash ; find max frequency . ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int mostFrequent ( int [ ] arr , int n ) { Dictionary < int , int > hp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int key = arr [ i ] ; if ( hp . ContainsKey ( key ) ) { int freq = hp [ key ] ; freq ++ ; hp [ key ] = freq ; } else hp . Add ( key , 1 ) ; } int min_count = 0 , res = - 1 ; foreach ( KeyValuePair < int , int > pair in hp ) { if ( min_count < pair . Value ) { res = pair . Key ; min_count = pair . Value ; } } return res ; } static void Main ( ) { int [ ] arr = new int [ ] { 1 , 5 , 2 , 1 , 3 , 2 , 1 } ; int n = arr . Length ; Console . Write ( mostFrequent ( arr , n ) ) ; } }
Smallest subarray with all occurrences of a most frequent element | C # implementation to find smallest subarray with all occurrences of a most frequent element ; To store left most occurrence of elements ; To store counts of elements ; To store maximum frequency ; To store length and starting index of smallest result window ; First occurrence of an element , store the index ; increase the frequency of elements ; Find maximum repeated element and store its last occurrence and first occurrence ; length of subsegment ; select subsegment of smallest size ; Print the subsegment with all occurrences of a most frequent element ; Driver code
using System ; using System . Collections . Generic ; class GfG { static void smallestSubsegment ( int [ ] a , int n ) { Dictionary < int , int > left = new Dictionary < int , int > ( ) ; Dictionary < int , int > count = new Dictionary < int , int > ( ) ; int mx = 0 ; int mn = - 1 , strindex = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { int x = a [ i ] ; if ( ! count . ContainsKey ( x ) ) { left . Add ( x , i ) ; count . Add ( x , 1 ) ; } else count [ x ] = count [ x ] + 1 ; if ( count [ x ] > mx ) { mx = count [ x ] ; mn = i - left [ x ] + 1 ; strindex = left [ x ] ; } else if ( ( count [ x ] == mx ) && ( i - left [ x ] + 1 < mn ) ) { mn = i - left [ x ] + 1 ; strindex = left [ x ] ; } } for ( int i = strindex ; i < strindex + mn ; i ++ ) Console . Write ( a [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] A = { 1 , 2 , 2 , 2 , 1 } ; int n = A . Length ; smallestSubsegment ( A , n ) ; } }
Given an array of pairs , find all symmetric pairs in it | C # program to find all symmetric pairs in a given array of pairs ; Print all pairs that have a symmetric counterpart ; Creates an empty hashMap hM ; Traverse through the given array ; First and second elements of current pair ; Look for second element of this pair in hash ; If found and value in hash matches with first element of this pair , we found symmetry ; Else put sec element of this pair in hash ; Driver code
using System ; using System . Collections . Generic ; public class SymmetricPairs { static void findSymPairs ( int [ , ] arr ) { Dictionary < int , int > hM = new Dictionary < int , int > ( ) ; int val = 0 ; for ( int i = 0 ; i < arr . GetLength ( 0 ) ; i ++ ) { int first = arr [ i , 0 ] ; int sec = arr [ i , 1 ] ; if ( hM . ContainsKey ( sec ) ) val = hM [ sec ] ; if ( val != 0 && val == first ) Console . WriteLine ( " ( " + sec + " , ▁ " + first + " ) " ) ; else hM . Add ( first , sec ) ; } } public static void Main ( String [ ] arg ) { int [ , ] arr = new int [ 5 , 2 ] ; arr [ 0 , 0 ] = 11 ; arr [ 0 , 1 ] = 20 ; arr [ 1 , 0 ] = 30 ; arr [ 1 , 1 ] = 40 ; arr [ 2 , 0 ] = 5 ; arr [ 2 , 1 ] = 10 ; arr [ 3 , 0 ] = 40 ; arr [ 3 , 1 ] = 30 ; arr [ 4 , 0 ] = 10 ; arr [ 4 , 1 ] = 5 ; findSymPairs ( arr ) ; } }