codeparrot/xlcost-text-to-code huggingface dataset:
Count nodes having highest value in the path from root to itself in a Binary Tree | C # program for the above approach ; Stores the count of nodes which are maximum in the path from root to the current node ; Binary Tree Node ; Function that performs Inorder Traversal on the Binary Tree ; If root does not exist ; Check if the node satisfies the condition ; Update the maximum value and recursively traverse left and right subtree ; Function that counts the good nodes in the given Binary Tree ; Perform inorder Traversal ; Return the readonly count ; Function that add the new node in the Binary Tree ; Return the node ; Driver Code ; A Binary Tree 3 / \ 2 5 / \ 4 6 ; Function Call ; Print the count of good nodes
using System ; class GfG { static int count = 0 ; public class Node { public int val ; public Node left , right ; } static void find ( Node root , int max ) { if ( root == null ) return ; if ( root . val >= max ) count ++ ; find ( root . left , Math . Max ( max , root . val ) ) ; find ( root . right , Math . Max ( max , root . val ) ) ; } static int NodesMaxInPath ( Node root ) { find ( root , int . MinValue ) ; return count ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . val = data ; temp . left = null ; temp . right = null ; return temp ; } public static void Main ( String [ ] args ) { Node root = null ; root = newNode ( 3 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 5 ) ; root . left . left = newNode ( 4 ) ; root . right . right = newNode ( 7 ) ; int answer = NodesMaxInPath ( root ) ; Console . WriteLine ( answer ) ; } }
Longest Consecutive Subsequence | C # program to find longest contiguous subsequence ; Returns length of the longest contiguous subsequence ; Sort the array ; Insert repeated elements only once in the vector ; Find the maximum length by traversing the array ; Check if the current element is equal to previous element + 1 ; Update the maximum ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int findLongestConseqSubseq ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int ans = 0 , count = 0 ; List < int > v = new List < int > ( ) ; v . Add ( 10 ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] != arr [ i - 1 ] ) v . Add ( arr [ i ] ) ; } for ( int i = 0 ; i < v . Count ; i ++ ) { if ( i > 0 && v [ i ] == v [ i - 1 ] + 1 ) count ++ ; else count = 1 ; ans = Math . Max ( ans , count ) ; } return ans ; } static void Main ( ) { int [ ] arr = { 1 , 9 , 3 , 10 , 4 , 20 , 2 } ; int n = arr . Length ; Console . WriteLine ( " Length ▁ of ▁ the ▁ Longest ▁ " + " contiguous ▁ subsequence ▁ is ▁ " + findLongestConseqSubseq ( arr , n ) ) ; } }
Count smaller primes on the right of each array element | C # Program for the above approach include " bits / stdJava . h " ; Function to check if a number is prime or not ; Function to update a Binary Tree ; Function to find the sum of all the elements which are less than or equal to index ; Function to find the number of smaller primes on the right for every array element ; Iterate the array in backwards ; Calculating the required number of primes ; If current array element is prime ; Update the Fenwick tree ; Driver Code ; Function call
using System ; class GFG { static int maxn = ( int ) 1e6 + 5 ; static int [ ] BITree = new int [ maxn ] ; static bool is_prime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i * i <= n ; i ++ ) if ( n % i == 0 ) return false ; return true ; } static void update_bitree ( int [ ] BITree , int index , int value ) { while ( index <= maxn ) { BITree [ index ] += value ; index += ( index & ( - index ) ) ; } } static int sum_bitree ( int [ ] BITree , int index ) { int s = 0 ; while ( index > 0 ) { s += BITree [ index ] ; index -= ( index & ( - index ) ) ; } return s ; } static void countSmallerPrimes ( int [ ] BITree , int [ ] ar , int N ) { int [ ] ans = new int [ N ] ; for ( int i = N - 1 ; i >= 0 ; i -- ) { ans [ i ] = sum_bitree ( BITree , ar [ i ] ) ; if ( is_prime ( ar [ i ] ) ) update_bitree ( BITree , ar [ i ] , 1 ) ; } for ( int i = 0 ; i < N ; i ++ ) Console . Write ( ans [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] ar = { 5 , 5 , 17 , 9 , 12 , 15 , 11 , 7 , 39 , 3 } ; int N = ar . Length ; countSmallerPrimes ( BITree , ar , N ) ; } }
Longest Consecutive Subsequence | C # program to find longest consecutive subsequence ; Returns length of the longest consecutive subsequence ; Hash all the array elements ; check each possible sequence from the start then update optimal length ; if current element is the starting element of a sequence ; Then check for next elements in the sequence ; update optimal length if this length is more ; Driver code
public class ArrayElements { public static int findLongestConseqSubseq ( int [ ] arr , int n ) { HashSet < int > S = new HashSet < int > ( ) ; int ans = 0 ; for ( int i = 0 ; i < n ; ++ i ) { S . Add ( arr [ i ] ) ; } for ( int i = 0 ; i < n ; ++ i ) { if ( ! S . Contains ( arr [ i ] - 1 ) ) { int j = arr [ i ] ; while ( S . Contains ( j ) ) { j ++ ; } if ( ans < j - arr [ i ] ) { ans = j - arr [ i ] ; } } } return ans ; } public static void Main ( string [ ] args ) { int [ ] arr = new int [ ] { 1 , 9 , 3 , 10 , 4 , 20 , 2 } ; int n = arr . Length ; Console . WriteLine ( " Length ▁ of ▁ the ▁ Longest ▁ consecutive ▁ subsequence ▁ is ▁ " + findLongestConseqSubseq ( arr , n ) ) ; } }
Kth space | C # program of the above approach ; Function to extract integer at key position in the given String ; StringTokenizer ( ) : Extracts the number at key c_str ( ) : Type cast String to char * ; Driver Code ; Given String ; Given K ; Function call
using System ; class GFG { static void print_kth_String ( String str , int K ) { String [ ] iss = str . Split ( ' ▁ ' ) ; K -- ; Console . Write ( iss [ K ] ) ; } public static void Main ( String [ ] args ) { String s = ( "10 ▁ 20 ▁ 30 ▁ 40" ) ; int K = 2 ; print_kth_String ( s , K ) ; } }
Largest increasing subsequence of consecutive integers | C # implementation of longest continuous increasing subsequence ; Function for LIS ; Initialize result ; iterate through array and find end index of LIS and its Size ; print LIS size ; print LIS after setting start element ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void findLIS ( int [ ] A , int n ) { Dictionary < int , int > hash = new Dictionary < int , int > ( ) ; int LIS_size = 1 ; int LIS_index = 0 ; hash . Add ( A [ 0 ] , 1 ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( hash . ContainsKey ( A [ i ] - 1 ) ) { var val = hash [ A [ i ] - 1 ] ; hash . Remove ( A [ i ] ) ; hash . Add ( A [ i ] , val + 1 ) ; } else { hash . Add ( A [ i ] , 1 ) ; } if ( LIS_size < hash [ A [ i ] ] ) { LIS_size = hash [ A [ i ] ] ; LIS_index = A [ i ] ; } } Console . WriteLine ( " LIS _ size ▁ = ▁ " + LIS_size ) ; Console . Write ( " LIS ▁ : ▁ " ) ; int start = LIS_index - LIS_size + 1 ; while ( start <= LIS_index ) { Console . Write ( start + " ▁ " ) ; start ++ ; } } public static void Main ( String [ ] args ) { int [ ] A = { 2 , 5 , 3 , 7 , 4 , 8 , 5 , 13 , 6 } ; int n = A . Length ; findLIS ( A , n ) ; } }
Count subsets having distinct even numbers | C # implementation to count subsets having even numbers only and all are distinct ; function to count the required subsets ; inserting even numbers in the set ' us ' single copy of each number is retained ; counting distinct even numbers ; total count of required subsets ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int countSubsets ( int [ ] arr , int n ) { HashSet < int > us = new HashSet < int > ( ) ; int even_count = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] % 2 == 0 ) us . Add ( arr [ i ] ) ; even_count = us . Count ; return ( int ) ( Math . Pow ( 2 , even_count ) - 1 ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 2 , 1 , 9 , 2 , 6 , 5 , 3 } ; int n = arr . Length ; Console . WriteLine ( " Number ▁ of ▁ subsets ▁ = ▁ " + countSubsets ( arr , n ) ) ; } }
Count distinct elements in every window of size k | Simple C # program to count distinct elements in every window of size k ; Counts distinct elements in window of size k ; Traverse the window ; Check if element arr [ i ] exists in arr [ 0. . i - 1 ] ; Counts distinct elements in all windows of size k ; Traverse through every window ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int countWindowDistinct ( int [ ] win , int k ) { int dist_count = 0 ; for ( int i = 0 ; i < k ; i ++ ) { int j ; for ( j = 0 ; j < i ; j ++ ) if ( win [ i ] == win [ j ] ) break ; if ( j == i ) dist_count ++ ; } return dist_count ; } static void countDistinct ( int [ ] arr , int n , int k ) { for ( int i = 0 ; i <= n - k ; i ++ ) { int [ ] newArr = new int [ k ] ; Array . Copy ( arr , i , newArr , 0 , k ) ; Console . WriteLine ( countWindowDistinct ( newArr , k ) ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 1 , 3 , 4 , 2 , 3 } ; int k = 4 ; countDistinct ( arr , arr . Length , k ) ; } }
Iterative method to check if two trees are mirror of each other | C # implementation to check whether the two binary tress are mirrors of each other or not ; structure of a node in binary tree ; Utility function to create and return a new node for a binary tree ; function to check whether the two binary trees are mirrors of each other or not ; iterative inorder traversal of 1 st tree and reverse inoder traversal of 2 nd tree ; if the corresponding nodes in the two traversal have different data values , then they are not mirrors of each other . ; if at any point one root becomes null and the other root is not null , then they are not mirrors . This condition verifies that structures of tree are mirrors of each other . ; we have visited the node and its left subtree . Now , it ' s ▁ right ▁ subtree ' s turn ; we have visited the node and its right subtree . Now , it ' s ▁ left ▁ subtree ' s turn ; both the trees have been completely traversed ; tress are mirrors of each other ; Driver program to test above ; 1 st binary tree formation ; 1 ; / \ ; 3 2 ; / \ ; 2 nd binary tree formation ; 1 ; / \ ; 2 3 ; / \ ; 4 5
using System ; using System . Collections . Generic ; 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 String areMirrors ( Node root1 , Node root2 ) { Stack < Node > st1 = new Stack < Node > ( ) ; Stack < Node > st2 = new Stack < Node > ( ) ; while ( true ) { while ( root1 != null && root2 != null ) { if ( root1 . data != root2 . data ) return " No " ; st1 . Push ( root1 ) ; st2 . Push ( root2 ) ; root1 = root1 . left ; root2 = root2 . right ; } if ( ! ( root1 == null && root2 == null ) ) return " No " ; if ( st1 . Count != 0 && st2 . Count != 0 ) { root1 = st1 . Peek ( ) ; root2 = st2 . Peek ( ) ; st1 . Pop ( ) ; st2 . Pop ( ) ; root1 = root1 . right ; root2 = root2 . left ; } else break ; } return " Yes " ; } public static void Main ( String [ ] args ) { Node root1 = newNode ( 1 ) ; root1 . left = newNode ( 3 ) ; root1 . right = newNode ( 2 ) ; root1 . right . left = newNode ( 5 ) ; root1 . right . right = newNode ( 4 ) ; Node root2 = newNode ( 1 ) ; root2 . left = newNode ( 2 ) ; root2 . right = newNode ( 3 ) ; root2 . left . left = newNode ( 4 ) ; root2 . left . right = newNode ( 5 ) ; Console . WriteLine ( areMirrors ( root1 , root2 ) ) ; } }
Maximum score assigned to a subsequence of numerically consecutive and distinct array elements | C # program for the above approach ; Function to find the maximum score possible ; Base Case ; If previously occurred subproblem occurred ; Check if lastpicked element differs by 1 from the current element ; Calculate score by including the current element ; Calculate score by excluding the current element ; Return maximum score from the two possibilities ; Function to print maximum score ; DP array to store results ; Initialise dp with - 1 ; Function call ; Driver Code ; Given arrays ; Function Call
using System ; class GFG { public static int maximumSum ( int [ ] a , int [ ] b , int n , int index , int lastpicked , int [ , ] dp ) { if ( index == n ) return 0 ; if ( dp [ index , lastpicked + 1 ] != - 1 ) return dp [ index , lastpicked + 1 ] ; int option1 = 0 , option2 = 0 ; if ( lastpicked == - 1 a [ lastpicked ] != a [ index ] ) { option1 = b [ index ] + maximumSum ( a , b , n , index + 1 , index , dp ) ; } option2 = maximumSum ( a , b , n , index + 1 , lastpicked , dp ) ; return dp [ index , lastpicked + 1 ] = Math . Max ( option1 , option2 ) ; } public static void maximumPoints ( int [ ] arr , int [ ] brr , int n ) { int index = 0 , lastPicked = - 1 ; int [ , ] dp = new int [ n + 5 , n + 5 ] ; for ( int i = 0 ; i < n + 5 ; i ++ ) { for ( int j = 0 ; j < n + 5 ; j ++ ) { dp [ i , j ] = - 1 ; } } Console . WriteLine ( maximumSum ( arr , brr , n , index , lastPicked , dp ) ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 3 , 3 , 1 } ; int [ ] brr = { - 1 , 2 , 10 , 20 , - 10 , - 9 } ; int N = arr . Length ; maximumPoints ( arr , brr , N ) ; } }
Maximum possible sum of a window in an array such that elements of same window in other array are unique | C # program to find the maximum possible sum of a window in one array such that elements in same window of other array are unique . ; Function to return maximum sum of window in A [ ] according to given constraints . ; Map is used to store elements and their counts . ; Initialize result ; calculating the maximum possible sum for each subarray containing unique elements . ; Remove all duplicate instances of A [ i ] in current window . ; Add current instance of A [ i ] to map and to current sum . ; Update result if current sum is more . ; Driver Code
using System ; using System . Collections . Generic ; public class MaxPossibleSuminWindow { static int returnMaxSum ( int [ ] A , int [ ] B , int n ) { HashSet < int > mp = new HashSet < int > ( ) ; int result = 0 ; int curr_sum = 0 , curr_begin = 0 ; for ( int i = 0 ; i < n ; ++ i ) { while ( mp . Contains ( A [ i ] ) ) { mp . Remove ( A [ curr_begin ] ) ; curr_sum -= B [ curr_begin ] ; curr_begin ++ ; } mp . Add ( A [ i ] ) ; curr_sum += B [ i ] ; result = Math . Max ( result , curr_sum ) ; } return result ; } public static void Main ( String [ ] args ) { int [ ] A = { 0 , 1 , 2 , 3 , 0 , 1 , 4 } ; int [ ] B = { 9 , 8 , 1 , 2 , 3 , 4 , 5 } ; int n = A . Length ; Console . WriteLine ( returnMaxSum ( A , B , n ) ) ; } }
Length of second longest sequence of consecutive 1 s in a binary array | C # implementation of the above approach ; Function to find maximum and second maximum length ; Initialise maximum length ; Initialise second maximum length ; Initialise count ; Iterate over the array ; If sequence ends ; Reset count ; Otherwise ; Increase length of current sequence ; Update maximum ; Traverse the given array ; If sequence continues ; Increase length of current sequence ; Update second max ; Reset count when 0 is found ; Print the result ; Driver code ; Given array ; Function Call
using System . Collections . Generic ; using System ; class GFG { static void FindMax ( int [ ] arr , int N ) { int maxi = - 1 ; int maxi2 = - 1 ; int count = 0 ; for ( int i = 0 ; i < N ; ++ i ) { if ( arr [ i ] == 0 ) count = 0 ; else { count ++ ; maxi = Math . Max ( maxi , count ) ; } } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 1 ) { count ++ ; if ( count > maxi2 && count < maxi ) { maxi2 = count ; } } if ( arr [ i ] == 0 ) count = 0 ; } maxi = Math . Max ( maxi , 0 ) ; maxi2 = Math . Max ( maxi2 , 0 ) ; Console . WriteLine ( maxi2 ) ; } public static void Main ( ) { int [ ] arr = { 1 , 1 , 1 , 0 , 0 , 1 , 1 } ; int n = arr . Length ; FindMax ( arr , n ) ; } }
Count nodes from all lower levels smaller than minimum valued node of current level for every level in a Binary Tree | C # program of the above approach ; Stores the nodes to be deleted ; Structure of a Tree node ; Function to create a new node ; Function to find the min value of node for each level ; Count is used to diffentiate each level of the tree ; Function to check whether the nodes in the level below it are smaller by performing post order traversal ; Traverse the left subtree ; Traverse right subtree ; Check from minimum values computed at each level ; Function to print count of nodes from all lower levels having values less than the the nodes in the current level ; Stores the number of levels ; Stores the required count of nodes for each level ; Driver Code ; 4 / \ 3 5 / \ / \ 10 2 3 1
using System ; using System . Collections . Generic ; class GFG { static Dictionary < int , Boolean > mp = new Dictionary < int , Boolean > ( ) ; 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 calculateMin ( Node root , List < int > levelMin ) { Queue < Node > qt = new Queue < Node > ( ) ; qt . Enqueue ( root ) ; int count = 1 ; int min_v = int . MaxValue ; while ( qt . Count != 0 ) { Node temp = qt . Peek ( ) ; min_v = Math . Min ( min_v , temp . key ) ; qt . Dequeue ( ) ; if ( temp . left != null ) { qt . Enqueue ( temp . left ) ; } if ( temp . right != null ) { qt . Enqueue ( temp . right ) ; } count -- ; if ( count == 0 ) { levelMin . Add ( min_v ) ; min_v = int . MaxValue ; count = qt . Count ; } } } static void findNodes ( Node root , List < int > levelMin , int [ ] levelResult , int level ) { if ( root == null ) return ; findNodes ( root . left , levelMin , levelResult , level + 1 ) ; findNodes ( root . right , levelMin , levelResult , level + 1 ) ; for ( int i = 0 ; i < level ; i ++ ) { if ( root . key <= levelMin [ i ] ) { levelResult [ i ] += 1 ; } } } static void printNodes ( Node root ) { List < int > levelMin = new List < int > ( ) ; calculateMin ( root , levelMin ) ; int numLevels = levelMin . Count ; int [ ] levelResult = new int [ numLevels ] ; findNodes ( root , levelMin , levelResult , 0 ) ; for ( int i = 0 ; i < numLevels ; i ++ ) { Console . Write ( levelResult [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { Node root = newNode ( 4 ) ; root . left = newNode ( 3 ) ; root . right = newNode ( 5 ) ; root . right . left = newNode ( 3 ) ; root . right . right = newNode ( 1 ) ; root . left . left = newNode ( 10 ) ; root . left . right = newNode ( 2 ) ; printNodes ( root ) ; } }
Count subarrays which contains both the maximum and minimum array element | C # program for the above approach ; Function to count subarray containing both maximum and minimum array elements ; If the length of the array is less than 2 ; Find the index of maximum element ; Find the index of minimum element ; If i > j , then swap the value of i and j ; Return the answer ; Function to return max_element index ; Function to return min_element index ; Driver Code ; Function call
using System ; class GFG { static int countSubArray ( int [ ] arr , int n ) { if ( n < 2 ) return n ; int i = max_element ( arr ) ; int j = min_element ( arr ) ; if ( i > j ) { int tmp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = tmp ; } return ( i + 1 ) * ( n - j ) ; } static int max_element ( int [ ] arr ) { int idx = 0 ; int max = arr [ 0 ] ; for ( int i = 1 ; i < arr . Length ; i ++ ) { if ( max < arr [ i ] ) { max = arr [ i ] ; idx = i ; } } return idx ; } static int min_element ( int [ ] arr ) { int idx = 0 ; int min = arr [ 0 ] ; for ( int i = 1 ; i < arr . Length ; i ++ ) { if ( arr [ i ] < min ) { min = arr [ i ] ; idx = i ; } } return idx ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 1 , 2 , 3 } ; int n = arr . Length ; Console . WriteLine ( countSubArray ( arr , n ) ) ; } }
Count pairs of leaf nodes in a Binary Tree which are at most K distance apart | C # implementation of the above approach ; Structure of a Node ; Constructor of the class ; Stores the count of required pairs ; Function to perform dfs to find pair of leaf nodes at most K distance apart ; Return empty array if node is null ; If node is a leaf node and return res ; Traverse to the left ; Traverse to the right ; Update the distance between left and right leaf node ; Count all pair of leaf nodes which are at most K distance apart ; Return res to parent node ; Driver Code ; 1 / \ 2 3 / 4 ; Given distance K ; Function Call
using System ; class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } class GFG { Node root ; static int result ; static int [ ] dfs ( Node root , int distance ) { int [ ] res ; if ( root == null ) return new int [ distance + 1 ] ; if ( root . left == null && root . right == null ) { res = new int [ distance + 1 ] ; res [ 1 ] ++ ; return res ; } int [ ] left = dfs ( root . left , distance ) ; int [ ] right = dfs ( root . right , distance ) ; res = new int [ distance + 1 ] ; for ( int i = res . Length - 2 ; i >= 1 ; i -- ) { res [ i + 1 ] = left [ i ] + right [ i ] ; } for ( int l = 1 ; l < left . Length ; l ++ ) { for ( int r = 0 ; r < right . Length ; r ++ ) { if ( l + r <= distance ) { result += left [ l ] * right [ r ] ; } } } return res ; } public static void Main ( String [ ] args ) { GFG tree = new GFG ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; result = 0 ; int K = 3 ; dfs ( tree . root , K ) ; Console . WriteLine ( result ) ; } }
Maximum absolute difference between any two level sum in a N | C # program for the above approach ; Function to find the maximum absolute difference of level sum ; Create the adjacency list ; Initialize value of maximum and minimum level sum ; Do Level order traversal keeping track of nodes at every level ; Get the size of queue when the level order traversal for one level finishes ; Iterate for all the nodes in the queue currently ; Dequeue an node from queue ; Enqueue the children of dequeued node ; Update the maximum level sum value ; Update the minimum level sum value ; Return the result ; Driver Code ; Number of nodes and edges ; Edges of the N - ary tree ; Given cost ; Function call
using System ; using System . Collections . Generic ; class GFG { static void maxAbsDiffLevelSum ( int N , int M , int [ ] cost , int [ , ] Edges ) { List < int > [ ] adj = new List < int > [ N ] ; for ( int i = 0 ; i < adj . Length ; i ++ ) adj [ i ] = new List < int > ( ) ; for ( int i = 0 ; i < M ; i ++ ) { int u = Edges [ i , 0 ] ; int v = Edges [ i , 1 ] ; adj [ u ] . Add ( v ) ; } int maxSum = cost [ 0 ] , minSum = cost [ 0 ] ; Queue < int > q = new Queue < int > ( ) ; q . Enqueue ( 0 ) ; while ( q . Count != 0 ) { int count = q . Count ; int sum = 0 ; while ( count -- > 0 ) { int temp = q . Peek ( ) ; q . Dequeue ( ) ; sum = sum + cost [ temp ] ; for ( int i = 0 ; i < adj [ temp ] . Count ; i ++ ) { q . Enqueue ( adj [ temp ] [ i ] ) ; } } maxSum = Math . Max ( sum , maxSum ) ; minSum = Math . Min ( sum , minSum ) ; } Console . Write ( Math . Abs ( maxSum - minSum ) ) ; } public static void Main ( String [ ] args ) { int N = 10 , M = 9 ; int [ , ] Edges = { { 0 , 1 } , { 0 , 2 } , { 0 , 3 } , { 1 , 4 } , { 1 , 5 } , { 3 , 6 } , { 6 , 7 } , { 6 , 8 } , { 6 , 9 } } ; int [ ] cost = { 1 , 2 , - 1 , 3 , 4 , 5 , 8 , 6 , 12 , 7 } ; maxAbsDiffLevelSum ( N , M , cost , Edges ) ; } }