codeparrot/xlcost-text-to-code huggingface dataset:
Maximize sum of K elements selected from a Matrix such that each selected element must be preceded by selected row elements | C # program for the above approach ; Function to return the maximum of two elements ; Function to find the maximum sum of selecting K elements from the given 2D array [ , ] arr ; dp table of size ( K + 1 ) * ( N + 1 ) ; Initialize dp [ 0 , i ] = 0 ; Initialize dp [ i , 0 ] = 0 ; Selecting i elements ; Select i elements till jth row ; sum = 0 , to keep track of cummulative elements sum ; Traverse arr [ j , k ] until number of elements until k > i ; Select arr [ j , k - 1 ] th item ; Store the maxSum in dp [ i , j + 1 ] ; Return the maximum sum ; Driver Code ; Function Call
using System ; class GFG { static int max ( int a , int b ) { return a > b ? a : b ; } static int maximumsum ( int [ , ] arr , int K , int N , int M ) { int sum = 0 , maxSum ; int i , j , k ; int [ , ] dp = new int [ K + 1 , N + 1 ] ; for ( i = 0 ; i <= N ; i ++ ) dp [ 0 , i ] = 0 ; for ( i = 0 ; i <= K ; i ++ ) dp [ i , 0 ] = 0 ; for ( i = 1 ; i <= K ; i ++ ) { for ( j = 0 ; j < N ; j ++ ) { sum = 0 ; maxSum = dp [ i , j ] ; for ( k = 1 ; k <= M && k <= i ; k ++ ) { sum += arr [ j , k - 1 ] ; maxSum = Math . Max ( maxSum , sum + dp [ i - k , j ] ) ; } dp [ i , j + 1 ] = maxSum ; } } return dp [ K , N ] ; } public static void Main ( String [ ] args ) { int [ , ] arr = { { 10 , 10 , 100 , 30 } , { 80 , 50 , 10 , 50 } } ; int N = 2 , M = 4 ; int K = 5 ; Console . Write ( maximumsum ( arr , K , N , M ) ) ; } }
Subsequences of given string consisting of non | C # program to implement the above approach ; Function to find all the subsequences of the string with non - repeating characters ; Base case ; Insert current subsequence ; If str [ i ] is not present in the current subsequence ; Insert str [ i ] into the set ; Insert str [ i ] into the current subsequence ; Remove str [ i ] from the set ; Not including str [ i ] from the current subsequence ; Utility function to print all subsequences of string with non - repeating characters ; Stores all possible subsequences with non - repeating characters ; Stores subsequence with non - repeating characters ; Traverse all possible subsequences containing non - repeating characters ; Print subsequence ; Driver Code
using System ; using System . Collections . Generic ; public class GFG { public static void FindSub ( HashSet < String > sub , HashSet < char > ch , String str , String res , int i ) { if ( i == str . Length ) { sub . Add ( res ) ; return ; } if ( ! ch . Contains ( str [ i ] ) ) { ch . Add ( str [ i ] ) ; FindSub ( sub , ch , str , res + str [ i ] , i + 1 ) ; ch . Remove ( str [ i ] ) ; } FindSub ( sub , ch , str , res , i + 1 ) ; } public static void printSubwithUniqueChar ( String str , int N ) { HashSet < String > sub = new HashSet < String > ( ) ; HashSet < char > ch = new HashSet < char > ( ) ; FindSub ( sub , ch , str , " " , 0 ) ; foreach ( String subString in sub ) { Console . Write ( subString + " ▁ " ) ; } } public static void Main ( String [ ] args ) { String str = " abac " ; int N = str . Length ; printSubwithUniqueChar ( str , N ) ; } }
Count lexicographically increasing K | C # program for the above approach ; Function to count K - length strings from first N alphabets ; To keep track of column sum in dp ; Auxiliary 2d dp array ; Initialize dp [ 0 ] [ i ] = 1 and update the column_sum ; Iterate for K times ; Iterate for N times ; dp [ i ] [ j ] : Stores the number of ways to form i - length strings consisting of j letters ; Update the column_sum ; Print number of ways to arrange K - length strings with N alphabets ; Driver Code ; Given N and K ; Function Call
using System ; class GFG { static void waysToArrangeKLengthStrings ( int N , int K ) { int [ ] column_sum = new int [ N + 1 ] ; int i , j ; for ( i = 1 ; i < N + 1 ; i ++ ) { column_sum [ i ] = 0 ; } int [ , ] dp = new int [ K + 1 , N + 1 ] ; for ( i = 1 ; i < K + 1 ; i ++ ) { for ( j = 1 ; j < N + 1 ; j ++ ) { dp [ i , j ] = 0 ; } } for ( i = 0 ; i <= N ; i ++ ) { dp [ 0 , i ] = 1 ; column_sum [ i ] = 1 ; } for ( i = 1 ; i <= K ; i ++ ) { for ( j = 1 ; j <= N ; j ++ ) { dp [ i , j ] += column_sum [ j - 1 ] ; column_sum [ j ] += dp [ i , j ] ; } } Console . Write ( dp [ K , N ] ) ; } public static void Main ( ) { int N = 5 , K = 2 ; waysToArrangeKLengthStrings ( N , K ) ; } }
Count N | C # program to implement the above approach ; Function to count N - length strings consisting of vowels only sorted lexicographically ; Stores count of strings consisting of vowels sorted lexicographically of all possible lengths ; Initialize DP [ 1 ] [ 1 ] ; Traverse the matrix row - wise ; Base Case ; Return the result ; Driver Code ; Function Call
using System ; class GFG { static int findNumberOfStrings ( int n ) { int [ , ] DP = new int [ n + 1 , 6 ] ; DP [ 1 , 1 ] = 1 ; for ( int i = 1 ; i < n + 1 ; i ++ ) { for ( int j = 1 ; j < 6 ; j ++ ) { if ( i == 1 ) { DP [ i , j ] = DP [ i , j - 1 ] + 1 ; } else { DP [ i , j ] = DP [ i , j - 1 ] + DP [ i - 1 , j ] ; } } } return DP [ n , 5 ] ; } public static void Main ( string [ ] args ) { int N = 2 ; Console . Write ( findNumberOfStrings ( N ) ) ; } }
Largest subtree sum for each vertex of given N | C # program for the above approach ; Function to perform the DFS Traversal on the given Tree ; To check if v is leaf vertex ; Initialize answer for vertex v ; Traverse adjacency list of v ; Update maximum subtree sum ; If v is leaf ; Function to calculate maximum subtree sum for each vertex ; Stores the adjacency list ; Add Edegs to the list ; Stores largest subtree sum for each vertex ; Calculate answer ; Print the result ; Driver Code ; Given nodes ; Give N edges ; Given values ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void dfs ( int v , int p , List < int > [ ] adj , int [ ] ans , int [ ] vals ) { bool isLeaf = true ; ans [ v ] = int . MinValue ; foreach ( int u in adj [ v ] ) { if ( u == p ) continue ; isLeaf = false ; dfs ( u , v , adj , ans , vals ) ; ans [ v ] = Math . Max ( ans [ u ] + vals [ v ] , Math . Max ( ans [ u ] , vals [ u ] ) ) ; } if ( isLeaf ) { ans [ v ] = vals [ v ] ; } } static void printAnswer ( int n , int [ , ] edges , int [ ] values ) { List < int > [ ] adj = new List < int > [ n ] ; for ( int i = 0 ; i < n ; i ++ ) adj [ i ] = new List < int > ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int u = edges [ i , 0 ] - 1 ; int v = edges [ i , 1 ] - 1 ; adj [ u ] . Add ( v ) ; adj [ v ] . Add ( u ) ; } int [ ] ans = new int [ n ] ; dfs ( 0 , - 1 , adj , ans , values ) ; foreach ( int x in ans ) { Console . Write ( x + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int N = 4 ; int [ , ] edges = new int [ , ] { { 1 , 2 } , { 1 , 3 } , { 3 , 4 } } ; int [ ] values = { 1 , - 1 , 0 , 1 } ; printAnswer ( N , edges , values ) ; } }
Queries to count frequencies of a given character in a given range of indices | C # program for the above approach ; Function to print count of char y present in the range [ l , r ] ; Length of the string ; Stores the precomputed results ; Iterate the given string ; Increment dp [ i , y - ' a ' ] by 1 ; Pre - compute ; Number of queries ; Traverse each query ; Print the result for each query ; Driver Code ; Given string ; Given Queries ; Function Call
using System ; public class GFG { static void noOfChars ( String s , char [ , ] queries ) { int n = s . Length ; int [ , ] dp = new int [ n + 1 , 26 ] ; for ( int i = 0 ; i < n ; i ++ ) { dp [ i + 1 , s [ i ] - ' a ' ] ++ ; for ( int j = 0 ; j < 26 ; j ++ ) { dp [ i + 1 , j ] += dp [ i , j ] ; } } int q = queries . GetLength ( 0 ) ; for ( int i = 0 ; i < q ; i ++ ) { int l = ( int ) queries [ i , 0 ] ; int r = ( int ) queries [ i , 1 ] ; int c = queries [ i , 2 ] - ' a ' ; Console . Write ( dp [ r , c ] - dp [ l - 1 , c ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { String S = " aabv " ; char [ , ] queries = new char [ , ] { { ( char ) 1 , ( char ) 2 , ' a ' } , { ( char ) 2 , ( char ) 3 , ' b ' } } ; noOfChars ( S , queries ) ; } }
Minimum cost required to rearrange a given array to make it equal to another given array | C # program for the above approach ; Function to find lower_bound ; Function to find length of the longest common subsequence ; Find position where element is to be inserted ; Return the length of LCS ; Function to find the minimum cost required to convert the sequence A exactly same as B ; Auxiliary array ; Stores positions of elements of A [ ] ; Initialize index array with - 1 ; Update the index array with index of corresponding elements of B ; Place only A 's array values  with its mapped values  into nums array ; Find LCS ; No of elements to be added in array A [ ] ; Stores minimum cost ; Print the minimum cost ; Driver code ; Given array A [ ] ; Given C ; Size of arr A ; Size of arr B ; Function call
using System ; class GFG { static int LowerBound ( int [ ] a , int k , int x ) { int l = - 1 ; int r = k ; while ( l + 1 < r ) { int m = ( l + r ) >> 1 ; if ( a [ m ] >= x ) { r = m ; } else { l = m ; } } return r ; } static int findLCS ( int [ ] nums , int N ) { int k = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int pos = LowerBound ( nums , k , nums [ i ] ) ; nums [ pos ] = nums [ i ] ; if ( k == pos ) { k = pos + 1 ; } } return k ; } static int minimumCost ( int [ ] A , int [ ] B , int M , int N , int C ) { int [ ] nums = new int [ 100000 ] ; int [ ] index = new int [ 100000 ] ; for ( int i = 0 ; i < 100000 ; i ++ ) index [ i ] = - 1 ; for ( int i = 0 ; i < N ; i ++ ) { index [ B [ i ] ] = i ; } int k = 0 ; for ( int i = 0 ; i < M ; i ++ ) { if ( index [ A [ i ] ] != - 1 ) { nums [ k ++ ] = index [ A [ i ] ] ; } } int lcs_length = findLCS ( nums , k ) ; int elements_to_be_added = N - lcs_length ; int min_cost = elements_to_be_added * C ; Console . WriteLine ( min_cost ) ; return 0 ; } public static void Main ( ) { int [ ] A = { 1 , 6 , 3 , 5 , 10 } ; int [ ] B = { 3 , 1 , 5 } ; int C = 2 ; int M = A . Length ; int N = B . Length ; minimumCost ( A , B , M , N , C ) ; } }
Maximize count of array elements required to obtain given sum | C # program for the above approach ; Function that count the maximum number of elements to obtain sum V ; Stores the maximum number of elements required to obtain V ; Base Case ; Initialize all table values as Infinite ; Find the max arr required for all values from 1 to V ; Go through all arr smaller than i ; If current coin value is less than i ; Update table [ i ] ; Return the final count ; Driver code ; Given array ; Given sum V ; Function Call
using System ; class GFG { static int maxCount ( int [ ] arr , int m , int V ) { int [ ] table = new int [ V + 1 ] ; table [ 0 ] = 0 ; for ( int i = 1 ; i <= V ; i ++ ) table [ i ] = - 1 ; for ( int i = 1 ; i <= V ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( arr [ j ] <= i ) { int sub_res = table [ i - arr [ j ] ] ; if ( sub_res != - 1 && sub_res + 1 > table [ i ] ) table [ i ] = sub_res + 1 ; } } } return table [ V ] ; } static void Main ( ) { int [ ] arr = { 25 , 10 , 5 } ; int m = arr . Length ; int V = 30 ; Console . WriteLine ( maxCount ( arr , m , V ) ) ; } }
Longest Subsequence from a numeric String divisible by K | C # program for the above approach ; Function to if the integer representation of the current string is divisible by K ; Stores the integer representation of the string ; Check if the num is divisible by K ; Function to find the longest subsequence which is divisible by K ; If the number is divisible by K ; If current number is the maximum obtained so far ; Include the digit at current index ; Exclude the digit at current index ; Driver code ; Printing the largest number which is divisible by K ; If no number is found to be divisible by K
using System ; using System . Text ; class GFG { static bool isdivisible ( StringBuilder newstr , long K ) { long num = 0 ; for ( int i = 0 ; i < newstr . Length ; i ++ ) { num = num * 10 + newstr [ i ] - '0' ; } if ( num % K == 0 ) return true ; else return false ; } static void findLargestNo ( String str , StringBuilder newstr , StringBuilder ans , int index , long K ) { if ( index == str . Length ) { if ( isdivisible ( newstr , K ) ) { if ( ( newstr . ToString ( ) . CompareTo ( ans . ToString ( ) ) > 0 && ans . Length == newstr . Length ) newstr . Length > ans . Length ) { ans . Append ( newstr ) ; } } return ; } StringBuilder x = new StringBuilder ( newstr . ToString ( ) + str [ index ] ) ; findLargestNo ( str , x , ans , index + 1 , K ) ; findLargestNo ( str , newstr , ans , index + 1 , K ) ; } public static void Main ( String [ ] args ) { String str = "121400" ; StringBuilder ans = new StringBuilder ( ) , newstr = new StringBuilder ( ) ; long K = 8 ; findLargestNo ( str , newstr , ans , 0 , K ) ; if ( ans . ToString ( ) != " " ) Console . WriteLine ( ans ) ; else Console . WriteLine ( - 1 ) ; } }
Maximize path sum from top | C # program to implement the above approach ; Function to get the maximum path sum from top - left cell to all other cells of the given matrix ; Store the maximum path sum ; Base case ; Compute the value of dp [ i , j ] using the recurrence relation ; Print maximum path sum from the top - left cell ; Driver Code
using System ; class GFG { static readonly int SZ = 100 ; static void pathSum ( int [ , ] mat , int N , int M ) { int [ , ] dp = new int [ N , M ] ; dp [ 0 , 0 ] = mat [ 0 , 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { dp [ i , 0 ] = mat [ i , 0 ] + dp [ i - 1 , 0 ] ; } for ( int j = 1 ; j < M ; j ++ ) { dp [ 0 , j ] = mat [ 0 , j ] + dp [ 0 , j - 1 ] ; } for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 1 ; j < M ; j ++ ) { dp [ i , j ] = mat [ i , j ] + Math . Max ( dp [ i - 1 , j ] , dp [ i , j - 1 ] ) ; } } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { Console . Write ( dp [ i , j ] + " ▁ " ) ; } Console . WriteLine ( ) ; } } public static void Main ( String [ ] args ) { int [ , ] mat = { { 3 , 2 , 1 } , { 6 , 5 , 4 } , { 7 , 8 , 9 } } ; int N = 3 ; int M = 3 ; pathSum ( mat , N , M ) ; } }
Length of Longest Palindrome Substring | C # program for the above approach ; Function to find the length of the longest palindromic subString ; Length of String str ; Stores the dp states ; All subStrings of length 1 are palindromes ; Check for sub - String of length 2 ; If adjacent character are same ; Update table [ i , i + 1 ] ; Check for lengths greater than 2 k is length of subString ; Fix the starting index ; Ending index of subString of length k ; Check for palindromic subString str [ i , j ] ; Mark true ; Update the maximum length ; Return length of LPS ; Driver Code ; Given String str ; Function Call
using System ; class GFG { static int longestPalSubstr ( String str ) { int n = str . Length ; bool [ , ] table = new bool [ n , n ] ; int maxLength = 1 ; for ( int i = 0 ; i < n ; ++ i ) table [ i , i ] = true ; int start = 0 ; for ( int i = 0 ; i < n - 1 ; ++ i ) { if ( str [ i ] == str [ i + 1 ] ) { table [ i , i + 1 ] = true ; start = i ; maxLength = 2 ; } } for ( int k = 3 ; k <= n ; ++ k ) { for ( int i = 0 ; i < n - k + 1 ; ++ i ) { int j = i + k - 1 ; if ( table [ i + 1 , j - 1 ] && str [ i ] == str [ j ] ) { table [ i , j ] = true ; if ( k > maxLength ) { start = i ; maxLength = k ; } } } } return maxLength ; } public static void Main ( String [ ] args ) { String str = " forgeeksskeegfor " ; Console . Write ( longestPalSubstr ( str ) ) ; } }
Maximize sum of an Array by flipping sign of all elements of a single subarray | C # program for the above approach ; Function to find the maximum sum after flipping a subarray ; Stores the total sum of array ; Initialize the maximum sum ; Iterate over all possible subarrays ; Initialize sum of the subarray before flipping sign ; Initialize sum of subarray after flipping sign int flip_sum = 0 ; ; Calculate the sum of original subarray ; Subtract the original subarray sum and add the flipped subarray sum to the total sum ; Return the max_sum ; Driver Code ; Function call
using System ; class GFG { public static int maxSumFlip ( int [ ] a , int n ) { int total_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) total_sum += a [ i ] ; int max_sum = int . MinValue ; for ( int i = 0 ; i < n ; i ++ ) { int sum = 0 ; for ( int j = i ; j < n ; j ++ ) { sum += a [ j ] ; max_sum = Math . Max ( max_sum , total_sum - 2 * sum ) ; } } return Math . Max ( max_sum , total_sum ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { - 2 , 3 , - 1 , - 4 , - 2 } ; int N = arr . Length ; Console . WriteLine ( maxSumFlip ( arr , N ) ) ; } }
Minimum repeated addition of even divisors of N required to convert N to M | C # program for the above approach ; INF is the maximum value which indicates Impossible state ; Stores the dp states ; Recursive Function that considers all possible even divisors of cur ; Indicates impossible state ; Check dp [ cur ] is already calculated or not ; Initially it is set to INF that meanswe cur can 't be transform to M ; Loop to find even divisors of cur ; If i is divisor of cur ; If i is even ; Find recursively for cur + i ; Check another divisor ; Find recursively for cur + ( cur / i ) ; Finally store the current state result and return the answer ; Function that counts the minimum operation to reduce N to M ; Initialise dp state ; Function call ; Driver Code ; Given N and M ; Function call ; INF indicates impossible state
using System ; class GFG { static int INF = ( int ) 1e7 ; static int max_size = 100007 ; static int [ ] dp = new int [ max_size ] ; static int min_op ( int cur , int M ) { if ( cur > M ) return INF ; if ( cur == M ) return 0 ; if ( dp [ cur ] != - 1 ) return dp [ cur ] ; int op = INF ; for ( int i = 2 ; i * i <= cur ; i ++ ) { if ( cur % i == 0 ) { if ( i % 2 == 0 ) { op = Math . Min ( op , 1 + min_op ( cur + i , M ) ) ; } if ( ( cur / i ) != i && ( cur / i ) % 2 == 0 ) { op = Math . Min ( op , 1 + min_op ( cur + ( cur / i ) , M ) ) ; } } } return dp [ cur ] = op ; } static int min_operations ( int N , int M ) { for ( int i = N ; i <= M ; i ++ ) { dp [ i ] = - 1 ; } return min_op ( N , M ) ; } public static void Main ( String [ ] args ) { int N = 6 , M = 24 ; int op = min_operations ( N , M ) ; if ( op >= INF ) Console . Write ( " - 1" ) ; else Console . Write ( op + " STRNEWLINE " ) ; } }
Maximize Sum possible from an Array by the given moves | C # Program to implement the above approach ; Function to find the maximum sum possible by given moves from the array ; Checking for boundary ; If previously computed subproblem occurs ; If element can be moved left ; Calculate maximum possible sum by moving left from current index ; If element can be moved right ; Calculate maximum possible sum by moving right from current index and update the maximum sum ; Store the maximum sum ; Driver Code ; Function call
using System ; class GFG { public static int maxValue ( int [ ] a , int n , int pos , int moves , int left , int [ , ] dp ) { if ( moves == 0 || ( pos > n - 1 pos < 0 ) ) return 0 ; if ( dp [ pos , left ] != - 1 ) return dp [ pos , left ] ; int value = 0 ; if ( left > 0 && pos >= 1 ) value = Math . Max ( value , a [ pos ] + maxValue ( a , n , pos - 1 , moves - 1 , left - 1 , dp ) ) ; if ( pos <= n - 1 ) value = Math . Max ( value , a [ pos ] + maxValue ( a , n , pos + 1 , moves - 1 , left , dp ) ) ; return dp [ pos , left ] = value ; } public static void Main ( String [ ] args ) { int n = 5 ; int [ ] a = { 1 , 5 , 4 , 3 , 2 } ; int k = 1 ; int m = 4 ; int [ , ] dp = new int [ n + 1 , k + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j <= k ; j ++ ) dp [ i , j ] = - 1 ; Console . WriteLine ( ( a [ 0 ] + maxValue ( a , n , 1 , m , k , dp ) ) ) ; } }
Maximize the Sum of a Subsequence from an Array based on given conditions | C # program to implement the above approach ; Function to select the array elements to maximize the sum of the selected elements ; If the entire array is solved ; Memoized subproblem ; Calculate sum considering the current element in the subsequence ; Calculate sum without considering the current element in the subsequence ; Update the maximum of the above sums in the [ , ] dp table ; Driver Code ; Initialize the dp array ; Function call
using System ; class GFG { public static int maximumSum ( int [ ] a , int count , int index , int n , int [ , ] dp ) { if ( index == n ) return 0 ; if ( dp [ index , count ] != - 1 ) return dp [ index , count ] ; int take_element = a [ index ] * count + maximumSum ( a , count + 1 , index + 1 , n , dp ) ; int dont_take = maximumSum ( a , count , index + 1 , n , dp ) ; return dp [ index , count ] = Math . Max ( take_element , dont_take ) ; } public static void Main ( String [ ] args ) { int n = 5 ; int [ ] a = { - 1 , - 9 , 0 , 5 , - 7 } ; int [ , ] dp = new int [ n + 1 , n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { for ( int j = 0 ; j < n + 1 ; j ++ ) { dp [ i , j ] = - 1 ; } } Console . WriteLine ( maximumSum ( a , 1 , 0 , n , dp ) ) ; } }
Minimum number of Nodes to be removed such that no subtree has more than K nodes | C # program for the above approach ; Variables used to store data globally ; Adjacency list representation of tree ; Function to perform DFS Traversal ; Mark the node as true ; Traverse adjacency list of child node ; If already visited then omit the node ; Add number of nodes in subtree ; Increment the count ; Return the nodes ; Function to add edges in graph ; Function that finds the number of nodes to be removed such that every subtree has size at most K ; Function Call to find the number of nodes to remove ; Print Removed Nodes ; Driver Code ; Creating list for all nodes ; Insert of nodes in graph ; Required subtree size ; Function Call
using System ; using System . Collections . Generic ; class GFG { static readonly int N = 20 ; static bool [ ] visited = new bool [ N ] ; static int K ; static int removals = 0 ; static List < int > removed_nodes = new List < int > ( ) ; static List < List < int > > adj = new List < List < int > > ( ) ; static int dfs ( int s ) { visited [ s ] = true ; int nodes = 1 ; foreach ( int child in adj [ s ] ) { if ( visited [ child ] ) continue ; nodes += dfs ( child ) ; } if ( nodes > K ) { removals ++ ; removed_nodes . Add ( s ) ; nodes = 0 ; } return nodes ; } static void addEdge ( int a , int b ) { adj [ a ] . Add ( b ) ; adj [ b ] . Add ( a ) ; } public static void findRemovedNodes ( int K ) { dfs ( 1 ) ; Console . WriteLine ( " Number ▁ of ▁ nodes " + " ▁ removed : ▁ " + removals ) ; Console . Write ( " Removed ▁ Nodes : ▁ " ) ; foreach ( int node in removed_nodes ) Console . Write ( node + " ▁ " ) ; } public static void Main ( String [ ] args ) { for ( int i = 0 ; i < N ; i ++ ) adj . Add ( new List < int > ( ) ) ; addEdge ( 1 , 2 ) ; addEdge ( 1 , 3 ) ; addEdge ( 2 , 4 ) ; addEdge ( 2 , 5 ) ; addEdge ( 3 , 6 ) ; K = 3 ; findRemovedNodes ( K ) ; } }