codeparrot/xlcost-text-to-code huggingface dataset:
Sum of Nodes and respective Neighbors on the path from root to a vertex V | C # program to implement the above approach ; Creating Adjacency list ; Function to perform DFS traversal ; Initializing parent of each node to p ; Iterate over the children ; Function to add values of children to respective parent nodes ; Root node ; Function to return sum of values of each node in path from V to the root ; Path from node V to root node ; Driver Code ; Insert edges into the graph ; Values assigned to each vertex ; Constructing the tree using adjacency list ; Parent array ; store values in the parent array ; Add values of children to the parent ; Find sum of nodes lying in the path ; Add root node since its value is not included yet
using System ; using System . Collections . Generic ; class GFG { private static List < List < int > > constructTree ( int n , int [ , ] edges ) { List < List < int > > adjl = new List < List < int > > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { adjl . Add ( new List < int > ( ) ) ; } for ( int i = 0 ; i < edges . GetLength ( 0 ) ; i ++ ) { int u = edges [ i , 0 ] ; int v = edges [ i , 1 ] ; adjl [ u ] . Add ( v ) ; adjl [ v ] . Add ( u ) ; } return adjl ; } private static void DFS ( List < List < int > > adjl , int [ ] parent , int u , int p ) { parent [ u ] = p ; foreach ( int v in adjl [ u ] ) { if ( v != p ) { DFS ( adjl , parent , v , u ) ; } } } private static int [ ] valuesFromChildren ( int [ ] parent , int [ ] values ) { int [ ] valuesChildren = new int [ parent . Length ] ; for ( int i = 0 ; i < parent . Length ; i ++ ) { if ( parent [ i ] == - 1 ) continue ; else { int p = parent [ i ] ; valuesChildren [ p ] += values [ i ] ; } } return valuesChildren ; } private static int findSumOfValues ( int v , int [ ] parent , int [ ] valuesChildren ) { int cur_node = v ; int sum = 0 ; while ( cur_node != - 1 ) { sum += valuesChildren [ cur_node ] ; cur_node = parent [ cur_node ] ; } return sum ; } public static void Main ( string [ ] args ) { int n = 8 ; int [ , ] edges = { { 0 , 1 } , { 0 , 2 } , { 0 , 3 } , { 1 , 4 } , { 1 , 5 } , { 4 , 7 } , { 3 , 6 } } ; int v = 7 ; int [ ] values = new int [ ] { 1 , 2 , 3 , 0 , 0 , 4 , 3 , 6 } ; List < List < int > > adjl = constructTree ( n , edges ) ; int [ ] parent = new int [ n ] ; DFS ( adjl , parent , 0 , - 1 ) ; int [ ] valuesChildren = valuesFromChildren ( parent , values ) ; int sum = findSumOfValues ( v , parent , valuesChildren ) ; sum += values [ 0 ] ; Console . WriteLine ( sum ) ; } }
Find a pair in Array with second largest product | C # program for the above approach ; Function to find second largest product pair in arr [ 0. . n - 1 ] ; No pair exits ; Initialize max product pair ; Traverse through every possible pair and keep track of largest product ; If pair is largest ; Second largest ; If pair dose not largest but larger then second largest ; Print the pairs ; Driver Code ; Given array ; Function call
using System ; class GFG { static void maxProduct ( int [ ] arr , int N ) { if ( N < 3 ) { return ; } int a = arr [ 0 ] , b = arr [ 1 ] ; int c = 0 , d = 0 ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = i + 1 ; j < N - 1 ; j ++ ) { if ( arr [ i ] * arr [ j ] > a * b ) { c = a ; d = b ; a = arr [ i ] ; b = arr [ j ] ; } if ( arr [ i ] * arr [ j ] < a * b && arr [ i ] * arr [ j ] > c * d ) c = arr [ i ] ; d = arr [ j ] ; } Console . WriteLine ( c + " ▁ " + d ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 2 , 67 , 45 , 160 , 78 } ; int N = arr . Length ; maxProduct ( arr , N ) ; } }
Count of substrings consisting of even number of vowels | C # program to implement the above approach ; Utility function to check if a character is a vowel ; Function to calculate and return the count of substrings with even number of vowels ; Stores the count of substrings ; If the current character is a vowel ; Increase count ; If substring contains even number of vowels ; Increase the answer ; Print the final answer ; Driver Code
using System ; class GFG { static bool isVowel ( char c ) { if ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) return true ; return false ; } static void countSubstrings ( String s , int n ) { int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int count = 0 ; for ( int j = i ; j < n ; j ++ ) { if ( isVowel ( s [ j ] ) ) { count ++ ; } if ( count % 2 == 0 ) result ++ ; } } Console . WriteLine ( result ) ; } public static void Main ( String [ ] args ) { int n = 5 ; String s = " abcde " ; countSubstrings ( s , n ) ; } }
Count of substrings consisting of even number of vowels | C # Program to implement the above approach ; Utility function to check if a character is a vowel ; Function to calculate and return the count of substrings with even number of vowels ; Stores the count of substrings with even and odd number of vowels respectively ; Update count of vowels modulo 2 in sum to obtain even or odd ; Increment even / odd count ; Count substrings with even number of vowels using Handshaking Lemma ; Driver Code
using System ; class GFG { static bool isVowel ( char c ) { if ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) return true ; return false ; } static void countSubstrings ( String s , int n ) { int [ ] temp = { 1 , 0 } ; int result = 0 , sum = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { sum += ( isVowel ( s [ i ] ) ? 1 : 0 ) ; sum %= 2 ; temp [ sum ] ++ ; } result += ( ( temp [ 0 ] * ( temp [ 0 ] - 1 ) ) / 2 ) ; result += ( ( temp [ 1 ] * ( temp [ 1 ] - 1 ) ) / 2 ) ; Console . Write ( result ) ; } public static void Main ( string [ ] args ) { int n = 5 ; String s = " abcde " ; countSubstrings ( s , n ) ; } }
Find the Level of a Binary Tree with Width K | C # program to implement the above approach ; Structure of binary tree node ; Function to create new node ; Function returns required level of width k , if found else - 1 ; To store the node and the label and perform traversal ; Taking the last label of each level of the tree ; Check width of current level ; If the width is equal to k then return that level ; Taking the first label of each level of the tree ; If any level does not has width equal to k , return - 1 ; Driver code
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { class Node { public int data ; public Node left , right ; } ; class pair { public Node first ; public int second ; public pair ( Node first , int second ) { this . first = first ; this . second = second ; } } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } static int findLevel ( Node root , int k , int level ) { Queue qt = new Queue ( ) ; qt . Enqueue ( new pair ( root , 0 ) ) ; int count = 1 , b = 0 , a = 0 ; while ( qt . Count != 0 ) { pair temp = ( pair ) qt . Dequeue ( ) ; if ( count == 1 ) { b = temp . second ; } if ( temp . first . left != null ) { qt . Enqueue ( new pair ( temp . first . left , 2 * temp . second ) ) ; } if ( temp . first . right != null ) { qt . Enqueue ( new pair ( temp . first . right , 2 * temp . second + 1 ) ) ; } count -- ; if ( count == 0 ) { if ( ( b - a + 1 ) == k ) return level ; pair secondLabel = ( pair ) qt . Peek ( ) ; a = secondLabel . second ; level += 1 ; count = qt . Count ; } } return - 1 ; } public static void Main ( string [ ] args ) { Node root = newNode ( 5 ) ; root . left = newNode ( 6 ) ; root . right = newNode ( 2 ) ; root . right . right = newNode ( 8 ) ; root . left . left = newNode ( 7 ) ; root . left . left . left = newNode ( 5 ) ; root . left . right = newNode ( 3 ) ; root . left . right . right = newNode ( 4 ) ; int k = 4 ; Console . Write ( findLevel ( root , k , 1 ) ) ; } }
Maximize median after doing K addition operation on the Array | C # program for the above approach ; Function to check operation can be perform or not ; Number of operation to perform s . t . mid is median ; If mid is median of the array ; Function to find max median of the array ; Lowest possible median ; Highest possible median ; Checking for mid is possible for the median of array after doing at most k operation ; Return the max possible ans ; Driver code ; Given array ; Given number of operation ; Size of array ; Sort the array ; Function call
using System ; class GFG { static bool possible ( int [ ] arr , int N , int mid , int K ) { int add = 0 ; for ( int i = N / 2 - ( N + 1 ) % 2 ; i < N ; ++ i ) { if ( mid - arr [ i ] > 0 ) { add += ( mid - arr [ i ] ) ; if ( add > K ) return false ; } } if ( add <= K ) return true ; else return false ; } static int findMaxMedian ( int [ ] arr , int N , int K ) { int low = 1 ; int mx = 0 ; for ( int i = 0 ; i < N ; ++ i ) { mx = Math . Max ( mx , arr [ i ] ) ; } int high = K + mx ; while ( low <= high ) { int mid = ( high + low ) / 2 ; if ( possible ( arr , N , mid , K ) ) { low = mid + 1 ; } else { high = mid - 1 ; } } if ( N % 2 == 0 ) { if ( low - 1 < arr [ N / 2 ] ) { return ( arr [ N / 2 ] + low - 1 ) / 2 ; } } return low - 1 ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 3 , 6 } ; int K = 10 ; int N = arr . Length ; Array . Sort ( arr ) ; Console . Write ( findMaxMedian ( arr , N , K ) ) ; } }
Kth Smallest Element of a Matrix of given dimensions filled with product of indices | C # program to implement the above approach ; Function that returns true if the count of elements is less than mid ; To store count of elements less than mid ; Loop through each row ; Count elements less than mid in the ith row ; Function that returns the Kth smallest element in the NxM Matrix after sorting in an array ; Initialize low and high ; Perform binary search ; Find the mid ; Check if the count of elements is less than mid ; Return Kth smallest element of the matrix ; Driver code
using System ; class GFG { public static bool countLessThanMid ( int mid , int N , int M , int K ) { int count = 0 ; for ( int i = 1 ; i <= Math . Min ( N , mid ) ; ++ i ) { count = count + Math . Min ( mid / i , M ) ; } if ( count >= K ) return false ; else return true ; } public static int findKthElement ( int N , int M , int K ) { int low = 1 , high = N * M ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( countLessThanMid ( mid , N , M , K ) ) low = mid + 1 ; else high = mid - 1 ; } return high + 1 ; } public static void Main ( String [ ] args ) { int N = 2 , M = 3 ; int K = 5 ; Console . WriteLine ( findKthElement ( N , M , K ) ) ; } }
Count of substrings containing only the given character | C # program for the above approach ; Function that finds the count of substrings containing only character C in the string S ; To store total count of substrings ; To store count of consecutive C 's ; Loop through the string ; Increase the consecutive count of C 's ; Add count of sub - strings from consecutive strings ; Reset the consecutive count of C 's ; Add count of sub - strings from consecutive strings ; Print the count of sub - strings containing only C ; Driver Code
using System ; class GFG { static void countSubString ( String S , char C ) { int count = 0 ; int conCount = 0 ; for ( int i = 0 ; i < S . Length ; i ++ ) { char ch = S [ i ] ; if ( ch == C ) conCount ++ ; else { count += ( conCount * ( conCount + 1 ) ) / 2 ; conCount = 0 ; } } count += ( conCount * ( conCount + 1 ) ) / 2 ; Console . Write ( count ) ; } public static void Main ( String [ ] args ) { String S = " geeksforgeeks " ; char C = ' e ' ; countSubString ( S , C ) ; } }
Check if both halves of a string are Palindrome or not | C # program to check whether both halves of a string is Palindrome or not ; Function to check and return if both halves of a string are palindromic or not ; Length of string ; Initialize both part as true ; If first half is not palindrome ; If second half is not palindrome ; If both halves are Palindrome ; Driver code
using System ; class GFG { public static void checkPalindrome ( String S ) { int N = S . Length ; bool first_half = true ; bool second_half = true ; int cnt = ( N / 2 ) - 1 ; for ( int i = 0 ; i < ( N / 2 ) ; i ++ ) { if ( S [ i ] != S [ cnt ] ) { first_half = false ; break ; } if ( S [ N / 2 + i ] != S [ N / 2 + cnt ] ) { second_half = false ; break ; } cnt -- ; } if ( first_half && second_half ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } public static void Main ( ) { String S = " momdad " ; checkPalindrome ( S ) ; } }
Kth Smallest element in a Perfect Binary Search Tree | C # program to find K - th smallest element in a perfect BST ; A BST node ; A utility function to create a new BST node ; A utility function to insert a new node with given key in BST ; If the tree is empty ; Recur down the left subtree for smaller values ; Recur down the right subtree for smaller values ; Return the ( unchanged ) node pointer ; Function to find Kth Smallest element in a perfect BST ; Find the median ( division operation is floored ) ; If the element is at the median ; calculate the number of nodes in the right and left sub - trees ( division operation is floored ) ; If median is located higher ; If median is located lower ; Driver Code ; Let us create following BST 50 / \ 30 70 / \ / \ 20 40 60 80 / \ / \ / \ / \ 14 25 35 45 55 65 75 85 ; Function call
using System ; class GFG { public class Node { public int key ; public Node left , right ; } ; static int kth_smallest ; public static Node newNode ( int item ) { Node temp = new Node ( ) ; temp . key = item ; temp . left = temp . right = null ; return temp ; } static Node insert ( Node node , int key ) { if ( node == null ) return newNode ( key ) ; if ( key < node . key ) node . left = insert ( node . left , key ) ; else if ( key > node . key ) node . right = insert ( node . right , key ) ; return node ; } static bool KSmallestPerfectBST ( Node root , int k , int treeSize ) { if ( root == null ) return false ; int median_loc = ( treeSize / 2 ) + 1 ; if ( k == median_loc ) { kth_smallest = root . key ; return true ; } int newTreeSize = treeSize / 2 ; if ( k < median_loc ) { return KSmallestPerfectBST ( root . left , k , newTreeSize ) ; } int newK = k - median_loc ; return KSmallestPerfectBST ( root . right , newK , newTreeSize ) ; } public static void Main ( String [ ] args ) { Node root = null ; root = insert ( root , 50 ) ; insert ( root , 30 ) ; insert ( root , 20 ) ; insert ( root , 40 ) ; insert ( root , 70 ) ; insert ( root , 60 ) ; insert ( root , 80 ) ; insert ( root , 14 ) ; insert ( root , 25 ) ; insert ( root , 35 ) ; insert ( root , 45 ) ; insert ( root , 55 ) ; insert ( root , 65 ) ; insert ( root , 75 ) ; insert ( root , 85 ) ; int n = 15 , k = 5 ; if ( KSmallestPerfectBST ( root , k , n ) ) { Console . Write ( kth_smallest + " ▁ " ) ; } } }
Check if given Binary string follows then given condition or not | C # program for the above approach ; Function to check if the String follows rules or not ; Check for the first condition ; Check for the third condition ; Check for the second condition ; Driver Code ; Given String str ; Function call
using System ; class GFG { static bool checkrules ( String s ) { if ( s . Length == 0 ) return true ; if ( s [ 0 ] != '1' ) return false ; if ( s . Length > 2 ) { if ( s [ 1 ] == '0' && s [ 2 ] == '0' ) return checkrules ( s . Substring ( 3 ) ) ; } return checkrules ( s . Substring ( 1 ) ) ; } public static void Main ( String [ ] args ) { String str = "1111" ; if ( checkrules ( str ) ) { Console . Write ( " Valid ▁ String " ) ; } else { Console . Write ( " Invalid ▁ String " ) ; } } }
Length of longest subsequence in an Array having all elements as Nude Numbers | C # program for the above approach ; Function to check if the number is a Nude number ; Variable initialization ; int ' copy ' is converted to a String ; Total digits in the number ; Loop through all digits and check if every digit divides n or not ; flag is used to keep check ; Return true or false as per the condition ; Function to find the longest subsequence which contain all Nude numbers ; Find the length of longest Nude number subsequence ; Driver Code ; Given array [ ] arr ; Function call
using System ; class GFG { static bool isNudeNum ( int n ) { int copy , length , flag = 0 ; copy = n ; String temp ; temp = String . Join ( " " , copy ) ; length = temp . Length ; for ( int i = 0 ; i < length ; i ++ ) { int num = temp [ i ] - '0' ; if ( num == 0 n % num != 0 ) { flag = 1 ; } } if ( flag == 1 ) return false ; else return true ; } static int longestNudeSubseq ( int [ ] arr , int n ) { int answer = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isNudeNum ( arr [ i ] ) ) answer ++ ; } return answer ; } public static void Main ( String [ ] args ) { int [ ] arr = { 34 , 34 , 2 , 2 , 3 , 333 , 221 , 32 } ; int n = arr . Length ; Console . Write ( longestNudeSubseq ( arr , n ) + " STRNEWLINE " ) ; } }
Longest subarray with difference exactly K between any two distinct values | C # implementation to find the longest subarray consisting of only two values with difference K ; Function to return the length of the longest sub - array ; Initialize set ; Store 1 st element of sub - array into set ; Check absolute difference between two elements ; If the new element is not present in the set ; If the set contains two elements ; Otherwise ; Update the maximum length ; Remove the set elements ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int longestSubarray ( int [ ] arr , int n , int k ) { int i , j , Max = 1 ; HashSet < int > s = new HashSet < int > ( ) ; for ( i = 0 ; i < n - 1 ; i ++ ) { s . Add ( arr [ i ] ) ; for ( j = i + 1 ; j < n ; j ++ ) { if ( Math . Abs ( arr [ i ] - arr [ j ] ) == 0 || Math . Abs ( arr [ i ] - arr [ j ] ) == k ) { if ( ! s . Contains ( arr [ j ] ) ) { if ( s . Count == 2 ) break ; else s . Add ( arr [ j ] ) ; } } else break ; } if ( s . Count == 2 ) { Max = Math . Max ( Max , j - i ) ; s . Clear ( ) ; } else s . Clear ( ) ; } return Max ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 0 , 2 , 2 , 5 , 5 , 5 } ; int N = arr . Length ; int K = 1 ; int length = longestSubarray ( arr , N , K ) ; if ( length == 1 ) Console . Write ( - 1 ) ; else Console . Write ( length ) ; } }
Longest subarray of non | C # program for the above approach ; Function to find the maximum length of a subarray of 1 s after removing at most one 0 ; Stores the count of consecutive 1 's from left ; Stores the count of consecutive 1 's from right ; Traverse left to right ; If cell is non - empty ; Increase count ; If cell is empty ; Store the count of consecutive 1 's  till (i - 1)-th index ; Traverse from right to left ; Store the count of consecutive 1 s till ( i + 1 ) - th index ; Stores the length of longest subarray ; Store the maximum ; If array a contains only 1 s return n else return ans ; Driver code
using System ; class GFG { public static int longestSubarray ( int [ ] a , int n ) { int [ ] l = new int [ n ] ; int [ ] r = new int [ n ] ; for ( int i = 0 , count = 0 ; i < n ; i ++ ) { if ( a [ i ] == 1 ) count ++ ; else { l [ i ] = count ; count = 0 ; } } for ( int i = n - 1 , count = 0 ; i >= 0 ; i -- ) { if ( a [ i ] == 1 ) count ++ ; else { r [ i ] = count ; count = 0 ; } } int ans = - 1 ; for ( int i = 0 ; i < n ; ++ i ) { if ( a [ i ] == 0 ) ans = Math . Max ( ans , l [ i ] + r [ i ] ) ; } return ans < 0 ? n : ans ; } public static void Main ( ) { int [ ] arr = { 0 , 1 , 1 , 1 , 0 , 1 , 0 , 1 , 1 } ; int n = arr . Length ; Console . Write ( longestSubarray ( arr , n ) ) ; } }
Find largest factor of N such that N / F is less than K | C # program for the above approach ; Function to find the value of X ; Loop to check all the numbers divisible by N that yield minimum N / i value ; Print the value of packages ; Driver code ; Given N and K ; Function call
using System ; class GFG { static void findMaxValue ( int N , int K ) { int packages ; int maxi = 1 ; for ( int i = 1 ; i <= K ; i ++ ) { if ( N % i == 0 ) maxi = Math . Max ( maxi , i ) ; } packages = N / maxi ; Console . WriteLine ( packages ) ; } public static void Main ( String [ ] args ) { int N = 8 , K = 7 ; findMaxValue ( N , K ) ; } }
Largest square which can be formed using given rectangular blocks | C # program for the above approach ; Function to find maximum side length of square ; Sort array in asc order ; Traverse array in desc order ; Driver code ; Given array arr [ ] ; Function Call
using System ; class GFG { static void maxSide ( int [ ] a , int n ) { int sideLength = 0 ; Array . Sort ( a ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( a [ i ] > sideLength ) { sideLength ++ ; } else { break ; } } Console . Write ( sideLength ) ; } public static void Main ( ) { int N = 6 ; int [ ] arr = new int [ ] { 3 , 2 , 1 , 5 , 2 , 4 } ; maxSide ( arr , N ) ; } }
Minimize difference after changing all odd elements to even | C # program for the above approach ; Function to minimize the difference between two elements of array ; Find all the element which are possible by multiplying 2 to odd numbers ; Sort the array ; Find the minimum difference Iterate and find which adjacent elements have the minimum difference ; Print the minimum difference ; Driver Code ; Given array ; Function call
using System ; class GFG { public static void minDiff ( long [ ] a , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) a [ i ] *= 2 ; } Array . Sort ( a ) ; long mindifference = a [ 1 ] - a [ 0 ] ; for ( int i = 1 ; i < a . Length ; i ++ ) { mindifference = Math . Min ( mindifference , a [ i ] - a [ i - 1 ] ) ; } Console . Write ( mindifference ) ; } public static void Main ( ) { long [ ] arr = { 3 , 8 , 13 , 30 , 50 } ; int n = arr . Length ; minDiff ( arr , n ) ; } }
Longest subarray having sum K | Set 2 | C # program for the above approach ; To store the prefix sum array ; Function for searching the lower bound of the subarray ; Iterate until low less than equal to high ; For each mid finding sum of sub array less than or equal to k ; Return the final answer ; Function to find the length of subarray with sum K ; Initialize sum to 0 ; Push the prefix sum of the array [ ] arr in prefix [ ] ; Search r for each i ; Update ans ; Print the length of subarray found in the array ; Driver Code ; Given array [ ] arr ; Given sum K ; Function call
using System ; using System . Collections . Generic ; class GFG { static List < int > v = new List < int > ( ) ; static int bin ( int val , int k , int n ) { int lo = 0 ; int hi = n ; int mid ; int ans = - 1 ; while ( lo <= hi ) { mid = lo + ( hi - lo ) / 2 ; if ( v [ mid ] - val <= k ) { lo = mid + 1 ; ans = mid ; } else hi = mid - 1 ; } return ans ; } static void findSubarraySumK ( int [ ] arr , int N , int K ) { int sum = 0 ; v . Add ( 0 ) ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; v . Add ( sum ) ; } int ans = 0 , r ; for ( int i = 0 ; i < v . Count ; i ++ ) { r = bin ( v [ i ] , K , N ) ; ans = Math . Max ( ans , r - i ) ; } Console . Write ( ans ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 6 , 8 , 14 , 9 , 4 , 11 , 10 } ; int N = arr . Length ; int K = 13 ; findSubarraySumK ( arr , N , K ) ; } }