codeparrot/xlcost-text-to-code huggingface dataset:
3 | C # program for 3 - way quick sort ; This function partitions a [ ] in three parts a ) a [ l . . i ] contains all elements smaller than pivot b ) a [ i + 1. . j - 1 ] contains all occurrences of pivot c ) a [ j . . r ] contains all elements greater than pivot ; From left , find the first element greater than or equal to v . This loop will definitely terminate as v is last element ; From right , find the first element smaller than or equal to v ; If i and j cross , then we are done ; Swap , so that smaller goes on left greater goes on right ; Move all same left occurrence of pivot to beginning of array and keep count using p ; Move all same right occurrence of pivot to end of array and keep count using q ; Move pivot element to its correct index ; Move all left same occurrences from beginning to adjacent to arr [ i ] ; Move all right same occurrences from end to adjacent to arr [ i ] ; 3 - way partition based quick sort ; Note that i and j are passed as reference ; Recur ; A utility function to print an array ; Driver code ; Function Call
using System ; class GFG { public static void partition ( int [ ] a , int l , int r , ref int i , ref int j ) { i = l - 1 ; j = r ; int p = l - 1 , q = r ; int v = a [ r ] ; while ( true ) { while ( a [ ++ i ] < v ) ; while ( v < a [ -- j ] ) if ( j == l ) break ; if ( i >= j ) break ; swap ( ref a [ i ] , ref a [ j ] ) ; if ( a [ i ] == v ) { p ++ ; swap ( ref a [ p ] , ref a [ i ] ) ; } if ( a [ j ] == v ) { q -- ; swap ( ref a [ j ] , ref a [ q ] ) ; } } swap ( ref a [ i ] , ref a [ r ] ) ; j = i - 1 ; for ( int k = l ; k < p ; k ++ , j -- ) swap ( ref a [ k ] , ref a [ j ] ) ; i = i + 1 ; for ( int k = r - 1 ; k > q ; k -- , i ++ ) swap ( ref a [ i ] , ref a [ k ] ) ; } public static void quicksort ( int [ ] a , int l , int r ) { if ( r <= l ) return ; int i = 0 , j = 0 ; partition ( a , l , r , ref i , ref j ) ; quicksort ( a , l , j ) ; quicksort ( a , i , r ) ; } public static void printarr ( int [ ] a , int n ) { for ( int i = 0 ; i < n ; ++ i ) Console . Write ( a [ i ] + " ▁ " ) ; Console . Write ( " STRNEWLINE " ) ; } static void Main ( ) { int [ ] a = { 4 , 9 , 4 , 4 , 1 , 9 , 4 , 4 , 9 , 4 , 4 , 1 , 4 } ; int size = a . Length ; static void swap < T > ( ref T lhs , ref T rhs ) { T temp ; temp = lhs ; lhs = rhs ; rhs = temp ; } printarr ( a , size ) ; quicksort ( a , 0 , size - 1 ) ; printarr ( a , size ) ; } }
3 | C # program for 3 - way quick sort ; A function which is used to swap values ; A utility function to print an array ; It uses Dutch National Flag Algorithm ; To handle 2 elements ; update i and j ; j = mid ; or high + 1 ; 3 - way partition based quick sort ; if ( low >= high ) 1 or 0 elements ; Note that i and j are passed as reference ; Recur two halves ; Driver code ; int [ ] a = { 4 , 39 , 54 , 14 , 31 , 89 , 44 , 34 , 59 , 64 , 64 , 11 , 41 } ; int [ ] a = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ; int [ ] a = { 91 , 82 , 73 , 64 , 55 , 46 , 37 , 28 , 19 , 10 } ; int [ ] a = { 4 , 9 , 4 , 4 , 9 , 1 , 1 , 1 } ; ; Function Call
using System ; class GFG { static void swap < T > ( ref T lhs , ref T rhs ) { T temp ; temp = lhs ; lhs = rhs ; rhs = temp ; } public static void printarr ( int [ ] a , int n ) { for ( int i = 0 ; i < n ; ++ i ) Console . Write ( a [ i ] + " ▁ " ) ; Console . Write ( " STRNEWLINE " ) ; } public static void partition ( int [ ] a , int low , int high , ref int i , ref int j ) { if ( high - low <= 1 ) { if ( a [ high ] < a [ low ] ) swap ( ref a [ high ] , ref a [ low ] ) ; i = low ; j = high ; return ; } int mid = low ; int pivot = a [ high ] ; while ( mid <= high ) { if ( a [ mid ] < pivot ) swap ( ref a [ low ++ ] , ref a [ mid ++ ] ) ; else if ( a [ mid ] == pivot ) mid ++ ; else if ( a [ mid ] > pivot ) swap ( ref a [ mid ] , ref a [ high -- ] ) ; } i = low - 1 ; } public static void quicksort ( int [ ] a , int low , int high ) { return ; int i = 0 , j = 0 ; partition ( a , low , high , ref i , ref j ) ; quicksort ( a , low , i ) ; quicksort ( a , j , high ) ; } static void Main ( ) { int [ ] a = { 4 , 9 , 4 , 4 , 1 , 9 , 4 , 4 , 9 , 4 , 4 , 1 , 4 } ; int size = a . Length ; printarr ( a , size ) ; quicksort ( a , 0 , size - 1 ) ; printarr ( a , size ) ; } }
Check if a Regular Bracket Sequence can be formed with concatenation of given strings | C # program for the above approach ; Function to check possible RBS from the given Strings ; Stores the values { sum , min_prefix } ; Iterate over the range ; Stores the total sum ; Stores the minimum prefix ; Check for minimum prefix ; Store these values in vector ; Make two pair vectors pos and neg ; Store values according to the mentioned approach ; Sort the positive vector ; Stores the extra count of open brackets ; No valid bracket sequence can be formed ; Sort the negative vector ; Stores the count of the negative elements ; No valid bracket sequence can be formed ; Check if open is equal to negative ; Driver Code
using System ; using System . Collections . Generic ; public class GFG { class pair : IComparable < pair > { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } public int CompareTo ( pair p ) { return this . first - p . first ; } } static int checkRBS ( String [ ] S ) { int N = S . Length ; pair [ ] v = new pair [ N ] ; for ( int i = 0 ; i < N ; ++ i ) { String s = S [ i ] ; int sum = 0 ; int pre = 0 ; foreach ( char c in s . ToCharArray ( ) ) { if ( c == ' ( ' ) { ++ sum ; } else { -- sum ; } pre = Math . Min ( sum , pre ) ; } v [ i ] = new pair ( sum , pre ) ; } List < pair > pos = new List < pair > ( ) ; List < pair > neg = new List < pair > ( ) ; for ( int i = 0 ; i < N ; ++ i ) { if ( v [ i ] . first >= 0 ) { pos . Add ( new pair ( - v [ i ] . second , v [ i ] . first ) ) ; } else { neg . Add ( new pair ( v [ i ] . first - v [ i ] . second , - v [ i ] . first ) ) ; } } pos . Sort ( ) ; int open = 0 ; foreach ( pair p in pos ) { if ( open - p . first >= 0 ) { open += p . second ; } else { Console . Write ( " No " + " STRNEWLINE " ) ; return 0 ; } } neg . Sort ( ) ; int negative = 0 ; foreach ( pair p in neg ) { if ( negative - p . first >= 0 ) { negative += p . second ; } else { Console . Write ( " No STRNEWLINE " ) ; return 0 ; } } if ( open != negative ) { Console . Write ( " No STRNEWLINE " ) ; return 0 ; } Console . Write ( " Yes STRNEWLINE " ) ; return 0 ; } public static void Main ( String [ ] args ) { String [ ] arr = { " ) " , " ( ) ( " } ; checkRBS ( arr ) ; } }
Maximize count of unique Squares that can be formed with N arbitrary points in coordinate plane | C # program for the above approach ; Function to find the maximum number of unique squares that can be formed from the given N points ; Stores the resultant count of squares formed ; Base Case ; Subtract the maximum possible grid size as sqrt ( N ) ; Find the total squares till now for the maximum grid ; A i * i grid contains ( i - 1 ) * ( i - 1 ) + ( i - 2 ) * ( i - 2 ) + ... + 1 * 1 squares ; When N >= len then more squares will be counted ; Return total count of squares ; Driver Code
using System ; public class GFG { static int maximumUniqueSquares ( int N ) { int ans = 0 ; if ( N < 4 ) { return 0 ; } int len = ( int ) ( Math . Sqrt ( N ) ) ; N -= len * len ; for ( int i = 1 ; i < len ; i ++ ) { ans += i * i ; } if ( N >= len ) { N -= len ; for ( int i = 1 ; i < len ; i ++ ) { ans += i ; } } for ( int i = 1 ; i < N ; i ++ ) { ans += i ; } return ans ; } public static void Main ( string [ ] args ) { int N = 9 ; Console . WriteLine ( maximumUniqueSquares ( N ) ) ; } }
Lexicographically smallest permutation number up to K having given array as a subsequence | C # program for the above approach ; Function to find the lexicographically smallest permutation such that the given array is a subsequence of it ; Stores the missing elements in arr in the range [ 1 , K ] ; Stores if the ith element is present in arr or not ; Loop to mark all integers present in the array as visited ; Loop to insert all the integers not visited into missing ; Append Int32 . MaxValue at end in order to prevent going out of bounds ; Pointer to the current element ; Pointer to the missing element ; Stores the required permutation ; Loop to conthe permutation using greedy approach ; If missing element is smaller that the current element insert missing element ; Insert current element ; Print the required Permutation ; Driver Code ; Function Call
using System ; using System . Collections ; class GFG { static void findPermutation ( int K , ArrayList arr ) { ArrayList missing = new ArrayList ( ) ; bool [ ] visited = new bool [ K + 1 ] ; for ( int i = 0 ; i < arr . Count ; i ++ ) { visited [ ( int ) arr [ i ] ] = true ; } for ( int i = 1 ; i <= K ; i ++ ) { if ( ! visited [ i ] ) { missing . Add ( i ) ; } } arr . Add ( Int32 . MaxValue ) ; missing . Add ( Int32 . MaxValue ) ; int p1 = 0 ; int p2 = 0 ; ArrayList ans = new ArrayList ( ) ; while ( ans . Count < K ) { if ( ( int ) arr [ p1 ] < ( int ) missing [ p2 ] ) { ans . Add ( arr [ p1 ] ) ; p1 ++ ; } else { ans . Add ( missing [ p2 ] ) ; p2 ++ ; } } for ( int i = 0 ; i < K ; i ++ ) { Console . Write ( ans [ i ] + " ▁ " ) ; } } public static void Main ( ) { int K = 7 ; int [ ] a = { 6 , 4 , 2 , 1 } ; ArrayList arr = new ArrayList ( a ) ; findPermutation ( K , arr ) ; } }
Last element remaining after repeated removal of Array elements at perfect square indices | C # program for the above approach ; Function to find last remaining index after repeated removal of perfect square indices ; Initialize the ans variable as N ; Iterate a while loop and discard the possible values ; Total discarded values ; Check if this forms a perfect square ; Decrease answer by 1 ; Subtract the value from the current value of N ; Return the value remained ; Function to find last remaining element after repeated removal of array element at perfect square indices ; Find the remaining index ; Print the element at that index as the result ; Driver Code ; Given input
using System ; class GFG { static int findRemainingIndex ( int N ) { int ans = N ; while ( N > 1 ) { int discard = ( int ) ( Math . Sqrt ( N ) ) ; if ( discard * discard == N ) { ans -- ; } N -= discard ; } return ans ; } static void findRemainingElement ( int [ ] arr , int N ) { int remainingIndex = findRemainingIndex ( N ) ; Console . Write ( arr [ remainingIndex - 1 ] ) ; } public static void Main ( ) { int [ ] arr = { 2 , 3 , 4 , 4 , 2 , 4 , - 3 , 1 , 1 } ; int N = 9 ; findRemainingElement ( arr , N ) ; } }
Lexicographically largest possible by merging two strings by adding one character at a time | C # program for the approach ; Recursive bfunction for finding the lexicographically largest string ; If either of the string length is 0 , return the other string ; If s1 is lexicographically larger than s2 ; Take first character of s1 and call the function ; Take first character of s2 and recursively call function for remaining string ; Driver Code ; Given Input ; Function Call
using System ; class GFG { static string largestMerge ( string s1 , string s2 ) { if ( s1 . Length == 0 s2 . Length == 0 ) return s1 + s2 ; if ( string . Compare ( s1 , s2 ) == 1 ) { return s1 [ 0 ] + largestMerge ( s1 . Substring ( 1 ) , s2 ) ; } return s2 [ 0 ] + largestMerge ( s1 , s2 . Substring ( 1 ) ) ; } public static void Main ( ) { string s1 = " geeks " ; string s2 = " forgeeks " ; Console . Write ( largestMerge ( s1 , s2 ) ) ; } }
Minimum distance between duplicates in a String | C # implementation of above approach ; This function is used to find minimum distance between same repeating characters ; Define a hashmap and an arraylist ; Temporary variable ; Traverse through string ; If character present in dictionary ; Difference between current position and last stored value ; Updating current position ; Storing difference in list ; If character not in dictionary assign it with initial of its position ; If no element inserted in list i . e . no repeating characterss ; Return minimum distance ; Driver code ; Given input ; Function call
using System ; using System . Collections . Generic ; public class GFG { static int shortestDistance ( String S , int N ) { Dictionary < char , int > dic = new Dictionary < char , int > ( ) ; List < int > dis = new List < int > ( ) ; int var ; for ( int i = 0 ; i < N ; i ++ ) { if ( dic . ContainsKey ( S [ i ] ) ) { var = i - dic [ S [ i ] ] ; dic [ S [ i ] ] = i ; dis . Add ( var ) ; } else { dic . Add ( S [ i ] , i ) ; } } dis . Sort ( ) ; if ( dis . Count == 0 ) return - 1 ; else return dis [ 0 ] - 1 ; } public static void Main ( String [ ] args ) { String S = " geeksforgeeks " ; int N = 13 ; Console . WriteLine ( shortestDistance ( S , N ) ) ; } }
Number of open doors | TCS Coding Question | Function that counts the number of doors opens after the Nth turn ; Find the number of open doors ; Return the resultant count of open doors ; Driver Code
using System ; class GFG { static int countOpenDoors ( int N ) { int doorsOpen = ( int ) Math . Sqrt ( N ) ; return doorsOpen ; } public static void Main ( ) { int N = 100 ; Console . Write ( countOpenDoors ( N ) ) ; } }
Minimum number of candies required to distribute among children based on given conditions | Function to count the minimum number of candies that is to be distributed ; Stores total count of candies ; Stores the amount of candies allocated to a student ; If the value of N is 1 ; Initialize with 1 all array element ; Traverse the array arr [ ] ; If arr [ i + 1 ] is greater than arr [ i ] and ans [ i + 1 ] is at most ans [ i ] ; Assign ans [ i ] + 1 to ans [ i + 1 ] ; Iterate until i is atleast 0 ; If arr [ i ] is greater than arr [ i + 1 ] and ans [ i ] is at most ans [ i + 1 ] ; Assign ans [ i + 1 ] + 1 to ans [ i ] ; If arr [ i ] is equals arr [ i + 1 ] and ans [ i ] is less than ans [ i + 1 ] ; Assign ans [ i + 1 ] + 1 to ans [ i ] ; Increment the sum by ans [ i ] ; Return the resultant sum ; Driver Code
using System ; public class GFG { static int countCandies ( int [ ] arr , int n ) { int sum = 0 ; int [ ] ans = new int [ n ] ; if ( n == 1 ) { return 1 ; } for ( int i = 0 ; i < n ; i ++ ) ans [ i ] = 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i + 1 ] > arr [ i ] && ans [ i + 1 ] <= ans [ i ] ) { ans [ i + 1 ] = ans [ i ] + 1 ; } } for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] > arr [ i + 1 ] && ans [ i ] <= ans [ i + 1 ] ) { ans [ i ] = ans [ i + 1 ] + 1 ; } else if ( arr [ i ] == arr [ i + 1 ] && ans [ i ] < ans [ i + 1 ] ) { ans [ i ] = ans [ i + 1 ] + 1 ; } sum += ans [ i ] ; } sum += ans [ n - 1 ] ; return sum ; } static public void Main ( ) { int [ ] arr = { 1 , 0 , 2 } ; int N = arr . Length ; Console . WriteLine ( countCandies ( arr , N ) ) ; } }
Maximize profit possible by selling M products such that profit of a product is the number of products left of that supplier | C # program for the above approach ; Function to find the maximum profit by selling M number of products ; Initialize a Max - Heap to keep track of the maximum value ; Stores the maximum profit ; Traverse the array and push all the elements in max_heap ; Iterate a loop until M > 0 ; Decrement the value of M by 1 ; Pop the maximum element from the heap ; Update the maxProfit ; Push ( X - 1 ) to max heap ; Print the result ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void findMaximumProfit ( int [ ] arr , int M , int N ) { List < int > max_heap = new List < int > ( ) ; int maxProfit = 0 ; for ( int i = 0 ; i < N ; i ++ ) max_heap . Add ( arr [ i ] ) ; max_heap . Sort ( ) ; max_heap . Reverse ( ) ; while ( M > 0 ) { M -- ; int X = max_heap [ 0 ] ; max_heap . RemoveAt ( 0 ) ; maxProfit += X ; max_heap . Add ( X - 1 ) ; max_heap . Sort ( ) ; max_heap . Reverse ( ) ; } Console . Write ( maxProfit ) ; } static void Main ( ) { int [ ] arr = { 4 , 6 } ; int M = 4 ; int N = arr . Length ; findMaximumProfit ( arr , M , N ) ; } }
Generate an array with K positive numbers such that arr [ i ] is either | C # program for the above approach ; Function to generate the resultant sequence of first N natural numbers ; Initialize an array of size N ; Stores the sum of positive and negative elements ; Mark the first N - K elements as negative ; Update the value of arr [ i ] ; Update the value of sumNeg ; Mark the remaining K elements as positive ; Update the value of arr [ i ] ; Update the value of sumPos ; If the sum of the sequence is negative , then print - 1 ; Print the required sequence ; Driver Code
using System ; class GFG { static void findSequence ( int n , int k ) { int [ ] arr = new int [ n ] ; int sumPos = 0 , sumNeg = 0 ; for ( int i = 0 ; i < n - k ; i ++ ) { arr [ i ] = - ( i + 1 ) ; sumNeg += arr [ i ] ; } for ( int i = n - k ; i < n ; i ++ ) { arr [ i ] = i + 1 ; sumPos += arr [ i ] ; } if ( Math . Abs ( sumNeg ) >= sumPos ) { Console . Write ( - 1 ) ; return ; } for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } public static void Main ( ) { int N = 10 , K = 6 ; findSequence ( N , K ) ; } }
Print the indices for every row of a grid from which escaping from the grid is possible | C # program for the above approach ; Function to find the row index for every row of a matrix from which one can exit the grid after entering from left ; Iterate over the range [ 0 , M - 1 ] ; Stores the direction from which a person enters a cell ; Row index from which one enters the grid ; Column index from which one enters the grid ; Iterate until j is atleast N - 1 ; If Mat [ i ] [ j ] is equal to 1 ; If entry is from left cell ; Decrement i by 1 ; Assign ' D ' to dir ; If entry is from upper cell ; Decrement j by 1 ; Assign ' R ' to dir ; If entry is from right cell ; Increment i by 1 ; Assign ' U ' to dir ; If entry is from bottom cell ; Increment j by 1 ; Assign ' L ' to dir ; Otherwise , ; If entry is from left cell ; Increment i by 1 ; Assign ' U ' to dir ; If entry is from upper cell ; Increment j by 1 ; Assign ' L ' to dir ; If entry is from right cell ; Decrement i by 1 ; Assign ' D ' to dir ; If entry is from lower cell ; Decrement j by 1 ; Assign ' R ' to dir ; If i or j is less than 0 or i is equal to M or j is equal to N ; If j is equal to N ; Otherwise ; Driver Code ; Input ; Function call
using System ; class GFG { static void findPath ( int [ , ] arr , int M , int N ) { for ( int row = 0 ; row < M ; row ++ ) { char dir = ' L ' ; int i = row ; int j = 0 ; while ( j < N ) { if ( arr [ i , j ] == 1 ) { if ( dir == ' L ' ) { i -- ; dir = ' D ' ; } else if ( dir == ' U ' ) { j -- ; dir = ' R ' ; } else if ( dir == ' R ' ) { i ++ ; dir = ' U ' ; } else if ( dir == ' D ' ) { j ++ ; dir = ' L ' ; } } else { if ( dir == ' L ' ) { i ++ ; dir = ' U ' ; } else if ( dir == ' U ' ) { j ++ ; dir = ' L ' ; } else if ( dir == ' R ' ) { i -- ; dir = ' D ' ; } else if ( dir == ' D ' ) { j -- ; dir = ' R ' ; } } if ( i < 0 i == M j < 0 j == N ) break ; } if ( j == N ) Console . Write ( i + " ▁ " ) ; else Console . Write ( - 1 + " ▁ " ) ; } } public static void Main ( ) { int [ , ] arr = { { 1 , 1 , 0 , 1 } , { 1 , 1 , 0 , 0 } , { 1 , 0 , 0 , 0 } , { 1 , 1 , 0 , 1 } , { 0 , 1 , 0 , 1 } } ; int M = arr . GetLength ( 0 ) ; int N = arr . GetLength ( 1 ) ; findPath ( arr , M , N ) ; } }
Maximize sum of array by repeatedly removing an element from pairs whose concatenation is a multiple of 3 | C # program for the above approach ; Function to calculate sum of digits of an integer ; Function to calculate maximum sum of array after removing pairs whose concatenation is divisible by 3 ; Stores the sum of digits of array element ; Find the sum of digits ; If i is divisible by 3 ; Otherwise , if i modulo 3 is 1 ; Otherwise , if i modulo 3 is 2 ; Return the resultant sum of array elements ; Driver Code
using System ; class GFG { static int getSum ( int n ) { int ans = 0 ; string str = n . ToString ( ) ; Char [ ] arr = str . ToCharArray ( ) ; foreach ( Char ch in arr ) { ans += ( int ) Char . GetNumericValue ( ch ) ; } return ans ; } static void getMax ( int [ ] arr ) { int maxRem0 = 0 ; int rem1 = 0 ; int rem2 = 0 ; foreach ( int i in arr ) { int digitSum = getSum ( i ) ; if ( digitSum % 3 == 0 ) maxRem0 = Math . Max ( maxRem0 , i ) ; else if ( digitSum % 3 == 1 ) rem1 += i ; else rem2 += i ; } Console . WriteLine ( maxRem0 + Math . Max ( rem1 , rem2 ) ) ; } static void Main ( ) { int [ ] arr = { 23 , 12 , 43 , 3 , 56 } ; getMax ( arr ) ; } }
Minimum edge reversals to make a root | C # program to find min edge reversal to make every node reachable from root ; pair class ; method to dfs in tree and populates disRev values ; visit current node ; looping over all neighbors ; distance of v will be one more than distance of u ; initialize back edge count same as parent node 's count ; if there is a reverse edge from u to i , then only update ; return total reversal in subtree rooted at u ; method prints root and minimum number of edge reversal ; number of nodes are one more than number of edges ; data structure to store directed tree ; disRev stores two values - distance and back edge count from root node ; add 0 weight in direction of u to v ; add 1 weight in reverse direction ; initialize all variables ; dfs populates disRev data structure and store total reverse edge counts ; for ( int i = 0 ; i < V ; i ++ ) { cout << i << " ▁ : ▁ " << disRev [ i ] . first << " ▁ " << disRev [ i ] . second << endl ; } ; loop over all nodes to choose minimum edge reversal ; ( reversal in path to i ) + ( reversal in all other tree parts ) ; choose minimum among all values ; print the designated root and total edge reversal made ; Driver code
using System ; using System . Collections . Generic ; class GFG { public class pair { public int first , second ; public pair ( int a , int b ) { first = a ; second = b ; } } static int dfs ( List < List < pair > > g , pair [ ] disRev , Boolean [ ] visit , int u ) { visit [ u ] = true ; int totalRev = 0 ; for ( int i = 0 ; i < g [ u ] . Count ; i ++ ) { int v = g [ u ] [ i ] . first ; if ( ! visit [ v ] ) { disRev [ v ] . first = disRev [ u ] . first + 1 ; disRev [ v ] . second = disRev [ u ] . second ; if ( g [ u ] [ i ] . second != 0 ) { disRev [ v ] . second = disRev [ u ] . second + 1 ; totalRev ++ ; } totalRev += dfs ( g , disRev , visit , v ) ; } } return totalRev ; } static void printMinEdgeReverseForRootNode ( int [ , ] edges , int e ) { int V = e + 1 ; List < List < pair > > g = new List < List < pair > > ( ) ; for ( int i = 0 ; i < V + 1 ; i ++ ) g . Add ( new List < pair > ( ) ) ; pair [ ] disRev = new pair [ V ] ; for ( int i = 0 ; i < V ; i ++ ) disRev [ i ] = new pair ( 0 , 0 ) ; Boolean [ ] visit = new Boolean [ V ] ; int u , v ; for ( int i = 0 ; i < e ; i ++ ) { u = edges [ i , 0 ] ; v = edges [ i , 1 ] ; g [ u ] . Add ( new pair ( v , 0 ) ) ; g [ v ] . Add ( new pair ( u , 1 ) ) ; } for ( int i = 0 ; i < V ; i ++ ) { visit [ i ] = false ; disRev [ i ] . first = disRev [ i ] . second = 0 ; } int root = 0 ; int totalRev = dfs ( g , disRev , visit , root ) ; int res = int . MaxValue ; for ( int i = 0 ; i < V ; i ++ ) { int edgesToRev = ( totalRev - disRev [ i ] . second ) + ( disRev [ i ] . first - disRev [ i ] . second ) ; if ( edgesToRev < res ) { res = edgesToRev ; root = i ; } } Console . WriteLine ( root + " ▁ " + res ) ; } public static void Main ( String [ ] args ) { int [ , ] edges = { { 0 , 1 } , { 2 , 1 } , { 3 , 2 } , { 3 , 4 } , { 5 , 4 } , { 5 , 6 } , { 7 , 6 } } ; int e = edges . GetLength ( 0 ) ; printMinEdgeReverseForRootNode ( edges , e ) ; } }