codeparrot/xlcost-text-to-code huggingface dataset:
Check if all array elements are pairwise co | C # program for the above approach ; Function to calculate GCD ; Function to calculate LCM ; Function to check if all elements in the array are pairwise coprime ; Initialize variables ; Iterate over the array ; Calculate product of array elements ; Calculate LCM of array elements ; If the product of array elements is equal to LCM of the array ; Driver Code ; Function call
using System ; using System . Collections . Generic ; class GFG { static long GCD ( long a , long b ) { if ( a == 0 ) return b ; return GCD ( b % a , a ) ; } static long LCM ( long a , long b ) { return ( a * b ) / GCD ( a , b ) ; } static void checkPairwiseCoPrime ( int [ ] A , int n ) { long prod = 1 ; long lcm = 1 ; for ( int i = 0 ; i < n ; i ++ ) { prod *= A [ i ] ; lcm = LCM ( A [ i ] , lcm ) ; } if ( prod == lcm ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } public static void Main ( String [ ] args ) { int [ ] A = { 2 , 3 , 5 } ; int n = A . Length ; checkPairwiseCoPrime ( A , n ) ; } }
Partition a set into two non | C # Program for above approach ; Function to return the maximum difference between the subset sums ; Stores the total sum of the array ; Checks for positive and negative elements ; Stores the minimum element from the given array ; Traverse the array ; Calculate total sum ; Mark positive element present in the set ; Mark negative element present in the set ; Find the minimum element of the set ; If the array contains both positive and negative elements ; Otherwise ; Driver Code ; Given the array ; Length of the array ; Function Call
using System ; class GFG { static int maxDiffSubsets ( int [ ] arr ) { int totalSum = 0 ; bool pos = false , neg = false ; int min = int . MaxValue ; for ( int i = 0 ; i < arr . Length ; i ++ ) { totalSum += Math . Abs ( arr [ i ] ) ; if ( arr [ i ] > 0 ) pos = true ; if ( arr [ i ] < 0 ) neg = true ; if ( arr [ i ] < min ) min = arr [ i ] ; } if ( pos && neg ) return totalSum ; else return totalSum - 2 * min ; } public static void Main ( String [ ] args ) { int [ ] S = { 1 , 2 , 1 } ; int N = S . Length ; if ( N < 2 ) Console . WriteLine ( " Not ▁ Possible " ) ; else Console . WriteLine ( maxDiffSubsets ( S ) ) ; } }
Split array into two subarrays such that difference of their sum is minimum | C # program for the above approach ; Function to return minimum difference between two subarray sums ; To store prefix sums ; Generate prefix sum array ; To store suffix sums ; Generate suffix sum array ; Stores the minimum difference ; Traverse the given array ; Calculate the difference ; Update minDiff ; Return minDiff ; Driver Code ; Given array ; Length of the array
using System ; class GFG { static int minDiffSubArray ( int [ ] arr , int n ) { int [ ] prefix_sum = new int [ n ] ; prefix_sum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) prefix_sum [ i ] = prefix_sum [ i - 1 ] + arr [ i ] ; int [ ] suffix_sum = new int [ n ] ; suffix_sum [ n - 1 ] = arr [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) suffix_sum [ i ] = suffix_sum [ i + 1 ] + arr [ i ] ; int minDiff = int . MaxValue ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int diff = Math . Abs ( prefix_sum [ i ] - suffix_sum [ i + 1 ] ) ; if ( diff < minDiff ) minDiff = diff ; } return minDiff ; } public static void Main ( String [ ] args ) { int [ ] arr = { 7 , 9 , 5 , 10 } ; int n = arr . Length ; Console . WriteLine ( minDiffSubArray ( arr , n ) ) ; } }
Count of days remaining for the next day with higher temperature | C # program for the above approach ; Function to determine how many days required to wait for the next warmer temperature ; To store the answer ; Traverse all the temperatures ; Check if current index is the next warmer temperature of any previous indexes ; Pop the element ; Push the current index ; Print waiting days ; Driver Code ; Given temperatures ; Function call
using System ; using System . Collections . Generic ; class GFG { static void dailyTemperatures ( int [ ] T ) { int n = T . Length ; int [ ] daysOfWait = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) daysOfWait [ i ] = - 1 ; Stack < int > s = new Stack < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { while ( s . Count != 0 && T [ s . Peek ( ) ] < T [ i ] ) { daysOfWait [ s . Peek ( ) ] = i - s . Peek ( ) ; s . Pop ( ) ; } s . Push ( i ) ; } for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( daysOfWait [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 73 , 74 , 75 , 71 , 69 , 72 , 76 , 73 } ; dailyTemperatures ( arr ) ; } }
Check if an array contains only one distinct element | C # program for the above approach ; Function to find if the array contains only one distinct element ; Create a set ; Traversing the array ; Compare and print the result ; Driver Code ; Function call
using System ; using System . Collections . Generic ; class GFG { public static void uniqueElement ( int [ ] arr ) { HashSet < int > set = new HashSet < int > ( ) ; for ( int i = 0 ; i < arr . Length ; i ++ ) { set . Add ( arr [ i ] ) ; } if ( set . Count == 1 ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 9 , 9 , 9 , 9 , 9 , 9 , 9 } ; uniqueElement ( arr ) ; } }
Maximum cost of splitting given Binary Tree into two halves | C # program for the above approach ; To store the results and sum of all nodes in the array ; To create adjacency list ; Function to add edges into the adjacency list ; Recursive function that calculate the value of the cost of splitting the tree recursively ; Fetch the child of node - r ; Neglect if cur node is parent ; Add all values of nodes which are decendents of r ; The two trees formed are rooted at ' r ' with its decendents ; Check and replace if current product t1 * t2 is large ; Function to find the maximum cost after splitting the tree in 2 halves ; Find sum of values in all nodes ; Traverse edges to create adjacency list ; Function Call ; Driver Code ; Values in each node ; Given Edges
using System ; using System . Collections . Generic ; class GFG { static int ans = 0 , allsum = 0 ; static List < int > [ ] edges = new List < int > [ 100001 ] ; static void addedge ( int a , int b ) { edges [ a ] . Add ( b ) ; edges [ b ] . Add ( a ) ; } static void findCost ( int r , int p , int [ ] arr ) { int i , cur ; for ( i = 0 ; i < edges [ r ] . Count ; i ++ ) { cur = edges [ r ] [ i ] ; if ( cur == p ) continue ; findCost ( cur , r , arr ) ; arr [ r ] += arr [ cur ] ; } int t1 = arr [ r ] ; int t2 = allsum - t1 ; if ( t1 * t2 > ans ) { ans = t1 * t2 ; } } static void maximumCost ( int r , int p , int N , int M , int [ ] arr , int [ , ] Edges ) { for ( int i = 0 ; i < N ; i ++ ) { allsum += arr [ i ] ; } for ( int i = 0 ; i < M ; i ++ ) { addedge ( Edges [ i , 0 ] , Edges [ i , 1 ] ) ; } findCost ( r , p , arr ) ; } public static void Main ( String [ ] args ) { int N = 6 ; int [ ] arr = { 13 , 8 , 7 , 4 , 5 , 9 } ; int M = 5 ; int [ , ] Edges = { { 0 , 1 } , { 1 , 2 } , { 1 , 4 } , { 3 , 4 } , { 4 , 5 } } ; for ( int i = 0 ; i < edges . Length ; i ++ ) edges [ i ] = new List < int > ( ) ; maximumCost ( 1 , - 1 , N , M , arr , Edges ) ; Console . Write ( ans ) ; } }
Diameters for each node of Tree after connecting it with given disconnected component | C # Program to implement the above approach ; Keeps track of the farthest end of the diameter ; Keeps track of the length of the diameter ; Stores the nodes which are at ends of the diameter ; Perform DFS on the given tree ; Update diameter and X ; If current node is an end of diameter ; Traverse its neighbors ; Function to call DFS for the required purposes ; DFS from a random node and find the node farthest from it ; DFS from X to calculate diameter ; DFS from farthest_node to find the farthest node ( s ) from it ; DFS from X ( other end of diameter ) and check the farthest node ( s ) from it ; If node i is the end of a diameter ; Increase diameter by 1 ; Otherwise ; Remains unchanged ; Driver Code ; constructed tree is 1 / \ 2 3 7 / | \ / | \ 4 5 6 ; creating undirected edges
using System ; using System . Collections . Generic ; class GFG { static int X = 1 ; static int diameter = 0 ; static Dictionary < int , Boolean > mp = new Dictionary < int , Boolean > ( ) ; static void dfs ( int current_node , int prev_node , int len , bool add_to_map , List < int > [ ] adj ) { if ( len > diameter ) { diameter = len ; X = current_node ; } if ( add_to_map && len == diameter ) { mp . Add ( current_node , true ) ; } foreach ( int it in adj [ current_node ] ) { if ( it != prev_node ) dfs ( it , current_node , len + 1 , add_to_map , adj ) ; } } static void dfsUtility ( List < int > [ ] adj ) { dfs ( 1 , - 1 , 0 , false , adj ) ; int farthest_node = X ; dfs ( farthest_node , - 1 , 0 , false , adj ) ; dfs ( farthest_node , - 1 , 0 , true , adj ) ; dfs ( X , - 1 , 0 , true , adj ) ; } static void printDiameters ( List < int > [ ] adj ) { dfsUtility ( adj ) ; for ( int i = 1 ; i <= 6 ; i ++ ) { if ( mp . ContainsKey ( i ) && mp [ i ] == true ) Console . Write ( diameter + 1 + " , ▁ " ) ; else Console . Write ( diameter + " , ▁ " ) ; } } public static void Main ( String [ ] args ) { List < int > [ ] adj = new List < int > [ 7 ] ; for ( int i = 0 ; i < adj . Length ; i ++ ) adj [ i ] = new List < int > ( ) ; adj [ 1 ] . Add ( 2 ) ; adj [ 2 ] . Add ( 1 ) ; adj [ 1 ] . Add ( 3 ) ; adj [ 3 ] . Add ( 1 ) ; adj [ 2 ] . Add ( 4 ) ; adj [ 4 ] . Add ( 2 ) ; adj [ 2 ] . Add ( 5 ) ; adj [ 5 ] . Add ( 2 ) ; adj [ 2 ] . Add ( 6 ) ; adj [ 6 ] . Add ( 2 ) ; printDiameters ( adj ) ; } }
Count of replacements required to make the sum of all Pairs of given type from the Array equal | C # program implement the above approach ; Function to find the minimum replacements required ; Stores the maximum and minimum values for every pair of the form arr [ i ] , arr [ n - i - 1 ] ; Map for storing frequencies of every sum formed by pairs ; Minimum element in the pair ; Maximum element in the pair ; Incrementing the frequency of sum encountered ; Insert minimum and maximum values ; Sorting the vectors ; Iterate over all possible values of x ; Count of pairs for which x > x + k ; Count of pairs for which x < mn + 1 ; Count of pairs requiring 2 replacements ; Count of pairs requiring no replacements ; Count of pairs requiring 1 replacement ; Update the answer ; Return the answer ; Driver Code
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int minimumReplacement ( int [ ] arr , int N , int K ) { int ans = int . MaxValue ; ArrayList max_values = new ArrayList ( ) ; ArrayList min_values = new ArrayList ( ) ; Dictionary < int , int > sum_equal_to_x = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N / 2 ; i ++ ) { int mn = Math . Min ( arr [ i ] , arr [ N - i - 1 ] ) ; int mx = Math . Max ( arr [ i ] , arr [ N - i - 1 ] ) ; sum_equal_to_x [ arr [ i ] + arr [ N - i - 1 ] ] = sum_equal_to_x . GetValueOrDefault ( arr [ i ] + arr [ N - i - 1 ] , 0 ) + 1 ; min_values . Add ( mn ) ; max_values . Add ( mx ) ; } max_values . Sort ( ) ; min_values . Sort ( ) ; for ( int x = 2 ; x <= 2 * K ; x ++ ) { int mp1 = max_values . IndexOf ( x - K ) ; int mp2 = min_values . IndexOf ( x ) ; int rep2 = mp1 + ( N / 2 - mp2 ) ; int rep0 = sum_equal_to_x . GetValueOrDefault ( x , - 1 ) ; int rep1 = ( N / 2 - rep2 - rep0 ) ; ans = Math . Min ( ans , rep2 * 2 + rep1 ) ; } return ans ; } public static void Main ( string [ ] args ) { int N = 4 ; int K = 3 ; int [ ] arr = { 1 , 2 , 2 , 1 } ; Console . Write ( minimumReplacement ( arr , N , K ) ) ; } }
Print the Forests of a Binary Tree after removal of given Nodes | C # program to implement the above approach ; Stores the nodes to be deleted ; Structure of a Tree node ; Function to create a new node ; Function to check whether the node needs to be deleted or not ; Function to perform tree pruning by performing post order traversal ; If the node needs to be deleted ; Store the its subtree ; Perform Inorder Traversal ; Function to print the forests ; Stores the remaining nodes ; Print the inorder traversal of Forests ; Driver Code
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 bool deleteNode ( int nodeVal ) { return mp . ContainsKey ( nodeVal ) ; } static Node treePruning ( Node root , List < Node > result ) { if ( root == null ) return null ; root . left = treePruning ( root . left , result ) ; root . right = treePruning ( root . right , result ) ; if ( deleteNode ( root . key ) ) { if ( root . left != null ) { result . Add ( root . left ) ; } if ( root . right != null ) { result . Add ( root . right ) ; } return null ; } return root ; } static void printInorderTree ( Node root ) { if ( root == null ) return ; printInorderTree ( root . left ) ; Console . Write ( root . key + " ▁ " ) ; printInorderTree ( root . right ) ; } static void printForests ( Node root , int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) { mp . Add ( arr [ i ] , true ) ; } List < Node > result = new List < Node > ( ) ; if ( treePruning ( root , result ) != null ) result . Add ( root ) ; for ( int i = 0 ; i < result . Count ; i ++ ) { printInorderTree ( result [ i ] ) ; Console . WriteLine ( ) ; } } public static void Main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 12 ) ; root . right = newNode ( 13 ) ; root . right . left = newNode ( 14 ) ; root . right . right = newNode ( 15 ) ; root . right . left . left = newNode ( 21 ) ; root . right . left . right = newNode ( 22 ) ; root . right . right . left = newNode ( 23 ) ; root . right . right . right = newNode ( 24 ) ; int [ ] arr = { 14 , 23 , 1 } ; int n = arr . Length ; printForests ( root , 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 ; If target + sum is odd or S exceeds sum ; No sultion exists ; Return the answer ; Driver Code
using System ; class GFG { static int knapSack ( int [ ] nums , int S ) { int sum = 0 ; foreach ( int i in nums ) sum += i ; if ( sum < S || - sum > - S || ( S + sum ) % 2 == 1 ) return 0 ; int [ ] dp = new int [ ( S + sum ) / 2 + 1 ] ; dp [ 0 ] = 1 ; foreach ( int num in nums ) { for ( int i = dp . Length - 1 ; i >= num ; i -- ) { dp [ i ] += dp [ i - num ] ; } } return dp [ dp . Length - 1 ] ; } public static void Main ( String [ ] args ) { int S = 3 ; int [ ] arr = new int [ ] { 1 , 2 , 3 , 4 , 5 } ; int answer = knapSack ( arr , S ) ; Console . WriteLine ( answer ) ; } }
Maximum distance between two elements whose absolute difference is K | C # program for the above approach ; Function that find maximum distance between two elements whose absolute difference is K ; To store the first index ; Traverse elements and find maximum distance between 2 elements whose absolute difference is K ; If this is first occurrence of element then insert its index in map ; If next element present in map then update max distance ; If previous element present in map then update max distance ; Return the answer ; Driver Code ; Given array [ ] arr ; Given difference K ; Function call
using System ; using System . Collections . Generic ; class GFG { static int maxDistance ( int [ ] arr , int K ) { Dictionary < int , int > map = new Dictionary < int , int > ( ) ; int maxDist = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( ! map . ContainsKey ( arr [ i ] ) ) map . Add ( arr [ i ] , i ) ; if ( map . ContainsKey ( arr [ i ] + K ) ) { maxDist = Math . Max ( maxDist , i - map [ arr [ i ] + K ] ) ; } if ( map . ContainsKey ( arr [ i ] - K ) ) { maxDist = Math . Max ( maxDist , i - map [ arr [ i ] - K ] ) ; } } if ( maxDist == 0 ) return - 1 ; else return maxDist ; } public static void Main ( String [ ] args ) { int [ ] arr = { 11 , 2 , 3 , 8 , 5 , 2 } ; int K = 2 ; Console . WriteLine ( maxDistance ( arr , K ) ) ; } }
Count of Substrings with at least K pairwise Distinct Characters having same Frequency | C # program for the above approach ; Function to find the subString with K pairwise distinct characters and with same frequency ; Stores the occurrence of each character in the subString ; Length of the String ; Iterate over the String ; Set all values at each index to zero ; Stores the count of unique characters ; Moving the subString ending at j ; Calculate the index of character in frequency array ; Increment the frequency ; Update the maximum index ; Check for both the conditions ; Return the answer ; Driver Code ; Function call
using System ; class GFG { static int no_of_subString ( String s , int N ) { int [ ] fre = new int [ 26 ] ; int str_len ; str_len = ( int ) s . Length ; int count = 0 ; for ( int i = 0 ; i < str_len ; i ++ ) { fre = new int [ 26 ] ; int max_index = 0 ; int dist = 0 ; for ( int j = i ; j < str_len ; j ++ ) { int x = s [ j ] - ' a ' ; if ( fre [ x ] == 0 ) dist ++ ; fre [ x ] ++ ; max_index = Math . Max ( max_index , fre [ x ] ) ; if ( dist >= N && ( ( max_index * dist ) == ( j - i + 1 ) ) ) count ++ ; } } return count ; } public static void Main ( String [ ] args ) { String s = " abhay " ; int N = 3 ; Console . Write ( no_of_subString ( s , N ) ) ; } }
Count of Ordered Pairs ( X , Y ) satisfying the Equation 1 / X + 1 / Y = 1 / N | C # program for the above approach ; Function to find number of ordered positive integer pairs ( x , y ) such that they satisfy the equation ; Initialize answer variable ; Iterate over all possible values of y ; For valid x and y , ( n * n ) % ( y - n ) has to be 0 ; Increment count of ordered pairs ; Print the answer ; Driver Code ; Function call
using System ; class GFG { static void solve ( int n ) { int ans = 0 ; for ( int y = n + 1 ; y <= n * n + n ; y ++ ) { if ( ( n * n ) % ( y - n ) == 0 ) { ans += 1 ; } } Console . Write ( ans ) ; } public static void Main ( String [ ] args ) { int n = 5 ; solve ( n ) ; } }
Count of Root to Leaf Paths consisting of at most M consecutive Nodes having value K | C # program to implement the above approach ; Initialize the adjacency list and visited array ; Function to find the number of root to leaf paths that contain atmost m consecutive nodes with value k ; Mark the current node as visited ; If value at current node is k ; Increment counter ; If count is greater than m return from that path ; Path is allowed if size of present node becomes 0 i . e it has no child root and no more than m consecutive 1 's ; Driver Code ; Desigining the tree ; Counter counts no . of consecutive nodes
using System ; using System . Collections . Generic ; class GFG { static List < int > [ ] adj = new List < int > [ 100005 ] ; static int [ ] visited = new int [ 100005 ] ; static int ans = 0 ; static void dfs ( int node , int count , int m , int [ ] arr , int k ) { visited [ node ] = 1 ; if ( arr [ node - 1 ] == k ) { count ++ ; } else { count = 0 ; } if ( count > m ) { return ; } if ( adj [ node ] . Count == 1 && node != 1 ) { ans ++ ; } foreach ( int x in adj [ node ] ) { if ( visited [ x ] == 0 ) { dfs ( x , count , m , arr , k ) ; } } } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 1 , 3 , 2 , 1 , 2 , 1 } ; int K = 2 , M = 2 ; for ( int i = 0 ; i < adj . Length ; i ++ ) adj [ i ] = new List < int > ( ) ; adj [ 1 ] . Add ( 2 ) ; adj [ 2 ] . Add ( 1 ) ; adj [ 1 ] . Add ( 3 ) ; adj [ 3 ] . Add ( 1 ) ; adj [ 2 ] . Add ( 4 ) ; adj [ 4 ] . Add ( 2 ) ; adj [ 2 ] . Add ( 5 ) ; adj [ 5 ] . Add ( 2 ) ; adj [ 3 ] . Add ( 6 ) ; adj [ 6 ] . Add ( 3 ) ; adj [ 3 ] . Add ( 7 ) ; adj [ 7 ] . Add ( 3 ) ; int counter = 0 ; dfs ( 1 , counter , M , arr , K ) ; Console . Write ( ans + " STRNEWLINE " ) ; } }
Count of N | C # program to implement the above approach ; Function to calculate and return the reverse of the number ; Function to calculate the total count of N - digit numbers satisfying the necessary conditions ; Initialize two variables ; Calculate the sum of odd and even positions ; Check for divisibility ; Return the answer ; Driver Code
using System ; class GFG { public static long reverse ( long num ) { long rev = 0 ; while ( num > 0 ) { int r = ( int ) ( num % 10 ) ; rev = rev * 10 + r ; num /= 10 ; } return rev ; } public static long count ( int N , int A , int B ) { long l = ( long ) Math . Pow ( 10 , N - 1 ) , r = ( long ) Math . Pow ( 10 , N ) - 1 ; if ( l == 1 ) l = 0 ; long ans = 0 ; for ( long i = l ; i <= r ; i ++ ) { int even_sum = 0 , odd_sum = 0 ; long itr = 0 , num = reverse ( i ) ; while ( num > 0 ) { if ( itr % 2 == 0 ) odd_sum += ( int ) num % 10 ; else even_sum += ( int ) num % 10 ; num /= 10 ; itr ++ ; } if ( even_sum % A == 0 && odd_sum % B == 0 ) ans ++ ; } return ans ; } public static void Main ( String [ ] args ) { int N = 2 , A = 5 , B = 3 ; Console . WriteLine ( count ( N , A , B ) ) ; } }
Distance Traveled by Two Trains together in the same Direction | C # program to find the distance traveled together by the two trains ; Function to find the distance traveled together ; Stores distance travelled by A ; Stpres distance travelled by B ; Stores the total distance travelled together ; Sum of distance travelled ; Condition for traveling together ; Driver Code
using System ; class GFG { static int calc_distance ( int [ ] A , int [ ] B , int n ) { int distance_traveled_A = 0 ; int distance_traveled_B = 0 ; int answer = 0 ; for ( int i = 0 ; i < 5 ; i ++ ) { distance_traveled_A += A [ i ] ; distance_traveled_B += B [ i ] ; if ( ( distance_traveled_A == distance_traveled_B ) && ( A [ i ] == B [ i ] ) ) { answer += A [ i ] ; } } return answer ; } public static void Main ( string [ ] s ) { int [ ] A = { 1 , 2 , 3 , 2 , 4 } ; int [ ] B = { 2 , 1 , 3 , 1 , 4 } ; int N = A . Length ; Console . Write ( calc_distance ( A , B , N ) ) ; } }