codeparrot/xlcost-text-to-code huggingface dataset:
Largest sub | C # mplementation of the approach ; Function to return the size of the required sub - set ; Sort the array ; HashMap to store the contents of the required sub - set ; Insert the elements satisfying the conditions ; Return the size of the set ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int sizeSubSet ( int [ ] a , int k , int n ) { Array . Sort ( a ) ; Dictionary < int , int > s = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % k != 0 || ! s . ContainsKey ( a [ i ] / k ) ) { if ( s . ContainsKey ( a [ i ] ) ) { var val = s [ a [ i ] ] ; s . Remove ( a [ i ] ) ; s . Add ( a [ i ] , val + 1 ) ; } else { s . Add ( a [ i ] , 1 ) ; } } } return s . Count ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ; int n = a . Length ; int k = 2 ; Console . WriteLine ( sizeSubSet ( a , k , n ) ) ; } }
Minimum number of sets with numbers less than Y | C # program to find the minimum number sets with consecutive numbers and less than Y ; Function to find the minimum number of shets ; Variable to count the number of sets ; Iterate in the string ; Add the number to string ; Mark that we got a number ; else Every time it exceeds ; Check if previous was anytime less than Y ; Current number ; Check for current number ; Check for last added number ; Driver Code
using System ; class solution { static int minimumSets ( string s , int y ) { int cnt = 0 ; int num = 0 ; int l = s . Length ; bool f = false ; for ( int i = 0 ; i < l ; i ++ ) { num = num * 10 + ( s [ i ] - '0' ) ; if ( num <= y ) f = true ; { if ( f ) cnt += 1 ; num = s [ i ] - '0' ; f = false ; if ( num <= y ) f = true ; else num = 0 ; } } if ( f == true ) cnt += 1 ; return cnt ; } public static void Main ( ) { string s = "1234" ; int y = 30 ; Console . WriteLine ( minimumSets ( s , y ) ) ; } }
Find the non decreasing order array from given array | C # implementation of the approach ; Utility function to print the contents of the array ; Function to build array B [ ] ; Lower and upper limits ; To store the required array ; Apply greedy approach ; Print the built array b [ ] ; Driver code
using System ; public class GFG { static void printArr ( int [ ] b , int n ) { for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( " ▁ " + b [ i ] + " ▁ " ) ; } } static void ModifiedArray ( int [ ] a , int n ) { int l = 0 , r = int . MaxValue ; int [ ] b = new int [ n ] ; for ( int i = 0 ; i < n / 2 ; i ++ ) { b [ i ] = Math . Max ( l , a [ i ] - r ) ; b [ n - i - 1 ] = a [ i ] - b [ i ] ; l = b [ i ] ; r = b [ n - i - 1 ] ; } printArr ( b , n ) ; } static public void Main ( ) { int [ ] a = { 5 , 6 } ; int n = a . Length ; ModifiedArray ( a , 2 * n ) ; } }
Count of lines required to write the given String | C # implementation of the approach ; Function to return the number of lines required ; If String is empty ; Initialize lines and width ; Iterate through S ; Return lines and width used ; Driver Code ; Function call to print required answer
using System ; class GFG { static int [ ] numberOfLines ( String S , int [ ] widths ) { if ( S . Length == 0 ) return new int [ ] { 0 , 0 } ; int lines = 1 , width = 0 ; foreach ( char character in S . ToCharArray ( ) ) { int w = widths [ character - ' a ' ] ; width += w ; if ( width >= 10 ) { lines ++ ; width = w ; } } return new int [ ] { lines , width } ; } public static void Main ( String [ ] args ) { String S = " bbbcccdddaa " ; int [ ] widths = { 4 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 } ; int [ ] ans = numberOfLines ( S , widths ) ; Console . Write ( ans [ 0 ] + " ▁ " + ans [ 1 ] + " STRNEWLINE " ) ; } }
Check if elements of an array can be arranged satisfying the given condition | C # implementation of the approach ; Function to return true if the elements can be arranged in the desired order ; If 2 * x is not found to pair ; Remove an occurrence of x and an occurrence of 2 * x ; Driver code ; Function call to print required answer
using System ; using System . Collections . Generic ; class GFG { static String canReorder ( int [ ] A , int n ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( m . ContainsKey ( A [ i ] ) ) m [ A [ i ] ] = m [ A [ i ] ] + 1 ; else m . Add ( A [ i ] , 1 ) ; } Array . Sort ( A ) ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( m [ A [ i ] ] == 0 ) continue ; if ( m . ContainsKey ( 2 * A [ i ] ) ) { count += 2 ; m [ A [ i ] ] = m [ A [ i ] ] - 1 ; if ( m . ContainsKey ( 2 * A [ i ] ) ) m [ 2 * A [ i ] ] = m [ 2 * A [ i ] ] - 1 ; else m . Add ( 2 * A [ i ] , m [ 2 * A [ i ] ] - 1 ) ; } } if ( count == n ) return " True " ; else return " False " ; } public static void Main ( String [ ] args ) { int [ ] A = { 4 , - 2 , 2 , - 4 } ; int n = A . Length ; Console . WriteLine ( canReorder ( A , n ) ) ; } }
Maximum sum of all elements of array after performing given operations | C # program to find the maximum sum after given operations ; Function to calculate Maximum Subarray Sum or Kadane 's Algorithm ; Function to find the maximum sum after given operations ; To store sum of all elements ; Maximum sum of a subarray ; Calculate the sum of all elements ; Driver Code ; size of an array
using System ; class GFG { static int maxSubArraySum ( int [ ] a , int size ) { int max_so_far = int . MinValue , max_ending_here = 0 ; for ( int i = 0 ; i < size ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; if ( max_ending_here < 0 ) max_ending_here = 0 ; } return max_so_far ; } static int maxSum ( int [ ] a , int n ) { int S = 0 ; int S1 = maxSubArraySum ( a , n ) ; for ( int i = 0 ; i < n ; i ++ ) S += a [ i ] ; return ( 2 * S1 - S ) ; } public static void Main ( ) { int [ ] a = { - 35 , 32 , - 24 , 0 , 27 , - 10 , 0 , - 19 } ; int n = a . Length ; Console . WriteLine ( maxSum ( a , n ) ) ; } }
Minimize the difference between minimum and maximum elements | C # program to minimize the difference between minimum and maximum elements ; Function to minimize the difference between minimum and maximum elements ; Find max and min elements of the array ; Check whether the difference between the max and min element is less than or equal to k or not ; Calculate average of max and min ; If the array element is greater than the average then decrease it by k ; If the array element is smaller than the average then increase it by k ; Find max and min of the modified array ; return the new difference ; Driver code
using System ; using System . Linq ; class GFG { static int minimizeDiff ( int [ ] arr , int n , int k ) { int max = arr . Max ( ) ; int min = arr . Min ( ) ; if ( ( max - min ) <= k ) { return ( max - min ) ; } int avg = ( max + min ) / 2 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > avg ) { arr [ i ] -= k ; } else { arr [ i ] += k ; } } max = arr . Max ( ) ; min = arr . Min ( ) ; return ( max - min ) ; } public static void Main ( ) { int [ ] arr = { 3 , 16 , 12 , 9 , 20 } ; int n = 5 ; int k = 3 ; Console . WriteLine ( " Max ▁ height ▁ difference ▁ = ▁ " + minimizeDiff ( arr , n , k ) ) ; } }
Maximum litres of water that can be bought with N Rupees | C # implementation of the above approach ; if buying glass bottles is profitable ; Glass bottles that can be bought ; Change budget according the bought bottles ; Plastic bottles that can be bought ; if only plastic bottles need to be bought ; Driver Code
using System ; class GFG { static void maxLitres ( int budget , int plastic , int glass , int refund ) { if ( glass - refund < plastic ) { int ans = Math . Max ( ( budget - refund ) / ( glass - refund ) , 0 ) ; budget -= ans * ( glass - refund ) ; ans += budget / plastic ; Console . WriteLine ( ans ) ; } else { Console . WriteLine ( ( budget / plastic ) ) ; } } public static void Main ( String [ ] args ) { int budget = 10 , plastic = 11 , glass = 9 , refund = 8 ; maxLitres ( budget , plastic , glass , refund ) ; } }
Find number from given list for which value of the function is closest to A | C # program to find number from given list for which value of the function is closest to A ; Function to find number from given list for which value of the function is closest to A ; Stores the final index ; Declaring a variable to store the minimum absolute difference ; Finding F ( n ) ; Updating the index of the answer if new absolute difference is less than tmp ; Driver code
using System ; class GFG { static int leastValue ( int P , int A , int N , int [ ] a ) { int ans = - 1 ; float tmp = float . MaxValue ; for ( int i = 0 ; i < N ; i ++ ) { float t = ( float ) ( P - a [ i ] * 0.006 ) ; if ( Math . Abs ( t - A ) < tmp ) { tmp = Math . Abs ( t - A ) ; ans = i ; } } return a [ ans ] ; } public static void Main ( String [ ] args ) { int N = 2 , P = 12 , A = 2005 ; int [ ] a = { 1000 , 2000 } ; Console . WriteLine ( leastValue ( P , A , N , a ) ) ; } }
Find permutation of n which is divisible by 3 but not divisible by 6 | C # program to find permutation of n which is divisible by 3 but not divisible by 6 ; Function to find the permutation ; length of integer ; if integer is even ; return odd integer ; rotate integer ; return - 1 in case no required permutation exists ; Driver Code
using System ; class GFG { static int findPermutation ( int n ) { int len = ( int ) Math . Ceiling ( Math . Log10 ( n ) ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( n % 2 != 0 ) { return n ; } else { n = ( n / 10 ) + ( n % 10 ) * ( int ) Math . Pow ( 10 , len - i - 1 ) ; continue ; } } return - 1 ; } public static void Main ( ) { int n = 132 ; Console . WriteLine ( findPermutation ( n ) ) ; } }
Maximum score after flipping a Binary Matrix atmost K times | C # program to find the maximum score after flipping a Binary Matrix atmost K times ; Function to find maximum score of matrix ; find value of rows having first column value equal to zero ; Update those rows which lead to maximum score after toggle ; Calculating answer ; Check if K > 0 we can toggle if necessary . ; return max answer possible ; Driver code ; function call to print required answer
using System ; using System . Collections . Generic ; class GFG { static int n = 3 ; static int m = 4 ; static int maxMatrixScore ( int [ , ] A , int K ) { Dictionary < int , int > update = new Dictionary < int , int > ( ) ; int ans = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( A [ i , 0 ] == 0 ) { ans = 0 ; for ( int j = 1 ; j < m ; ++ j ) ans = ( int ) ( ans + A [ i , j ] * Math . Pow ( 2 , m - j - 1 ) ) ; update . Add ( ( int ) ans , i ) ; } } foreach ( KeyValuePair < int , int > it in update ) if ( K > 0 ) { int idx = it . Value ; for ( int j = 0 ; j < m ; ++ j ) A [ idx , j ] = ( A [ idx , j ] + 1 ) % 2 ; K -- ; } ans = 0 ; for ( int j = 0 ; j < m ; ++ j ) { int zero = 0 , one = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( A [ i , j ] == 0 ) zero ++ ; else one ++ ; } if ( K > 0 && zero > one ) { ans += zero * ( int ) Math . Pow ( 2 , m - j - 1 ) ; K -- ; } else ans += one * ( int ) Math . Pow ( 2 , m - j - 1 ) ; } return ans ; } public static void Main ( String [ ] args ) { int [ , ] A = { { 0 , 0 , 1 , 1 } , { 1 , 0 , 1 , 0 } , { 1 , 1 , 0 , 0 } } ; int K = 2 ; Console . Write ( maxMatrixScore ( A , K ) ) ; } }
Check if it is possible to serve customer queue with different notes | C # implementation of the approach ; Function that returns true is selling of the tickets is possible ; Nothing to return to the customer ; Check if 25 can be returned to customer . ; Try returning one 50 and one 25 ; Try returning three 25 ; If the loop did not break , all the tickets were sold ; Driver Code
using System ; class GFG { static bool isSellingPossible ( int n , int [ ] a ) { int i , c25 = 0 , c50 = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 25 ) c25 ++ ; else if ( a [ i ] == 50 ) { c50 ++ ; if ( c25 == 0 ) break ; c25 -- ; } else { if ( c50 > 0 && c25 > 0 ) { c50 -- ; c25 -- ; } else if ( c25 >= 3 ) c25 -= 3 ; else break ; } } if ( i == n ) return true ; else return false ; } public static void Main ( ) { int [ ] a = { 25 , 25 , 50 , 100 } ; int n = a . Length ; if ( isSellingPossible ( n , a ) ) { Console . WriteLine ( " YES " ) ; } else { Console . WriteLine ( " NO " ) ; } } }
Check if a cell can be visited more than once in a String | C # program to check if any cell of the string can be visited more than once ; Function to check if any cell can be visited more than once ; Array to mark cells ; Traverse the string ; Increase the visit count of the left and right cells within the array which can be visited ; If any cell can be visited more than once , Return True ; Driver code
using System ; class GFG { static bool checkIfOverlap ( String str ) { int len = str . Length ; int [ ] visited = new int [ len + 1 ] ; for ( int i = 0 ; i < len ; i ++ ) { if ( str [ i ] == ' . ' ) continue ; for ( int j = Math . Max ( 0 , i - str [ i ] ) ; j <= Math . Min ( len , i + str [ i ] ) ; j ++ ) visited [ j ] ++ ; } for ( int i = 0 ; i < len ; i ++ ) { if ( visited [ i ] > 1 ) { return true ; } } return false ; } public static void Main ( ) { String str = " . 2 . . 2 . " ; if ( checkIfOverlap ( str ) ) Console . Write ( " YES " ) ; else Console . Write ( " NO " ) ; } }
Check if a number has digits in the given Order | C # program to check if the digits are in the given order ; Check if the digits follow the correct order ; to store the previous digit ; pointer to tell what type of sequence are we dealing with ; check if we have same digit as the previous digit ; checking the peak point of the number ; check if we have same digit as the previous digit ; check if the digit is greater than the previous one If true , then break from the loop as we are in descending order part ; Driver code
using System ; class GFG { static bool isCorrectOrder ( int n ) { bool flag = true ; int prev = - 1 ; int type = - 1 ; while ( n != 0 ) { if ( type == - 1 ) { if ( prev == - 1 ) { prev = n % 10 ; n = n / 10 ; continue ; } if ( prev == n % 10 ) { flag = false ; break ; } if ( prev > n % 10 ) { type = 1 ; prev = n % 10 ; n = n / 10 ; continue ; } prev = n % 10 ; n = n / 10 ; } else { if ( prev == n % 10 ) { flag = false ; break ; } if ( prev < n % 10 ) { flag = false ; break ; } prev = n % 10 ; n = n / 10 ; } } return flag ; } public static void Main ( ) { int n = 123454321 ; if ( isCorrectOrder ( n ) ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }
Coin game of two corners ( Greedy Approach ) | C # program to find coins to be picked to make sure that we never loose . ; Returns optimal value possible that a player can collect from an array of coins of size n . Note than n must be even ; Find sum of odd positioned coins ; Find sum of even positioned coins ; Print even or odd coins depending upon which sum is greater . ; Driver Code
using System ; class GFG { static void printCoins ( int [ ] arr , int n ) { int oddSum = 0 ; for ( int i = 0 ; i < n ; i += 2 ) oddSum += arr [ i ] ; int evenSum = 0 ; for ( int i = 1 ; i < n ; i += 2 ) evenSum += arr [ i ] ; int start = ( ( oddSum > evenSum ) ? 0 : 1 ) ; for ( int i = start ; i < n ; i += 2 ) Console . Write ( arr [ i ] + " ▁ " ) ; } public static void Main ( ) { int [ ] arr1 = { 8 , 15 , 3 , 7 } ; int n = arr1 . Length ; printCoins ( arr1 , n ) ; Console . Write ( " STRNEWLINE " ) ; int [ ] arr2 = { 2 , 2 , 2 , 2 } ; n = arr2 . Length ; printCoins ( arr2 , n ) ; Console . Write ( " STRNEWLINE " ) ; int [ ] arr3 = { 20 , 30 , 2 , 2 , 2 , 10 } ; n = arr3 . Length ; printCoins ( arr3 , n ) ; } }
Final cell position in the matrix | C # implementation to find the final cell position in the given matrix ; function to find the final cell position in the given matrix ; to count up , down , left and cright movements ; to store the final coordinate position ; traverse the command array ; calculate final values ; Driver Code
class GFG { static void finalPos ( string command , int n , int x , int y ) { int cup , cdown , cleft , cright ; int final_x , final_y ; cup = cdown = cleft = cright = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( command [ i ] == ' U ' ) cup ++ ; else if ( command [ i ] == ' D ' ) cdown ++ ; else if ( command [ i ] == ' L ' ) cleft ++ ; else if ( command [ i ] == ' R ' ) cright ++ ; } final_x = x + ( cright - cleft ) ; final_y = y + ( cdown - cup ) ; System . Console . WriteLine ( " Final ▁ Position : ▁ " + " ( " + final_x + " , ▁ " + final_y + " ) " ) ; } public static void Main ( ) { string command = " DDLRULL " ; int n = command . Length ; int x = 3 , y = 4 ; finalPos ( command , n , x , y ) ; } }
Prim 's Algorithm (Simple Implementation for Adjacency Matrix Representation) | A simple C # implementation to find minimum spanning tree for adjacency representation . ; Returns true if edge u - v is a valid edge to be include in MST . An edge is valid if one end is already included in MST and other is not in MST . ; Include first vertex in MST ; Keep adding edges while number of included edges does not become V - 1. ; Find minimum weight valid edge . ; Driver Code ; Let us create the following graph 2 3 ( 0 ) -- ( 1 ) -- ( 2 ) | / \ | 6 | 8 / \ 5 | 7 | / \ | ( 3 ) -- -- -- - ( 4 ) 9 ; Print the solution
using System ; class GFG { static int V = 5 ; static int INT_MAX = int . MaxValue ; static bool isValidEdge ( int u , int v , bool [ ] inMST ) { if ( u == v ) return false ; if ( inMST [ u ] == false && inMST [ v ] == false ) return false ; else if ( inMST [ u ] == true && inMST [ v ] == true ) return false ; return true ; } static void primMST ( int [ , ] cost ) { bool [ ] inMST = new bool [ V ] ; inMST [ 0 ] = true ; int edge_count = 0 , mincost = 0 ; while ( edge_count < V - 1 ) { int min = INT_MAX , a = - 1 , b = - 1 ; for ( int i = 0 ; i < V ; i ++ ) { for ( int j = 0 ; j < V ; j ++ ) { if ( cost [ i , j ] < min ) { if ( isValidEdge ( i , j , inMST ) ) { min = cost [ i , j ] ; a = i ; b = j ; } } } } if ( a != - 1 && b != - 1 ) { Console . Write ( " Edge ▁ { 0 } : ( { 1 } , ▁ { 2 } ) ▁ cost : ▁ { 3 } ▁ STRNEWLINE " , edge_count ++ , a , b , min ) ; mincost = mincost + min ; inMST [ b ] = inMST [ a ] = true ; } } Console . Write ( " cost = { 0 } " , mincost ) ; } public static void Main ( String [ ] args ) { int [ , ] cost = { { INT_MAX , 2 , INT_MAX , 6 , INT_MAX } , { 2 , INT_MAX , 3 , 8 , 5 } , { INT_MAX , 3 , INT_MAX , INT_MAX , 7 } , { 6 , 8 , INT_MAX , INT_MAX , 9 } , { INT_MAX , 5 , 7 , 9 , INT_MAX } } ; primMST ( cost ) ; } }
Length and Breadth of rectangle such that ratio of Area to diagonal ^ 2 is maximum | C # for finding maximum p ^ 2 / A ratio of rectangle ; function to print length and breadth ; sort the input array ; create array vector of integers occurring in pairs ; push the same pairs ; calculate length and breadth as per requirement ; check for given condition ; print the required answer ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void findLandB ( int [ ] arr , int n ) { Array . Sort ( arr ) ; List < Double > arr_pairs = new List < Double > ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] == arr [ i + 1 ] ) { arr_pairs . Add ( ( double ) arr [ i ] ) ; i ++ ; } } double length = arr_pairs [ 0 ] ; double breadth = arr_pairs [ 1 ] ; double size = arr_pairs . Count ; for ( int i = 2 ; i < size ; i ++ ) { if ( ( length / breadth + breadth / length ) > ( arr_pairs [ i ] / arr_pairs [ i - 1 ] + arr_pairs [ i - 1 ] / arr_pairs [ i ] ) ) { length = arr_pairs [ i ] ; breadth = arr_pairs [ i - 1 ] ; } } Console . Write ( ( int ) length + " , ▁ " + ( int ) breadth + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 2 , 2 , 2 , 5 , 6 , 5 , 6 , 7 , 2 } ; int n = arr . Length ; findLandB ( arr , n ) ; } }
Smallest sum contiguous subarray | Set | C # program for Smallest sum contiguous subarray | Set 2 ; function to find the smallest sum contiguous subarray ; First invert the sign of the elements ; Apply the normal Kadane algorithm But on the elements Of the array having inverted sign ; Invert the answer to get minimum val ; Driver Code
using System ; class GFG { static int smallestSumSubarr ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = - arr [ i ] ; int sum_here = arr [ 0 ] , max_sum = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { sum_here = Math . Max ( sum_here + arr [ i ] , arr [ i ] ) ; max_sum = Math . Max ( max_sum , sum_here ) ; } return ( - 1 ) * max_sum ; } public static void Main ( ) { int [ ] arr = { 3 , - 4 , 2 , - 3 , - 1 , 7 , - 5 } ; int n = arr . Length ; Console . WriteLine ( " Smallest ▁ sum : ▁ " + smallestSumSubarr ( arr , n ) ) ; } }
Maximum number by concatenating every element in a rotation of an array | C # program to print the Maximum number by concatenating every element in rotation of array ; Function to print the largest number ; store the index of largest left most digit of elements ; Iterate for all numbers ; check for the last digit ; check for the largest left most digit ; print the rotation of array ; print the rotation of array ; Driver Code
using System ; class GFG { static void printLargest ( int [ ] a , int n ) { int max = - 1 ; int ind = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { int num = a [ i ] ; while ( num > 0 ) { int r = num % 10 ; num = num / 10 ; if ( num == 0 ) { if ( max < r ) { max = r ; ind = i ; } } } } for ( int i = ind ; i < n ; i ++ ) Console . Write ( a [ i ] ) ; for ( int i = 0 ; i < ind ; i ++ ) Console . Write ( a [ i ] ) ; } public static void Main ( ) { int [ ] a = { 54 , 546 , 548 , 60 } ; int n = 4 ; printLargest ( a , n ) ; } }