codeparrot/xlcost-text-to-code huggingface dataset:
Minimum operations to make Array equal by repeatedly adding K from an element and subtracting K from other | C # program for the above approach ; Function to find the minimum number of operations to make all the elements of the array equal ; Store the sum of the array arr [ ] ; Traverse through the array ; If it is not possible to make all array element equal ; Store the minimum number of operations needed ; Traverse through the array ; Finally , print the minimum number operation to make array elements equal ; Given Input ; Function Call
using System ; class GFG { static void miniOperToMakeAllEleEqual ( int [ ] arr , int n , int k ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } if ( sum % n != 0 ) { Console . WriteLine ( - 1 ) ; return ; } int valueAfterDivision = sum / n ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( Math . Abs ( valueAfterDivision - arr [ i ] ) % k != 0 ) { Console . WriteLine ( - 1 ) ; return ; } count += Math . Abs ( valueAfterDivision - arr [ i ] ) / k ; } Console . WriteLine ( ( int ) count / 2 ) ; } static void Main ( ) { int n = 3 , k = 3 ; int [ ] arr = { 5 , 8 , 11 } ; miniOperToMakeAllEleEqual ( arr , n , k ) ; } }
Check if a string is a scrambled form of another string | C # Program to check if a given string is a scrambled form of another string ; map declaration for storing key value pair means for storing subproblem result ; Strings of non - equal length cant ' be scramble strings ; Empty strings are scramble strings ; Equal strings are scramble strings ; Check for the condition of anagram ; make key of type string for search in map ; checking if both string are before calculated or not if calculated means find in map then return it 's  value ; declaring flag variable to store result ; Check if S2 [ 0. . . i ] is a scrambled string of S1 [ 0. . . i ] and if S2 [ i + 1. . . n ] is a scrambled string of S1 [ i + 1. . . n ] ; Check if S2 [ 0. . . i ] is a scrambled string of S1 [ n - i ... n ] and S2 [ i + 1. . . n ] is a scramble string of S1 [ 0. . . n - i - 1 ] ; add key & flag value to map ( store for future use ) so next time no required to calculate it again ; If none of the above conditions are satisfied ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static Dictionary < string , bool > mp = new Dictionary < string , bool > ( ) ; static bool isScramble ( string S1 , string S2 ) { if ( S1 . Length != S2 . Length ) { return false ; } int n = S1 . Length ; if ( n == 0 ) { return true ; } if ( S1 == S2 ) { return true ; } string copy_S1 = S1 , copy_S2 = S2 ; char [ ] t1 = copy_S1 . ToCharArray ( ) ; char [ ] t2 = copy_S2 . ToCharArray ( ) ; Array . Sort ( t1 ) ; Array . Sort ( t2 ) ; copy_S1 = new string ( t1 ) ; copy_S2 = new string ( t2 ) ; if ( copy_S1 != copy_S2 ) { return false ; } string key = ( S1 + " ▁ " + S2 ) ; if ( mp . ContainsKey ( key ) ) { return mp [ key ] ; } bool flag = false ; for ( int i = 1 ; i < n ; i ++ ) { if ( isScramble ( S1 . Substring ( 0 , i ) , S2 . Substring ( 0 , i ) ) && isScramble ( S1 . Substring ( i , n - i ) , S2 . Substring ( i , n - i ) ) ) { flag = true ; return true ; } if ( isScramble ( S1 . Substring ( 0 , i ) , S2 . Substring ( n - i , i ) ) && isScramble ( S1 . Substring ( i , n - i ) , S2 . Substring ( 0 , n - i ) ) ) { flag = true ; return true ; } } mp [ key ] = flag ; return false ; } static void Main ( ) { string S1 = " coder " ; string S2 = " ocred " ; if ( isScramble ( S1 , S2 ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }
Print the longest palindromic prefix of a given string | C # program for the above approach ; Function to find the longest prefix which is palindromic ; Find the length of the given String ; For storing the length of longest prefix palindrome ; Loop to check the subString of all length from 1 to N which is palindrome ; String of length i ; To store the reversed of temp ; Reversing String temp2 ; If String temp is palindromic then update the length ; Print the palindromic String of max_len ; Driver Code ; Given String ; Function Call
using System ; class GFG { static void longestPalindromicPrefix ( String s ) { int n = s . Length ; int max_len = 0 ; for ( int len = 1 ; len <= n ; len ++ ) { String temp = s . Substring ( 0 , len ) ; String temp2 = temp ; temp2 = reverse ( temp2 ) ; if ( temp . Equals ( temp2 ) ) { max_len = len ; } } Console . Write ( s . Substring ( 0 , max_len ) ) ; } static String reverse ( String input ) { char [ ] a = input . ToCharArray ( ) ; int l , r = a . Length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . Join ( " " , a ) ; } public static void Main ( String [ ] args ) { String str = " abaab " ; longestPalindromicPrefix ( str ) ; } }
K | C # program to find the K - th lexicographical String of length N ; Initialize the array to store the base 26 representation of K with all zeroes , that is , the initial String consists of N a 's ; Start filling all the N slots for the base 26 representation of K ; Store the remainder ; Reduce K ; If K is greater than the possible number of Strings that can be represented by a String of length N ; Store the Kth lexicographical String ; Driver Code ; Reducing k value by 1 because our stored value starts from 0
using System ; class GFG { static String find_kth_String_of_n ( int n , int k ) { int [ ] d = new int [ n ] ; for ( int i = n - 1 ; i > - 1 ; i -- ) { d [ i ] = k % 26 ; k /= 26 ; } if ( k > 0 ) return " - 1" ; string s = " " ; for ( int i = 0 ; i < n ; i ++ ) s += ( char ) ( d [ i ] + ( ' a ' ) ) ; return s ; } public static void Main ( ) { int n = 3 ; int k = 10 ; k -= 1 ; Console . Write ( find_kth_String_of_n ( n , k ) ) ; } }
Length of the smallest substring which contains all vowels | C # program to find the length of the smallest substring of which contains all vowels ; Map to store the frequency of vowels ; Store the indices which contains the vowels ; If all vowels are not present in the string ; If the frequency of the vowel at i - th index exceeds 1 ; Decrease the frequency of that vowel ; Move to the left ; Otherwise set flag1 ; If the frequency of the vowel at j - th index exceeds 1 ; Decrease the frequency of that vowel ; Move to the right ; Otherwise set flag2 ; If both flag1 and flag2 are set , break out of the loop as the substring length cannot be minimized ; Return the length of the substring ; Driver Code
using System ; using System . Collections . Generic ; class GFG { public static int findMinLength ( String s ) { int n = s . Length ; int i = 0 ; Dictionary < char , int > counts = new Dictionary < char , int > ( ) ; List < int > indices = new List < int > ( ) ; for ( i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' a ' s [ i ] == ' e ' s [ i ] == ' o ' s [ i ] == ' i ' s [ i ] == ' u ' ) { if ( counts . ContainsKey ( s [ i ] ) ) { counts [ s [ i ] ] = counts [ s [ i ] ] + 1 ; } else { counts . Add ( s [ i ] , 1 ) ; } indices . Add ( i ) ; } } if ( counts . Count < 5 ) return - 1 ; int flag1 = 0 , flag2 = 0 ; i = 0 ; int j = indices . Count - 1 ; while ( ( j - i ) >= 4 ) { if ( flag1 == 0 && counts [ s [ indices [ i ] ] ] > 1 ) { counts [ s [ indices [ i ] ] ] = counts [ s [ indices [ i ] ] ] - 1 ; i ++ ; } else flag1 = 1 ; if ( flag2 == 0 && counts [ s [ indices [ j ] ] ] > 1 ) { counts [ s [ indices [ j ] ] ] = counts [ s [ indices [ j ] ] ] - 1 ; j -- ; } else flag2 = 1 ; if ( flag1 == 1 && flag2 == 1 ) break ; } return ( indices [ j ] - indices [ i ] + 1 ) ; } public static void Main ( String [ ] args ) { String s = " aaeebbeaccaaoiuooooooooiuu " ; Console . Write ( findMinLength ( s ) ) ; } }
Longest Subsequence of a String containing only Consonants | C # program to find the longest subsequence which contain all consonants ; Returns true if x is consonants . ; Function to check whether a character is consonants or not ; Function to find the longest subsequence which contain all consonants ; Driver code ; Function call
using System ; class GFG { static bool isConsonants ( char x ) { x = Char . ToLower ( x ) ; return ! ( x == ' a ' x == ' e ' x == ' i ' x == ' o ' x == ' u ' ) ; } static string longestConsonantsSubsequence ( string str ) { string answer = " " ; int n = str . Length ; for ( int i = 0 ; i < n ; i ++ ) { if ( isConsonants ( str [ i ] ) ) { answer += str [ i ] ; } } return answer ; } static void Main ( ) { string str = " geeksforgeeks " ; Console . Write ( longestConsonantsSubsequence ( str ) + " STRNEWLINE " ) ; } }
Find the length of the longest subsequence with first K alphabets having same frequency | C # program to find the longest subsequence with first K alphabets having same frequency ; Function to return the length of the longest subsequence with first K alphabets having same frequency ; Store frequency of all characters in the string ; Variable to store the frequency of the least frequent of first K alphabets ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int lengthOfSubsequence ( string str , int K ) { Dictionary < char , int > mp = new Dictionary < char , int > ( ) ; foreach ( char ch in str . ToCharArray ( ) ) { mp [ ch ] = mp . GetValueOrDefault ( ch , 0 ) + 1 ; } int minimum = mp [ ' A ' ] ; for ( int i = 1 ; i < K ; i ++ ) { minimum = Math . Min ( minimum , mp [ ( char ) ( i + ' A ' ) ] ) ; } return minimum * K ; } public static void Main ( string [ ] args ) { string str = " ACAABCCAB " ; int K = 3 ; Console . Write ( lengthOfSubsequence ( str , K ) ) ; } }
Detecting negative cycle using Floyd Warshall | C # Program to check if there is a negative weight cycle using Floyd Warshall Algorithm ; Number of vertices in the graph ; Define Infinite as a large enough value . This value will be used for vertices not connected to each other ; Returns true if graph has negative weight cycle else false . ; dist [ ] [ ] will be the output matrix that will finally have the shortest distances between every pair of vertices ; Initialize the solution matrix same as input graph matrix . Or we can say the initial values of shortest distances are based on shortest paths considering no intermediate vertex . ; Add all vertices one by one to the set of intermediate vertices . -- -> Before start of a iteration , we have shortest distances between all pairs of vertices such that the shortest distances consider only the vertices in set { 0 , 1 , 2 , . . k - 1 } as intermediate vertices . -- -- > After the end of a iteration , vertex no . k is added to the set of intermediate vertices and the set becomes { 0 , 1 , 2 , . . k } ; Pick all vertices as source one by one ; Pick all vertices as destination for the above picked source ; If vertex k is on the shortest path from i to j , then update the value of dist [ i ] [ j ] ; If distance of any verex from itself becomes negative , then there is a negative weight cycle . ; Driver code ; Let us create the following weighted graph 1 ( 0 ) -- -- -- -- -- -> ( 1 ) / | \ | | | - 1 | | - 1 | \ | / ( 3 ) < -- -- -- -- -- - ( 2 ) - 1
using System ; namespace Cycle { public class GFG { static int V = 4 ; static int INF = 99999 ; static bool negCyclefloydWarshall ( int [ , ] graph ) { int [ , ] dist = new int [ V , V ] ; int i , j , k ; for ( i = 0 ; i < V ; i ++ ) for ( j = 0 ; j < V ; j ++ ) dist [ i , j ] = graph [ i , j ] ; for ( k = 0 ; k < V ; k ++ ) { for ( i = 0 ; i < V ; i ++ ) { for ( j = 0 ; j < V ; j ++ ) { if ( dist [ i , k ] + dist [ k , j ] < dist [ i , j ] ) dist [ i , j ] = dist [ i , k ] + dist [ k , j ] ; } } } for ( i = 0 ; i < V ; i ++ ) if ( dist [ i , i ] < 0 ) return true ; return false ; } public static void Main ( ) { int [ , ] graph = { { 0 , 1 , INF , INF } , { INF , 0 , - 1 , INF } , { INF , INF , 0 , - 1 } , { - 1 , INF , INF , 0 } } ; if ( negCyclefloydWarshall ( graph ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } } }
Digital Root of a given large integer using Recursion | C # program to print the digital root of a given very large number ; Function to convert given sum into String ; Loop to extract digit one by one from the given sum and concatenate into the String ; Type casting for concatenation ; Return converted String ; Function to get individual digit sum from String ; Loop to get individual digit sum ; Function call to convert sum into String ; Function to calculate the digital root of a very large number ; Base condition ; Function call to get individual digit sum ; Recursive function to get digital root of a very large number ; Driver code ; Function to print readonly digit
using System ; class GFG { static String convertToString ( int sum ) { String str = " " ; while ( sum > 0 ) { str = str + ( char ) ( ( sum % 10 ) + '0' ) ; sum = sum / 10 ; } return str ; } static String GetIndividulaDigitSum ( String str , int len ) { int sum = 0 ; for ( int i = 0 ; i < len ; i ++ ) { sum = sum + str [ i ] - '0' ; } return convertToString ( sum ) ; } static int GetDigitalRoot ( String str ) { if ( str . Length == 1 ) { return str [ 0 ] - '0' ; } str = GetIndividulaDigitSum ( str , str . Length ) ; return GetDigitalRoot ( str ) ; } public static void Main ( String [ ] args ) { String str = "675987890789756545689070986776987" ; Console . Write ( GetDigitalRoot ( str ) ) ; } }
Count the Number of matching characters in a pair of strings | C # code to count number of matching characters in a pair of strings ; Function to count the matching characters ; Traverse the string 1 char by char ; This will check if str1 [ i ] is present in str2 or not str2 . find ( str1 [ i ] ) returns - 1 if not found otherwise it returns the starting occurrence index of that character in str2 ; Driver code
using System ; class GFG { static void count ( string str1 , string str2 ) { int c = 0 , j = 0 ; for ( int i = 0 ; i < str1 . Length ; i ++ ) { if ( str2 . IndexOf ( str1 [ i ] ) >= 0 ) { c += 1 ; } } Console . WriteLine ( " No . ▁ of ▁ matching ▁ characters ▁ are : ▁ " + c ) ; } public static void Main ( ) { string str1 = " aabcddekll12 @ " ; string str2 = " bb2211@55k " ; count ( str1 , str2 ) ; } }
Check if count of Alphabets and count of Numbers are equal in the given String | C # program to check if the count of alphabets and numbers in a String are equal or not . ; Function to count the number of alphabets ; Counter to store the number of alphabets in the String ; Every character in the String is iterated ; To check if the character is an alphabet or not ; Function to count the number of numbers ; Counter to store the number of alphabets in the String ; Every character in the String is iterated ; To check if the character is a digit or not ; Function to check if the count of alphabets is equal to the count of numbers or not ; Driver code
using System ; class GFG { static int countOfLetters ( String str ) { int letter = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( ( str [ i ] >= ' A ' && str [ i ] <= ' Z ' ) || ( str [ i ] >= ' a ' && str [ i ] <= ' z ' ) ) letter ++ ; } return letter ; } static int countOfNumbers ( String str ) { int number = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) { if ( str [ i ] >= '0' && str [ i ] <= '9' ) number ++ ; } return number ; } static void check ( String str ) { if ( countOfLetters ( str ) == countOfNumbers ( str ) ) Console . Write ( " Yes STRNEWLINE " ) ; else Console . Write ( " No STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { String str = " GeeKs01324" ; check ( str ) ; } }
Minimum cost to traverse from one index to another in the String | C # implementation of the above approach . ; function to find the minimum cost ; graph ; adjacency matrix ; initialising adjacency matrix ; creating adjacency list ; pushing left adjacent elelemt for index ' k ' ; pushing right adjacent element for index ' k ' ; queue to perform BFS ; visited array ; variable to store depth of BFS ; BFS ; number of elements in the current level ; inner loop ; current element ; popping queue ; base case ; checking if the current node is required node ; iterating through the current node ; updating depth ; Driver Code ; input variables ; function to find the minimum cost
using System ; using System . Collections . Generic ; class GFG { static int findMinCost ( char [ ] s , int i , int j ) { List < int > [ ] gr = new List < int > [ 26 ] ; for ( int iN = 0 ; iN < 26 ; iN ++ ) gr [ iN ] = new List < int > ( ) ; bool [ , ] edge = new bool [ 26 , 26 ] ; for ( int k = 0 ; k < 26 ; k ++ ) for ( int l = 0 ; l < 26 ; l ++ ) edge [ k , l ] = false ; for ( int k = 0 ; k < s . Length ; k ++ ) { if ( k - 1 >= 0 && ! edge [ s [ k ] - 97 , s [ k - 1 ] - 97 ] ) { gr [ s [ k ] - 97 ] . Add ( s [ k - 1 ] - 97 ) ; edge [ s [ k ] - 97 , s [ k - 1 ] - 97 ] = true ; } if ( k + 1 <= s . Length - 1 && ! edge [ s [ k ] - 97 , s [ k + 1 ] - 97 ] ) { gr [ s [ k ] - 97 ] . Add ( s [ k + 1 ] - 97 ) ; edge [ s [ k ] - 97 , s [ k + 1 ] - 97 ] = true ; } } Queue < int > q = new Queue < int > ( ) ; q . Enqueue ( s [ i ] - 97 ) ; bool [ ] v = new bool [ 26 ] ; int d = 0 ; while ( q . Count > 0 ) { int cnt = q . Count ; while ( cnt -- > 0 ) { int curr = q . Peek ( ) ; q . Dequeue ( ) ; if ( v [ curr ] ) continue ; v [ curr ] = true ; if ( curr == s [ j ] - 97 ) return d ; foreach ( int it in gr [ curr ] ) q . Enqueue ( it ) ; } d ++ ; } return - 1 ; } public static void Main ( String [ ] args ) { String s = " abcde " ; int i = 0 ; int j = 4 ; Console . Write ( findMinCost ( s . ToCharArray ( ) , i , j ) ) ; } }
Index of character depending on frequency count in string | C # implementation of the approach ; Function to perform the queries ; L [ i , j ] stores the largest i such that ith character appears exactly jth times in str [ 0. . . i ] ; F [ i , j ] stores the smallest i such that ith character appears exactly jth times in str [ 0. . . i ] ; To store the frequency of each of the character of str ; Current character of str ; Update its frequency ; For every lowercase character of the English alphabet ; If it is equal to the character under consideration then update L [ , ] and R [ , ] as it is cnt [ j ] th occurrence of character k ; Only update L [ , ] as k has not been occurred so only index has to be incremented ; Perform the queries ; Type 1 query ; Type 2 query ; Driver code ; Queries ; Perform the queries
using System ; class GFG { static int MAX = 26 ; static void performQueries ( String str , int q , int [ ] type , char [ ] ch , int [ ] freq ) { int n = str . Length ; int [ , ] L = new int [ MAX , n ] ; int [ , ] F = new int [ MAX , n ] ; int [ ] cnt = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) { int k = str [ i ] - ' a ' ; cnt [ k ] ++ ; for ( int j = 0 ; j < MAX ; j ++ ) { if ( k == j ) { L [ j , cnt [ j ] ] = i ; F [ j , cnt [ j ] ] = i ; } else L [ j , cnt [ j ] ] = L [ j , cnt [ j ] ] + 1 ; } } for ( int i = 0 ; i < q ; i ++ ) { if ( type [ i ] == 1 ) { Console . Write ( L [ ch [ i ] - ' a ' , freq [ i ] ] ) ; } else { Console . Write ( F [ ch [ i ] - ' a ' , freq [ i ] ] ) ; } Console . Write ( " STRNEWLINE " ) ; } } public static void Main ( String [ ] args ) { String str = " geeksforgeeks " ; int [ ] type = { 1 , 2 } ; char [ ] ch = { ' e ' , ' k ' } ; int [ ] freq = { 2 , 2 } ; int q = type . Length ; performQueries ( str , q , type , ch , freq ) ; } }
Number of index pairs such that s [ i ] and s [ j ] are anagrams | C # program to find number of pairs of integers i , j such that s [ i ] is an anagram of s [ j ] . ; Function to find number of pairs of integers i , j such that s [ i ] is an anagram of s [ j ] . ; To store count of strings ; Traverse all strings and store in the map ; Sort the string ; Store in the map ; To store the number of pairs ; Traverse through the map ; Count the pairs for each string ; Return the required answer ; Driver code ; Function call
using System ; using System . Collections . Generic ; class GFG { static int anagram_pairs ( String [ ] s , int n ) { Dictionary < String , int > mp = new Dictionary < String , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { char [ ] chArr = s [ i ] . ToCharArray ( ) ; Array . Sort ( chArr ) ; s [ i ] = new String ( chArr ) ; if ( mp . ContainsKey ( s [ i ] ) ) { mp [ s [ i ] ] = mp [ s [ i ] ] + 1 ; } else { mp . Add ( s [ i ] , 1 ) ; } } int ans = 0 ; foreach ( KeyValuePair < String , int > i in mp ) { int k = i . Value ; ans += ( k * ( k - 1 ) ) / 2 ; } return ans ; } public static void Main ( String [ ] args ) { String [ ] s = { " aaab " , " aaba " , " baaa " , " cde " , " dec " } ; int n = s . Length ; Console . WriteLine ( anagram_pairs ( s , n ) ) ; } }
Insert a Character in a Rotated String | C # implementation of the approach ; Function to insert the character ; To store last position where the insertion is done ; To store size of the string ; To store the modified string ; To store characters ; Add first character to the string ; Update the size ; Update the index of last insertion ; Insert all other characters to the string ; Take the character ; Take substring upto ind ; Take modulo value of k with the size of the string ; Check if we need to move to the start of the string ; If we don 't need to move to  start of the string ; Take substring from upto temp ; Take substring which will be after the inserted character ; Insert into the string ; Store new inserted position ; Store size of the new string Technically sz + 1 ; If we need to move to start of the string ; Take substring which will before the inserted character ; Take substring which will be after the inserted character ; Insert into the string ; Store new inserted position ; Store size of the new string Technically sz + 1 ; Return the required character ; Driver code ; Function call
using System ; class GFG { static char Insert ( char [ ] arr , int n , int k ) { int ind = 0 ; int sz = 0 ; String s = " " ; char ch = arr [ 0 ] ; s += ch ; sz = 1 ; ind = 0 ; for ( int i = 1 ; i < n ; i ++ ) { ch = arr [ i ] ; string s1 = s . Substring ( 0 , ind + 1 ) ; int temp = k % sz ; int ro = temp - Math . Min ( temp , sz - ind - 1 ) ; if ( ro == 0 ) { string s2 = s . Substring ( ind + 1 , temp ) ; string s3 = s . Substring ( ind + temp + 1 , sz - ind - temp - 1 ) ; s = s1 + s2 + ch + s3 ; ind = s1 . Length + s2 . Length ; sz = s . Length ; } else { string s2 = s . Substring ( 0 , ro ) ; string s3 = s . Substring ( ro , sz - ro ) ; s = s2 + ch + s3 ; ind = s2 . Length ; sz = s . Length ; } } if ( ind == 0 ) { return s [ sz - 1 ] ; } else { return s [ ind - 1 ] ; } } static public void Main ( ) { char [ ] arr = { '1' , '2' , '3' , '4' , '5' } ; int k = 2 ; int n = arr . Length ; Console . WriteLine ( Insert ( arr , n , k ) ) ; } }