codeparrot/xlcost-text-to-code huggingface dataset:
Sum of indices of Characters removed to obtain an Empty String based on given conditions | C # program to implement the above approach ; Function to add index of the deleted character ; If index is beyond the range ; Insert the index of the deleted characeter ; Search over the subtrees to find the desired index ; Function to return count of deleted indices which are to the left of the current index ; Function to generate the sum of indices ; Stores the original index of the characters in sorted order of key ; Traverse the map ; Extract smallest index of smallest character ; Delete the character from the map if it has no remaining occurrence ; Stores the original index ; Count of elements removed to the left of current character ; Current index of the current character ; For 1 - based indexing ; Insert the deleted index in the segment tree ; Final answer ; Driver Code
using System ; using System . Collections . Generic ; using System . Linq ; class GFG { static void add_seg ( int [ ] seg , int start , int end , int current , int index ) { if ( index > end index < start ) return ; if ( start == end ) { seg [ current ] = 1 ; return ; } int mid = ( start + end ) / 2 ; add_seg ( seg , start , mid , 2 * current + 1 , index ) ; add_seg ( seg , mid + 1 , end , 2 * current + 2 , index ) ; seg [ current ] = seg [ 2 * current + 1 ] + seg [ 2 * current + 2 ] ; } static int deleted ( int [ ] seg , int l , int r , int start , int end , int current ) { if ( end < l start > r ) return 0 ; if ( start >= l && end <= r ) return seg [ current ] ; int mid = ( start + end ) / 2 ; return deleted ( seg , l , r , start , mid , 2 * current + 1 ) + deleted ( seg , l , r , mid + 1 , end , 2 * current + 2 ) ; } static void sumOfIndices ( string s ) { int N = s . Length ; int x = ( int ) ( Math . Ceiling ( Math . Log ( N ) / Math . Log ( 2 ) ) ) ; int seg_size = 2 * ( int ) Math . Pow ( 2 , x ) - 1 ; int [ ] segment = new int [ seg_size ] ; int count = 4 ; Dictionary < int , List < int > > fre = new Dictionary < int , List < int > > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { int key = ( int ) ( s [ i ] ) ; List < int > que = new List < int > ( ) ; if ( fre . ContainsKey ( key ) ) { que = fre [ key ] ; } que . Add ( i ) ; fre [ key ] = que ; } while ( fre . Count > 0 ) { int it = fre . Keys . First ( ) ; if ( fre [ it ] . Count == 0 ) fre . Remove ( it ) ; else { List < int > que = fre [ it ] ; int original_index = que [ 0 ] ; int curr_index = deleted ( segment , 0 , original_index - 1 , 0 , N - 1 , 0 ) ; int new_index = original_index - curr_index ; count += new_index + 1 ; add_seg ( segment , 0 , N - 1 , 0 , original_index ) ; que . RemoveAt ( 0 ) ; fre [ it ] = que ; } } Console . Write ( count ) ; } static void Main ( ) { string s = " geeksforgeeks " ; sumOfIndices ( s ) ; } }
Maximum Length of Sequence of Sums of prime factors generated by the given operations | C # program to implement the above approach ; Smallest prime factor array ; Stores if a number is prime or not ; Function to compute all primes using Sieve of Eratosthenes ; Function for finding smallest prime factors for every integer ; Function to find the sum of prime factors of number ; Add smallest prime factor to the sum ; Reduce N ; Return the answer ; Function to return the length of sequence of for the given number ; If the number is prime ; If a previously computed subproblem occurred ; Calculate the sum of prime factors ; Function to return the maximum length of sequence for the given range ; Pre - calculate primes ; Precalculate smallest prime factors ; Iterate over the range ; Update maximum length ; Driver Code
using System ; class GFG { static int [ ] spf = new int [ 100005 ] ; static bool [ ] prime = new bool [ 100005 ] ; static int [ ] dp = new int [ 100005 ] ; static void sieve ( ) { prime [ 0 ] = prime [ 1 ] = false ; for ( int i = 2 ; i < 100005 ; i ++ ) prime [ i ] = true ; for ( int i = 2 ; i * i < 100005 ; i ++ ) { if ( prime [ i ] ) { for ( int j = i * i ; j < 100005 ; j += i ) { prime [ j ] = false ; } } } } static void smallestPrimeFactors ( ) { for ( int i = 0 ; i < 100005 ; i ++ ) spf [ i ] = - 1 ; for ( int i = 2 ; i * i < 100005 ; i ++ ) { for ( int j = i ; j < 100005 ; j += i ) { if ( spf [ j ] == - 1 ) { spf [ j ] = i ; } } } } static int sumOfPrimeFactors ( int n ) { int ans = 0 ; while ( n > 1 ) { ans += spf [ n ] ; n /= spf [ n ] ; } return ans ; } static int findLength ( int n ) { if ( prime [ n ] ) { return 1 ; } if ( dp [ n ] != 0 ) { return dp [ n ] ; } int sum = sumOfPrimeFactors ( n ) ; return dp [ n ] = 1 + findLength ( sum ) ; } static int maxLength ( int n , int m ) { sieve ( ) ; smallestPrimeFactors ( ) ; int ans = int . MinValue ; for ( int i = n ; i <= m ; i ++ ) { if ( i == 4 ) { continue ; } ans = Math . Max ( ans , findLength ( i ) ) ; } return ans ; } public static void Main ( String [ ] args ) { int n = 2 , m = 14 ; Console . Write ( maxLength ( n , m ) ) ; } }
Longest Subarray consisting of unique elements from an Array | C # program to implement the above approach ; Function to find largest subarray with no duplicates ; Stores index of array elements ; Update j based on previous occurrence of a [ i ] ; Update ans to store maximum length of subarray ; Store the index of current occurrence of a [ i ] ; Return readonly ans ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int largest_subarray ( int [ ] a , int n ) { Dictionary < int , int > index = new Dictionary < int , int > ( ) ; int ans = 0 ; for ( int i = 0 , j = 0 ; i < n ; i ++ ) { j = Math . Max ( index . ContainsKey ( a [ i ] ) ? index [ a [ i ] ] : 0 , j ) ; ans = Math . Max ( ans , i - j + 1 ) ; if ( index . ContainsKey ( a [ i ] ) ) index [ a [ i ] ] = i + 1 ; else index . Add ( a [ i ] , i + 1 ) ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 1 , 2 , 3 } ; int n = arr . Length ; Console . Write ( largest_subarray ( arr , n ) ) ; } }
Count of Ways to obtain given Sum from the given Array elements | C # Program to implement the above approach ; Function to call dfs ( ) to calculate the number of ways ; Function to count the number of ways ; Base Case : Reached the end of the array ; Sum is equal to the required sum ; Recursively check if required sum can be obtained by adding current element or by subtracting the current index element ; Driver Code
using System ; class GFG { static int findWays ( int [ ] nums , int S ) { return dfs ( nums , S , 0 , 0 ) ; } static int dfs ( int [ ] nums , int S , int curr_sum , int index ) { if ( index == nums . Length ) { if ( S == curr_sum ) return 1 ; else return 0 ; } return dfs ( nums , S , curr_sum + nums [ index ] , index + 1 ) + dfs ( nums , S , curr_sum - nums [ index ] , index + 1 ) ; } public static void Main ( String [ ] args ) { int S = 3 ; int [ ] arr = new int [ ] { 1 , 2 , 3 , 4 , 5 } ; int answer = findWays ( arr , S ) ; Console . WriteLine ( answer ) ; } }
Check if all the Nodes in a Binary Tree having common values are at least D distance apart | C # program to implement the above approach ; Structure of a Tree node ; Function to create a new node ; Function to count the frequency of node value present in the tree ; Function that returns the max distance between the nodes that have the same key ; If right and left subtree did not have node whose key is value ; Check if the current node is equal to value ; If the left subtree has no node whose key is equal to value ; If the right subtree has no node whose key is equal to value ; Function that finds if the distance between any same nodes is at most K ; Create the map to look for same value of nodes ; Counting the frequency of nodes ; If the returned value of distance is exceeds dist ; Print the result ; Driver Code
using System ; using System . Collections . Generic ; class GFG { public class Node { public int key ; public Node left , right ; } ; static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; temp . left = temp . right = null ; return ( temp ) ; } static void frequencyCounts ( Dictionary < int , int > map , Node root ) { if ( root == null ) return ; if ( map . ContainsKey ( root . key ) ) map [ root . key ] = map [ root . key ] + 1 ; else map [ root . key ] = 1 ; frequencyCounts ( map , root . left ) ; frequencyCounts ( map , root . right ) ; } static int computeDistance ( Node root , int value ) { if ( root == null ) { return - 1 ; } int left = computeDistance ( root . left , value ) ; int right = computeDistance ( root . right , value ) ; if ( left == - 1 && right == - 1 ) { if ( root . key == value ) { return 1 ; } else return - 1 ; } if ( left == - 1 ) { return right + 1 ; } if ( right == - 1 ) { return left + 1 ; } else { return 1 + Math . Max ( left , right ) ; } } static void solve ( Node root , int dist ) { Dictionary < int , int > map = new Dictionary < int , int > ( ) ; frequencyCounts ( map , root ) ; int flag = 0 ; foreach ( KeyValuePair < int , int > it in map ) { if ( it . Value > 1 ) { int result = computeDistance ( root , it . Key ) ; if ( result > dist result == - 1 ) { flag = 1 ; break ; } } } if ( flag == 0 ) Console . Write ( " Yes STRNEWLINE " ) ; else Console . Write ( " No STRNEWLINE " ) ; } 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 ( 3 ) ; root . right . right = newNode ( 4 ) ; root . right . left = newNode ( 4 ) ; int dist = 7 ; solve ( root , dist ) ; } }
Minimum Sum of a pair at least K distance apart from an Array | C # Program to implement the above approach ; Function to find the minimum sum of two elements that are atleast K distance apart ; Length of the array ; Iterate over the array ; Initialize the min value ; Iterate from i + k to N ; Find the minimum ; Update the minimum sum ; Print the answer ; Driver Code
using System ; class GFG { public static void findMinSum ( int [ ] A , int K ) { int n = A . Length ; int minimum_sum = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { int min = int . MaxValue ; for ( int j = i + K ; j < n ; j ++ ) min = Math . Min ( min , A [ j ] ) ; if ( min == int . MaxValue ) continue ; minimum_sum = Math . Min ( minimum_sum , A [ i ] + min ) ; } Console . WriteLine ( minimum_sum ) ; } public static void Main ( String [ ] args ) { int [ ] A = { 4 , 2 , 5 , 4 , 3 , 2 , 5 } ; int K = 3 ; findMinSum ( A , K ) ; } }
Absolute distinct count in a Linked List | C # program to calculate the count of distinct absolute values in a linked list ; Node of the singly linked list ; Function to insert a node at the beginning of the singly Linked List ; Allocate node ; Insert the data ; Point the current Node to the current head ; Make the current node as the new head ; Function to return the count of distinct absolute values present in the Linked list ; Driver Code ; Create the head ; Insert nodes to the Linked List
using System ; using System . Collections . Generic ; class GFG { class Node { public int data ; public Node next ; } ; static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = ( head_ref ) ; ( head_ref ) = new_node ; return head_ref ; } static int distinctCount ( Node head_ref1 ) { HashSet < int > s = new HashSet < int > ( ) ; Node ptr1 = head_ref1 ; while ( ptr1 != null ) { s . Add ( Math . Abs ( ptr1 . data ) ) ; ptr1 = ptr1 . next ; } return s . Count ; } public static void Main ( String [ ] args ) { Node head1 = null ; head1 = push ( head1 , - 1 ) ; head1 = push ( head1 , - 2 ) ; head1 = push ( head1 , 0 ) ; head1 = push ( head1 , 4 ) ; head1 = push ( head1 , 5 ) ; head1 = push ( head1 , 8 ) ; int ans = distinctCount ( head1 ) ; Console . WriteLine ( ans ) ; } }
Count of all possible pairs having sum of LCM and GCD equal to N | C # program to implement the above approach ; Recursive function to return gcd of a and b ; Function to calculate and return LCM of two numbers ; Function to count pairs whose sum of GCD and LCM is equal to N ; Driver Code
using System ; class GFG { static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } static int lcm ( int a , int b ) { return ( a * b ) / __gcd ( a , b ) ; } static int countPair ( int N ) { int count = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <= N ; j ++ ) { if ( __gcd ( i , j ) + lcm ( i , j ) == N ) { count ++ ; } } } return count ; } public static void Main ( String [ ] args ) { int N = 14 ; Console . Write ( countPair ( N ) ) ; } }
Minimize cost to color all the vertices of an Undirected Graph using given operation | C # program to find the minimum cost to color all vertices of an Undirected Graph ; Function to add edge in the given graph ; Function to perform DFS traversal and find the node with minimum cost ; Update the minimum cost ; Recur for all connected nodes ; Function to calculate and return the minimum cost of coloring all vertices of the Undirected Graph ; Marks if a vertex is visited or not ; Perform DFS traversal ; If vertex is not visited ; Update minimum cost ; Return the readonly cost ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static readonly int MAX = 10 ; static List < int > [ ] adj = new List < int > [ MAX ] ; static int min_cost_node ; static void addEdge ( int u , int v ) { adj [ u ] . Add ( v ) ; adj [ v ] . Add ( u ) ; } static void dfs ( int v , int [ ] cost , bool [ ] vis ) { vis [ v ] = true ; min_cost_node = Math . Min ( min_cost_node , cost [ v - 1 ] ) ; for ( int i = 0 ; i < adj [ v ] . Count ; i ++ ) { if ( vis [ adj [ v ] [ i ] ] == false ) { dfs ( adj [ v ] [ i ] , cost , vis ) ; } } } static int minimumCost ( int V , int [ ] cost ) { bool [ ] vis = new bool [ V + 1 ] ; int min_cost = 0 ; for ( int i = 1 ; i <= V ; i ++ ) { if ( ! vis [ i ] ) { min_cost_node = int . MaxValue ; dfs ( i , cost , vis ) ; min_cost += min_cost_node ; } } return min_cost ; } public static void Main ( String [ ] args ) { int V = 6 ; int [ ] cost = { 12 , 25 , 8 , 11 , 10 , 7 } ; for ( int i = 0 ; i < adj . Length ; i ++ ) adj [ i ] = new List < int > ( ) ; addEdge ( 1 , 2 ) ; addEdge ( 1 , 3 ) ; addEdge ( 3 , 2 ) ; addEdge ( 2 , 5 ) ; addEdge ( 4 , 6 ) ; int min_cost = minimumCost ( V , cost ) ; Console . Write ( min_cost + " STRNEWLINE " ) ; } }
Minimize count of divisions by D to obtain at least K equal array elements | C # program to implement the above approach ; Function to return minimum number of moves required ; Stores the number of moves required to obtain respective values from the given array ; Traverse the array ; Insert 0 into V [ a [ i ] ] as it is the initial state ; Insert the moves required to obtain current a [ i ] ; Traverse v [ ] to obtain minimum count of moves ; Check if there are at least K equal elements for v [ i ] ; Add the sum of minimum K moves ; Update answer ; Return the final answer ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int getMinimumMoves ( int n , int k , int d , int [ ] a ) { int MAX = 100000 ; List < int > [ ] v = new List < int > [ MAX ] ; for ( int i = 0 ; i < v . Length ; i ++ ) v [ i ] = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int cnt = 0 ; v [ a [ i ] ] . Add ( 0 ) ; while ( a [ i ] > 0 ) { a [ i ] /= d ; cnt ++ ; v [ a [ i ] ] . Add ( cnt ) ; } } int ans = int . MaxValue ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( v [ i ] . Count >= k ) { int move = 0 ; v [ i ] . Sort ( ) ; for ( int j = 0 ; j < k ; j ++ ) { move += v [ i ] [ j ] ; } ans = Math . Min ( ans , move ) ; } } return ans ; } public static void Main ( String [ ] args ) { int N = 5 , K = 3 , D = 2 ; int [ ] A = { 1 , 2 , 3 , 4 , 5 } ; Console . Write ( getMinimumMoves ( N , K , D , A ) ) ; } }
Longest subarray with odd product | C # program to find the longest subarray with odd product ; Function to return length of longest subarray with odd product ; If even element is encountered ; Update maximum ; Driver Code
using System ; class GFG { static int Maxlen ( int [ ] arr , int n ) { int ans = 0 ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) count = 0 ; else count ++ ; ans = Math . Max ( ans , count ) ; } return ans ; } public static void Main ( ) { int [ ] arr = { 1 , 7 , 2 } ; int n = arr . Length ; Console . WriteLine ( Maxlen ( arr , n ) ) ; } }
Min difference between maximum and minimum element in all Y size subarrays | C # program for the above approach ; Function to get the maximum of all the subarrays of size Y ; ith index of maxarr array will be the index upto which Arr [ i ] is maximum ; Stack is used to find the next larger element and keeps track of index of current iteration ; Loop for remaining indexes ; j < i used to keep track whether jth element is inside or outside the window ; Return submax ; Function to get the minimum for all subarrays of size Y ; ith index of minarr array will be the index upto which Arr [ i ] is minimum ; Stack is used to find the next smaller element and keeping track of index of current iteration ; Loop for remaining indexes ; j < i used to keep track whether jth element is inside or outside the window ; Return submin ; Function to get minimum difference ; Create submin and submax arrays ; Store initial difference ; Calculate temporary difference ; Final minimum difference ; Given array arr [ ] ; Given subarray size ; Function Call
using System ; using System . Collections . Generic ; class GFG { static List < int > get_submaxarr ( int [ ] arr , int n , int y ) { int j = 0 ; Stack < int > stk = new Stack < int > ( ) ; int [ ] maxarr = new int [ n ] ; Array . Fill ( maxarr , 0 ) ; stk . Push ( 0 ) ; for ( int i = 1 ; i < n ; i ++ ) { while ( stk . Count != 0 && arr [ i ] > arr [ stk . Peek ( ) ] ) { maxarr [ stk . Peek ( ) ] = i - 1 ; stk . Pop ( ) ; } stk . Push ( i ) ; } while ( stk . Count != 0 ) { maxarr [ stk . Count ] = n - 1 ; stk . Pop ( ) ; } List < int > submax = new List < int > ( ) ; for ( int i = 0 ; i <= n - y ; i ++ ) { while ( maxarr [ j ] < i + y - 1 j < i ) { j ++ ; } submax . Add ( arr [ j ] ) ; } return submax ; } static List < int > get_subminarr ( int [ ] arr , int n , int y ) { int j = 0 ; Stack < int > stk = new Stack < int > ( ) ; int [ ] minarr = new int [ n ] ; Array . Fill ( minarr , 0 ) ; stk . Push ( 0 ) ; for ( int i = 1 ; i < n ; i ++ ) { while ( stk . Count != 0 && arr [ i ] < arr [ stk . Peek ( ) ] ) { minarr [ stk . Peek ( ) ] = i ; stk . Pop ( ) ; } stk . Push ( i ) ; } while ( stk . Count != 0 ) { minarr [ stk . Peek ( ) ] = n ; stk . Pop ( ) ; } List < int > submin = new List < int > ( ) ; for ( int i = 0 ; i <= n - y ; i ++ ) { while ( minarr [ j ] <= i + y - 1 j < i ) { j ++ ; } submin . Add ( arr [ j ] ) ; } return submin ; } static void getMinDifference ( int [ ] Arr , int N , int Y ) { List < int > submin = get_subminarr ( Arr , N , Y ) ; List < int > submax = get_submaxarr ( Arr , N , Y ) ; int minn = submax [ 0 ] - submin [ 0 ] ; int b = submax . Count ; for ( int i = 1 ; i < b ; i ++ ) { int dif = submax [ i ] - submin [ i ] + 1 ; minn = Math . Min ( minn , dif ) ; } Console . WriteLine ( minn ) ; } static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 3 , 2 , 2 } ; int N = arr . Length ; int Y = 4 ; getMinDifference ( arr , N , Y ) ; } }
Least root of given quadratic equation for value greater than equal to K | C # program for the above approach ; Function to calculate value of quadratic equation for some x ; Function to calculate the minimum value of x such that F ( x ) >= K using binary search ; Start and end value for binary search ; Binary Search ; Computing F ( mid ) and F ( mid - 1 ) ; If F ( mid ) >= K and F ( mid - 1 ) < K return mid ; If F ( mid ) < K go to mid + 1 to end ; If F ( mid ) > K go to start to mid - 1 ; If no such value exist ; Driver code ; Given coefficients of Equations ; Find minimum value of x
using System ; class GFG { static int func ( int A , int B , int C , int x ) { return ( A * x * x + B * x + C ) ; } static int findMinx ( int A , int B , int C , int K ) { int start = 1 ; int end = ( int ) Math . Ceiling ( Math . Sqrt ( K ) ) ; while ( start <= end ) { int mid = start + ( end - start ) / 2 ; int x = func ( A , B , C , mid ) ; int Y = func ( A , B , C , mid - 1 ) ; if ( x >= K && Y < K ) { return mid ; } else if ( x < K ) { start = mid + 1 ; } else { end = mid - 1 ; } } return - 1 ; } public static void Main ( String [ ] args ) { int A = 3 , B = 4 , C = 5 , K = 6 ; Console . WriteLine ( findMinx ( A , B , C , K ) ) ; } }
Find the node at the center of an N | C # implementation of the above approach ; To create tree ; Function to store the path from given vertex to the target vertex in a vector path ; If the target node is found , push it into path vector ; To prevent visiting a node already visited ; Recursive call to the neighbours of current node inorder to get the path ; Function to obtain and return the farthest node from a given vertex ; If the current height is maximum so far , then save the current node ; Iterate over all the neighbours of current node ; This is to prevent visiting a already visited node ; Next call will be at 1 height higher than our current height ; Function to add edges ; Reset to - 1 ; Reset to - 1 ; Stores one end of the diameter ; Reset the maxHeight ; Stores the second end of the diameter ; Store the diameter into the vector path ; Diameter is equal to the path between the two farthest nodes leaf1 and leaf2 ; Driver code
using System ; using System . Collections . Generic ; class GFG { static Dictionary < int , List < int > > tree ; static List < int > path ; static int maxHeight , maxHeightNode ; static bool getDiameterPath ( int vertex , int targetVertex , int parent , List < int > path ) { if ( vertex == targetVertex ) { path . Add ( vertex ) ; return true ; } foreach ( int i in tree [ vertex ] ) { if ( i == parent ) continue ; if ( getDiameterPath ( i , targetVertex , vertex , path ) ) { path . Add ( vertex ) ; return true ; } } return false ; } static void farthestNode ( int vertex , int parent , int height ) { if ( height > maxHeight ) { maxHeight = height ; maxHeightNode = vertex ; } if ( tree . ContainsKey ( vertex ) && tree [ vertex ] . Count > 0 ) { foreach ( int i in tree [ vertex ] ) { if ( i == parent ) continue ; farthestNode ( i , vertex , height + 1 ) ; } } } static void addedge ( int a , int b ) { if ( ! tree . ContainsKey ( a ) ) tree [ a ] = new List < int > ( ) ; tree [ a ] . Add ( b ) ; if ( ! tree . ContainsKey ( b ) ) tree [ b ] = new List < int > ( ) ; tree [ b ] . Add ( a ) ; } static void FindCenter ( int n ) { maxHeight = - 1 ; maxHeightNode = - 1 ; farthestNode ( 0 , - 1 , 0 ) ; int leaf1 = maxHeightNode ; maxHeight = - 1 ; farthestNode ( maxHeightNode , - 1 , 0 ) ; int leaf2 = maxHeightNode ; path = new List < int > ( ) ; getDiameterPath ( leaf1 , leaf2 , - 1 , path ) ; int pathSize = path . Count ; if ( pathSize % 2 == 1 ) { Console . WriteLine ( path [ pathSize / 2 ] ) ; } else { Console . WriteLine ( path [ pathSize / 2 ] + " , ▁ " + path [ ( pathSize - 1 ) / 2 ] ) ; } } static void Main ( ) { int N = 4 ; tree = new Dictionary < int , List < int > > ( ) ; addedge ( 1 , 0 ) ; addedge ( 1 , 2 ) ; addedge ( 1 , 3 ) ; FindCenter ( N ) ; } }