codeparrot/xlcost-text-to-code huggingface dataset:
Find the maximum path sum between two leaves of a binary tree | C # program to find maximum path sum between two leaves of a binary tree ; A binary tree node ; An object of Res is passed around so that the same value can be used by multiple recursive calls . ; A utility function to find the maximum sum between any two leaves . This function calculates two values : 1 ) Maximum path sum between two leaves which is stored in res . 2 ) The maximum root to leaf path sum which is returned . If one side of root is empty , then it returns INT_MIN ; Base cases ; Find maximum sum in left and right subtree . Also find maximum root to leaf sums in left and right subtrees and store them in ls and rs ; If both left and right children exist ; Update result if needed ; Return maxium possible value for root being on one side ; If any of the two children is empty , return root sum for root being on one side ; The main function which returns sum of the maximum sum path between two leaves . This function mainly uses maxPathSumUtil ( ) ; Driver program to test above functions
using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class Res { public int val ; } public class BinaryTree { public static Node root ; public virtual int maxPathSumUtil ( Node node , Res res ) { if ( node == null ) { return 0 ; } if ( node . left == null && node . right == null ) { return node . data ; } int ls = maxPathSumUtil ( node . left , res ) ; int rs = maxPathSumUtil ( node . right , res ) ; if ( node . left != null && node . right != null ) { res . val = Math . Max ( res . val , ls + rs + node . data ) ; return Math . Max ( ls , rs ) + node . data ; } return ( node . left == null ) ? rs + node . data : ls + node . data ; } public virtual int maxPathSum ( Node node ) { Res res = new Res ( ) ; res . val = int . MinValue ; maxPathSumUtil ( root , res ) ; return res . val ; } public static void Main ( string [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; BinaryTree . root = new Node ( - 15 ) ; BinaryTree . root . left = new Node ( 5 ) ; BinaryTree . root . right = new Node ( 6 ) ; BinaryTree . root . left . left = new Node ( - 8 ) ; BinaryTree . root . left . right = new Node ( 1 ) ; BinaryTree . root . left . left . left = new Node ( 2 ) ; BinaryTree . root . left . left . right = new Node ( 6 ) ; BinaryTree . root . right . left = new Node ( 3 ) ; BinaryTree . root . right . right = new Node ( 9 ) ; BinaryTree . root . right . right . right = new Node ( 0 ) ; BinaryTree . root . right . right . right . left = new Node ( 4 ) ; BinaryTree . root . right . right . right . right = new Node ( - 1 ) ; BinaryTree . root . right . right . right . right . left = new Node ( 10 ) ; Console . WriteLine ( " Max ▁ pathSum ▁ of ▁ the ▁ given ▁ binary ▁ tree ▁ is ▁ " + tree . maxPathSum ( root ) ) ; } }
Minimum number of swaps required to sort an array | C # program to find minimum number of swaps required to sort an array ; swap function ; indexOf function ; Return the minimum number of swaps required to sort the array ; This is checking whether the current element is at the right place or not ; Swap the current element with the right index so that arr [ 0 ] to arr [ i ] is sorted ; Driver program to test the above function ; Output will be 5
using System ; public class GFG { static void swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } static int indexOf ( int [ ] arr , int ele ) { for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( arr [ i ] == ele ) { return i ; } } return - 1 ; } static int minSwaps ( int [ ] arr , int N ) { int ans = 0 ; int [ ] temp = new int [ N ] ; Array . Copy ( arr , temp , N ) ; Array . Sort ( temp ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] != temp [ i ] ) { ans ++ ; swap ( arr , i , indexOf ( arr , temp [ i ] ) ) ; } } return ans ; } static public void Main ( ) { int [ ] a = { 101 , 758 , 315 , 730 , 472 , 619 , 460 , 479 } ; int n = a . Length ; Console . WriteLine ( minSwaps ( a , n ) ) ; } }
Union and Intersection of two sorted arrays | C # program to find union of two sorted arrays ; Function prints union of arr1 [ ] and arr2 [ ] m is the number of elements in arr1 [ ] n is the number of elements in arr2 [ ] ; Print remaining elements of the larger array ; Driver program to test above function
using System ; class GFG { static int printUnion ( int [ ] arr1 , int [ ] arr2 , int m , int n ) { int i = 0 , j = 0 ; while ( i < m && j < n ) { if ( arr1 [ i ] < arr2 [ j ] ) Console . Write ( arr1 [ i ++ ] + " ▁ " ) ; else if ( arr2 [ j ] < arr1 [ i ] ) Console . Write ( arr2 [ j ++ ] + " ▁ " ) ; else { Console . Write ( arr2 [ j ++ ] + " ▁ " ) ; i ++ ; } } while ( i < m ) Console . Write ( arr1 [ i ++ ] + " ▁ " ) ; while ( j < n ) Console . Write ( arr2 [ j ++ ] + " ▁ " ) ; return 0 ; } public static void Main ( ) { int [ ] arr1 = { 1 , 2 , 4 , 5 , 6 } ; int [ ] arr2 = { 2 , 3 , 5 , 7 } ; int m = arr1 . Length ; int n = arr2 . Length ; printUnion ( arr1 , arr2 , m , n ) ; } }
Check if N can be converted to the form K power K by the given operation | C # implementation to check whether a given number N can be converted to the form K power K by the given operation ; Function to check if a number is converatable ; Check if n is of the form k ^ k ; Check if the subproblem has been solved before ; Iterate through each digit of n ; Check if it is possible to obtain number of given form ; Reduce the number each time ; Store and return the answer to this subproblem ; Fcuntion to check the above method ; Initialise the dp table ; Check if conversion if possible ; Driver code ; Pre store K power K form of numbers Loop till 8 , because 8 ^ 8 > 10 ^ 7
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static HashSet < int > kPowKform = new HashSet < int > ( ) ; static int [ ] dp = new int [ 100005 ] ; static int func ( int n ) { if ( n <= 0 ) return 0 ; if ( kPowKform . Contains ( n ) ) return 1 ; if ( dp [ n ] != - 1 ) return dp [ n ] ; int answer = 0 ; int x = n ; while ( x > 0 ) { int d = x % 10 ; if ( d != 0 ) { if ( func ( n - d * d ) != 0 ) { answer = 1 ; break ; } } x /= 10 ; } dp [ n ] = answer ; return answer ; } static void canBeConverted ( int n ) { Array . Fill ( dp , - 1 ) ; if ( func ( n ) != 0 ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } public static void Main ( string [ ] args ) { int N = 13 ; for ( int i = 1 ; i <= 8 ; i ++ ) { int val = 1 ; for ( int j = 1 ; j <= i ; j ++ ) val *= i ; kPowKform . Add ( val ) ; } canBeConverted ( N ) ; } }
Union and Intersection of two sorted arrays | C # program to find union of two sorted arrays ( Handling Duplicates ) ; Taking max element present in either array ; Finding elements from 1 st array ( non duplicates only ) . Using another array for storing union elements of both arrays Assuming max element present in array is not more than 10 ^ 7 ; First element is always present in final answer ; Incrementing the First element ' s ▁ ▁ count ▁ in ▁ it ' s corresponding index in newtable ; Starting traversing the first array from 1 st index till last ; Checking whether current element is not equal to it 's previous element ; Finding only non common elements from 2 nd array ; By checking whether it 's already  present in newtable or not ; Driver Code
using System ; class GFG { static void UnionArray ( int [ ] arr1 , int [ ] arr2 ) { int m = arr1 [ arr1 . Length - 1 ] ; int n = arr2 [ arr2 . Length - 1 ] ; int ans = 0 ; if ( m > n ) ans = m ; else ans = n ; int [ ] newtable = new int [ ans + 1 ] ; Console . Write ( arr1 [ 0 ] + " ▁ " ) ; ++ newtable [ arr1 [ 0 ] ] ; for ( int i = 1 ; i < arr1 . Length ; i ++ ) { if ( arr1 [ i ] != arr1 [ i - 1 ] ) { Console . Write ( arr1 [ i ] + " ▁ " ) ; ++ newtable [ arr1 [ i ] ] ; } } for ( int j = 0 ; j < arr2 . Length ; j ++ ) { if ( newtable [ arr2 [ j ] ] == 0 ) { Console . Write ( arr2 [ j ] + " ▁ " ) ; ++ newtable [ arr2 [ j ] ] ; } } } public static void Main ( ) { int [ ] arr1 = { 1 , 2 , 2 , 2 , 3 } ; int [ ] arr2 = { 2 , 3 , 4 , 5 } ; UnionArray ( arr1 , arr2 ) ; } }
Union and Intersection of two sorted arrays | C # program to find Intersection of two sorted arrays ; Function prints Intersection of arr1 [ ] and arr2 [ ] m is the number of elements in arr1 [ ] n is the number of elements in arr2 [ ] ; driver code ; Function calling
using System ; class GFG { static void printIntersection ( int [ ] arr1 , int [ ] arr2 , int m , int n ) { int i = 0 , j = 0 ; while ( i < m && j < n ) { if ( arr1 [ i ] < arr2 [ j ] ) i ++ ; else if ( arr2 [ j ] < arr1 [ i ] ) j ++ ; else { Console . Write ( arr2 [ j ++ ] + " ▁ " ) ; i ++ ; } } } public static void Main ( ) { int [ ] arr1 = { 1 , 2 , 4 , 5 , 6 } ; int [ ] arr2 = { 2 , 3 , 5 , 7 } ; int m = arr1 . Length ; int n = arr2 . Length ; printIntersection ( arr1 , arr2 , m , n ) ; } }
Find the Nth Pure number | C # program to find the Nth pure number ; Function to check if it is a power of 2 or not ; if a number belongs to 4 series it should lie between 2 ^ blocks - 1 to 2 ^ blocks + 2 ^ ( blocks - 1 ) - 1 ; Method to find pure number ; Iterate from 1 to N ; Check if number is power of two ; Distance to previous block numbers ; Distance to previous block numbers ; Driver Code ; Create an object of the class ; Function call to find the Nth pure number
using System ; class PureNumbers { public bool isPowerOfTwo ( int N ) { double number = Math . Log ( N ) / Math . Log ( 2 ) ; int checker = ( int ) number ; return number - checker == 0 ; } public bool isSeriesFour ( int N , int digits ) { int upperBound = ( int ) ( Math . Pow ( 2 , digits ) + Math . Pow ( 2 , digits - 1 ) - 1 ) ; int lowerBound = ( int ) ( Math . Pow ( 2 , digits ) - 1 ) ; return ( N >= lowerBound ) && ( N < upperBound ) ; } public string getPureNumber ( int N ) { string [ ] numbers = new string [ N + 1 ] ; numbers [ 0 ] = " " ; int blocks = 0 ; int displacement = 0 ; for ( int i = 1 ; i < N + 1 ; i ++ ) { if ( isPowerOfTwo ( i + 1 ) ) { blocks = blocks + 1 ; } if ( isSeriesFour ( i , blocks ) ) { displacement = ( int ) Math . Pow ( 2 , blocks - 1 ) ; numbers [ i ] = "4" + numbers [ i - displacement ] + "4" ; } else { displacement = ( int ) Math . Pow ( 2 , blocks ) ; numbers [ i ] = "5" + numbers [ i - displacement ] + "5" ; } } return numbers [ N ] ; } public static void Main ( ) { int N = 5 ; PureNumbers ob = new PureNumbers ( ) ; string pure = ob . getPureNumber ( N ) ; Console . Write ( pure ) ; } }
Count numbers less than N containing digits from the given set : Digit DP | C # implementation to find the count of numbers possible less than N , such that every digit is from the given set of digits ; Function to convert integer into the String ; Recursive function to find the count of numbers possible less than N , such that every digit is from the given set of digits ; Base case ; Condition when the subproblem is computed previously ; Condition when the number chosen till now is definietly smaller than the given number N ; Loop to traverse all the digits of the given set ; Loop to traverse all the digits from the given set ; Store the solution for current subproblem ; Function to count the numbers less then N from given set of digits ; Converting the number to String ; Initially no subproblem is solved till now ; Find the solution of all the number equal to the length of the given number N ; Loop to find the number less in in the length of the given number ; Driver Code ; Function Call
using System ; class GFG { static int [ , ] dp = new int [ 15 , 2 ] ; static String convertToString ( int num ) { return String . Join ( " " , num ) ; } static int calculate ( int pos , int tight , int [ ] D , int sz , String num ) { if ( pos == num . Length ) return 1 ; if ( dp [ pos , tight ] != - 1 ) return dp [ pos , tight ] ; int val = 0 ; if ( tight == 0 ) { for ( int i = 0 ; i < sz ; i ++ ) { if ( D [ i ] < ( num [ pos ] - '0' ) ) { val += calculate ( pos + 1 , 1 , D , sz , num ) ; } else if ( D [ i ] == num [ pos ] - '0' ) val += calculate ( pos + 1 , tight , D , sz , num ) ; } } else { for ( int i = 0 ; i < sz ; i ++ ) { val += calculate ( pos + 1 , tight , D , sz , num ) ; } } return dp [ pos , tight ] = val ; } static int countNumbers ( int [ ] D , int N , int sz ) { String num = convertToString ( N ) ; int len = num . Length ; for ( int i = 0 ; i < 15 ; i ++ ) for ( int j = 0 ; j < 2 ; j ++ ) dp [ i , j ] = - 1 ; int ans = calculate ( 0 , 0 , D , sz , num ) ; for ( int i = 1 ; i < len ; i ++ ) ans += calculate ( i , 1 , D , sz , num ) ; return ans ; } public static void Main ( String [ ] args ) { int sz = 3 ; int [ ] D = { 1 , 4 , 9 } ; int N = 10 ; Console . Write ( countNumbers ( D , N , sz ) ) ; } }
Maximum sum of elements divisible by K from the given array |  ; Function to return the maximum sum divisible by k from elements of v ; check if sum of elements excluding the current one is divisible by k ; check if sum of elements including the current one is divisible by k ; Store the maximum ; Driver code
using System ; class GFG { static int [ , ] dp = new int [ 1001 , 1001 ] ; static int find_max ( int i , int sum , int [ ] v , int k ) { if ( i == v . Length ) return 0 ; if ( dp [ i , sum ] != - 1 ) return dp [ i , sum ] ; int ans = 0 ; if ( ( sum + find_max ( i + 1 , sum , v , k ) ) % k == 0 ) ans = find_max ( i + 1 , sum , v , k ) ; if ( ( sum + v [ i ] + find_max ( i + 1 , ( sum + v [ i ] ) % k , v , k ) ) % k == 0 ) ans = Math . Max ( ans , v [ i ] + find_max ( i + 1 , ( sum + v [ i ] ) % k , v , k ) ) ; return dp [ i , sum ] = ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 43 , 1 , 17 , 26 , 15 } ; int k = 16 ; for ( int i = 0 ; i < 1001 ; i ++ ) for ( int j = 0 ; j < 1001 ; j ++ ) dp [ i , j ] = - 1 ; Console . Write ( find_max ( 0 , 0 , arr , k ) ) ; } }
Find Union and Intersection of two unsorted arrays | A C # program to print union and intersection of two unsorted arrays ; Prints union of arr1 [ 0. . m - 1 ] and arr2 [ 0. . n - 1 ] ; Before finding union , make sure arr1 [ 0. . m - 1 ] is smaller ; Now arr1 [ ] is smaller Sort the first array and print its elements ( these two steps can be swapped as order in output is not important ) ; Search every element of bigger array in smaller array and print the element if not found ; Prints intersection of arr1 [ 0. . m - 1 ] and arr2 [ 0. . n - 1 ] ; Before finding intersection , make sure arr1 [ 0. . m - 1 ] is smaller ; Now arr1 [ ] is smaller Sort smaller array arr1 [ 0. . m - 1 ] ; Search every element of bigger array in smaller array and print the element if found ; A recursive binary search function . It returns location of x in given array arr [ l . . r ] is present , otherwise - 1 ; If the element is present at the middle itself ; If element is smaller than mid , then it can only be present in left subarray ; Else the element can only be present in right subarray ; We reach here when element is not present in array ; Driver Code ; Function call
using System ; class GFG { static void printUnion ( int [ ] arr1 , int [ ] arr2 , int m , int n ) { if ( m > n ) { int [ ] tempp = arr1 ; arr1 = arr2 ; arr2 = tempp ; int temp = m ; m = n ; n = temp ; } Array . Sort ( arr1 ) ; for ( int i = 0 ; i < m ; i ++ ) Console . Write ( arr1 [ i ] + " ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( binarySearch ( arr1 , 0 , m - 1 , arr2 [ i ] ) == - 1 ) Console . Write ( arr2 [ i ] + " ▁ " ) ; } } static void printIntersection ( int [ ] arr1 , int [ ] arr2 , int m , int n ) { if ( m > n ) { int [ ] tempp = arr1 ; arr1 = arr2 ; arr2 = tempp ; int temp = m ; m = n ; n = temp ; } Array . Sort ( arr1 ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( binarySearch ( arr1 , 0 , m - 1 , arr2 [ i ] ) != - 1 ) Console . Write ( arr2 [ i ] + " ▁ " ) ; } } static int binarySearch ( int [ ] arr , int l , int r , int x ) { if ( r >= l ) { int mid = l + ( r - l ) / 2 ; if ( arr [ mid ] == x ) return mid ; if ( arr [ mid ] > x ) return binarySearch ( arr , l , mid - 1 , x ) ; return binarySearch ( arr , mid + 1 , r , x ) ; } return - 1 ; } static public void Main ( ) { int [ ] arr1 = { 7 , 1 , 5 , 2 , 3 , 6 } ; int [ ] arr2 = { 3 , 8 , 6 , 20 , 7 } ; int m = arr1 . Length ; int n = arr2 . Length ; Console . WriteLine ( " Union ▁ of ▁ two ▁ arrays ▁ is ▁ " ) ; printUnion ( arr1 , arr2 , m , n ) ; Console . WriteLine ( " " ) ; Console . WriteLine ( " Intersection ▁ of ▁ two ▁ arrays ▁ is ▁ " ) ; printIntersection ( arr1 , arr2 , m , n ) ; } }
Find the maximum sum leaf to root path in a Binary Tree | C # program to find maximum sum leaf to root path in Binary Tree ; A Binary Tree node ; A wrapper class is used so that max_no can be updated among function calls . ; A utility function that prints all nodes on the path from root to target_leaf ; base case ; return true if this node is the target_leaf or target leaf is present in one of its descendants ; This function Sets the target_leaf_ref to refer the leaf node of the maximum path sum . Also , returns the max_sum using max_sum_ref ; Update current sum to hold sum of nodes on path from root to this node ; If this is a leaf node and path to this node has maximum sum so far , the n make this node target_leaf ; If this is not a leaf node , then recur down to find the target_leaf ; Returns the maximum sum and prints the nodes on max sum path ; base case ; find the target leaf and maximum sum ; print the path from root to the target leaf ; return maximum sum ; driver function to test the above functions
using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class Maximum { public int max_no = int . MinValue ; } public class BinaryTree { public Node root ; public Maximum max = new Maximum ( ) ; public Node target_leaf = null ; public virtual bool printPath ( Node node , Node target_leaf ) { if ( node == null ) { return false ; } if ( node == target_leaf || printPath ( node . left , target_leaf ) || printPath ( node . right , target_leaf ) ) { Console . Write ( node . data + " ▁ " ) ; return true ; } return false ; } public virtual void getTargetLeaf ( Node node , Maximum max_sum_ref , int curr_sum ) { if ( node == null ) { return ; } curr_sum = curr_sum + node . data ; if ( node . left == null && node . right == null ) { if ( curr_sum > max_sum_ref . max_no ) { max_sum_ref . max_no = curr_sum ; target_leaf = node ; } } getTargetLeaf ( node . left , max_sum_ref , curr_sum ) ; getTargetLeaf ( node . right , max_sum_ref , curr_sum ) ; } public virtual int maxSumPath ( ) { if ( root == null ) { return 0 ; } getTargetLeaf ( root , max , 0 ) ; printPath ( root , target_leaf ) ; return max . max_no ; } public static void Main ( string [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 10 ) ; tree . root . left = new Node ( - 2 ) ; tree . root . right = new Node ( 7 ) ; tree . root . left . left = new Node ( 8 ) ; tree . root . left . right = new Node ( - 4 ) ; Console . WriteLine ( " Following ▁ are ▁ the ▁ nodes ▁ " + " on ▁ maximum ▁ sum ▁ path " ) ; int sum = tree . maxSumPath ( ) ; Console . WriteLine ( " " ) ; Console . WriteLine ( " Sum ▁ of ▁ nodes ▁ is ▁ : ▁ " + sum ) ; } }
Find Union and Intersection of two unsorted arrays | C # code to find intersection when elements may not be distinct ; Function to find intersection ; when both are equal ; Driver Code ; sort ; Function call
using System ; class GFG { static void intersection ( int [ ] a , int [ ] b , int n , int m ) { int i = 0 , j = 0 ; while ( i < n && j < m ) { if ( a [ i ] > b [ j ] ) { j ++ ; } else if ( b [ j ] > a [ i ] ) { i ++ ; } else { Console . Write ( a [ i ] + " ▁ " ) ; i ++ ; j ++ ; } } } public static void Main ( ) { int [ ] a = { 1 , 3 , 2 , 3 , 4 , 5 , 5 , 6 } ; int [ ] b = { 3 , 3 , 5 } ; int n = a . Length ; int m = b . Length ; Array . Sort ( a ) ; Array . Sort ( b ) ; intersection ( a , b , n , m ) ; } }
Count of 1 's in any path in a Binary Tree | C # implementation of the above approach ; A binary tree node ; A utility function to allocate a new node ; This function updates overall count of 1 in ' res ' And returns count 1 s going through root . ; Base Case ; l and r store count of 1 s going through left and right child of root respectively ; maxCount represents the count of 1 s when the Node under consideration is the root of the maxCount path and no ancestors of the root are there in maxCount path ; if the value at node is 1 then its count will be considered including the leftCount and the rightCount ; Store the Maximum Result . ; if the value at node is 1 then its count will be considered including the maximum of leftCount or the rightCount ; Returns maximum count of 1 in any path in tree with given root ; Initialize result ; Compute and return result ; Driver program
using System ; class GFG { class Node { public int data ; public Node left , right ; } ; static int res ; static Node newNode ( int data ) { Node newNode = new Node ( ) ; newNode . data = data ; newNode . left = newNode . right = null ; return ( newNode ) ; } static int countUntil ( Node root ) { if ( root == null ) return 0 ; int l = countUntil ( root . left ) ; int r = countUntil ( root . right ) ; int maxCount ; if ( root . data == 1 ) maxCount = l + r + 1 ; else maxCount = l + r ; res = Math . Max ( res , maxCount ) ; if ( root . data == 1 ) return Math . Max ( l , r ) + 1 ; else return Math . Max ( l , r ) ; } static int findMaxCount ( Node root ) { res = int . MinValue ; countUntil ( root ) ; return res ; } public static void Main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 0 ) ; root . right = newNode ( 1 ) ; root . left . left = newNode ( 1 ) ; root . left . right = newNode ( 1 ) ; root . left . right . left = newNode ( 1 ) ; root . left . right . right = newNode ( 0 ) ; Console . Write ( findMaxCount ( root ) ) ; } }
Find Union and Intersection of two unsorted arrays | C # program to find union and intersection using Hashing ; Prints union of arr1 [ 0. . m - 1 ] and arr2 [ 0. . n - 1 ] ; Prints intersection of arr1 [ 0. . m - 1 ] and arr2 [ 0. . n - 1 ] ; Driver Code ; Function call
using System ; using System . Linq ; using System . Collections . Generic ; class GFG { static void printUnion ( int [ ] arr1 , int [ ] arr2 ) { HashSet < int > hs = new HashSet < int > ( ) ; for ( int i = 0 ; i < arr1 . Length ; i ++ ) hs . Add ( arr1 [ i ] ) ; for ( int i = 0 ; i < arr2 . Length ; i ++ ) hs . Add ( arr2 [ i ] ) ; Console . WriteLine ( string . Join ( " , ▁ " , hs ) ) ; } static void printIntersection ( int [ ] arr1 , int [ ] arr2 ) { HashSet < int > hs = new HashSet < int > ( ) ; for ( int i = 0 ; i < arr1 . Length ; i ++ ) hs . Add ( arr1 [ i ] ) ; for ( int i = 0 ; i < arr2 . Length ; i ++ ) if ( hs . Contains ( arr2 [ i ] ) ) Console . Write ( arr2 [ i ] + " ▁ " ) ; } static void Main ( ) { int [ ] arr1 = { 7 , 1 , 5 , 2 , 3 , 6 } ; int [ ] arr2 = { 3 , 8 , 6 , 20 , 7 } ; Console . WriteLine ( " Union ▁ of ▁ two ▁ arrays ▁ is ▁ : ▁ " ) ; printUnion ( arr1 , arr2 ) ; Console . WriteLine ( " STRNEWLINE Intersection ▁ of ▁ two ▁ arrays ▁ is ▁ : ▁ " ) ; printIntersection ( arr1 , arr2 ) ; } }