codeparrot/xlcost-text-to-code huggingface dataset:
Find Minimum Depth of a Binary Tree | C # program to find minimum depth of a given Binary Tree ; A binary Tree node ; A queue item ( Stores pointer tonode and an integer ) ; Iterative method to find minimum depth of Binary Tree ; Corner Case ; Create an empty queue for level order traversal ; Enqueue Root and initialize depth as 1 ; Do level order traversal ; Remove the front queue item ; Get details of the remove item ; If this is the first leaf node seen so far . Then return its depth as answer ; If left subtree is not null , add it to queue ; If right subtree is not null , add it to queue ; Utility function to create a new tree Node ; Driver Code ; Let us create binary tree shown in above diagram
using System ; using System . Collections . Generic ; class GFG { public class Node { public int data ; public Node left , right ; } public class qItem { public Node node ; public int depth ; public qItem ( Node node , int depth ) { this . node = node ; this . depth = depth ; } } static int minDepth ( Node root ) { if ( root == null ) return 0 ; Queue < qItem > q = new Queue < qItem > ( ) ; qItem qi = new qItem ( root , 1 ) ; q . Enqueue ( qi ) ; while ( q . Count != 0 ) { qi = q . Peek ( ) ; q . Dequeue ( ) ; Node node = qi . node ; int depth = qi . depth ; if ( node . left == null && node . right == null ) return depth ; if ( node . left != null ) { qi . node = node . left ; qi . depth = depth + 1 ; q . Enqueue ( qi ) ; } if ( node . right != null ) { qi . node = node . right ; qi . depth = depth + 1 ; q . Enqueue ( qi ) ; } } return 0 ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } public static void Main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; Console . WriteLine ( minDepth ( root ) ) ; } }
Maximum Manhattan distance between a distinct pair from N coordinates | C # program for the above approach ; Pair class ; Function to calculate the maximum Manhattan distance ; Lists to store maximum and minimum of all the four forms ; Sorting both the Lists ; Driver Code ; Given Co - ordinates ; Function call
using System ; using System . Collections . Generic ; class GFG { class Pair { public int x ; public int y ; public Pair ( int x , int y ) { this . x = x ; this . y = y ; } } static void MaxDist ( List < Pair > A , int N ) { List < int > V = new List < int > ( ) ; List < int > V1 = new List < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { V . Add ( A [ i ] . x + A [ i ] . y ) ; V1 . Add ( A [ i ] . x - A [ i ] . y ) ; } V . Sort ( ) ; V1 . Sort ( ) ; int maximum = Math . Max ( ( V [ V . Count - 1 ] - V [ 0 ] ) , ( V1 [ V1 . Count - 1 ] - V1 [ 0 ] ) ) ; Console . WriteLine ( maximum ) ; } public static void Main ( String [ ] args ) { int n = 3 ; List < Pair > al = new List < Pair > ( ) ; Pair p1 = new Pair ( 1 , 2 ) ; al . Add ( p1 ) ; Pair p2 = new Pair ( 2 , 3 ) ; al . Add ( p2 ) ; Pair p3 = new Pair ( 3 , 4 ) ; al . Add ( p3 ) ; MaxDist ( al , n ) ; } }
Maximum distance between two points in coordinate plane using Rotating Caliper 's Method |  ; Function calculates distance between two points ; Function to find the maximum distance between any two points ; Iterate over all possible pairs ; Update max ; Return actual distance ; Driver code ; Given points ; Function Call
using System ; class GFG { static long dist ( Tuple < int , int > p1 , Tuple < int , int > p2 ) { long x0 = p1 . Item1 - p2 . Item1 ; long y0 = p1 . Item2 - p2 . Item2 ; return x0 * x0 + y0 * y0 ; } static double maxDist ( Tuple < int , int > [ ] p ) { int n = p . Length ; double max = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { max = Math . Max ( max , dist ( p [ i ] , p [ j ] ) ) ; } } return Math . Sqrt ( max ) ; } static void Main ( ) { Tuple < int , int > [ ] p = { Tuple . Create ( 4 , 0 ) , Tuple . Create ( 0 , 2 ) , Tuple . Create ( - 1 , - 7 ) , Tuple . Create ( 1 , 10 ) , Tuple . Create ( 2 , - 3 ) , } ; Console . WriteLine ( maxDist ( p ) ) ; } }
Reorder an array such that sum of left half is not equal to sum of right half | C # code for the above approach ; Function to print the required reordering of array if possible ; Sort the array in increasing order ; If all elements are equal , then it is not possible ; Else print the sorted array arr [ ] ; Driver code ; Given array ; Function call
using System ; class GFG { public static void printArr ( int [ ] arr , int n ) { Array . Sort ( arr ) ; if ( arr [ 0 ] == arr [ n - 1 ] ) { Console . Write ( " No STRNEWLINE " ) ; } else { Console . Write ( " Yes STRNEWLINE " ) ; for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } } public static void Main ( ) { int [ ] arr = new int [ 6 ] { 1 , 2 , 2 , 1 , 3 , 1 } ; int N = arr . Length ; printArr ( arr , N ) ; } }
Find Minimum Depth of a Binary Tree | C # implementation to find minimum depth of a given Binary tree ; Class containing left and right child of current Node and key value ; Root of the Binary Tree ; Function to calculate the minimum depth of the tree ; Driver code
using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class MinimumTreeHeight { Node root ; int minimumDepth ( ) { return minimumDepth ( root , 0 ) ; } int minimumDepth ( Node root , int level ) { if ( root == null ) return level ; level ++ ; return Math . Min ( minimumDepth ( root . left , level ) , minimumDepth ( root . right , level ) ) ; } public static void Main ( String [ ] args ) { MinimumTreeHeight tree = new MinimumTreeHeight ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; Console . WriteLine ( " The ▁ minimum ▁ depth ▁ of ▁ " + " binary ▁ tree ▁ is ▁ : ▁ " + tree . minimumDepth ( ) ) ; } }
Maximize Array sum by swapping at most K elements with another array | C # implementation to find maximum sum of array A by swapping at most K elements with array B ; Function to find the maximum sum ; If element of array a is smaller than that of array b , swap them . ; Find sum of resultant array ; Driver Code
using System ; class GFG { static void maximumSum ( int [ ] a , int [ ] b , int k , int n ) { int i , j ; Array . Sort ( a ) ; Array . Sort ( b ) ; for ( i = 0 , j = n - 1 ; i < k ; i ++ , j -- ) { if ( a [ i ] < b [ j ] ) { int temp = a [ i ] ; a [ i ] = b [ j ] ; b [ j ] = temp ; } else break ; } int sum = 0 ; for ( i = 0 ; i < n ; i ++ ) sum += a [ i ] ; Console . Write ( sum + " STRNEWLINE " ) ; } public static void Main ( ) { int K = 1 ; int [ ] A = { 2 , 3 , 4 } ; int [ ] B = { 6 , 8 , 5 } ; int N = A . Length ; maximumSum ( A , B , K , N ) ; } }
Maximize distinct elements by incrementing / decrementing an element or keeping it same | C # program to maximize the count of distinct element ; Function that maximize the count of distinct element ; Sort thr array ; Keeping track of previous change ; Decrement is possible ; Remain as it is ; Increment is possible ; Driver Code
using System ; class GFG { static int max_dist_ele ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int ans = 0 ; int prev = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prev < ( arr [ i ] - 1 ) ) { ans ++ ; prev = arr [ i ] - 1 ; } else if ( prev < ( arr [ i ] ) ) { ans ++ ; prev = arr [ i ] ; } else if ( prev < ( arr [ i ] + 1 ) ) { ans ++ ; prev = arr [ i ] + 1 ; } } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 1 , 8 , 8 , 8 , 9 , 9 } ; int n = arr . Length ; Console . WriteLine ( max_dist_ele ( arr , n ) ) ; } }
Find if array can be sorted by swaps limited to multiples of k |  ; CheckSort function To check if array can be sorted ; sortarr is sorted array of arr ; If k = 1 then ( always possible to sort ) swapping can easily give sorted array ; Comparing sortarray with array ; Element at index j must be in j = i + l * k form where i = 0 , 1 , 2 , 3. . . where l = 0 , 1 , 2 , 3 , . . n - 1 ; If element is present then swapped ; If element of sorted array does not found in its sequence then flag remain zero that means arr can not be sort after swapping ; If flag is 0 Not possible else Possible ; Driver code ; Size of step ; Array initialized ; Length of arr ; Calling function
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static void CheckSort ( ArrayList arr , int k , int n ) { ArrayList sortarr = new ArrayList ( arr ) ; sortarr . Sort ( ) ; if ( k == 1 ) Console . Write ( " yes " ) ; else { int flag = 0 ; for ( int i = 0 ; i < n ; i ++ ) { flag = 0 ; for ( int j = i ; j < n ; j += k ) { if ( ( int ) sortarr [ i ] == ( int ) arr [ j ] ) { int tmp = ( int ) arr [ i ] ; arr [ i ] = ( int ) arr [ j ] ; arr [ j ] = tmp ; flag = 1 ; break ; } if ( j + k >= n ) { break ; } } if ( flag == 0 ) { break ; } } if ( flag == 0 ) Console . Write ( " Not ▁ possible ▁ to ▁ sort " ) ; else Console . Write ( " Possible ▁ to ▁ sort " ) ; } } public static void Main ( string [ ] args ) { int k = 3 ; ArrayList arr = new ArrayList ( ) { 1 , 5 , 6 , 9 , 2 , 3 , 5 , 9 } ; int n = arr . Count ; CheckSort ( arr , k , n ) ; } }
Replace node with depth in a binary tree | C # program to replace every key value with its depth . ; A tree node structure ; Utility function to create a new Binary Tree node ; Helper function replaces the data with depth Note : Default value of level is 0 for root . ; Base Case ; Replace data with current depth ; A utility function to print inorder traversal of a Binary Tree ; Driver code ; Constructing tree given in the above figure
using System ; public class GfG { public 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 void replaceNode ( Node node , int level ) { if ( node == null ) return ; node . data = level ; replaceNode ( node . left , level + 1 ) ; replaceNode ( node . right , level + 1 ) ; } static void printInorder ( Node node ) { if ( node == null ) return ; printInorder ( node . left ) ; Console . Write ( node . data + " ▁ " ) ; printInorder ( node . right ) ; } public static void Main ( String [ ] args ) { Node root = new Node ( ) ; root = newNode ( 3 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 5 ) ; root . left . left = newNode ( 1 ) ; root . left . right = newNode ( 4 ) ; Console . WriteLine ( " Before ▁ Replacing ▁ Nodes " ) ; printInorder ( root ) ; replaceNode ( root , 0 ) ; Console . WriteLine ( ) ; Console . WriteLine ( " After ▁ Replacing ▁ Nodes " ) ; printInorder ( root ) ; } }
Check whether an array can be made strictly decreasing by modifying at most one element | C # implementation of the approach ; Function that returns true if the array can be made strictly decreasing with at most one change ; To store the number of modifications required to make the array strictly decreasing ; Check whether the last element needs to be modify or not ; Check whether the first element needs to be modify or not ; Loop from 2 nd element to the 2 nd last element ; Check whether arr [ i ] needs to be modified ; Modifying arr [ i ] ; Check if arr [ i ] is equal to any of arr [ i - 1 ] or arr [ i + 1 ] ; If more than 1 modification is required ; Driver code
using System ; class GFG { public static bool check ( int [ ] arr , int n ) { int modify = 0 ; if ( arr [ n - 1 ] >= arr [ n - 2 ] ) { arr [ n - 1 ] = arr [ n - 2 ] - 1 ; modify ++ ; } if ( arr [ 0 ] <= arr [ 1 ] ) { arr [ 0 ] = arr [ 1 ] + 1 ; modify ++ ; } for ( int i = n - 2 ; i > 0 ; i -- ) { if ( ( arr [ i - 1 ] <= arr [ i ] && arr [ i + 1 ] <= arr [ i ] ) || ( arr [ i - 1 ] >= arr [ i ] && arr [ i + 1 ] >= arr [ i ] ) ) { arr [ i ] = ( arr [ i - 1 ] + arr [ i + 1 ] ) / 2 ; modify ++ ; if ( arr [ i ] == arr [ i - 1 ] arr [ i ] == arr [ i + 1 ] ) return false ; } } if ( modify > 1 ) return false ; return true ; } static public void Main ( ) { int [ ] arr = { 10 , 5 , 11 , 3 } ; int n = arr . Length ; if ( check ( arr , n ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Print k different sorted permutations of a given array | C # implementation of the approach ; Utility function to print the original indices of the elements of the array ; Function to print the required permutations ; To keep track of original indices ; Sort the array ; Count the number of swaps that can be made ; Cannot generate 3 permutations ; Print the first permutation ; Find an index to swap and create second permutation ; Print the last permutation ; Driver code ; Function call
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int next_pos = 1 ; public class pair { public int first , second ; public pair ( ) { first = 0 ; second = 0 ; } } class sortHelper : IComparer { int IComparer . Compare ( object a , object b ) { pair first = ( pair ) a ; pair second = ( pair ) b ; return first . first < second . first ? - 1 : 1 ; } } static void printIndices ( int n , pair [ ] a ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( a [ i ] . second + " ▁ " ) ; Console . WriteLine ( ) ; } static void printPermutations ( int n , int [ ] a , int k ) { pair [ ] arr = new pair [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = new pair ( ) ; arr [ i ] . first = a [ i ] ; arr [ i ] . second = i ; } Array . Sort ( arr , new sortHelper ( ) ) ; int count = 1 ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i ] . first == arr [ i - 1 ] . first ) count ++ ; if ( count < k ) { Console . Write ( " - 1" ) ; return ; } for ( int i = 0 ; i < k - 1 ; i ++ ) { printIndices ( n , arr ) ; for ( int j = next_pos ; j < n ; j ++ ) { if ( arr [ j ] . first == arr [ j - 1 ] . first ) { pair t = arr [ j ] ; arr [ j ] = arr [ j - 1 ] ; arr [ j - 1 ] = t ; next_pos = j + 1 ; break ; } } } printIndices ( n , arr ) ; } public static void Main ( string [ ] args ) { int [ ] a = { 1 , 3 , 3 , 1 } ; int n = a . Length ; int k = 3 ; printPermutations ( n , a , k ) ; } }
Maximum width of a binary tree | C # program to calculate width of binary tree ; A binary tree node has data , pointer to left child and a pointer to right child ; Function to get the maximum width of a binary tree ; Get width of each level and compare the width with maximum width so far ; Get width of a given level ; Compute the " height " of a tree -- the number of nodes along the longest path from the root node down to the farthest leaf node . ; compute the height of each subtree ; use the larger one ; Driver code ; Constructed binary tree is : 1 / \ 2 3 / \ \ 4 5 8 / \ 6 7 ; Function call
using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class BinaryTree { public Node root ; public virtual int getMaxWidth ( Node node ) { int maxWidth = 0 ; int width ; int h = height ( node ) ; int i ; for ( i = 1 ; i <= h ; i ++ ) { width = getWidth ( node , i ) ; if ( width > maxWidth ) { maxWidth = width ; } } return maxWidth ; } public virtual int getWidth ( Node node , int level ) { if ( node == null ) { return 0 ; } if ( level == 1 ) { return 1 ; } else if ( level > 1 ) { return getWidth ( node . left , level - 1 ) + getWidth ( node . right , level - 1 ) ; } return 0 ; } public virtual int height ( Node node ) { if ( node == null ) { return 0 ; } else { int lHeight = height ( node . left ) ; int rHeight = height ( node . right ) ; return ( lHeight > rHeight ) ? ( lHeight + 1 ) : ( rHeight + 1 ) ; } } public static void Main ( string [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . right = new Node ( 8 ) ; tree . root . right . right . left = new Node ( 6 ) ; tree . root . right . right . right = new Node ( 7 ) ; Console . WriteLine ( " Maximum ▁ width ▁ is ▁ " + tree . getMaxWidth ( tree . root ) ) ; } }
Count distinct elements in an array | C # Program to count Unique elements in Array ; This function prints all distinct elements ; Creates an empty hashset ; Traverse the input array ; If not present , then put it in hashtable and increment result ; Driver code
using System ; using System . Collections . Generic ; class GFG { public static int countDistinct ( int [ ] arr , int n ) { HashSet < int > hs = new HashSet < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { hs . Add ( arr [ i ] ) ; } return hs . Count ; } public static void Main ( ) { int [ ] arr = new int [ ] { 6 , 10 , 5 , 4 , 9 , 120 , 4 , 6 , 10 } ; Console . WriteLine ( countDistinct ( arr , arr . Length ) ) ; } }
Rearrange an array to maximize i * arr [ i ] | C # program for the Optimal Solution ; Function to calculate the maximum points earned by making an optimal selection on the given array ; Sorting the array ; Variable to store the total points earned ; Driver code
using System ; public class GFG { static int findOptimalSolution ( int [ ] a , int N ) { Array . Sort ( a ) ; int points = 0 ; for ( int i = 0 ; i < N ; i ++ ) { points += a [ i ] * i ; } return points ; } static public void Main ( ) { int [ ] a = { 1 , 4 , 2 , 3 , 9 } ; int N = a . Length ; Console . WriteLine ( findOptimalSolution ( a , N ) ) ; } }
Minimum number of towers required such that every house is in the range of at least one tower | C # implementation of above approach ; Function to count the number of tower ; first we sort the house numbers ; for count number of towers ; for iterate all houses ; count number of towers ; find find the middle location ; traverse till middle location ; this is point to middle house where we insert the tower ; now find the last location ; traverse till last house of the range ; return the number of tower ; Driver code ; given elements ; print number of towers
using System ; public class Improve { static int number_of_tower ( int [ ] house , int range , int n ) { Array . Sort ( house ) ; int numOfTower = 0 ; int i = 0 ; while ( i < n ) { numOfTower ++ ; int loc = house [ i ] + range ; while ( i < n && house [ i ] <= loc ) i ++ ; -- i ; loc = house [ i ] + range ; while ( i < n && house [ i ] <= loc ) i ++ ; } return numOfTower ; } public static void Main ( ) { int [ ] house = { 7 , 2 , 4 , 6 , 5 , 9 , 12 , 11 } ; int range = 2 ; int n = house . Length ; Console . WriteLine ( number_of_tower ( house , range , n ) ) ; } }
Check if the characters of a given string are in alphabetical order | C # implementation of above approach import Arrays class ; Function that checks whether the string is in alphabetical order or not ; length of the string ; create a character array of the length of the string ; assign the string to character array ; sort the character array ; check if the character array is equal to the string or not ; Driver Code ; check whether the string is in alphabetical order or not
using System ; public class GFG { static bool isAlphabaticOrder ( String s ) { int n = s . Length ; char [ ] c = new char [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { c [ i ] = s [ i ] ; } Array . Sort ( c ) ; for ( int i = 0 ; i < n ; i ++ ) if ( c [ i ] != s [ i ] ) return false ; return true ; } static public void Main ( ) { String s = " aabbbcc " ; if ( isAlphabaticOrder ( s ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Sort first k values in ascending order and remaining n | C # program to sort first k elements in increasing order and remaining n - k elements in decreasing ; function to sort the array ; Sort first k elements in ascending order ; Sort remaining n - k elements in descending order ; Driver code ; Our arr contains 8 elements
using System ; public class SortExample { static void printOrder ( int [ ] arr , int k ) { int n = arr . Length ; Array . Sort ( arr , 0 , k ) ; Array . Sort ( arr , k , n - k ) ; Array . Reverse ( arr , k , n - k ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 4 , 6 , 2 , 1 , 3 , 8 , 9 , - 1 } ; int k = 4 ; printOrder ( arr , k ) ; Console . Write ( " { 0 } " , String . Join ( " ▁ " , arr ) ) ; } }
Maximum width of a binary tree | C # program to calculate maximum width of a binary tree using queue ; A binary tree node has data , pointer to left child and a pointer to right child ; Function to find the maximum width of the tree using level order traversal ; Base case ; Initialize result ; Do Level order traversal keeping track of number of nodes at every level ; Get the size of queue when the level order traversal for one level finishes ; Update the maximum node count value ; Iterate for all the nodes in the queue currently ; Dequeue an node from queue ; Enqueue left and right children of dequeued node ; Driver code ; Function call
using System ; using System . Collections . Generic ; public class maxwidthusingqueue { public class node { public int data ; public node left , right ; public node ( int data ) { this . data = data ; } } static int maxwidth ( node root ) { if ( root == null ) return 0 ; int maxwidth = 0 ; Queue < node > q = new Queue < node > ( ) ; q . Enqueue ( root ) ; while ( q . Count != 0 ) { int count = q . Count ; maxwidth = Math . Max ( maxwidth , count ) ; while ( count -- > 0 ) { node temp = q . Dequeue ( ) ; if ( temp . left != null ) { q . Enqueue ( temp . left ) ; } if ( temp . right != null ) { q . Enqueue ( temp . right ) ; } } } return maxwidth ; } public static void Main ( String [ ] args ) { node root = new node ( 1 ) ; root . left = new node ( 2 ) ; root . right = new node ( 3 ) ; root . left . left = new node ( 4 ) ; root . left . right = new node ( 5 ) ; root . right . right = new node ( 8 ) ; root . right . right . left = new node ( 6 ) ; root . right . right . right = new node ( 7 ) ; Console . WriteLine ( " Maximum ▁ width ▁ = ▁ " + maxwidth ( root ) ) ; } }
Merging and Sorting Two Unsorted Stacks | C # program to merge to unsorted stacks into a third stack in sorted way . ; Sorts input stack and returns sorted stack . ; pop out the first element ; while temporary stack is not empty and top of stack is greater than temp ; pop from temporary stack and push it to the input stack ; push temp in temporary of stack ; Push contents of both stacks in result ; Sort the result stack . ; main function ; This is the temporary stack
using System ; using System . Collections . Generic ; class GFG { static Stack < int > sortStack ( ref Stack < int > input ) { Stack < int > tmpStack = new Stack < int > ( ) ; while ( input . Count != 0 ) { int tmp = input . Peek ( ) ; input . Pop ( ) ; while ( tmpStack . Count != 0 && tmpStack . Peek ( ) > tmp ) { input . Push ( tmpStack . Peek ( ) ) ; tmpStack . Pop ( ) ; } tmpStack . Push ( tmp ) ; } return tmpStack ; } static Stack < int > sortedMerge ( ref Stack < int > s1 , ref Stack < int > s2 ) { Stack < int > res = new Stack < int > ( ) ; while ( s1 . Count != 0 ) { res . Push ( s1 . Peek ( ) ) ; s1 . Pop ( ) ; } while ( s2 . Count != 0 ) { res . Push ( s2 . Peek ( ) ) ; s2 . Pop ( ) ; } return sortStack ( ref res ) ; } static void Main ( ) { Stack < int > s1 = new Stack < int > ( ) ; Stack < int > s2 = new Stack < int > ( ) ; s1 . Push ( 34 ) ; s1 . Push ( 3 ) ; s1 . Push ( 31 ) ; s2 . Push ( 1 ) ; s2 . Push ( 12 ) ; s2 . Push ( 23 ) ; Stack < int > tmpStack = new Stack < int > ( ) ; tmpStack = sortedMerge ( ref s1 , ref s2 ) ; Console . Write ( " Sorted ▁ and ▁ merged ▁ stack ▁ : STRNEWLINE " ) ; while ( tmpStack . Count != 0 ) { Console . Write ( tmpStack . Peek ( ) + " ▁ " ) ; tmpStack . Pop ( ) ; } } }