codeparrot/xlcost-text-to-code huggingface dataset:
Check if a number N can be expressed as the sum of powers of X or not | C # program for the above approach ; Function to check if the number N can be expressed as the sum of different powers of X or not ; While n is a positive number ; Find the remainder ; If rem is at least 2 , then representation is impossible ; Divide the value of N by x ; Driver code
using System ; class GFG { static bool ToCheckPowerofX ( int n , int x ) { while ( n > 0 ) { int rem = n % x ; if ( rem >= 2 ) { return false ; } n = n / x ; } return true ; } public static void Main ( String [ ] args ) { int N = 10 , X = 3 ; if ( ToCheckPowerofX ( N , X ) ) { Console . Write ( " Yes " ) ; } else { Console . Write ( " No " ) ; } } }
Find the maximum between N and the number formed by reversing 32 | C # program for tha above approach ; Function that obtains the number using said operations from N ; Stores the binary representation of the number N ; Find the binary representation of the number N ; Check for the set bits ; Reverse the string S ; Stores the obtained number ; Calculating the decimal value ; Check for set bits ; Function to find the maximum value between N and the obtained number ; Driver Code
using System ; class GFG { static string ReverseString ( string s ) { char [ ] array = s . ToCharArray ( ) ; Array . Reverse ( array ) ; return new string ( array ) ; } public static int reverseBin ( int N ) { string S = " " ; int i ; for ( i = 0 ; i < 32 ; i ++ ) { if ( ( N & ( 1L << i ) ) != 0 ) S += '1' ; else S += '0' ; } S = ReverseString ( S ) ; int M = 0 ; for ( i = 0 ; i < 32 ; i ++ ) { if ( S [ i ] == '1' ) M += ( 1 << i ) ; } return M ; } static int maximumOfTwo ( int N ) { int M = reverseBin ( N ) ; return Math . Max ( N , M ) ; } static void Main ( ) { int N = 6 ; Console . Write ( maximumOfTwo ( N ) ) ; } }
Sum of Euler Totient Functions obtained for each divisor of N | C # program for the above approach ; Function to find the sum of Euler Totient Function of divisors of N ; Return the value of N ; Driver code
using System ; class GFG { static int sumOfDivisors ( int N ) { return N ; } static void Main ( ) { int N = 5 ; Console . Write ( sumOfDivisors ( N ) ) ; } }
Minimum time required to schedule K processes | C # program for the above approach ; Function to find minimum required time to schedule all process ; Stores max element from A [ ] ; Find the maximum element ; Stores frequency of each element ; Stores minimum time required to schedule all process ; Count frequencies of elements ; Find the minimum time ; Decrease the value of K ; Increment tmp [ i / 2 ] ; Increment the count ; Return count , if all process are scheduled ; Increment count ; Return the count ; If it is not possible to schedule all process ; Driver Code
using System ; class GFG { static int minTime ( int [ ] A , int n , int K ) { int max_ability = A [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { max_ability = Math . Max ( max_ability , A [ i ] ) ; } int [ ] tmp = new int [ max_ability + 1 ] ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { tmp [ A [ i ] ] ++ ; } for ( int i = max_ability ; i >= 0 ; i -- ) { if ( tmp [ i ] != 0 ) { if ( tmp [ i ] * i < K ) { K -= ( i * tmp [ i ] ) ; tmp [ i / 2 ] += tmp [ i ] ; count += tmp [ i ] ; if ( K <= 0 ) { return count ; } } else { if ( K % i != 0 ) { count += ( K / i ) + 1 ; } else { count += ( K / i ) ; } return count ; } } } return - 1 ; } public static void Main ( string [ ] args ) { int [ ] arr = { 3 , 1 , 7 , 2 , 4 } ; int N = arr . Length ; int K = 15 ; Console . WriteLine ( minTime ( arr , N , K ) ) ; } }
Minimum length of a rod that can be split into N equal parts that can further be split into given number of equal parts | C # program for the above approach ; Function to find GCD of two numbers a and b ; Base Case ; Find GCD recursively ; Function to find the LCM of the resultant array ; Initialize a variable ans as the first element ; Traverse the array ; Update LCM ; Return the minimum length of the rod ; Function to find the minimum length of the rod that can be divided into N equals parts and each part can be further divided into arr [ i ] equal parts ; Print the result ; Driver code
using System ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int findlcm ( int [ ] arr , int n ) { int ans = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { ans = ( ( ( arr [ i ] * ans ) ) / ( gcd ( arr [ i ] , ans ) ) ) ; } return ans ; } static void minimumRod ( int [ ] A , int N ) { Console . WriteLine ( N * findlcm ( A , N ) ) ; } static void Main ( ) { int [ ] arr = { 1 , 2 } ; int N = arr . Length ; minimumRod ( arr , N ) ; } }
Check if N can be represented as sum of positive integers containing digit D at least once | C # program for the above approach ; Function to check if N contains digit D in it ; Iterate until N is positive ; Find the last digit ; If the last digit is the same as digit D ; Return false ; Function to check if the value of N can be represented as sum of integers having digit d in it ; Iterate until N is positive ; Check if N contains digit D or not ; Subtracting D from N ; Return false ; Driver Code
using System ; class GFG { static bool findDigit ( int N , int D ) { while ( N > 0 ) { int a = N % 10 ; if ( a == D ) { return true ; } N /= 10 ; } return false ; } static bool check ( int N , int D ) { while ( N > 0 ) { if ( findDigit ( N , D ) == true ) { return true ; } N -= D ; } return false ; } public static void Main ( ) { int N = 24 ; int D = 7 ; if ( check ( N , D ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }
Number of relations that are neither Reflexive nor Irreflexive on a Set | C # program for the above approach ; Function to calculate x ^ y modulo 10 ^ 9 + 7 in O ( log y ) ; Stores the result of ( x ^ y ) ; Update x , if it exceeds mod ; If x is divisible by mod ; If y is odd , then multiply x with res ; Divide y by 2 ; Update the value of x ; Return the value of x ^ y ; Function to count the number of relations that are neither reflexive nor irreflexive ; Return the resultant count ; Driver Code
using System ; class GFG { static int mod = 1000000007 ; static int power ( int x , int y ) { int res = 1 ; x = x % mod ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( ( y & 1 ) != 0 ) res = ( res * x ) % mod ; y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } static void countRelations ( int N ) { Console . Write ( ( power ( 2 , N ) - 2 ) * power ( 2 , N * N - N ) ) ; } public static void Main ( String [ ] args ) { int N = 2 ; countRelations ( N ) ; } }
Minimum operations required to make all elements in an array of first N odd numbers equal | C # program for the above approach ; Function to find the minimum number of operations required to make the array elements equal ; Stores the array elements ; Stores the sum of the array ; Iterate over the range [ 0 , N ] ; Update the value arr [ i ] ; Increment the sum by the value arr [ i ] ; Stores the middle element ; If N is even ; Otherwise ; Stores the result ; Traverse the range [ 0 , N / 2 ] ; Update the value of ans ; Return the result ; Driver Code
using System ; class GFG { public static int minOperations ( int N ) { int [ ] arr = new int [ N ] ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { arr [ i ] = ( 2 * i ) + 1 ; sum = sum + arr [ i ] ; } int mid = 0 ; if ( N % 2 == 0 ) { mid = sum / N ; } else { mid = arr [ N / 2 ] ; } int ans = 0 ; for ( int i = 0 ; i < N / 2 ; i ++ ) { ans += mid - arr [ i ] ; } return ans ; } public static void Main ( string [ ] args ) { int N = 6 ; Console . WriteLine ( minOperations ( N ) ) ; } }
Minimum operations required to make all elements in an array of first N odd numbers equal | C # program for the above approach ; Function to find the minimum number of operations required to make the array elements equal ; If the value of N is even ; Return the value ; Otherwise , N is odd ; Return the value ; Driver code
using System ; class GFG { public static int minOperation ( int N ) { if ( N % 2 == 0 ) { return ( N / 2 ) * ( N / 2 ) ; } int k = ( N - 1 ) / 2 ; return k * ( k + 1 ) ; } static void Main ( ) { int N = 6 ; Console . WriteLine ( minOperation ( N ) ) ; } }
Bitwise XOR of Bitwise AND of all pairs from two given arrays | C # program for the above approach ; Function to find the Bitwise XOR of Bitwise AND of all pairs from the arrays arr1 [ ] and arr2 [ ] ; Stores the result ; Iterate over the range [ 0 , N - 1 ] ; Iterate over the range [ 0 , M - 1 ] ; Stores Bitwise AND of the pair { arr1 [ i ] , arr2 [ j ] } ; Update res ; Return the res ; Driver Code ; Input
using System ; class GFG { static int findXORS ( int [ ] arr1 , int [ ] arr2 , int N , int M ) { int res = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { int temp = arr1 [ i ] & arr2 [ j ] ; res ^= temp ; } } return res ; } public static void Main ( ) { int [ ] arr1 = { 1 , 2 , 3 } ; int [ ] arr2 = { 6 , 5 } ; int N = arr1 . Length ; int M = arr2 . Length ; Console . Write ( findXORS ( arr1 , arr2 , N , M ) ) ; } }
Bitwise XOR of Bitwise AND of all pairs from two given arrays | C # program for the above approach ; Function to find the Bitwise XOR of Bitwise AND of all pairs from the arrays arr1 [ ] and arr2 [ ] ; Stores XOR of array arr1 [ ] ; Stores XOR of array arr2 [ ] ; Traverse the array arr1 [ ] ; Traverse the array arr2 [ ] ; Return the result ; Driver Code ; Input
using System ; class GFG { static int findXORS ( int [ ] arr1 , int [ ] arr2 , int N , int M ) { int XORS1 = 0 ; int XORS2 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { XORS1 ^= arr1 [ i ] ; } for ( int i = 0 ; i < M ; i ++ ) { XORS2 ^= arr2 [ i ] ; } return ( XORS1 & XORS2 ) ; } public static void Main ( String [ ] args ) { int [ ] arr1 = { 1 , 2 , 3 } ; int [ ] arr2 = { 6 , 5 } ; int N = arr1 . Length ; int M = arr2 . Length ; Console . WriteLine ( findXORS ( arr1 , arr2 , N , M ) ) ; } }
Count numbers up to N that cannot be expressed as sum of at least two consecutive positive integers | C # program for the above approach ; Function to check if a number can be expressed as a power of 2 ; f N is power of two ; Function to count numbers that cannot be expressed as sum of two or more consecutive + ve integers ; Stores the resultant count of integers ; Iterate over the range [ 1 , N ] ; Check if i is power of 2 ; Increment the count if i is not power of 2 ; Print the value of count ; Driver Code
using System ; class GFG { static bool isPowerof2 ( int n ) { return ( ( n & ( n - 1 ) ) > 0 && n > 0 ) ; } static void countNum ( int N ) { int count = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { bool flag = isPowerof2 ( i ) ; if ( ! flag ) { count ++ ; } } Console . Write ( count + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int N = 100 ; countNum ( N ) ; } }
Count numbers up to N that cannot be expressed as sum of at least two consecutive positive integers | C # program for the above approach ; Function to count numbers that cannot be expressed as sum of two or more consecutive + ve integers ; Stores the count of such numbers ; Driver Code
using System ; class GFG { static void countNum ( int N ) { int ans = ( int ) ( Math . Log ( N ) / Math . Log ( 2 ) ) + 1 ; Console . WriteLine ( ans ) ; } static void Main ( ) { int N = 100 ; countNum ( N ) ; } }
Partition a Linked List into K continuous groups with difference in their sizes at most 1 | C # program for the above approach ; Link List Node ; Function to insert a node into the Linked List ; Allocate a new dynamic node ; Update new_node . val ; Stores the head_ref ; Update head_ref ; Function to split the linked list in K groups ; Stores the K groups ; If head is null ; Iterate until K is non - zero ; Stores the length of the linked list ; Stores the head node of the linked list ; Iterate over the linked list ; Update p ; Update N ; Iterate over the linked list ; Stores the length of the current group ; Stores the current node ; Stores the previous node ; If rem is greater than 0 ; Update p ; Decrement rem by 1 ; Iterate until x is non - zero ; If the last is equal to p ; Otherwise ; Join the link between last and the current element ; Update the last node ; Update p node ; Assign null to last . next ; Push the current linked list in ans ; Decrement K ; While K greater than 0 ; Update the value of ans ; Increment K ; Print the result ; Print the value ; Update ans [ i ] ; Driver Code
using System ; using System . Collections . Generic ; public class GFG { class ListNode { public int val ; public ListNode next ; } ; static ListNode push ( ListNode head_ref , int node_val ) { ListNode new_node = new ListNode ( ) ; new_node . val = node_val ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } static void splitListInParts ( ListNode head , int K ) { List < ListNode > ans = new List < ListNode > ( ) ; if ( head == null ) { while ( K -- > 0 ) ans . Add ( null ) ; } int N = 0 ; ListNode p = head ; while ( p . next != null ) { p = p . next ; N ++ ; } int len = N / K ; int rem = N % K ; p = head ; while ( K > 0 && p . next != null ) { int x = len ; ListNode curr_head = p ; ListNode last = p ; if ( rem > 0 ) { p = p . next ; rem -- ; } while ( x -- > 0 ) { if ( last == p ) p = p . next ; else { last . next = p ; last = p ; p = p . next ; } } last . next = null ; ans . Add ( curr_head ) ; K -- ; } while ( K > 0 ) { ans . Add ( null ) ; K -- ; } Console . Write ( " { " ) ; for ( int i = 0 ; i < ans . Count ; i ++ ) { Console . Write ( " { " ) ; while ( ans [ i ] != null ) { Console . Write ( ans [ i ] . val + " ▁ " ) ; ans [ i ] = ans [ i ] . next ; } Console . Write ( " } " ) ; if ( i != ans . Count - 1 ) Console . Write ( " , ▁ " ) ; } Console . Write ( " } " ) ; } public static void Main ( String [ ] args ) { ListNode root = new ListNode ( ) ; root = push ( root , 8 ) ; root = push ( root , 7 ) ; root = push ( root , 6 ) ; root = push ( root , 5 ) ; root = push ( root , 4 ) ; root = push ( root , 3 ) ; root = push ( root , 2 ) ; root = push ( root , 1 ) ; int K = 3 ; splitListInParts ( root , K ) ; } }
Replace array elements that contains K as a digit with the nearest power of K | C # program for the above approach ; Function to calculate the power of base nearest to x ; Stores logX to the base K ; Function to replace array elements with nearest power of K ; Traverse the array ; Convert integer into a string ; If K is found , then replace with the nearest power of K ; Print the array ; Driver Code ; Given array ; Given value of K ; Function call to replace array elements with nearest power of K
using System ; using System . Collections . Generic ; class GFG { static int nearestPow ( int x , int base1 ) { int k = ( int ) ( Math . Log ( x ) / Math . Log ( base1 ) ) ; if ( Math . Abs ( Math . Pow ( base1 , k ) - x ) < Math . Abs ( Math . Pow ( base1 , ( k + 1 ) ) - x ) ) return ( int ) Math . Pow ( base1 , k ) ; else return ( int ) Math . Pow ( base1 , ( k + 1 ) ) ; } static void replaceWithNearestPowerOfK ( int [ ] arr , int K , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int num = arr [ i ] ; string strEle = num . ToString ( ) ; for ( int c = 0 ; c < strEle . Length ; c ++ ) { if ( ( strEle - '0' ) == K ) { arr [ i ] = nearestPow ( arr [ i ] , K ) ; break ; } } } for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } public static void Main ( ) { int [ ] arr = { 432 , 953 , 232 , 333 } ; int n = arr . Length ; int K = 3 ; replaceWithNearestPowerOfK ( arr , K , n ) ; } }
Difference between ceil of array sum divided by K and sum of ceil of array elements divided by K | C # approach for the above approach ; Function to find absolute difference between array sum divided by x and sum of ceil of array elements divided by x ; Stores the total sum ; Stores the sum of ceil of array elements divided by x ; Traverse the array ; Adding each array element ; Add the value ceil of arr [ i ] / x ; Find the ceil of the total sum divided by x ; Return absolute difference ; Driver Code
using System ; class GFG { static int ceilDifference ( int [ ] arr , int n , int x ) { int totalSum = 0 ; int perElementSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { totalSum += arr [ i ] ; perElementSum += ( int ) Math . Ceiling ( ( double ) ( arr [ i ] ) / ( double ) ( x ) ) ; } int totalCeilSum = ( int ) Math . Ceiling ( ( double ) ( totalSum ) / ( double ) ( x ) ) ; return Math . Abs ( perElementSum - totalCeilSum ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 } ; int K = 4 ; int N = arr . Length ; Console . Write ( ceilDifference ( arr , N , K ) ) ; } }
Remove trailing zeros from the sum of two numbers ( Using Stack ) | C # program for the above approach ; Function to remove trailing zeros from the sum of two numbers ; Stores the sum of A and B ; Stores the digits ; Stores the equivalent string of integer N ; Traverse the string ; Push the digit at i in the stack ; While top element is '0' ; Pop the top element ; Stores the resultant number without tailing 0 's ; While s is not empty ; Append top element of S in res ; Pop the top element of S ; Reverse the string res ; Driver Code ; Input ; Function Call
using System ; using System . Collections . Generic ; class GFG { static string removeTailing ( int A , int B ) { int N = A + B ; Stack < char > s = new Stack < char > ( ) ; string strsum = N . ToString ( ) ; for ( int i = 0 ; i < strsum . Length ; i ++ ) { s . Push ( strsum [ i ] ) ; } while ( s . Peek ( ) == '0' ) { s . Pop ( ) ; } string res = " " ; while ( s . Count != 0 ) { res = res + ( char ) s . Peek ( ) ; s . Pop ( ) ; } char [ ] str = res . ToCharArray ( ) ; Array . Reverse ( str ) ; return new string ( str ) ; } static public void Main ( ) { int A = 130246 , B = 450164 ; Console . WriteLine ( removeTailing ( A , B ) ) ; } }
Count number of triplets with product not exceeding a given number | C # program for the above approach ; Function to count the number of triplets whose product is at most N ; Stores the count of triplets ; Iterate over the range [ 0 , N ] ; Iterate over the range [ 0 , N ] ; If the product of pairs exceeds N ; Increment the count of possible triplets ; Return the total count ; Driver Code
using System ; class GFG { static int countTriplets ( int N ) { int ans = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <= N ; j ++ ) { if ( i * j > N ) break ; ans += N / ( i * j ) ; } } return ans ; } public static void Main ( String [ ] args ) { int N = 10 ; Console . Write ( countTriplets ( N ) ) ; } }
Sum of an Infinite Geometric Progression ( GP ) | C # program for the above approach ; Function to calculate the sum of an infinite Geometric Progression ; Case for Infinite Sum ; Store the sum of GP Series ; Print the value of sum ; Driver Code
using System ; class GFG { static void findSumOfGP ( double a , double r ) { if ( Math . Abs ( r ) >= 1 ) { Console . Write ( " Infinite " ) ; return ; } double sum = a / ( 1 - r ) ; Console . Write ( sum ) ; } public static void Main ( ) { double A = 1 , R = 0.5 ; findSumOfGP ( A , R ) ; } }
Number of Relations that are both Irreflexive and Antisymmetric on a Set | C # program for the above approach ; Function to calculate x ^ y % mod in O ( log y ) ; Stores the result of x ^ y ; Update x if it exceeds 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 relations that are irreflexive and antisymmetric in a set consisting of N elements ; Return the resultant count ; Driver Code
using System ; class GFG { static int mod = 1000000007 ; static int power ( long x , int y ) { int res = 1 ; x = x % mod ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( int ) ( res * x ) % mod ; y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } static int numberOfRelations ( int N ) { return power ( 3 , ( N * N - N ) / 2 ) ; } public static void Main ( String [ ] args ) { int N = 2 ; Console . Write ( numberOfRelations ( N ) ) ; } }
Count of numbers up to N having at least one prime factor common with N | C # program for the above approach ; Function to count all the numbers in the range [ 1 , N ] having common factor with N other than 1 ; Stores the count of numbers having more than 1 factor with N ; Iterate over the range [ 1 , N ] ; If gcd is not 1 then increment the count ; Print the resultant count ; Driver Code
using System ; public class GFG { static void countNumbers ( int N ) { int count = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( __gcd ( i , N ) != 1 ) count ++ ; } Console . Write ( count ) ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void Main ( String [ ] args ) { int N = 5 ; countNumbers ( N ) ; } }
Count of numbers up to N having at least one prime factor common with N | C # program for the above approach ; Function to calculate the value of Euler 's totient function ; Initialize result with N ; Find all prime factors of N and subtract their multiples ; Check if p is a prime factor ; If found to be true , then update N and result ; If N has a prime factor greater than sqrt ( N ) , then there can be at - most one such prime factor ; Function to count all the numbers in the range [ 1 , N ] having common factor with N other than 1 ; Stores the resultant count ; Print the count ; Driver code
using System ; public class GFG { static int phi ( int N ) { int result = N ; for ( int p = 2 ; p * p <= N ; ++ p ) { if ( N % p == 0 ) { while ( N % p == 0 ) N /= p ; result -= result / p ; } } if ( N > 1 ) result -= result / N ; return result ; } static void countNumbers ( int N ) { int count = N - phi ( N ) ; Console . Write ( count ) ; } public static void Main ( String [ ] args ) { int N = 5 ; countNumbers ( N ) ; } }
Queries to update array by adding or multiplying array elements and print the element present at specified index | C # program for the above approach ; Function to modify the array by performing given queries ; Stores the multiplication of all integers of type 1 ; Stores the value obtained after performing queries of type 1 & 2 ; Iterate over the queries ; Query of type 0 ; Update the value of add ; Query of type 1 ; Update the value of mul ; Update the value of add ; Otherwise ; Store the element at index Q [ i ] [ 1 ] ; Print the result for the current query ; Driver Code
using System ; class GFG { static void Query ( int [ ] arr , int N , int [ , ] Q ) { int mul = 1 ; int add = 0 ; for ( int i = 0 ; i < Q . GetLength ( 0 ) ; i ++ ) { if ( Q [ i , 0 ] == 0 ) { add = add + Q [ i , 1 ] ; } else if ( Q [ i , 0 ] == 1 ) { mul = mul * Q [ i , 1 ] ; add = add * Q [ i , 1 ] ; } else { int ans = arr [ Q [ i , 1 ] ] * mul + add ; Console . Write ( ans + " ▁ " ) ; } } } public static void Main ( ) { int [ ] arr = { 3 , 1 , 23 , 45 , 100 } ; int N = arr . Length ; int [ , ] Q = { { 1 , 2 } , { 0 , 10 } , { 2 , 3 } , { 1 , 5 } , { 2 , 4 } } ; Query ( arr , N , Q ) ; } }