codeparrot/xlcost-text-to-code huggingface dataset:
Construct an N | C # program to implement the above approach ; Keep track of visited nodes ; Function to construct a tree such that there are no two adjacent nodes with the same weight ; If minimum and maximum elements are equal , i . e . array contains one distinct element ; Tree cannot be constructed ; Otherwise ; Tree can be constructed ; Choose weights [ 0 ] as root ; First Node is visited ; Traverse the array ; Otherwise , make an edge ; Mark this node as visited ; Find a weight not same as the root & make edges with that node ; Join non - roots with remaining nodes ; Check if current node ' s ▁ weight ▁ ▁ is ▁ same ▁ as ▁ root ▁ node ' s weight and if it is not visited or not ; Driver Code ; Function Call
using System ; using System . Linq ; class GFG { static int N = 100000 + 5 ; static int [ ] visited = new int [ N ] ; static void construct_tree ( int [ ] weights , int n ) { int minimum = weights . Min ( ) ; int maximum = weights . Max ( ) ; if ( minimum == maximum ) { Console . WriteLine ( " No " ) ; return ; } else { Console . WriteLine ( " Yes " ) ; } int root = weights [ 0 ] ; visited [ 1 ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( weights [ i ] != root && visited [ i + 1 ] == 0 ) { Console . WriteLine ( 1 + " ▁ " + ( i + 1 ) + " ▁ " ) ; visited [ i + 1 ] = 1 ; } } int notroot = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( weights [ i ] != root ) { notroot = i + 1 ; break ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( weights [ i ] == root && visited [ i + 1 ] == 0 ) { Console . WriteLine ( notroot + " ▁ " + ( i + 1 ) ) ; visited [ i + 1 ] = 1 ; } } } public static void Main ( ) { int [ ] weights = { 1 , 2 , 1 , 2 , 5 } ; int N = weights . Length ; construct_tree ( weights , N ) ; } }
Minimize cost to convert given string into concatenation of equal substrings of length K | C # program for the above approach ; Function to find minimum cost to convert given string into string of K length same substring ; Stores length of string ; Stores the minimum cost ; Traverse left substring of k length ; Stores the frequency ; Stores minimum cost for sequence of S [ i ] % k indices ; Check for optimal character ; Find sum of distance ' a ' + ch from character S [ i ] % k indices ; Choose minimum cost for each index i ; Increment ans ; Print minimum cost to convert string ; Driver Code ; Given string S ; Function call
using System ; class GFG { static void minCost ( string s , int k ) { int n = s . Length ; int ans = 0 ; for ( int i = 0 ; i < k ; i ++ ) { int [ ] a = new int [ 26 ] ; for ( int j = i ; j < n ; j += k ) { a [ s [ j ] - ' a ' ] ++ ; } int min_cost = Int32 . MaxValue ; for ( int ch = 0 ; ch < 26 ; ch ++ ) { int cost = 0 ; for ( int tr = 0 ; tr < 26 ; tr ++ ) cost += Math . Abs ( ch - tr ) * a [ tr ] ; min_cost = Math . Min ( min_cost , cost ) ; } ans += min_cost ; } Console . WriteLine ( ans ) ; } public static void Main ( ) { string S = " abcdefabc " ; int K = 3 ; minCost ( S , K ) ; } }
Split first N natural numbers into two sets with minimum absolute difference of their sums | C # program to implement the above approach ; Function to split the first N natural numbers into two sets having minimum absolute difference of their sums ; Driver Code
using System ; class GFG { static int minAbsDiff ( int N ) { if ( N % 4 == 0 N % 4 == 3 ) { return 0 ; } return 1 ; } public static void Main ( String [ ] args ) { int N = 6 ; Console . WriteLine ( minAbsDiff ( N ) ) ; } }
Find maximum matching in a given Binary Tree | C # program for the above approach ; Adjacency list to store edges ; Add an edge between U and V in tree ; Edge from u to v ; Edge from V to U ; Function that finds the maximum matching of the DFS ; Go further as we are not allowed to go towards its parent ; If U and its parent P is not taken then we must take & mark them as taken ; Increment size of edge set ; Function to find the maximum matching in a graph ; Taking 1 as a root of the tree ; Print maximum Matching ; Driver Code ; Joining edge between two nodes in tree ; Function call
using System ; using System . Collections . Generic ; class GFG { static readonly int N = 10000 ; static List < int > [ ] adj = new List < int > [ N ] ; static int [ ] used = new int [ N ] ; static int max_matching ; static void AddEdge ( int u , int v ) { adj [ u ] . Add ( v ) ; adj [ v ] . Add ( u ) ; } static void Matching_dfs ( int u , int p ) { for ( int i = 0 ; i < adj [ u ] . Count ; i ++ ) { if ( adj [ u ] [ i ] != p ) { Matching_dfs ( adj [ u ] [ i ] , u ) ; } } if ( used [ u ] == 0 && used [ p ] == 0 && p != 0 ) { max_matching ++ ; used [ u ] = used [ p ] = 1 ; } } static void maxMatching ( ) { Matching_dfs ( 1 , 0 ) ; Console . Write ( max_matching + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { for ( int i = 0 ; i < adj . Length ; i ++ ) adj [ i ] = new List < int > ( ) ; AddEdge ( 1 , 2 ) ; AddEdge ( 1 , 3 ) ; AddEdge ( 3 , 4 ) ; AddEdge ( 3 , 5 ) ; maxMatching ( ) ; } }
Minimize cost to Swap two given Arrays | C # program to implement the above approach ; Function to calculate and return the minimum cost required to swap two arrays ; Return the total minimum cost ; Driver Code
using System ; class GFG { static int getMinCost ( int [ ] A , int [ ] B , int N ) { int mini = int . MaxValue ; for ( int i = 0 ; i < N ; i ++ ) { mini = Math . Min ( mini , Math . Min ( A [ i ] , B [ i ] ) ) ; } return mini * ( 2 * N - 1 ) ; } public static void Main ( String [ ] args ) { int N = 3 ; int [ ] A = { 1 , 4 , 2 } ; int [ ] B = { 10 , 6 , 12 } ; Console . Write ( getMinCost ( A , B , N ) ) ; } }
Print all possible ways to write N as sum of two or more positive integers | C # program for the above approach ; Function to print the values stored in vector arr ; Traverse the vector arr ; Recursive function to print different ways in which N can be written as a sum of at 2 or more positive integers ; If n is zero then print this ways of breaking numbers ; Start from previous element in the representation till n ; Include current element from representation ; Call function again with reduced sum ; Backtrack to remove current element from representation ; Driver code ; Given sum N ; To store the representation of breaking N ; Function call
using System ; using System . Collections . Generic ; class GFG { static void printList ( List < int > arr ) { if ( arr . Count != 1 ) { for ( int i = 0 ; i < arr . Count ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } Console . WriteLine ( ) ; } } static void findWays ( List < int > arr , int i , int n ) { if ( n == 0 ) printList ( arr ) ; for ( int j = i ; j <= n ; j ++ ) { arr . Add ( j ) ; findWays ( arr , j , n - j ) ; arr . RemoveAt ( arr . Count - 1 ) ; } } public static void Main ( String [ ] args ) { int n = 4 ; List < int > arr = new List < int > ( ) ; findWays ( arr , 1 , n ) ; } }
Maximize count of Decreasing Subsequences from the given Array | C # program for rearrange array to generate maximum decreasing subsequences ; Function to count maximum subsequence ; Stores the frequency of array elements ; Stores maximum frequency ; Update frequency of A [ i ] ; Update maximum subsequences ; Print the result ; Driver Code
using System ; using System . Collections . Generic ; class GFG { public static void Maximum_subsequence ( int [ ] A , int N ) { Dictionary < int , int > frequency = new Dictionary < int , int > ( ) ; int max_freq = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( frequency . ContainsKey ( A [ i ] ) ) { frequency [ A [ i ] ] = frequency [ A [ i ] ] + 1 ; } else { frequency . Add ( A [ i ] , 1 ) ; } } foreach ( KeyValuePair < int , int > it in frequency ) { if ( ( int ) it . Value > max_freq ) { max_freq = ( int ) it . Value ; } } Console . WriteLine ( max_freq ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 2 , 6 , 5 , 2 , 4 , 5 , 2 } ; int N = arr . Length ; Maximum_subsequence ( arr , N ) ; } }
Check if a string can be split into two strings with same number of K | C # program for the above problem ; Function to print the arrangement of characters ; Stores frequency of characters ; Count the character having frequency K ; Count the character having frequency greater than K and not equal to 2 K ; Case 1 ; Case 2 ; Case 3 ; If all cases fail ; Driver code
using System ; using System . Collections . Generic ; class GFG { public static void DivideString ( string s , int n , int k ) { int i , c = 0 , no = 1 ; int c1 = 0 , c2 = 0 ; int [ ] fr = new int [ 26 ] ; char [ ] ans = new char [ n ] ; for ( i = 0 ; i < n ; i ++ ) { fr [ s [ i ] - ' a ' ] ++ ; } char ch = ' a ' , ch1 = ' a ' ; for ( i = 0 ; i < 26 ; i ++ ) { if ( fr [ i ] == k ) { c ++ ; } if ( fr [ i ] > k && fr [ i ] != 2 * k ) { c1 ++ ; ch = ( char ) ( i + ' a ' ) ; } if ( fr [ i ] == 2 * k ) { c2 ++ ; ch1 = ( char ) ( i + ' a ' ) ; } } for ( i = 0 ; i < n ; i ++ ) ans [ i ] = '1' ; Dictionary < char , int > mp = new Dictionary < char , int > ( ) ; if ( c % 2 == 0 c1 > 0 c2 > 0 ) { for ( i = 0 ; i < n ; i ++ ) { if ( fr [ s [ i ] - ' a ' ] == k ) { if ( mp . ContainsKey ( s [ i ] ) ) { ans [ i ] = '2' ; } else { if ( no <= ( c / 2 ) ) { ans [ i ] = '2' ; no ++ ; mp [ s [ i ] ] = 1 ; } } } } if ( ( c % 2 == 1 ) && ( c1 > 0 ) ) { no = 1 ; for ( i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ch && no <= k ) { ans [ i ] = '2' ; no ++ ; } } } if ( c % 2 == 1 && c1 == 0 ) { no = 1 ; int flag = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ch1 && no <= k ) { ans [ i ] = '2' ; no ++ ; } if ( fr [ s [ i ] - ' a ' ] == k && flag == 0 && ans [ i ] == '1' ) { ans [ i ] = '2' ; flag = 1 ; } } } Console . Write ( ans ) ; } else { Console . Write ( " NO " ) ; } } public static void Main ( string [ ] args ) { string S = " abbbccc " ; int N = S . Length ; int K = 1 ; DivideString ( S , N , K ) ; } }
Check if two items can be selected from two different categories without exceeding price | C # implementation to check if two items can be selected from two different categories without exceeding the total price ; Function to check if two items can be selected from two different categories without exceeding the total price ; Loop to choose two different pairs using two nested loops ; Condition to check if the price of these two elements is less than S ; Driver Code ; Function call
using System ; class GFG { static String check ( int S , int [ ] prices , int [ ] type , int n ) { for ( int j = 0 ; j < n ; j ++ ) { for ( int k = j + 1 ; k < n ; k ++ ) { if ( ( type [ j ] == 0 && type [ k ] == 1 ) || ( type [ j ] == 1 && type [ k ] == 0 ) ) { if ( prices [ j ] + prices [ k ] <= S ) { return " Yes " ; } } } } return " No " ; } public static void Main ( String [ ] args ) { int [ ] prices = { 3 , 8 , 6 , 5 } ; int [ ] type = { 0 , 1 , 1 , 0 } ; int S = 10 ; int n = 4 ; Console . Write ( check ( S , prices , type , n ) ) ; } }
Find the maximum sum ( a + b ) for a given input integer N satisfying the given condition | C # implementation to find the largest value of a + b satisfying the given condition ; Function to return the maximum sum of a + b satisfying the given condition ; Initialize max_sum ; Consider all possible pairs and check the sum divides product property ; To find the largest factor k ; Check if the product is divisible by the sum ; Storing the maximum sum in the max_sum variable ; Return the max_sum value ; Driver code
using System ; class GFG { static int getLargestSum ( int N ) { int max_sum = 0 ; for ( int i = 1 ; i * i <= N ; i ++ ) { for ( int j = i + 1 ; j * j <= N ; j ++ ) { int k = N / j ; int a = k * i ; int b = k * j ; if ( a <= N && b <= N && a * b % ( a + b ) == 0 ) max_sum = Math . Max ( max_sum , a + b ) ; } } return max_sum ; } static public void Main ( String [ ] args ) { int N = 25 ; int max_sum = getLargestSum ( N ) ; Console . Write ( max_sum + " STRNEWLINE " ) ; } }
Encrypt a string by repeating i | C # implementation of the approach ; Function to return the encrypted String ; Number of times the current character will be repeated ; Repeat the current character in the encrypted String ; Driver code
using System ; class GFG { static String encryptString ( String str , int n ) { int i = 0 , cnt = 0 ; String encryptedStr = " " ; while ( i < n ) { cnt = i + 1 ; while ( cnt -- > 0 ) encryptedStr += str [ i ] ; i ++ ; } return encryptedStr ; } static public void Main ( ) { String str = " geeks " ; int n = str . Length ; Console . WriteLine ( encryptString ( str , n ) ) ; } }
Minimize the difference between the maximum and minimum values of the modified array | C # program to find the minimum difference . ; Function to return required minimum difference ; finding minimum and maximum values ; returning minimum possible difference ; Driver program ; function to return the answer
using System ; class GFG { static int minDiff ( int n , int x , int [ ] A ) { int mn = A [ 0 ] , mx = A [ 0 ] ; for ( int i = 0 ; i < n ; ++ i ) { mn = Math . Min ( mn , A [ i ] ) ; mx = Math . Max ( mx , A [ i ] ) ; } return Math . Max ( 0 , mx - mn - 2 * x ) ; } public static void Main ( ) { int n = 3 , x = 3 ; int [ ] A = { 1 , 3 , 6 } ; Console . WriteLine ( minDiff ( n , x , A ) ) ; } }
Minimum Swaps for Bracket Balancing | C # Program to count swaps required to balance string ; stores the total number of Left and Right brackets encountered ; swap stores the number of swaps required imbalance maintains the number of imbalance pair ; increment count of Left bracket ; swaps count is last swap count + total number imbalanced brackets ; imbalance decremented by 1 as it solved only one imbalance of Left and Right ; increment count of Right bracket ; imbalance is reset to current difference between Left and Right brackets ; Driver code
using System ; class GFG { public static long swapCount ( string s ) { char [ ] chars = s . ToCharArray ( ) ; int countLeft = 0 , countRight = 0 ; int swap = 0 , imbalance = 0 ; for ( int i = 0 ; i < chars . Length ; i ++ ) { if ( chars [ i ] == ' [ ' ) { countLeft ++ ; if ( imbalance > 0 ) { swap += imbalance ; imbalance -- ; } } else if ( chars [ i ] == ' ] ' ) { countRight ++ ; imbalance = ( countRight - countLeft ) ; } } return swap ; } public static void Main ( string [ ] args ) { string s = " [ ] ] [ ] [ " ; Console . WriteLine ( swapCount ( s ) ) ; s = " [ [ ] [ ] ] " ; Console . WriteLine ( swapCount ( s ) ) ; } }
Longest subsequence from an array of pairs having first element increasing and second element decreasing . | C # program for the above approach ; Function to find the length of the longest subsequence of pairs whose first element is increasing and second is decreasing ; dp [ i ] : Stores the longest subsequence upto i ; Base case ; When the conditions hold ; Finally , print the required answer ; Driver Code ; Given Input ; Function Call
using System ; class GFG { static void longestSubSequence ( int [ , ] A , int N ) { int [ ] dp = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { dp [ i ] = 1 ; for ( int j = 0 ; j < i ; j ++ ) { if ( A [ j , 0 ] < A [ i , 0 ] && A [ j , 1 ] > A [ i , 1 ] ) { dp [ i ] = Math . Max ( dp [ i ] , dp [ j ] + 1 ) ; } } } Console . Write ( dp [ N - 1 ] ) ; } static void Main ( ) { int [ , ] A = { { 1 , 2 } , { 2 , 2 } , { 3 , 1 } } ; int N = A . GetLength ( 0 ) ; longestSubSequence ( A , N ) ; } }
Count ways to obtain given sum by repeated throws of a dice | C # Program for the above approach ; Function to calculate the total number of ways to have sum N ; Base Case ; Return already stored result ; Recur for all 6 states ; Return the result ; Driver Code ; Given sum N ; Initialize the dp array ; Function Call
using System ; class GFG { static int findWays ( int N , int [ ] dp ) { if ( N == 0 ) { return 1 ; } if ( dp [ N ] != - 1 ) { return dp [ N ] ; } int cnt = 0 ; for ( int i = 1 ; i <= 6 ; i ++ ) { if ( N - i >= 0 ) { cnt = cnt + findWays ( N - i , dp ) ; } } return dp [ N ] = cnt ; } public static void Main ( String [ ] args ) { int N = 4 ; int [ ] dp = new int [ N + 1 ] ; for ( int i = 0 ; i < dp . Length ; i ++ ) dp [ i ] = - 1 ; Console . Write ( findWays ( N , dp ) ) ; } }
Count ways to obtain given sum by repeated throws of a dice | C # program for the above approach ; Function to calculate the total number of ways to have sum N ; Initialize dp array ; Iterate over all the possible intermediate values to reach N ; Calculate the sum for all 6 faces ; Print the total number of ways ; Driver Code ; Given sum N ; Function call
using System ; class GFG { static void findWays ( int N ) { int [ ] dp = new int [ N + 1 ] ; dp [ 0 ] = 1 ; for ( int i = 1 ; i <= N ; i ++ ) { dp [ i ] = 0 ; for ( int j = 1 ; j <= 6 ; j ++ ) { if ( i - j >= 0 ) { dp [ i ] = dp [ i ] + dp [ i - j ] ; } } } Console . Write ( dp [ N ] ) ; } public static void Main ( String [ ] args ) { int N = 4 ; findWays ( N ) ; } }
Split the string into minimum parts such that each part is in the another string | C # implementation to split the String into minimum number of parts such that each part is also present in the another String ; Node of Trie ; Function to insert a node in the Trie Data Structure ; Inserting every character from idx till end to String into trie ; If there is no edge corresponding to the ith character , then make a new node ; Function to find the minimum number of parts such that each part is present into another String ; Making a new trie ; Inserting every subString of S2 in trie ; Creating dp array and init it with infinity ; Base Case ; Starting the cut from ith character taking temporary node pointer for checking whether the subString [ i , j ) is present in trie of not ; If the jth character is not in trie we 'll break ; Updating the the ending of jth character with dp [ i ] + 1 ; Descending the trie pointer ; Answer not possible ; Driver Code
using System ; class GFG { static int INF = ( int ) ( 1e9 + 9 ) ; class TrieNode { public TrieNode [ ] child = new TrieNode [ 26 ] ; } ; static void insert ( int idx , String s , TrieNode root ) { TrieNode temp = root ; for ( int i = idx ; i < s . Length ; i ++ ) { if ( temp . child [ s [ i ] - ' a ' ] == null ) temp . child [ s [ i ] - ' a ' ] = new TrieNode ( ) ; temp = temp . child [ s [ i ] - ' a ' ] ; } } static int minCuts ( String S1 , String S2 ) { int n1 = S1 . Length ; int n2 = S2 . Length ; TrieNode root = new TrieNode ( ) ; for ( int i = 0 ; i < n2 ; i ++ ) { insert ( i , S2 , root ) ; } int [ ] dp = new int [ n1 + 1 ] ; for ( int i = 0 ; i <= n1 ; i ++ ) dp [ i ] = INF ; dp [ 0 ] = 0 ; for ( int i = 0 ; i < n1 ; i ++ ) { TrieNode temp = root ; for ( int j = i + 1 ; j <= n1 ; j ++ ) { if ( temp . child [ S1 [ j - 1 ] - ' a ' ] == null ) break ; dp [ j ] = Math . Min ( dp [ j ] , dp [ i ] + 1 ) ; temp = temp . child [ S1 [ j - 1 ] - ' a ' ] ; } } if ( dp [ n1 ] >= INF ) return - 1 ; else return dp [ n1 ] ; } public static void Main ( String [ ] args ) { String S1 = " abcdab " ; String S2 = " dabc " ; Console . Write ( minCuts ( S1 , S2 ) ) ; } }
Largest Square in a Binary Matrix with at most K 1 s for multiple Queries | C # implementation to find the largest square in the matrix such that it contains atmost K 1 's  ; Function to find the largest square in the matrix such that it contains atmost K 1 's  ; Precomputation of the countDP prefix sum of the matrix ; Loop to solve each query ; Binary Search to the side which have atmost in K 1 's in square  ; Count total number of 1 s in the sub square considered ; If the count is less than or equals to the maximum move to right half ; Driver code
using System . Collections . Generic ; using System ; class GFG { static void largestSquare ( int [ , ] matrix , int R , int C , int [ ] q_i , int [ ] q_j , int K , int Q ) { int [ , ] countDP = new int [ R , C ] ; for ( int i = 0 ; i < R ; i ++ ) for ( int j = 0 ; j < C ; j ++ ) countDP [ i , j ] = 0 ; countDP [ 0 , 0 ] = matrix [ 0 , 0 ] ; for ( int i = 1 ; i < R ; i ++ ) countDP [ i , 0 ] = countDP [ i - 1 , 0 ] + matrix [ i , 0 ] ; for ( int j = 1 ; j < C ; j ++ ) countDP [ 0 , j ] = countDP [ 0 , j - 1 ] + matrix [ 0 , j ] ; for ( int i = 1 ; i < R ; i ++ ) for ( int j = 1 ; j < C ; j ++ ) countDP [ i , j ] = matrix [ i , j ] + countDP [ i - 1 , j ] + countDP [ i , j - 1 ] - countDP [ i - 1 , j - 1 ] ; for ( int q = 0 ; q < Q ; q ++ ) { int i = q_i [ q ] ; int j = q_j [ q ] ; int min_dist = Math . Min ( Math . Min ( i , j ) , Math . Min ( R - i - 1 , C - j - 1 ) ) ; int ans = - 1 , l = 0 , u = min_dist ; while ( l <= u ) { int mid = ( l + u ) / 2 ; int x1 = i - mid , x2 = i + mid ; int y1 = j - mid , y2 = j + mid ; int count = countDP [ x2 , y2 ] ; if ( x1 > 0 ) count -= countDP [ x1 - 1 , y2 ] ; if ( y1 > 0 ) count -= countDP [ x2 , y1 - 1 ] ; if ( x1 > 0 && y1 > 0 ) count += countDP [ x1 - 1 , y1 - 1 ] ; if ( count <= K ) { ans = 2 * mid + 1 ; l = mid + 1 ; } else u = mid - 1 ; } Console . WriteLine ( ans ) ; } } public static void Main ( ) { int [ , ] matrix = { { 1 , 0 , 1 , 0 , 0 } , { 1 , 0 , 1 , 1 , 1 } , { 1 , 1 , 1 , 1 , 1 } , { 1 , 0 , 0 , 1 , 0 } } ; int K = 9 , Q = 1 ; int [ ] q_i = { 1 } ; int [ ] q_j = { 2 } ; largestSquare ( matrix , 4 , 5 , q_i , q_j , K , Q ) ; } }