codeparrot/xlcost-text-to-code huggingface dataset:
Reduce given three Numbers by decrementing in Pairs | C # program for the above approach ; Function to find the minimum number operations ; Insert the three numbers in array ; Sort the array ; Case 2 ; Case 1 ; Driver Code ; Given A , B , C ; Function call
using System ; class GFG { public static int solution ( int A , int B , int C ) { int [ ] arr = new int [ 3 ] ; arr [ 0 ] = A ; arr [ 1 ] = B ; arr [ 2 ] = C ; Array . Sort ( arr ) ; if ( arr [ 2 ] < arr [ 0 ] + arr [ 1 ] ) return ( ( arr [ 0 ] + arr [ 1 ] + arr [ 2 ] ) / 2 ) ; else return ( arr [ 0 ] + arr [ 1 ] ) ; } public static void Main ( String [ ] args ) { int A = 8 , B = 1 , C = 5 ; Console . WriteLine ( solution ( A , B , C ) ) ; } }
Search an element in a sorted and rotated array with duplicates | C # implementation of the approach ; Function to return the index of the key in arr [ l . . h ] if the key is present otherwise return - 1 ; The tricky case , just update left and right ; If arr [ l ... mid ] is sorted ; As this subarray is sorted , we can quickly check if key lies in any of the halves ; If key does not lie in the first half subarray then divide the other half into two subarrays such that we can quickly check if key lies in the other half ; If arr [ l . . mid ] first subarray is not sorted then arr [ mid ... h ] must be sorted subarray ; Driver code
using System ; class GFG { static int search ( int [ ] arr , int l , int h , int key ) { if ( l > h ) return - 1 ; int mid = ( l + h ) / 2 ; if ( arr [ mid ] == key ) return mid ; if ( ( arr [ l ] == arr [ mid ] ) && ( arr [ h ] == arr [ mid ] ) ) { ++ l ; -- h ; return search ( arr , l , h , key ) } if ( arr [ l ] <= arr [ mid ] ) { if ( key >= arr [ l ] && key <= arr [ mid ] ) return search ( arr , l , mid - 1 , key ) ; return search ( arr , mid + 1 , h , key ) ; } if ( key >= arr [ mid ] && key <= arr [ h ] ) return search ( arr , mid + 1 , h , key ) ; return search ( arr , l , mid - 1 , key ) ; } public static void Main ( ) { int [ ] arr = { 3 , 3 , 1 , 2 , 3 , 3 } ; int n = arr . Length ; int key = 3 ; Console . WriteLine ( search ( arr , 0 , n - 1 , key ) ) ; } }
Case | C # implementation of the approach ; Function to return the sorted string ; Vectors to store the lowercase and uppercase characters ; Sort both the vectors ; If current character is lowercase then pick the lowercase character from the sorted list ; Else pick the uppercase character ; Return the sorted string ; Driver code
using System ; using System . Collections . Generic ; class GFG { public static String getSortedString ( char [ ] s , int n ) { List < char > v1 = new List < char > ( ) ; List < char > v2 = new List < char > ( ) ; int i = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( s [ i ] > ' a ' && s [ i ] <= ' z ' ) v1 . Add ( s [ i ] ) ; if ( s [ i ] > ' A ' && s [ i ] <= ' z ' ) v2 . Add ( s [ i ] ) ; } v1 . Sort ( ) ; v2 . Sort ( ) ; int j = 0 ; i = 0 ; for ( int k = 0 ; k < n ; k ++ ) { if ( s [ k ] > ' a ' && s [ k ] <= ' z ' ) { s [ k ] = v1 [ i ] ; ++ i ; } else if ( s [ k ] > ' A ' && s [ k ] <= ' Z ' ) { s [ k ] = v2 [ j ] ; ++ j ; } } return String . Join ( " " , s ) ; } public static void Main ( String [ ] args ) { String s = " gEeksfOrgEEkS " ; int n = s . Length ; Console . WriteLine ( getSortedString ( s . ToCharArray ( ) , n ) ) ; } }
Check if the string contains consecutive letters and each letter occurs exactly once | C # program to implement the above approach ; Function to check if the condition holds ; Get the length of the string ; sort the given string ; Iterate for every index and check for the condition ; If are not consecutive ; Driver code ; 1 st example ; 2 nd example
using System ; using System . Collections ; class GfG { static bool check ( char [ ] s ) { int l = s . Length ; Array . Sort ( s ) ; for ( int i = 1 ; i < l ; i ++ ) { if ( s [ i ] - s [ i - 1 ] != 1 ) return false ; } return true ; } public static void Main ( ) { string str = " dcef " ; if ( check ( str . ToCharArray ( ) ) == true ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; String str1 = " xyza " ; if ( check ( str1 . ToCharArray ( ) ) == true ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Smallest subset with sum greater than all other elements | C # code to find minimum number of elements such that their sum is greater than sum of remaining elements of the array . ; Function to find minimum elements needed ; Calculating HALF of array sum ; Sort the array in ascending order and start traversing array from the ascending sort in descending order . ; Current sum greater than sum ; Driver Code
using System ; class GFG { static int minElements ( int [ ] arr , int n ) { int halfSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) halfSum = halfSum + arr [ i ] ; halfSum = halfSum / 2 ; Array . Sort ( arr ) ; int res = 0 , curr_sum = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { curr_sum += arr [ i ] ; res ++ ; if ( curr_sum > halfSum ) return res ; } return res ; } public static void Main ( ) { int [ ] arr = { 3 , 1 , 7 , 1 } ; int n = arr . Length ; Console . WriteLine ( minElements ( arr , n ) ) ; } }
Minimum increment and decrement by K of each pair elements required to make all array elements equal | C # program for the above approach ; Function to check if its possible to make all array elements equal or not ; Stores the sum of the array ; Traverse the array ; If sum is divisible by N ; Otherwise , not possible to make all array elements equal ; Driver Code ; Given array ; Size of the array
using System ; using System . Collections . Generic ; public class GFG { static void arrayElementEqual ( int [ ] arr , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; } if ( sum % N == 0 ) { Console . WriteLine ( " Yes " ) ; } else { Console . Write ( " No " + " STRNEWLINE " ) ; } } static public void Main ( ) { int [ ] arr = { 1 , 5 , 6 , 4 } ; int N = arr . Length ; arrayElementEqual ( arr , N ) ; } }
Rearrange array to maximize sum of GCD of array elements with their respective indices | C # program to implement the above approach ; Function to find the maximum sum of GCD ( arr [ i ] , i ) by rearranging the array ; Stores maximum sum of GCD ( arr [ i ] , i ) by rearranging the array elements ; Update res ; Driver Code
using System ; class GFG { static int findMaxValByRearrArr ( int [ ] arr , int N ) { int res = 0 ; res = ( N * ( N + 1 ) ) / 2 ; return res ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 1 } ; int N = arr . Length ; Console . Write ( findMaxValByRearrArr ( arr , N ) ) ; } }
Polygon with maximum sides that can be inscribed in an N | C # program for the above approach ; Function to find the maximum sided polygon that can be inscribed ; Base Case ; Return n / 2 if n is even Otherwise , return - 1 ; Driver Code ; Given N ; Function Call
using System ; class GFG { static int MaximumSides ( int n ) { if ( n < 4 ) return - 1 ; return n % 2 == 0 ? n / 2 : - 1 ; } public static void Main ( String [ ] args ) { int N = 8 ; Console . Write ( MaximumSides ( N ) ) ; } }
Mean of array generated by products of all pairs of the given array | C # program for the above approach ; Function to find the mean of pair product array of arr [ ] ; Initializing suffix sum array ; Build suffix sum array ; Size of pairProductArray ; Stores sum of pairProductArray ; Store the mean ; Find mean of pairProductArray ; Return the resultant mean ; Driver code ; Given array arr [ ] ; Function call
using System ; class GFG { static double pairProductMean ( int [ ] arr , int N ) { int [ ] suffixSumArray = new int [ N ] ; suffixSumArray [ N - 1 ] = arr [ N - 1 ] ; for ( int i = N - 2 ; i >= 0 ; i -- ) { suffixSumArray [ i ] = suffixSumArray [ i + 1 ] + arr [ i ] ; } int length = ( N * ( N - 1 ) ) / 2 ; double res = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { res += arr [ i ] * suffixSumArray [ i + 1 ] ; } double mean ; if ( length != 0 ) mean = res / length ; else mean = 0 ; return mean ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 4 , 8 } ; int N = arr . Length ; Console . WriteLine ( string . Format ( " { 0:0.00 } " , pairProductMean ( arr , N ) ) ) ; } }
Minimize count of unique paths from top left to bottom right of a Matrix by placing K 1 s | C # program to implement the above approach ; Function to return the value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate the value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- -- * 1 ] ; Function to find the minimum count of paths from top left to bottom right by placing K 1 s in the matrix ; Count of ways without 1 s ; Count of paths from starting point to mid point ; Count of paths from mid point to end point ; Driver Code
using System ; class GFG { static int ncr ( int n , int k ) { int res = 1 ; if ( k > n - k ) k = n - k ; for ( int i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } static int countPath ( int N , int M , int K ) { int answer ; if ( K >= 2 ) answer = 0 ; else if ( K == 0 ) answer = ncr ( N + M - 2 , N - 1 ) ; else { answer = ncr ( N + M - 2 , N - 1 ) ; int X = ( N - 1 ) / 2 + ( M - 1 ) / 2 ; int Y = ( N - 1 ) / 2 ; int midCount = ncr ( X , Y ) ; X = ( ( N - 1 ) - ( N - 1 ) / 2 ) + ( ( M - 1 ) - ( M - 1 ) / 2 ) ; Y = ( ( N - 1 ) - ( N - 1 ) / 2 ) ; midCount *= ncr ( X , Y ) ; answer -= midCount ; } return answer ; } public static void Main ( String [ ] args ) { int N = 3 ; int M = 3 ; int K = 1 ; Console . Write ( countPath ( N , M , K ) ) ; } }
Maximum number of operations required such that no pairs from a Matrix overlap | C # program to implement the above approach ; Function to find maximum count of operations ; Initialize count by 0 ; Iterate over remaining pairs ; Check if first operation is applicable ; Check if 2 nd operation is applicable ; Otherwise ; Return the count of operations ; Driver Code
using System ; using System . Collections . Generic ; class GFG { class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int find_max ( List < pair > v , int n ) { int count = 0 ; if ( n >= 2 ) count = 2 ; else count = 1 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( v [ i - 1 ] . first < ( v [ i ] . first - v [ i ] . second ) ) count ++ ; else if ( v [ i + 1 ] . first > ( v [ i ] . first + v [ i ] . second ) ) { count ++ ; v [ i ] . first = v [ i ] . first + v [ i ] . second ; } else continue ; } return count ; } public static void Main ( String [ ] args ) { int n = 3 ; List < pair > v = new List < pair > ( ) ; v . Add ( new pair ( 10 , 20 ) ) ; v . Add ( new pair ( 15 , 10 ) ) ; v . Add ( new pair ( 20 , 16 ) ) ; Console . Write ( find_max ( v , n ) ) ; } }
Count of substrings formed using a given set of characters only | C # program for the above approach ; Function to find the number of substrings that can be formed using given characters ; Boolean array for storing the available characters ; Mark indices of all available characters as 1 ; Initialize lastPos as - 1 ; Initialize ans with the total no of possible substrings ; Traverse the string from left to right ; If the current character is not present in B ; Subtract the total possible substrings ; Update the value of lastpos to current index ; Print the final answer ; Driver Code ; Given String ; Given character array ; Function Call
using System ; class GFG { public static void numberofsubstrings ( String str , int k , char [ ] charArray ) { int N = str . Length ; int [ ] available = new int [ 26 ] ; for ( int i = 0 ; i < k ; i ++ ) { available [ charArray [ i ] - ' a ' ] = 1 ; } int lastPos = - 1 ; int ans = ( N * ( N + 1 ) ) / 2 ; for ( int i = 0 ; i < N ; i ++ ) { if ( available [ str [ i ] - ' a ' ] == 0 ) { ans -= ( ( i - lastPos ) * ( N - i ) ) ; lastPos = i ; } } Console . WriteLine ( ans ) ; } public static void Main ( String [ ] args ) { String str = " abcb " ; int k = 2 ; char [ ] charArray = { ' a ' , ' b ' } ; numberofsubstrings ( str , k , charArray ) ; } }
Minimum cost to reach a point N from 0 with two different operations allowed | C # implementation of above approach ; Function to return minimum cost to reach destination ; Initialize cost to 0 ; going backwards until we reach initial position ; if 2 * X jump is better than X + 1 ; if X + 1 jump is better ; return cost ; Driver Code
class GFG { static int minCost ( int N , int P , int Q ) { int cost = 0 ; while ( N > 0 ) { if ( ( N & 1 ) > 0 ) { cost += P ; N -- ; } else { int temp = N / 2 ; if ( temp * P > Q ) cost += Q ; else cost += P * temp ; N /= 2 ; } } return cost ; } static void Main ( ) { int N = 9 , P = 5 , Q = 1 ; System . Console . WriteLine ( minCost ( N , P , Q ) ) ; } }
Number of ways to reach at starting node after travelling through exactly K edges in a complete graph | C # program for the above approach ; Function to find number of ways to reach from node 1 to 1 again , after moving exactly K edges ; Initialize a dp [ ] array , where dp [ i ] stores number of ways to reach at a i node ; Initialize the dp array with 0 ; Base Case ; Iterate for the number of edges moved ; Sum will store number of ways to reach all the nodes ; Iterate for every possible state for the current step ; Update the value of the dp array after travelling each edge ; Print dp [ 0 ] as the answer ; Driver Code ; Given Input ; Function Call
using System ; class GFG { static void numberOfWays ( int n , int k ) { int [ ] dp = new int [ 1000 ] ; for ( int i = 0 ; i < n ; i ++ ) { dp [ i ] = 0 ; } dp [ 0 ] = 1 ; for ( int i = 1 ; i <= k ; i ++ ) { int numWays = 0 ; for ( int j = 0 ; j < n ; j ++ ) { numWays += dp [ j ] ; } for ( int j = 0 ; j < n ; j ++ ) { dp [ j ] = numWays - dp [ j ] ; } } Console . Write ( dp [ 0 ] ) ; } static public void Main ( ) { int N = 5 , K = 3 ; numberOfWays ( N , K ) ; } }
Count possible decoding of a given digit sequence with hidden characters | C # program for the above approach ; If s [ i ] = = ' * ' there can be 9 possible values of * ; If previous character is 1 then words that can be formed are K ( 11 ) , L ( 12 ) , M ( 13 ) , N ( 14 ) O ( 15 ) , P ( 16 ) , Q ( 17 ) , R ( 18 ) , S ( 19 ) ; If previous character is 2 then the words that can be formed are U ( 21 ) , V ( 22 ) , W ( 23 ) , X ( 24 ) Y ( 25 ) , Z ( 26 ) ; If the previous digit is * then all 15 2 - digit characters can be formed ; If s [ i ] != ' * ' ; Adding first in second if s [ i - 1 ] = 1 ; Adding first in second if s [ i - 1 ] = = 2 and s [ i ] <= '6' ; if s [ i - 1 ] == ' * ' the union of above 2 cases has to be done ; Driver code
using System ; class GFG { static int M = 1000000007 ; static int waysOfDecoding ( string s ) { long first = 1 , second = s [ 0 ] == ' * ' ? 9 : s [ 0 ] == '0' ? 0 : 1 ; for ( int i = 1 ; i < s . Length ; i ++ ) { long temp = second ; if ( s [ i ] == ' * ' ) { second = 9 * second ; if ( s [ i - 1 ] == '1' ) second = ( second + 9 * first ) % M ; else if ( s [ i - 1 ] == '2' ) second = ( second + 6 * first ) % M ; else if ( s [ i - 1 ] == ' * ' ) second = ( second + 15 * first ) % M ; } else { second = s [ i ] != '0' ? second : 0 ; if ( s [ i - 1 ] == '1' ) second = ( second + first ) % M ; else if ( s [ i - 1 ] == '2' && s [ i ] <= '6' ) second = ( second + first ) % M ; else if ( s [ i - 1 ] == ' * ' ) second = ( second + ( s [ i ] <= '6' ? 2 : 1 ) * first ) % M ; } first = temp ; } return ( int ) second ; } static public void Main ( ) { string s = " * " ; Console . WriteLine ( waysOfDecoding ( s ) ) ; } }
Minimum cost of purchasing at least X chocolates | C # program for the above approach ; Function to calculate minimum cost of buying least X chocolates ; Base Case ; Include the i - th box ; Exclude the i - th box ; Return the minimum of the above two cases ; Driver Code ; Given array and value of X ; Store the size of the array ; Print the answer
using System ; class GFG { static int findMinCost ( int [ , ] arr , int X , int n , int i = 0 ) { if ( X <= 0 ) return 0 ; if ( i >= n ) return Int32 . MaxValue ; int inc = findMinCost ( arr , X - arr [ i , 0 ] , n , i + 1 ) ; if ( inc != Int32 . MaxValue ) inc += arr [ i , 1 ] ; int exc = findMinCost ( arr , X , n , i + 1 ) ; return Math . Min ( inc , exc ) ; } public static void Main ( ) { int [ , ] arr = { { 4 , 3 } , { 3 , 2 } , { 2 , 4 } , { 1 , 3 } , { 4 , 2 } } ; int X = 7 ; int n = arr . GetLength ( 0 ) ; int ans = findMinCost ( arr , X , n ) ; if ( ans != Int32 . MaxValue ) Console . Write ( ans ) ; else Console . Write ( - 1 ) ; } }
Probability that the sum of all numbers obtained on throwing a dice N times lies between two given integers | C # program for the above approach ; Function to calculate the probability for the given sum to be equal to sum in N throws of dice ; Base cases ; Driver code ; Print the answer
using System ; class GFG { static double find ( int N , int sum ) { if ( sum > 6 * N sum < N ) return 0 ; if ( N == 1 ) { if ( sum >= 1 && sum <= 6 ) return 1.0 / 6 ; else return 0 ; } double s = 0 ; for ( int i = 1 ; i <= 6 ; i ++ ) s = s + find ( N - 1 , sum - i ) / 6 ; return s ; } static void Main ( ) { int N = 4 , a = 13 , b = 17 ; double probability = 0.0 ; for ( int sum = a ; sum <= b ; sum ++ ) probability = probability + find ( N , sum ) ; Console . WriteLine ( Math . Round ( probability , 6 ) ) ; } }
Minimum steps to reduce N to 0 by given operations | C # program for the above approach ; Function to find the minimum number to steps to reduce N to 0 ; Base case ; Recursive call to count the minimum steps needed ; Return the answer ; Driver Code ; Given number N ; Function call
using System ; class GFG { static int minDays ( int n ) { if ( n < 1 ) return n ; int cnt = 1 + Math . Min ( n % 2 + minDays ( n / 2 ) , n % 3 + minDays ( n / 3 ) ) ; return cnt ; } public static void Main ( String [ ] args ) { int N = 6 ; Console . Write ( minDays ( N ) ) ; } }