codeparrot/xlcost-text-to-code huggingface dataset:
Minimum number that can be obtained by applying ' + ' and ' * ' operations on array elements | C # program for the above approach ; Function to find the smallest number that can be obtained after applying the arithmetic operations mentioned in the String S ; Stores the count of multiplication operator in the String ; Store the required result ; Iterate in the range to create the mask ; Checking the number of bits that are set in the mask ; Check if the number of bits that are set in the mask is multiplication operation ; Storing the elements that is to be added ; Apply the multiplications operation first ; If sign is ' * ' , then multiply last element of deque with arr [ i ] ; Push last multiplied element in the deque ; If the element is to be added , then add it to the deque ; Add all the element of the deque ; Minimize the answer with the given sum ; Driver Code
using System ; using System . Collections . Generic ; public class GFG { static int minimumSum ( int [ ] A , int N , String S ) { int mul = 0 ; for ( int i = 0 ; i < ( int ) S . Length ; i ++ ) { if ( S [ i ] == ' * ' ) mul += 1 ; } int ans = 1000000 ; for ( int i = 0 ; i < ( 1 << ( N - 1 ) ) ; i ++ ) { int cnt = 0 ; List < char > v = new List < char > ( ) ; for ( int j = 0 ; j < N - 1 ; j ++ ) { if ( ( ( 1 << j ) & ( i ) ) > 0 ) { cnt += 1 ; v . Add ( ' * ' ) ; } else { v . Add ( ' + ' ) ; } } if ( cnt == mul ) { List < int > d = new List < int > ( ) ; d . Add ( A [ 0 ] ) ; for ( int j = 0 ; j < N - 1 ; j ++ ) { if ( v [ j ] == ' * ' ) { int x = d [ d . Count - 1 ] ; d . RemoveAt ( d . Count - 1 ) ; x = x * A [ j + 1 ] ; d . Add ( x ) ; } else { d . Add ( A [ j + 1 ] ) ; } } int sum = 0 ; while ( d . Count > 0 ) { int x = d [ 0 ] ; sum += x ; d . RemoveAt ( 0 ) ; } ans = Math . Min ( ans , sum ) ; } } return ans ; } public static void Main ( String [ ] args ) { int [ ] A = { 2 , 2 , 2 , 2 } ; String S = " * * + " ; int N = A . Length ; Console . Write ( minimumSum ( A , N , S ) ) ; } }
Minimum sum of medians of all possible K length subsequences of a sorted array | C # program for the above approach ; Function to find the minimum sum of all the medians of the K sized sorted arrays formed from the given array ; Stores the distance between the medians ; Stores the number of subsequences required ; Stores the resultant sum ; Iterate from start and add all the medians ; Add the value of arr [ i ] to the variable minsum ; Increment i by select the median to get the next median index ; Decrement the value of totalArrays by 1 ; Print the resultant minimum sum ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void sumOfMedians ( int [ ] arr , int N , int K ) { int selectMedian = ( K + 1 ) / 2 ; int totalArrays = N / K ; int minSum = 0 ; int i = selectMedian - 1 ; while ( i < N && totalArrays != 0 ) { minSum = minSum + arr [ i ] ; i = i + selectMedian ; totalArrays -- ; } Console . WriteLine ( minSum ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 } ; int N = arr . Length ; int K = 2 ; sumOfMedians ( arr , N , K ) ; } }
Find K positive integers not exceeding N and having sum S | C # implementation for the above approach ; Function to represent S as the sum of K positive integers less than or equal to N ; If S can cannot be represented as sum of K integers ; If sum of first i natural numbers exceeds S ; Insert i into nums [ ] ; Insert first K - 1 positive numbers into answer [ ] ; Insert the K - th number ; Traverse the array answer [ ] ; If current element exceeds N ; Add the extra value to the previous element ; Reduce current element to N ; Printing the K numbers ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void solve ( int S , int K , int N ) { if ( K > N ) { Console . WriteLine ( " - 1" ) ; return ; } int max_sum = 0 , min_sum = 0 ; for ( int i = 1 ; i <= K ; i ++ ) { min_sum += i ; max_sum += N - i + 1 ; } if ( S < min_sum S > max_sum ) { Console . WriteLine ( " - 1" ) ; return ; } int s1 = 0 ; List < int > nums = new List < int > ( ) ; for ( int i = 1 ; i <= N ; i ++ ) { if ( s1 > S ) break ; s1 += i ; nums . Add ( i ) ; } List < int > answer = new List < int > ( ) ; int s2 = 0 ; for ( int i = 0 ; i < K - 1 ; i ++ ) { answer . Add ( nums [ i ] ) ; s2 += nums [ i ] ; } answer . Add ( S - s2 ) ; int Max = N ; for ( int i = answer . Count - 1 ; i >= 0 ; i -- ) { if ( answer [ i ] > Max ) { int extra = answer [ i ] - Max ; if ( i - 1 >= 0 ) answer [ i - 1 ] += extra ; answer [ i ] = Max ; Max -- ; } else break ; } foreach ( int x in answer ) Console . Write ( x + " ▁ " ) ; Console . WriteLine ( ) ; } public static void Main ( ) { int S = 15 , K = 4 , N = 8 ; solve ( S , K , N ) ; } }
Generate an N | C # program for the above approach ; Function to minimize the maximum element present in an N - length array having sum of elements divisible by K ; Return the ceil value of ( K / N ) ; Driver Code
using System ; class GFG { static int minimumValue ( int N , int K ) { return ( int ) Math . Ceiling ( ( double ) K / ( double ) N ) ; } public static void Main ( ) { int N = 4 , K = 50 ; Console . WriteLine ( minimumValue ( N , K ) ) ; } }
Minimum removal of elements from end of an array required to obtain sum K | C # program for the above approach ; Function to find the smallest array that can be removed from the ends of an array to obtain sum K ; Number of elements removed from the left and right ends of the array ; Sum of left and right subarrays ; No element is taken from left initially ; Start taking elements from right side ; ( left + 1 ) : Count of elements removed from the left ( N - right ) : Count of elements removed from the right ; If sum is greater than K ; If it is not possible to obtain sum K ; Driver Code ; Given Array ; Given target sum
using System ; class GFG { static void minSizeArr ( int [ ] A , int N , int K ) { int leftTaken = N , rightTaken = N ; int leftSum = 0 , rightSum = 0 ; for ( int left = - 1 ; left < N ; left ++ ) { if ( left != - 1 ) leftSum += A [ left ] ; rightSum = 0 ; for ( int right = N - 1 ; right > left ; right -- ) { rightSum += A [ right ] ; if ( leftSum + rightSum == K ) { if ( leftTaken + rightTaken > ( left + 1 ) + ( N - right ) ) { leftTaken = left + 1 ; rightTaken = N - right ; } break ; } if ( leftSum + rightSum > K ) break ; } } if ( leftTaken + rightTaken <= N ) { for ( int i = 0 ; i < leftTaken ; i ++ ) Console . Write ( A [ i ] + " ▁ " ) ; for ( int i = 0 ; i < rightTaken ; i ++ ) Console . Write ( A [ N - i - 1 ] + " ▁ " ) ; } else Console . Write ( - 1 ) ; } public static void Main ( ) { int N = 7 ; int [ ] A = { 3 , 2 , 1 , 1 , 1 , 1 , 3 } ; int K = 10 ; minSizeArr ( A , N , K ) ; } }
Minimum removal of elements from end of an array required to obtain sum K | C # program for the above approach ; Function to find the smallest array that can be removed from the ends of an array to obtain sum K ; Sum of complete array ; If given number is greater than sum of the array ; If number is equal to the sum of array ; tar is sum of middle subarray ; Find the longest subarray with sum equal to tar ; If there is no subarray with sum equal to tar ; Driver code ; Given Array ; Given target sum
using System ; using System . Collections . Generic ; class GFG { static void minSizeArr ( int [ ] A , int N , int K ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += A [ i ] ; if ( K > sum ) { Console . WriteLine ( - 1 ) ; return ; } if ( K == sum ) { for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( A [ i ] + " ▁ " ) ; } return ; } int tar = sum - K ; Dictionary < int , int > um = new Dictionary < int , int > ( ) ; um [ 0 ] = - 1 ; int left = 0 , right = 0 ; int cur = 0 , maxi = - 1 ; for ( int i = 0 ; i < N ; i ++ ) { cur += A [ i ] ; if ( um . ContainsKey ( cur - tar ) && i - um [ cur - tar ] > maxi ) { maxi = i - um [ cur - tar ] ; right = i ; left = um [ cur - tar ] ; } if ( ! um . ContainsKey ( cur ) ) um [ cur ] = i ; } if ( maxi == - 1 ) Console . Write ( - 1 ) ; else { for ( int i = 0 ; i <= left ; i ++ ) Console . Write ( A [ i ] + " ▁ " ) ; for ( int i = 0 ; i < right ; i ++ ) Console . Write ( A [ N - i - 1 ] + " ▁ " ) ; } } static public void Main ( ) { int N = 7 ; int [ ] A = { 3 , 2 , 1 , 1 , 1 , 1 , 3 } ; int K = 10 ; minSizeArr ( A , N , K ) ; } }
Minimum product modulo N possible for any pair from a given range | C # program for the above approach ; Function to return the minimum possible value of ( i * j ) % N ; Stores the minimum remainder ; Iterate from L to R ; Iterate from L to R ; Print the minimum value of remainder ; If R - L >= N ; Driver Code
using System ; class GFG { static void minModulo ( int L , int R , int N ) { if ( R - L < N ) { int ans = Int32 . MaxValue ; for ( int i = L ; i <= R ; i ++ ) for ( int j = L ; j <= R ; j ++ ) if ( i != j ) ans = Math . Min ( ans , ( i * j ) % N ) ; Console . WriteLine ( ans ) ; } else { Console . WriteLine ( 0 ) ; } } public static void Main ( string [ ] args ) { int L = 6 , R = 10 , N = 2019 ; minModulo ( L , R , N ) ; } }
Count numbers having GCD with N equal to the number itself | C # program for the above approach ; Function to count numbers whose GCD with N is the number itself ; Stores the count of factors of N ; Iterate over the range [ 1 , sqrt ( N ) ] ; If i is divisible by i ; Increment count ; Avoid counting the same factor twice ; Return the resultant count ; Driver Code
using System ; public class GFG { static int countNumbers ( int N ) { int count = 0 ; for ( int i = 1 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) { count ++ ; if ( N / i != i ) { count ++ ; } } } return count ; } public static void Main ( string [ ] args ) { int N = 10 ; Console . WriteLine ( countNumbers ( N ) ) ; } }
Maximum length of all possible K equal length ropes generated by cutting N ropes | C # program for the above approach ; Function to find the maximum size of ropes having frequency at least K by cutting the given ropes ; Stores the left and the right boundaries ; Stores the maximum length of rope possible ; Iterate while low is less than or equal to high ; Stores the mid value of the range [ low , high ] ; Stores the count of ropes of length mid ; Traverse the array [ ] arr ; If count is at least K ; Assign mid to ans ; Update the value of low ; Otherwise , update the value of high ; Return the value of ans ; Driver Code
using System ; using System . Linq ; class GFG { static int maximumSize ( int [ ] a , int k ) { int low = 1 ; int high = a . Max ( ) ; int ans = - 1 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; int count = 0 ; for ( int c = 0 ; c < a . Length ; c ++ ) { count += a / mid ; } if ( count >= k ) { ans = mid ; low = mid + 1 ; } else { high = mid - 1 ; } } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 9 } ; int K = 6 ; Console . WriteLine ( maximumSize ( arr , K ) ) ; } }
Quadratic equation whose roots are K times the roots of given equation | C # program for the above approach ; Function to find the quadratic equation whose roots are K times the roots of the given equation ; Print quadratic equation ; Driver Code
using System ; class GFG { static void findEquation ( int A , int B , int C , int K ) { Console . Write ( A + " ▁ " + K * B + " ▁ " + K * K * C ) ; } public static void Main ( ) { int A = 1 , B = 2 , C = 1 , K = 2 ; findEquation ( A , B , C , K ) ; } }
Minimum increments required to make absolute difference of all pairwise adjacent array elements even | C # program for the above approach ; Function to find the minimum number of increments of array elements required to make difference between all pairwise adjacent elements even ; Stores the count of odd and even elements ; Traverse the array ; Increment odd count ; Increment even count ; Return the minimum number of operations required ; Driver Code
using System ; class GFG { static int minOperations ( int [ ] arr , int n ) { int oddcount = 0 , evencount = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 1 ) oddcount ++ ; else evencount ++ ; } return Math . Min ( oddcount , evencount ) ; } public static void Main ( ) { int [ ] arr = { 2 , 4 , 3 , 1 , 8 } ; int N = ( arr . Length ) ; Console . WriteLine ( minOperations ( arr , N ) ) ; } }
Count numbers up to C that can be reduced to 0 by adding or subtracting A or B | C # program for the above approach ; Function to calculate GCD of the two numbers a and b ; Base Case ; Recursively find the GCD ; Function to count the numbers up to C that can be reduced to 0 by adding or subtracting A or B ; Stores GCD of A and B ; Stores the count of multiples of g in the range ( 0 , C ] ; Print the result ; Driver Code
using System ; class GFG { static long gcd ( long a , long b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static void countDistinctNumbers ( long A , long B , long C ) { long g = gcd ( A , B ) ; long count = C / g ; Console . Write ( count ) ; } static void Main ( ) { long A = 2 , B = 3 , C = 5 ; countDistinctNumbers ( A , B , C ) ; } }
Find the last element after repeatedly removing every second element from either end alternately | C # program for the above approach ; Function to find the last element remaining in the array after performing the given operations ; Checks if traversal is from left to right or vice versa ; Store the elements currently present in the array ; Store the distance between 2 consecutive array elements ; Store the first element of the remaining array ; Iterate while elements are greater than 1 ; If left to right turn ; Update head ; Otherwise , check if the remaining elements are odd ; If true , update head ; Eleminate half of the array elements ; Double the steps after each turn ; Alter the turn ; Print the remaining element ; Driver code
using System ; public class GFG { public static void printLastElement ( int [ ] arr , int N ) { bool leftTurn = true ; int remainElements = N ; int step = 1 ; int head = 1 ; while ( remainElements > 1 ) { if ( leftTurn ) { head = head + step ; } else { if ( remainElements % 2 == 1 ) head = head + step ; } remainElements = remainElements / 2 ; step = step * 2 ; leftTurn = ! leftTurn ; } Console . Write ( arr [ head - 1 ] ) ; } static public void Main ( ) { int [ ] arr = { 2 , 3 , 5 , 6 } ; int N = arr . Length ; printLastElement ( arr , N ) ; } }
Distributed C candies among N boys such that difference between maximum and minimum candies received is K | C # program for the above approach ; Function to calculate the maximum and minimum number of candies a boy can possess ; All candies will be given to one boy ; All the candies will be given to 1 boy ; Give K candies to 1 st boy initially ; Count remaining candies ; If the last candy of remaining candies is given to the last boy , i . e Nth boy ; Increase minimum count ; Driver code
using System ; class GFG { static void max_min ( int N , int C , int K ) { int maximum , minimum ; if ( N == 1 ) { maximum = minimum = C ; } else if ( K >= C ) { maximum = C ; minimum = 0 ; } else { maximum = K ; minimum = 0 ; int remain_candy = C - K ; maximum += remain_candy / N ; minimum = remain_candy / N ; if ( remain_candy % N == N - 1 ) { minimum ++ ; } } Console . WriteLine ( " Maximum ▁ = ▁ " + maximum ) ; Console . WriteLine ( " Minimum ▁ = ▁ " + minimum ) ; } static void Main ( ) { int N = 4 ; int C = 12 ; int K = 3 ; max_min ( N , C , K ) ; } }
Length of smallest subarray required to be removed to make remaining elements consecutive | C # program for the above approach ; Function to find the length of the smallest subarray to be removed to make remaining array elements consecutive ; Store the ending index of the longest prefix consecutive array ; Traverse the array to find the longest prefix consecutive sequence ; A [ 0. . . left_index ] is the prefix consecutive sequence ; Store the starting index of the longest suffix consecutive sequence ; Traverse the array to find the longest suffix consecutive sequence ; A [ right_index ... N - 1 ] is the consecutive sequence ; Store the smallest subarray required to be removed ; Check if subarray from the middle can be removed ; Update the right index s . t . A [ 0 , N - 1 ] is consecutive ; If updated_right < N , then update the minimumLength ; Print the required result ; Driver code
using System ; class GFG { static void shortestSubarray ( int [ ] A , int N ) { int i ; int left_index ; for ( i = 0 ; i < N - 1 ; i ++ ) { if ( A [ i ] + 1 != A [ i + 1 ] ) break ; } left_index = i ; int right_index ; for ( i = N - 1 ; i >= 1 ; i -- ) { if ( A [ i ] != A [ i - 1 ] + 1 ) break ; } right_index = i ; int updated_right ; int minLength = Math . Min ( N - left_index - 1 , right_index ) ; if ( A [ right_index ] <= A [ left_index ] + 1 ) { updated_right = right_index + A [ left_index ] - A [ right_index ] + 1 ; if ( updated_right < N ) minLength = Math . Min ( minLength , updated_right - left_index - 1 ) ; } Console . WriteLine ( minLength ) ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 7 , 4 , 3 , 5 } ; int N = arr . Length ; shortestSubarray ( arr , N ) ; } }
Check if a string can be split into 3 substrings such that one of them is a substring of the other two | C # program for the above approach ; Function to check if string S contains any character with frequency >= 3 or not ; Stores frequency of characters ; Iterate over the string ; Update the frequency of current character ; Iterate over the hash array ; If any character has frequency >= 3 ; Otherwise ; Driver code
using System ; class GFG { static string freqCheck ( string S , int N ) { int [ ] hash = new int [ 26 ] ; for ( int i = 0 ; i < N ; i ++ ) { hash [ S [ i ] - ' a ' ] ++ ; } for ( int i = 0 ; i < 26 ; i ++ ) { if ( hash [ i ] > 2 ) { return " Yes " ; } } return " No " ; } static public void Main ( ) { string S = " geekseekforgeeks " ; int N = S . Length ; Console . WriteLine ( freqCheck ( S , N ) ) ; } }
Minimize length of a string by removing suffixes and prefixes of same characters | C # program for the above approach ; Function to find the minimum length of the string after removing the same characters from the end and front of the two strings after dividing into 2 substrings ; Initialize two pointers ; Traverse the string S ; Current char on left pointer ; Shift i towards right ; Shift j towards left ; Return the minimum possible length of string ; Driver Code
using System ; class GFG { static int minLength ( string s ) { int i = 0 , j = s . Length - 1 ; for ( ; i < j && s [ i ] == s [ j ] ; ) { char d = s [ i ] ; while ( i <= j && s [ i ] == d ) i ++ ; while ( i <= j && s [ j ] == d ) j -- ; } return j - i + 1 ; } public static void Main ( string [ ] args ) { string S = " aacbcca " ; Console . WriteLine ( minLength ( S ) ) ; } }
Number of Binary Search Trees of height H consisting of H + 1 nodes | C # program for the above approach ; Function to calculate x ^ y modulo 1000000007 in O ( log y ) ; Stores the value of x ^ y ; Update x if it exceeds mod ; If x is divisible by mod ; If y is odd , then multiply x with result ; Divide y by 2 ; Update the value of x ; Return the value of x ^ y ; Function to count the number of of BSTs of height H consisting of ( H + 1 ) nodes ; Driver code
using System ; class GFG { static int mod = 1000000007 ; static long power ( long x , int y ) { long res = 1 ; x = x % mod ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( ( y & 1 ) == 1 ) res = ( res * x ) % mod ; y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } static long CountBST ( int H ) { return power ( 2 , H ) ; } static void Main ( ) { int H = 2 ; Console . Write ( CountBST ( H ) ) ; } }
Check if any pair of consecutive 1 s can be separated by at most M 0 s by circular rotation of a Binary String | C # program for the above approach ; Function to check if any pair of consecutive 1 s can be separated by at most M 0 s by circular rotation of string S ; Stores the indices of all 1 s ; Store the number of pairs separated by at least M 0 s ; Traverse the string ; Store the current index ; Traverse the array containing indices ; If the number of 0 s > M , then increment cnt by 1 ; Increment cnt ; Check if at least M '0' s lie between the first and last occurrence of '1' ; Increment cnt ; If the value of cnt <= 1 , then rotation of string is possible ; Otherwise ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void rotateString ( int n , int m , string s ) { List < int > v = new List < int > ( ) ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '1' ) { v . Add ( i ) ; } } for ( int i = 1 ; i < v . Count ; i ++ ) { if ( ( v [ i ] - v [ i - 1 ] - 1 ) > m ) { cnt ++ ; } } if ( v . Count >= 2 && ( n - ( v [ v . Count - 1 ] - v [ 0 ] ) - 1 ) > m ) { cnt ++ ; } if ( cnt <= 1 ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } public static void Main ( ) { string S = "101001" ; int M = 1 ; int N = S . Length ; rotateString ( N , M , S ) ; } }
Number obtained by reducing sum of digits of 2 N into a single digit | C # program for the above approach ; Function to find the number obtained by reducing sum of digits of 2 ^ N into a single digit ; Stores answers for different values of N ; Driver Code
using System ; class GFG { static int findNumber ( int N ) { int [ ] ans = { 1 , 2 , 4 , 8 , 7 , 5 } ; return ans [ N % 6 ] ; } public static void Main ( ) { int N = 6 ; Console . WriteLine ( findNumber ( N ) ) ; } }
Check if two piles of coins can be emptied by repeatedly removing 2 coins from a pile and 1 coin from the other | C # program for the above approach ; Function to check if two given piles can be emptied by repeatedly removing 2 coins from a pile and 1 coin from the other ; If maximum of A & B exceeds the twice of minimum of A & B ; Not possible to empty the piles ; If sum of both the coins is divisible by 3 , then print Yes ; Otherwise , print " No " ; Driver Code
using System ; class GFG { static void canBeEmptied ( int A , int B ) { if ( Math . Max ( A , B ) > 2 * Math . Min ( A , B ) ) { Console . WriteLine ( " No " ) ; return ; } if ( ( A + B ) % 3 == 0 ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } public static void Main ( ) { int A = 1 , B = 2 ; canBeEmptied ( A , B ) ; } }
Maximum index a pointer can reach in N steps by avoiding a given index B | Set 2 | C # program for the above approach ; Function to find the maximum index the pointer can reach ; Initialize two pointers ; Stores number of steps ; Stores sum of first N natural numbers ; Increment i with j ; Increment j with 1 ; Increment count ; If i points to B ; Break ; Print the pointer index ; Driver Code ; Given value of N & B ; Function call to find maximum index the pointer can reach
using System ; class GFG { static void maximumIndex ( int N , int B ) { int i = 0 , j = 1 ; int cnt = 0 ; int sum = N * ( N + 1 ) / 2 ; bool flag = false ; while ( cnt < N ) { i += j ; j ++ ; cnt ++ ; if ( i == B ) { flag = true ; break ; } } if ( ! flag == true ) { Console . Write ( sum ) ; } else { Console . Write ( sum - 1 ) ; } } static public void Main ( ) { int N = 4 , B = 6 ; maximumIndex ( N , B ) ; } }