codeparrot/xlcost-text-to-code huggingface dataset:
Mean of minimum of all possible K | C # code for the above approach ; Function to find the value of nCr ; Base Case ; Find nCr recursively ; Function to find the expected minimum values of all the subsets of size K ; Find the factorials that will be used later ; Find the factorials ; Total number of subsets ; Stores the sum of minimum over all possible subsets ; Iterate over all possible minimum ; Find the mean over all subsets ; Driver Code
using System ; public class GFG { static int nCr ( int n , int r , int [ ] f ) { if ( n < r ) { return 0 ; } return f [ n ] / ( f [ r ] * f [ n - r ] ) ; } static int findMean ( int N , int X ) { int [ ] f = new int [ N + 1 ] ; f [ 0 ] = 1 ; for ( int i = 1 ; i <= N ; i ++ ) { f [ i ] = f [ i - 1 ] * i ; } int total = nCr ( N , X , f ) ; int count = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { count += nCr ( N - i , X - 1 , f ) * i ; } double E_X = ( double ) ( count ) / ( double ) ( total ) ; Console . Write ( " { 0 : F9 } " , E_X ) ; return 0 ; } static publ int N = 3 , X = 2 ; findMean ( N , X ) ; } } void Main ( ) {
Maximize count of odd | C # implementation for the above approach ; To find maximum number of pairs in array with conversion of at most one element ; Initialize count of even elements ; Initialize count of odd elements ; If current number is even then increment x by 1 ; If current number is odd then increment y by 1 ; Initialize the answer by min ( x , y ) ; If difference in count of odd and even is more than 2 than increment answer ; Return final answer ; Driver code ; Given array
using System ; public class GFG { static int maximumNumberofpairs ( int n , int [ ] arr ) { int x = 0 ; int y = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { x ++ ; } else { y ++ ; } } int answer = Math . Min ( x , y ) ; if ( Math . Abs ( x - y ) >= 2 ) { answer ++ ; } return answer ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 2 , 4 , 6 , 5 , 10 , 12 } ; int n = arr . Length ; Console . WriteLine ( maximumNumberofpairs ( n , arr ) ) ; } }
Sum of product of all unordered pairs in given range with update queries | C # code for the above approach ; Function to calculate the Pairwise Product Sum in range from L to R ; Loop to iterate over all possible pairs from L to R ; Print answer ; Function to update the Array element at index P to X ; Update the value at Pth index in the array ; Function to solve Q queries ; If Query is of type 1 ; If Query is of type 2 ; Driver Code
using System ; public class GFG { static void pairwiseProductSum ( int [ ] a , int l , int r ) { int sum = 0 ; for ( int j = l - 1 ; j <= r - 1 ; j ++ ) { for ( int k = j + 1 ; k <= r - 1 ; k ++ ) { sum += ( a [ j ] * a [ k ] ) ; } } Console . Write ( sum + " STRNEWLINE " ) ; } static void updateArray ( int [ ] a , int p , int x ) { a [ p - 1 ] = x ; } static void solveQueries ( int [ ] a , int n , int Q , int [ ] [ ] query ) { for ( int i = 0 ; i < Q ; i ++ ) { if ( query [ i ] [ 0 ] == 1 ) pairwiseProductSum ( a , query [ i ] [ 1 ] , query [ i ] [ 2 ] ) ; else updateArray ( a , query [ i ] [ 1 ] , query [ i ] [ 2 ] ) ; } } static public void Main ( ) { int [ ] A = { 5 , 7 , 2 , 3 , 1 } ; int N = A . Length ; int Q = 3 ; int [ ] [ ] query = { new int [ 3 ] { 1 , 1 , 3 } , new int [ 3 ] { 2 , 2 , 5 } , new int [ 3 ] { 1 , 2 , 5 } } ; solveQueries ( A , N , Q , query ) ; } }
Check if final remainder is present in original Array by reducing it based on given conditions | C # program to implement above approach ; copying original array ; loop till length of array become 2. ; find middle element ; pop element from front and rear ; find remainder ; append remainder to a ; now since length of array is 2 take product and divide it by n ; if remainder is present is original array return 1 , else return 0 ; calling function Reduced ; if x = 1 print YES else NO
using System ; using System . Collections . Generic ; class GFg { static int Reduced ( List < int > a , int n ) { List < int > original_array = new List < int > ( a ) ; while ( a . Count != 2 ) { int mid = a . Count / 2 ; int mid_ele = a [ mid ] ; int start = a [ 0 ] ; a . RemoveAt ( 0 ) ; int end = a [ a . Count - 1 ] ; a . RemoveAt ( a . Count - 1 ) ; int rmd = ( start * end ) % mid_ele ; a . Add ( rmd ) ; int remainder = ( a [ 0 ] * a [ 1 ] ) % n ; for ( int i = 0 ; i < original_array . Count ; i ++ ) { if ( original_array [ i ] == remainder ) { return 1 ; } } } return 0 ; } int x = Reduced ( Arr , N ) ; if ( x > 0 ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } }
Find two numbers from their sum and OR | C # code for the above approach ; Function to find the two integers from the given sum and Bitwise OR value ; Check if Z is non negative ; Iterate through all the bits ; Find the kth bit of A & B ; Find the kth bit of A | B ; If bit1 = 1 and bit2 = 0 , then there will be no possible pairs ; Print the possible pairs ; Driver Code
using System ; public class GFG { static int MaxBit = 32 ; static void possiblePair ( int X , int Y ) { int Z = Y - X ; if ( Z < 0 ) { Console . Write ( " - 1" ) ; } for ( int k = 0 ; k < MaxBit ; k ++ ) { int bit1 = ( Z >> k ) & 1 ; int bit2 = ( Z >> k ) & 1 ; if ( bit1 != 0 && bit2 == 0 ) { Console . Write ( " - 1" ) ; } } Console . Write ( Z + " ▁ " + X ) ; } static public void Main ( ) { int X = 7 , Y = 11 ; possiblePair ( X , Y ) ; } }
Maximum frequencies in each M | Function to find the frequency of the most common element in each M length subarrays ; Stores frequency of array element ; Stores the maximum frequency ; Iterate for the first sub - array and store the maximum ; Print the maximum frequency for the first subarray ; Iterate over the range [ M , N ] ; Subtract the A [ i - M ] and add the A [ i ] in the map ; Find the maximum frequency ; Print the maximum frequency for the current subarray ; Driver Code
using System ; using System . Collections . Generic ; public class GFG { static void maxFrequencySubarrayUtil ( int [ ] A , int N , int M ) { int i = 0 ; Dictionary < int , int > m = new Dictionary < int , int > ( ) ; int val = 0 ; for ( ; i < M ; i ++ ) { if ( m . ContainsKey ( A [ i ] ) ) { val = m [ A [ i ] ] ; m . Remove ( A [ i ] ) ; m . Add ( A [ i ] , val + 1 ) ; } else { m . Add ( A [ i ] , 1 ) ; } val = Math . Max ( val , m [ A [ i ] ] ) ; } Console . Write ( val + " ▁ " ) ; for ( i = M ; i < N ; i ++ ) { if ( m . ContainsKey ( i - M ) ) { val = i - M ; m . Remove ( i - M ) ; m . Add ( i - M , val - 1 ) ; } if ( m . ContainsKey ( A [ i ] ) ) { val = m [ A [ i ] ] ; m . Remove ( A [ i ] ) ; m . Add ( A [ i ] , val + 1 ) ; } else { m . Add ( A [ i ] , 1 ) ; } val = Math . Max ( val , m [ A [ i ] ] ) ; val = 0 ; foreach ( KeyValuePair < int , int > x in m ) { val = Math . Max ( val , x . Value ) ; } Console . Write ( val + " ▁ " ) ; } } static public void Main ( ) { int [ ] A = { 1 , 1 , 2 , 2 , 3 , 5 } ; int N = 6 ; int M = 4 ; maxFrequencySubarrayUtil ( A , N , M ) ; } }
Generate an N | C # program for above approach ; Function to print target array ; Sort the given array ; Seeking for index of elements with minimum diff . ; Seeking for index ; To store target array ; Copying element ; Copying remaining element ; Printing target array ; Driver code ; Given Input ; Function Call
using System ; class GFG { public static void printArr ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int minDifference = 1000000007 ; int minIndex = - 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( minDifference > Math . Abs ( arr [ i ] - arr [ i - 1 ] ) ) { minDifference = Math . Abs ( arr [ i ] - arr [ i - 1 ] ) ; minIndex = i - 1 ; } } int [ ] Arr = new int [ n ] ; Arr [ 0 ] = arr [ minIndex ] ; Arr [ n - 1 ] = arr [ minIndex + 1 ] ; int pos = 1 ; for ( int i = minIndex + 2 ; i < n ; i ++ ) { Arr [ pos ++ ] = arr [ i ] ; } for ( int i = 0 ; i < minIndex ; i ++ ) { Arr [ pos ++ ] = arr [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( Arr [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int N = 8 ; int [ ] arr = { 4 , 6 , 2 , 6 , 8 , 2 , 6 , 4 } ; printArr ( arr , N ) ; } }
Construct a Perfect Binary Tree from Preorder Traversal | C # program for the above approach ; Structure of the tree ; Function to create a new node with the value val ; Return the newly created node ; Function to create the Perfect Binary Tree ; If preStart > preEnd return NULL ; Initialize root as pre [ preStart ] ; If the only node is left , then return node ; Parameters for further recursion ; Recursive Call to build the subtree of root node ; Return the created root ; Function to build Perfect Binary Tree ; Function to print the Inorder of the given Tree ; Base Case ; Left Recursive Call ; Print the data ; Right Recursive Call ; Driver Code ; Function Call ; Print Inorder Traversal
using System ; using System . Collections . Generic ; class GFG { class Node { public int data ; public Node left , right ; public Node ( int val ) { data = val ; left = right = null ; } } static Node getNewNode ( int val ) { Node newNode = new Node ( val ) ; return newNode ; } static Node buildPerfectBT_helper ( int preStart , int preEnd , int [ ] pre ) { if ( preStart > preEnd ) return null ; Node root = getNewNode ( pre [ preStart ] ) ; if ( preStart == preEnd ) return root ; int leftPreStart = preStart + 1 ; int rightPreStart = leftPreStart + ( preEnd - leftPreStart + 1 ) / 2 ; int leftPreEnd = rightPreStart - 1 ; int rightPreEnd = preEnd ; root . left = buildPerfectBT_helper ( leftPreStart , leftPreEnd , pre ) ; root . right = buildPerfectBT_helper ( rightPreStart , rightPreEnd , pre ) ; return root ; } static Node buildPerfectBT ( int [ ] pre , int size ) { return buildPerfectBT_helper ( 0 , size - 1 , pre ) ; } static void printInorder ( Node root ) { if ( root == null ) return ; printInorder ( root . left ) ; Console . Write ( root . data + " ▁ " ) ; printInorder ( root . right ) ; } static void Main ( ) { int [ ] pre = { 1 , 2 , 4 , 5 , 3 , 6 , 7 } ; int N = pre . Length ; Node root = buildPerfectBT ( pre , N ) ; Console . Write ( " Inorder ▁ traversal ▁ of ▁ the ▁ tree : ▁ " ) ; printInorder ( root ) ; } }
Minimize operations to convert each node of N | C # program for the above approach ; Create adjacency list ; Function to add an edges in graph ; Function to perform the DFS of graph recursively from a given vertex u ; Check for the condition for the flipping of node 's initial value ; Traverse all the children of the current source node u ; Swap foo and foo1 signifies there is change of level ; Function to perform the DFSUtil ( ) for all the unvisited vertices ; Traverse the given set of nodes ; If the current node is unvisited ; Print the number of operations ; Function to count the number of flips required to change initial node values to final node value ; Add the given edges ; DFS Traversal ; Driver code
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int N = 3 ; static List < List < int > > adj = new List < List < int > > ( ) ; static List < bool > visited ; static int ans ; static void addEdges ( int u , int v ) { adj [ u ] . Add ( v ) ; adj [ v ] . Add ( u ) ; } static void DFSUtil ( int u , bool foo , bool foo1 , bool [ ] initial , bool [ ] finall ) { visited [ u ] = true ; if ( ( initial [ u - 1 ] ^ foo ) ^ finall [ u - 1 ] == true ) { ans += 1 ; foo ^= true ; } for ( int i = 0 ; i < adj [ u ] . Count ; i ++ ) { if ( visited [ adj [ u ] [ i ] ] == false ) DFSUtil ( adj [ u ] [ i ] , foo1 , foo , initial , finall ) ; } } static void DFS ( int V , bool [ ] initial , bool [ ] finall ) { ans = 0 ; visited = new List < bool > ( ) ; for ( int i = 0 ; i < V ; i ++ ) { visited . Add ( false ) ; } for ( int u = 1 ; u <= 1 ; u ++ ) { if ( visited [ u ] == false ) DFSUtil ( u , false , false , initial , finall ) ; } Console . Write ( ans ) ; } static void countOperations ( int N , bool [ ] initial , bool [ ] finall , int [ , ] Edges ) { for ( int i = 0 ; i < N - 1 ; i ++ ) { addEdges ( Edges [ i , 0 ] , Edges [ i , 1 ] ) ; } DFS ( N + 1 , initial , finall ) ; } static void Main ( ) { for ( int i = 0 ; i < N + 1 ; i ++ ) { adj . Add ( new List < int > ( ) ) ; } int [ , ] Edges = { { 1 , 2 } , { 1 , 3 } } ; bool [ ] initial = { true , true , false } ; bool [ ] finall = { false , true , true } ; countOperations ( N , initial , finall , Edges ) ; } }
Minimum number of moves to make M and N equal by repeatedly adding any divisor of number to itself except 1 and the number | C # program for the above approach . ; Function to find the minimum number of moves to make N and M equal . ; Array to maintain the numbers included . ; pair of vertex , count ; run bfs from N ; if we reached goal ; Iterate in the range ; If i is a factor of aux ; If i is less than M - aux and is not included earlier . ; If aux / i is less than M - aux and is not included earlier . ; Not possible ; Driver Code
using System ; using System . Collections ; class GFG { static int countOperations ( int N , int M ) { bool [ ] visited = new bool [ 100001 ] ; Queue Q = new Queue ( ) ; Q . Enqueue ( new Tuple < int , int > ( N , 0 ) ) ; visited [ N ] = true ; while ( Q . Count > 0 ) { int aux = ( ( Tuple < int , int > ) ( Q . Peek ( ) ) ) . Item1 ; int cont = ( ( Tuple < int , int > ) ( Q . Peek ( ) ) ) . Item2 ; Q . Dequeue ( ) ; if ( aux == M ) return cont ; for ( int i = 2 ; i * i <= aux ; i ++ ) if ( aux % i == 0 ) { if ( aux + i <= M && ! visited [ aux + i ] ) { Q . Enqueue ( new Tuple < int , int > ( aux + i , cont + 1 ) ) ; visited [ aux + i ] = true ; } if ( aux + aux / i <= M && ! visited [ aux + aux / i ] ) { Q . Enqueue ( new Tuple < int , int > ( aux + aux / i , cont + 1 ) ) ; visited [ aux + aux / i ] = true ; } } } return - 1 ; } static void Main ( ) { int N = 4 , M = 24 ; Console . WriteLine ( countOperations ( N , M ) ) ; } }
Finding Astronauts from different countries | C # program for the above approach ; Function to perform the DFS Traversal to find the count of connected components ; Function to perform the DFS Traversal to find the count of connected components ; Marking vertex visited ; DFS call to neighbour vertices ; If the current node is not visited , then recursively call DFS ; Function to find the number of ways to choose two astronauts from the different countries ; Stores the Adjacency list ; Constructing the graph ; Stores the visited vertices ; Stores the size of every connected components ; DFS call to the graph ; Store size of every connected component ; Stores the total number of ways to count the pairs ; Traverse the array ; Print the value of ans ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static List < List < int > > adj ; static bool [ ] visited ; static int num ; static void dfs ( int v ) { visited [ v ] = true ; num ++ ; for ( int i = 0 ; i < adj [ v ] . Count ; i ++ ) { if ( ! visited [ adj [ v ] [ i ] ] ) { dfs ( adj [ v ] [ i ] ) ; } } } static void numberOfPairs ( int N , int [ , ] arr ) { adj = new List < List < int > > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { adj . Add ( new List < int > ( ) ) ; } for ( int i = 0 ; i < 2 ; i ++ ) { adj [ arr [ i , 0 ] ] . Add ( arr [ i , 1 ] ) ; adj [ arr [ i , 1 ] ] . Add ( arr [ i , 0 ] ) ; } visited = new bool [ N ] ; Array . Fill ( visited , false ) ; List < int > v = new List < int > ( ) ; num = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ! visited [ i ] ) { dfs ( i ) ; v . Add ( num ) ; num = 0 ; } } int ans = N * ( N - 1 ) / 2 + 1 ; for ( int i = 0 ; i < v . Count ; i ++ ) { ans -= ( v [ i ] * ( v [ i ] - 1 ) / 2 ) + 1 ; } Console . Write ( ans ) ; } static void Main ( ) { int N = 6 ; int [ , ] arr = { { 0 , 1 } , { 0 , 2 } , { 2 , 5 } } ; numberOfPairs ( N , arr ) ; } }
Count of Perfect Numbers in given range for Q queries | C # program for the above approach ; Function to check whether a number is perfect Number ; Stores sum of divisors ; Itearate over the range [ 2 , sqrt ( N ) ] ; If sum of divisors is equal to N , then N is a perfect number ; Function to find count of perfect numbers in a given range ; Stores the count of perfect Numbers upto a every number less than MAX ; Iterate over the range [ 1 , MAX ] ; Traverse the array arr [ ] ; Print the count of perfect numbers in the range [ arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ] ; Driver Code
using System ; public class MyClass { static int MAX = 100005 ; static int isPerfect ( long N ) { long sum = 1 ; for ( long i = 2 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) { if ( i * i != N ) sum = sum + i + N / i ; else sum = sum + i ; } } if ( sum == N && N != 1 ) return 1 ; return 0 ; } static void Query ( int [ , ] arr , int N ) { int [ ] prefix = new int [ MAX + 1 ] ; for ( int i = 2 ; i <= MAX ; i ++ ) { prefix [ i ] = prefix [ i - 1 ] + isPerfect ( i ) ; } for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( prefix [ arr [ i , 1 ] ] - prefix [ arr [ i , 0 ] - 1 ] + " ▁ " ) ; } } public static void Main ( ) { int [ , ] arr = { { 1 , 1000 } , { 1000 , 2000 } , { 2000 , 3000 } } ; int N = arr . GetLength ( 0 ) ; Query ( arr , N ) ; } }
2 Keys Keyboard Problem | C # program for the above approach ; Function to find the minimum number of steps required to form N number of A 's ; Stores the count of steps needed ; Traverse over the range [ 2 , N ] ; Iterate while N is divisible by d ; Increment the value of ans by d ; Divide N by d ; If N is not 1 ; Return the ans ; Driver Code
using System ; class GFG { static int minSteps ( int N ) { int ans = 0 ; for ( int d = 2 ; d * d <= N ; d ++ ) { while ( N % d == 0 ) { ans += d ; N /= d ; } } if ( N != 1 ) { ans += N ; } return ans ; } static public void Main ( ) { int N = 3 ; Console . Write ( minSteps ( N ) ) ; } }
Sum of all the prime divisors of a number | Set 2 | C # program for the above approach ; Function to find sum of prime divisors of the given number N ; Add the number 2 if it divides N ; Traverse the loop from [ 3 , sqrt ( N ) ] ; If i divides N , add i and divide N ; This condition is to handle the case when N is a prime number greater than 2 ; Driver code ; Given Input ; Function Call
using System ; class GFG { public static int SumOfPrimeDivisors ( int n ) { int sum = 0 ; if ( n % 2 == 0 ) { sum = sum + 2 ; } while ( n % 2 == 0 ) { n = n / 2 ; } for ( int i = 3 ; i <= Math . Sqrt ( n ) ; i = i + 2 ) { if ( n % i == 0 ) { sum = sum + i ; } while ( n % i == 0 ) { n = n / i ; } } if ( n > 2 ) { sum = sum + n ; } return sum ; } public static void Main ( String [ ] args ) { int n = 10 ; Console . Write ( SumOfPrimeDivisors ( n ) ) ; } }
Count bases which contains a set bit as the Most Significant Bit in the representation of N | C # program for the above approach ; Function to count bases having MSB of N as a set bit ; Store the required count ; Iterate over the range [ 2 , N ] ; Store the MSB of N ; If MSB is 1 , then increment the count by 1 ; Return the count ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int countOfBase ( int N ) { int count = 0 ; for ( int i = 2 ; i <= N ; ++ i ) { int highestPower = ( int ) ( Math . Log ( N ) / Math . Log ( i ) ) ; int firstDigit = N / ( int ) Math . Pow ( i , highestPower ) ; if ( firstDigit == 1 ) { ++ count ; } } return count ; } public static void Main ( ) { int N = 6 ; Console . Write ( countOfBase ( N ) ) ; } }
K | C # program for the above approach ; Function to find the kth digit from last in an integer n ; If k is less than equal to 0 ; Convert integer into string ; If k is greater than length of the string temp ; Print the k digit from last ; Driver code ; Given Input ; Function call
using System ; using System . Collections . Generic ; class GFG { static void kthDigitFromLast ( int n , int k ) { if ( k <= 0 ) { Console . Write ( - 1 ) ; return ; } string temp = n . ToString ( ) ; if ( k > temp . Length ) { Console . WriteLine ( - 1 ) ; } else { Console . Write ( temp [ temp . Length - k ] - 48 ) ; } } public static void Main ( ) { int n = 2354 ; int k = 2 ; kthDigitFromLast ( n , k ) ; } }
Count ways to split array into three non | C # program for the above approach ; Function to count ways to split array into three subarrays with equal Bitwise XOR ; Stores the XOR value of arr [ ] ; Update the value of arr_xor ; Stores the XOR value of prefix and suffix array respectively ; Stores the ending points of all the required prefix arrays ; Stores the count of suffix arrays whose XOR value is equal to the total XOR value at each index ; Find all prefix arrays with XOR value equal to arr_xor ; Update pref_xor ; Fill the values of suff_inds [ ] ; Update suff_xor ; Update suff_inds [ i ] ; Stores the total number of ways ; Count total number of ways ; Return the final count ; Driver code ; Given Input ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int countWays ( int [ ] arr , int N ) { int arr_xor = 0 ; for ( int i = 0 ; i < N ; i ++ ) arr_xor ^= arr [ i ] ; int pref_xor = 0 , suff_xor = 0 ; List < int > pref_ind = new List < int > ( ) ; int [ ] suff_inds = new int [ N + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { pref_xor ^= arr [ i ] ; if ( pref_xor == arr_xor ) pref_ind . Add ( i ) ; } for ( int i = N - 1 ; i >= 0 ; i -- ) { suff_xor ^= arr [ i ] ; suff_inds [ i ] += suff_inds [ i + 1 ] ; if ( suff_xor == arr_xor ) suff_inds [ i ] ++ ; } int tot_ways = 0 ; foreach ( int idx in pref_ind ) { if ( idx < N - 1 ) tot_ways += suff_inds [ idx + 2 ] ; } return tot_ways ; } public static void Main ( string [ ] args ) { int [ ] arr = { 7 , 0 , 5 , 2 , 7 } ; int N = arr . Length ; Console . WriteLine ( countWays ( arr , N ) ) ; } }
Count pairs up to N having sum equal to their XOR | C # program for the above approach ; 2D array for memoization ; Recursive Function to count pairs ( x , y ) such that x + y = x ^ y ; If the String is traversed completely ; If the current subproblem is already calculated ; If bound = 1 and s [ i ] = = '0' , only ( 0 , 0 ) can be placed ; Otherwise ; Placing ( 0 , 1 ) and ( 1 , 0 ) are equivalent . Hence , multiply by 2. ; Place ( 0 , 0 ) at the current position . ; Return the answer ;  ; Utility Function to convert N to its binary representation ; Function to count pairs ( x , y ) such that x + y = x ^ y ; Convert the number to equivalent binary representation ; Initialize dp array with - 1. ; Print answer returned by recursive function ; Driver code ; Input ; Function call
using System ; class GFG { static int [ , ] dp = new int [ 1000 , 2 ] ; static int IsSumEqualsXor ( int i , int n , int bound , char [ ] s ) { if ( i == n ) return 1 ; if ( dp [ i , bound ] != - 1 ) return dp [ i , bound ] ; int ans = 0 ; if ( bound != 0 && s [ i ] == '0' ) { ans = IsSumEqualsXor ( i + 1 , n , 1 , s ) ; } else { ans = 2 * IsSumEqualsXor ( i + 1 , n , bound != 0 & ( s [ i ] == '1' ) ? 1 : 0 , s ) ; ans += IsSumEqualsXor ( i + 1 , n , 0 , s ) ; } return dp [ i , bound ] = ans ; } static String reverse ( String input ) { char [ ] a = input . ToCharArray ( ) ; int l , r = a . Length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . Join ( " " , a ) ; } static String convertToBinary ( int n ) { String ans = " " ; while ( n > 0 ) { char rem = ( char ) ( n % 2 + '0' ) ; ans += ( rem ) ; n /= 2 ; } ans = reverse ( ans ) ; return ans ; } static void IsSumEqualsXorUtil ( int N ) { String s = convertToBinary ( N ) ; for ( int i = 0 ; i < dp . GetLength ( 0 ) ; i ++ ) { for ( int j = 0 ; j < dp . GetLength ( 1 ) ; j ++ ) { dp [ i , j ] = - 1 ; } } Console . Write ( IsSumEqualsXor ( 0 , s . Length , 1 , s . ToCharArray ( ) ) + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int N = 10 ; IsSumEqualsXorUtil ( N ) ; } }
Mean of fourth powers of first N natural numbers | C # program for the above approach ; Function to find the average of the fourth power of first N natural numbers ; Stores the sum of the fourth powers of first N natural numbers ; Calculate the sum of fourth power ; Return the average ; Driver Code
using System ; class GFG { static double findAverage ( int N ) { double S = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { S += i * i * i * i ; } return S / N ; } public static void Main ( ) { int N = 3 ; Console . WriteLine ( findAverage ( N ) ) ; } }