codeparrot/xlcost-text-to-code huggingface dataset:
Count ways to divide C in two parts and add to A and B to make A strictly greater than B | C # implementation of the above approach ; Function to count the number of ways to divide C into two parts and add to A and B such that A is strictly greater than B ; Minimum value added to A to satisfy the given relation ; Number of different values of A , i . e . , number of ways to divide C ; Driver Code
using System ; class GFG { static int countWays ( int A , int B , int C ) { int minAddA = Math . Max ( 0 , ( C + B - A + 2 ) / 2 ) ; int count_ways = Math . Max ( C - minAddA + 1 , 0 ) ; return count_ways ; } public static void Main ( String [ ] args ) { int A = 3 , B = 5 , C = 5 ; Console . Write ( countWays ( A , B , C ) ) ; } }
GCD of elements occurring Fibonacci number of times in an Array | C # program to find the GCD of elements which occur Fibonacci number of times ; Function to create hash table to check Fibonacci numbers ; Inserting the first two numbers into the hash ; Adding the remaining Fibonacci numbers using the previously added elements ; Function to return the GCD of elements in an array having fibonacci frequency ; Creating the hash ; Map is used to store the frequencies of the elements ; Iterating through the array ; Traverse the map using iterators ; Calculate the gcd of elements having fibonacci frequencies ; Driver code
using System ; using System . Linq ; using System . Collections . Generic ; class GFG { static void createHash ( HashSet < int > hash , int maxElement ) { int prev = 0 , curr = 1 ; hash . Add ( prev ) ; hash . Add ( curr ) ; while ( curr <= maxElement ) { int temp = curr + prev ; hash . Add ( temp ) ; prev = curr ; curr = temp ; } } static int gcdFibonacciFreq ( int [ ] arr , int n ) { HashSet < int > hash = new HashSet < int > ( ) ; createHash ( hash , hash . Count > 0 ? hash . Max ( ) : 0 ) ; int i ; Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( i = 0 ; i < n ; i ++ ) { if ( m . ContainsKey ( arr [ i ] ) ) { m [ arr [ i ] ] = m [ arr [ i ] ] + 1 ; } else { m . Add ( arr [ i ] , 1 ) ; } } int gcd = 0 ; foreach ( KeyValuePair < int , int > it in m ) { if ( hash . Contains ( it . Value ) ) { gcd = __gcd ( gcd , it . Key ) ; } } return gcd ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 5 , 3 , 6 , 5 , 6 , 6 , 5 , 5 } ; int n = arr . Length ; Console . Write ( gcdFibonacciFreq ( arr , n ) ) ; } }
Program to print the series 1 , 9 , 17 , 33 , 49 , 73 , 97. . . till N terms | C # implementation of the above approach ; Function to print the series ; Generate the ith term and print it ; Driver Code
using System ; class GFG { static void printSeries ( int N ) { int ith_term = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { ith_term = i % 2 == 0 ? 2 * i * i + 1 : 2 * i * i - 1 ; Console . Write ( ith_term + " , ▁ " ) ; } } public static void Main ( ) { int N = 7 ; printSeries ( N ) ; } }
Find Nth term of the series where each term differs by 6 and 2 alternately | C # program for the above approach ; Function to find Nth term ; Iterate from 1 till Nth term ; Check if i is even and then add 6 ; Else add 2 ; Print ans ; Driver Code
using System ; class GFG { static void findNthTerm ( int N ) { int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( i % 2 == 0 ) { ans = ans + 6 ; } else { ans = ans + 2 ; } } Console . Write ( ans + " STRNEWLINE " ) ; } public static void Main ( ) { int N = 3 ; findNthTerm ( N ) ; } }
Find the number of ordered pairs such that a * p + b * q = N , where p and q are primes | C # program to find the number of ordered pairs such that a * p + b * q = N where p and q are primes ; Sieve of erastothenes to store the prime numbers and their frequency in form a * p + b * q ; Performing Sieve of Eratosthenes to find the prime numbers unto 10001 ; Loop to find the number of ordered pairs for every combination of the prime numbers ; Driver code ; Printing the number of ordered pairs for every query
using System ; class GFG { static int size = 10001 ; static int [ ] prime = new int [ size ] ; static int [ ] freq = new int [ size ] ; static void sieve ( int a , int b ) { prime [ 1 ] = 1 ; for ( int i = 2 ; i * i < size ; i ++ ) { if ( prime [ i ] == 0 ) { for ( int j = i * 2 ; j < size ; j += i ) prime [ j ] = 1 ; } } for ( int p = 1 ; p < size ; p ++ ) { for ( int q = 1 ; q < size ; q ++ ) { if ( prime [ p ] == 0 && prime [ q ] == 0 && a * p + b * q < size ) { freq [ a * p + b * q ] ++ ; } } } } public static void Main ( string [ ] args ) { int queries = 2 , a = 1 , b = 2 ; sieve ( a , b ) ; int [ ] arr = { 15 , 25 } ; for ( int i = 0 ; i < queries ; i ++ ) { Console . Write ( freq [ arr [ i ] ] + " ▁ " ) ; } } }
Find the Sum of the series 1 / 2 | C # program for the above approach ; Function to find the sum of series ; Generate the ith term and add it to the sum if i is even and subtract if i is odd ; Print the sum ; Driver Code
using System ; class GFG { static void printSeriesSum ( int N ) { double sum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( ( i & 1 ) == 0 ) { sum += ( double ) i / ( i + 1 ) ; } else { sum -= ( double ) i / ( i + 1 ) ; } } Console . WriteLine ( sum ) ; } public static void Main ( string [ ] args ) { int N = 10 ; printSeriesSum ( N ) ; } }
Find the Sum of the series 1 + 2 + 9 + 64 + 625 + 7776 . . . till N terms | C # program for the above approach ; Function to find the sum of series ; Generate the ith term and add it to the sum ; Print the sum ; Driver Code
using System ; class GFG { static void printSeriesSum ( int N ) { double sum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { sum += Math . Pow ( i , i - 1 ) ; } Console . Write ( sum + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int N = 5 ; printSeriesSum ( N ) ; } }
Find the Sum of the series 1 + 1 / 3 + 1 / 5 + 1 / 7 + ... till N terms | C # program to find the sum of the series 1 + 1 / 3 + 1 / 5 + ... ; Function to find the sum of the given series ; Initialise the sum to 0 ; Generate the ith term and add it to the sum ; Print the final sum ; Driver Code
using System ; class GFG { static void printSumSeries ( int N ) { float sum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { sum += ( float ) 1.0 / ( 2 * i - 1 ) ; } Console . WriteLine ( sum ) ; } public static void Main ( string [ ] args ) { int N = 6 ; printSumSeries ( N ) ; } }
Find the Sum of the series 1 , 2 , 3 , 6 , 9 , 18 , 27 , 54 , ... till N terms | C # program for the above series ; Function to find the sum of series ; Flag to find the multiplicating factor . . i . e , by 2 or 3 / 2 ; First term ; If flag is true , multiply by 2 ; If flag is false , multiply by 3 / 2 ; Update the previous element to nextElement ; Print the sum ; Driver Code
using System ; class GFG { static void printSeriesSum ( int N ) { double sum = 0 ; int a = 1 ; int cnt = 0 ; bool flag = true ; sum += a ; while ( cnt < N ) { int nextElement ; if ( flag == true ) { nextElement = a * 2 ; sum += nextElement ; flag = ! flag ; } else { nextElement = a * 3 / 2 ; sum += nextElement ; flag = ! flag ; } a = nextElement ; cnt ++ ; } Console . WriteLine ( sum ) ; } public static void Main ( string [ ] args ) { int N = 8 ; printSeriesSum ( N ) ; } }
Program to add two integers of given base | C # implementation to find the sum of two integers of base B ; Function to find the sum of two integers of base B ; Padding 0 in front of the number to make both numbers equal ; Condition to check if the strings have lengths mis - match ; Loop to find the find the sum of two integers of base B ; Current Place value for the resultant sum ; Update carry ; Find current digit ; Update sum result ; Driver Code ; Function Call
using System ; class GFG { static string sumBaseB ( string a , string b , int base_var ) { int len_a , len_b ; len_a = a . Length ; len_b = b . Length ; string sum , s ; s = " " ; sum = " " ; int diff ; diff = Math . Abs ( len_a - len_b ) ; for ( int i = 1 ; i <= diff ; i ++ ) s += "0" ; if ( len_a < len_b ) a = s + a ; else b = s + b ; int curr , carry = 0 ; for ( int i = Math . Max ( len_a , len_b ) - 1 ; i > - 1 ; i -- ) { curr = carry + ( a [ i ] - '0' ) + ( b [ i ] - '0' ) ; carry = curr / base_var ; curr = curr % base_var ; sum = ( char ) ( curr + '0' ) + sum ; } if ( carry > 0 ) sum = ( char ) ( carry + '0' ) + sum ; return sum ; } public static void Main ( string [ ] args ) { string a , b , sum ; int base_var ; a = "123" ; b = "234" ; base_var = 6 ; sum = sumBaseB ( a , b , base_var ) ; Console . WriteLine ( sum ) ; } }
Minimum number of swaps required to make a number divisible by 60 | C # program to find minimum number of swap operations required ; Function that print minimum number of swap operations required ; Condition if more than one zero exist ; Condition if zero_exist ; Computing total sum of all digits ; Condition if zero does not exist or the sum is not divisible by 3 ; Condition to find a digit that is multiple of 2 other than one zero ; Condition if multiple of 2 do not exist ; Condition for zero swaps means the number is already is divisible by 60 ; Condition for only one swap ; Otherwise 2 swaps required ; Driver Code
using System ; class GFG { static void MinimumSwapOperations ( string s ) { bool zero_exist = false ; bool multiple_of_2 = false ; int sum = 0 ; int index_of_zero = 0 ; bool more_zero = false ; for ( int i = 0 ; i < s . Length ; i ++ ) { int val = s [ i ] - '0' ; if ( zero_exist == true ) more_zero = true ; if ( val == 0 ) { zero_exist = true ; index_of_zero = i ; } sum += val ; } if ( zero_exist == false sum % 3 != 0 ) { Console . WriteLine ( " - 1" ) ; return ; } for ( int i = 0 ; i < s . Length ; i ++ ) { int val = s [ i ] - '0' ; if ( val % 2 == 0 && i != index_of_zero ) multiple_of_2 = true ; } if ( multiple_of_2 == false ) { Console . WriteLine ( " - 1" ) ; return ; } int last_val = s [ ( s . Length - 1 ) - '0' ] ; int second_last_val = s [ ( s . Length - 2 ) - '0' ] ; if ( last_val == 0 && second_last_val % 2 == 0 ) Console . WriteLine ( 0 ) ; else if ( ( last_val == 0 && second_last_val % 2 != 0 ) || ( last_val % 2 == 0 && second_last_val == 0 ) ) Console . WriteLine ( 1 ) ; else if ( more_zero == true && ( last_val == 0 && second_last_val % 2 != 0 ) ) Console . WriteLine ( 1 ) ; else Console . WriteLine ( 2 ) ; } public static void Main ( string [ ] args ) { string N = "20" ; MinimumSwapOperations ( N ) ; } }
Largest palindrome which is product of two N | C # implementation of the above approach ; Function to check if a number is a Palindrome or not ; Taking the string value of the number ; Loop to check if every i - th character from beginning is equal to every ( N - i ) th char ; Function to find the largest palindrome which is a product of two N digited numbers ; Find lowerBound , upperBound for a given nDigits . for n = 2 ; [ 10 , 99 ] ; Result variables ; Keep p decrementing by 11 ; Find the nearest number divisible by 11 ; Keep decrementing q by 1 ; Update the result if t > r and is a palindrome ; Printing the readonly result ; Driver code
using System ; class GFG { static bool isPalindrome ( long x ) { String num = String . Join ( " " , x ) ; bool result = true ; int i = 0 ; int j = num . Length - 1 ; while ( i < j && result ) { result = num [ i ++ ] == num [ j -- ] ; } return result ; } public static void find ( int nDigits ) { long lowerBound = ( long ) Math . Pow ( 10 , nDigits - 1 ) ; long upperBound = ( lowerBound * 10 ) - 1 ; long resultP = 0 , resultQ = 0 , resultR = 0 ; for ( long p = upperBound ; p > lowerBound ; p -= 11 ) { while ( p % 11 != 0 ) { p -- ; } for ( long q = upperBound ; q > lowerBound ; q -- ) { long t = p * q ; if ( t > resultR && isPalindrome ( t ) ) { resultP = p ; resultQ = q ; resultR = t ; break ; } } } Console . WriteLine ( resultR ) ; } public static void Main ( String [ ] args ) { int N = 2 ; find ( N ) ; } }
Minimize the cost of selecting two numbers whose product is X | C # implementation of the above approach ; max_prime [ i ] represents maximum prime number that divides the number i ; min_prime [ i ] represents minimum prime number that divides the number i ; Function to store the minimum prime factor and the maximum prime factor in two arrays ; Check for prime number if min_prime [ i ] > 0 , then it is not a prime number ; If i is a prime number , then both minimum and maximum prime numbers that divide the number is the number itself ; If this number is being visited for first time then this divisor must be the smallest prime number that divides this number ; The last prime number that divides this number will be maximum . ; Function to minimize the cost of finding two numbers for every number such that the product of those two is equal to X ; Pre - calculation ; If X == 1 , then there is no way to find N and M . Print - 1 ; Case 3 is always valid and cost for that is C + X C for choosing 1 and M = X / 1 ; Case 1 N is prime , first number cost is fixed N is max_prime number divides this number ; If X is prime then the maximum prime number is the number itself . For this case , M becomes 1 and this shouldn 't be considered. ; Find M for this case ; Add cost for the second number also ; Update min_cost , if the cost for prime is minimum ; Case 2 If N is composite For this find the minimum prime number that divides A [ i ] and consider this as M ; Find N for that number ; Check if this number is composite or not if N is prime then there is no way to find any composite number that divides X If N = min_prime [ N ] then N is prime ; Update min_cost , if the cost for the composite is minimum ; Driver code
using System ; class GFG { static int MAX = 1000 ; static int [ ] max_prime = new int [ MAX ] ; static int [ ] min_prime = new int [ MAX ] ; static void sieve ( int n ) { for ( int i = 2 ; i < n ; ++ i ) { if ( min_prime [ i ] > 0 ) { continue ; } min_prime [ i ] = i ; max_prime [ i ] = i ; int j = i + i ; while ( j < n ) { if ( min_prime [ j ] == 0 ) { min_prime [ j ] = i ; } max_prime [ j ] = i ; j += i ; } } } static int findCost ( int A , int B , int C , int X ) { sieve ( MAX ) ; int N , M ; if ( X == 1 ) { return - 1 ; } int min_cost = C + X ; int cost_for_prime = A ; N = max_prime [ X ] ; if ( N != X ) { M = X / N ; cost_for_prime += M ; min_cost = Math . Min ( min_cost , cost_for_prime ) ; } M = min_prime [ X ] ; N = X / M ; if ( N != min_prime [ N ] ) { int cost_for_comp = B + M ; min_cost = Math . Min ( min_cost , cost_for_comp ) ; } return min_cost ; } public static void Main ( string [ ] args ) { int A = 7 , B = 11 , C = 2 , X = 20 ; Console . WriteLine ( findCost ( A , B , C , X ) ) ; } }
Count the occurrence of digit K in a given number N using Recursion | C # implementation to count the occurrence of a digit in number using Recursion ; Function to count the digit K in the given number N ; Extracting least significant digit ; Driver Code
using System ; class GFG { static double countdigits ( int n , int k ) { if ( n == 0 ) return 0 ; int digit = n % 10 ; if ( digit == k ) return 1 + countdigits ( n / 10 , k ) ; return countdigits ( n / 10 , k ) ; } static public void Main ( ) { int n = 1000 ; int k = 0 ; Console . WriteLine ( countdigits ( n , k ) ) ; } }
Product of all Subarrays of an Array | C # program to find product of all subarray of an array ; Function to find product of all subarrays ; Variable to store the product ; Compute the product while traversing for subarrays ; Printing product of all subarray ; Driver code ; Function call
using System ; class GFG { static void product_subarrays ( int [ ] arr , int n ) { int res = 1 ; for ( int i = 0 ; i < n ; i ++ ) { int product = 1 ; for ( int j = i ; j < n ; j ++ ) { product *= arr [ j ] ; res = res * product ; } } Console . WriteLine ( res + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 10 , 3 , 7 } ; int n = arr . Length ; product_subarrays ( arr , n ) ; } }
Find numbers that divide X and Y to produce the same remainder | C # program to find numbers that divide X and Y to produce the same remainder ; Function to find the required number as M ; Finding the maximum value among X and Y ; Loop to iterate through maximum value among X and Y . ; If the condition satisfies , then print the value of M ; Driver code
using System ; class GFG { static void printModulus ( int X , int Y ) { int n = Math . Max ( X , Y ) ; for ( int i = 1 ; i <= n ; i ++ ) { if ( X % i == Y % i ) Console . Write ( i + " ▁ " ) ; } } public static void Main ( ) { int X , Y ; X = 10 ; Y = 20 ; printModulus ( X , Y ) ; } }
Find numbers that divide X and Y to produce the same remainder | C # program to find numbers that divide X and Y to produce the same remainder ; Function to print all the possible values of M such that X % M = Y % M ; Finding the absolute difference of X and Y ; Iterating from 1 ; Loop to print all the factors of D ; If i is a factor of d , then print i ; If d / i is a factor of d , then print d / i ; Driver code
using System ; public class GFG { static void printModulus ( int X , int Y ) { int d = Math . Abs ( X - Y ) ; int i = 1 ; while ( i * i <= d ) { if ( d % i == 0 ) { Console . Write ( i + " ▁ " ) ; if ( d / i != i ) Console . Write ( d / i + " ▁ " ) ; } i ++ ; } } public static void Main ( String [ ] args ) { int X = 10 ; int Y = 26 ; printModulus ( X , Y ) ; } }
Check whether a number can be represented as difference of two squares | C # program to check whether a number can be represented by the difference of two squares ; Function to check whether a number can be represented by the difference of two squares ; Checking if n % 4 = 2 or not ; Driver code
using System ; class GFG { static bool difSquare ( int n ) { if ( n % 4 != 2 ) { return true ; } return false ; } public static void Main ( ) { int n = 45 ; if ( difSquare ( n ) ) { Console . Write ( " Yes STRNEWLINE " ) ; } else { Console . Write ( " No STRNEWLINE " ) ; } } }
Count of Fibonacci divisors of a given number | C # program to count number of divisors of N which are Fibonacci numbers ; Function to create hash table to check Fibonacci numbers ; Function to count number of divisors of N which are fibonacci numbers ; If divisors are equal , check and count only one ; Otherwise check and count both ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void createHash ( HashSet < int > hash , int maxElement ) { int prev = 0 , curr = 1 ; hash . Add ( prev ) ; hash . Add ( curr ) ; while ( curr <= maxElement ) { int temp = curr + prev ; hash . Add ( temp ) ; prev = curr ; curr = temp ; } } static int countFibonacciDivisors ( int n ) { HashSet < int > hash = new HashSet < int > ( ) ; createHash ( hash , n ) ; int cnt = 0 ; for ( int i = 1 ; i <= Math . Sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( ( n / i == i ) && ( hash . Contains ( n / i ) ) ) cnt ++ ; else { if ( hash . Contains ( n / i ) ) cnt ++ ; if ( hash . Contains ( n / ( n / i ) ) ) cnt ++ ; } } } return cnt ; } public static void Main ( String [ ] args ) { int n = 12 ; Console . Write ( countFibonacciDivisors ( n ) ) ; } }
Count of subtrees in a Binary Tree having XOR value K | C # program to find the count of subtrees in a Binary Tree having XOR value K ; A binary tree node ; A utility function to allocate a new node ; Base Case : If node is null , return 0 ; Calculating the XOR of the current subtree int xr = ( root . data ) ; ^ rec ( root . left ) ^ rec ( root . right ) ; ; Increment res if xr is equal to k ; Return the XOR value of the current subtree ; Function to find the required count ; Initialize result variable ' res ' ; Recursively traverse the tree and compute the count ; return the count ' res ' ; Driver program ; Create the binary tree by adding nodes to it
using System ; public class GFG { class Node { public int data ; public Node left , right ; } ; static int res ; static int k ; static Node newNode ( int data ) { Node newNode = new Node ( ) ; newNode . data = data ; newNode . left = null ; newNode . right = null ; return newNode ; } static int rec ( Node root ) { if ( root == null ) { return 0 ; } xr ^= rec ( root . left ) ; xr ^= rec ( root . right ) ; if ( xr == k ) { res ++ ; } return xr ; } static int findCount ( Node root , int K ) { res = 0 ; k = K ; rec ( root ) ; return res ; } public static void Main ( String [ ] args ) { Node root = newNode ( 2 ) ; root . left = newNode ( 1 ) ; root . right = newNode ( 9 ) ; root . left . left = newNode ( 10 ) ; root . left . right = newNode ( 5 ) ; int K = 5 ; Console . WriteLine ( findCount ( root , K ) ) ; } }
Make array elements equal with minimum cost | C # implementation to make array elements equal with minimum cost ; Function to find the minimum cost required to make array elements equal ; Loop to find the count of odd elements ; Driver Code
using System ; class GFG { static void makearrayequal ( int [ ] arr , int n ) { int x = 0 ; for ( int i = 0 ; i < n ; i ++ ) { x += ( arr [ i ] & 1 ) ; } Console . WriteLine ( Math . Min ( x , n - x ) ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 4 , 3 , 2 , 1 } ; int n = arr . Length ; makearrayequal ( arr , n ) ; } }
Count of Prime digits in a Number | C # program to count the number of prime digits in a number ; Function to find the count of prime digits in a number ; Loop to compute all the digits of the number ; Finding every digit of the given number ; Checking if digit is prime or not Only 2 , 3 , 5 and 7 are prime one - digit number ; Driver code
using System ; class GFG { static int countDigit ( int n ) { int temp = n , count = 0 ; while ( temp != 0 ) { int d = temp % 10 ; temp /= 10 ; if ( d == 2 d == 3 d == 5 d == 7 ) count ++ ; } return count ; } public static void Main ( string [ ] args ) { int n = 1234567890 ; Console . WriteLine ( countDigit ( n ) ) ; } }
Check if an Array is a permutation of numbers from 1 to N : Set 2 | C # program to decide if an array represents a permutation or not ; Function to check if an array represents a permutation or not ; Counting the frequency ; Check if each frequency is 1 only ; Driver code
using System ; class GFG { static string permutation ( int [ ] arr , int N ) { int [ ] hash = new int [ N + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { hash [ arr [ i ] ] ++ ; } for ( int i = 1 ; i <= N ; i ++ ) { if ( hash [ i ] != 1 ) return " No " ; } return " Yes " ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 1 , 5 , 5 , 3 } ; int n = arr . Length ; Console . Write ( permutation ( arr , n ) + " STRNEWLINE " ) ; } }
Check if the XOR of an array of integers is Even or Odd | C # program to check if the XOR of an array is Even or Odd ; Function to check if the XOR of an array of integers is Even or Odd ; Count the number of odd elements ; If count of odd elements is odd , then XOR will be odd ; Else even ; Driver Code ; Function call
using System ; using System . Collections . Generic ; using System . Linq ; class GFG { static String check ( int [ ] arr , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 1 ) count ++ ; } if ( count == 1 ) return " Odd " ; else return " Even " ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 9 , 12 , 13 , 15 } ; int n = arr . Length ; Console . Write ( check ( arr , n ) ) ; } }