codeparrot/xlcost-text-to-code huggingface dataset:
Check if original Array is retained after performing XOR with M exactly K times | C # implementation for the above mentioned problem ; Function to check if original Array can be retained by performing XOR with M exactly K times ; Check if O is present or not ; If K is odd and 0 is not present then the answer will always be No . ; Else it will be Yes ; Driver code
using System ; class GFG { static String check ( int [ ] Arr , int n , int M , int K ) { int flag = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( Arr [ i ] == 0 ) flag = 1 ; } if ( K % 2 != 0 && flag == 0 ) return " No " ; else return " Yes " ; } public static void Main ( String [ ] args ) { int [ ] Arr = { 1 , 1 , 2 , 4 , 7 , 8 } ; int M = 5 ; int K = 6 ; int n = Arr . Length ; Console . Write ( check ( Arr , n , M , K ) ) ; } }
Longest sub | C # program to find longest subarray of prime numbers ; To store the prime numbers ; Function that find prime numbers till limit ; Find primes using Sieve of Eratosthenes ; Function that finds all prime numbers in given range using Segmented Sieve ; Find the limit ; To store the prime numbers ; Comput all primes less than or equals to sqrt ( high ) using Simple Sieve ; Count the elements in the range [ low , high ] ; Declaring boolean for the range [ low , high ] ; Traverse the prime numbers till limit ; Find the minimum number in [ low . . high ] that is a multiple of prime [ i ] ; Mark the multiples of prime [ i ] in [ low , high ] as true ; Element which are not marked in range are Prime ; Function that finds longest subarray of prime numbers ; If element is Non - prime then updated current_max to 0 ; If element is prime , then update current_max and max_so_far ; Return the count of longest subarray ; Driver code ; Find minimum and maximum element ; Find prime in the range [ min_el , max_el ] ; Function call
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static HashSet < int > allPrimes = new HashSet < int > ( ) ; static void simpleSieve ( int limit , ref ArrayList prime ) { bool [ ] mark = new bool [ limit + 1 ] ; Array . Fill ( mark , false ) ; for ( int i = 2 ; i <= limit ; ++ i ) { if ( mark [ i ] == false ) { prime . Add ( i ) ; for ( int j = i ; j <= limit ; j += i ) { mark [ j ] = true ; } } } } static void primesInRange ( int low , int high ) { int limit = ( int ) Math . Floor ( Math . Sqrt ( high ) ) + 1 ; ArrayList prime = new ArrayList ( ) ; simpleSieve ( limit , ref prime ) ; int n = high - low + 1 ; bool [ ] mark = new bool [ n + 1 ] ; Array . Fill ( mark , false ) ; for ( int i = 0 ; i < prime . Count ; i ++ ) { int loLim = ( int ) Math . Floor ( ( double ) low / ( int ) prime [ i ] ) ; loLim *= ( int ) prime [ i ] ; if ( loLim < low ) { loLim += ( int ) prime [ i ] ; } if ( loLim == ( int ) prime [ i ] ) { loLim += ( int ) prime [ i ] ; } for ( int j = loLim ; j <= high ; j += ( int ) prime [ i ] ) mark [ j - low ] = true ; } for ( int i = low ; i <= high ; i ++ ) { if ( ! mark [ i - low ] ) { allPrimes . Add ( i ) ; } } } static int maxPrimeSubarray ( int [ ] arr , int n ) { int current_max = 0 ; int max_so_far = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! allPrimes . Contains ( arr [ i ] ) ) current_max = 0 ; else { current_max ++ ; max_so_far = Math . Max ( current_max , max_so_far ) ; } } return max_so_far ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 2 , 4 , 3 , 29 , 11 , 7 , 8 , 9 } ; int n = arr . Length ; int max_el = Int32 . MinValue ; int min_el = Int32 . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < min_el ) { min_el = arr [ i ] ; } if ( arr [ i ] > max_el ) { max_el = arr [ i ] ; } } primesInRange ( min_el , max_el ) ; Console . Write ( maxPrimeSubarray ( arr , n ) ) ; } }
Sum of all armstrong numbers lying in the range [ L , R ] for Q queries | C # program to find the sum of all armstrong numbers in the given range ; pref [ ] array to precompute the sum of all armstrong number ; Function that return number num if num is armstrong else return 0 ; Function to precompute the sum of all armstrong numbers upto 100000 ; checkarmstrong ( ) return the number i if i is armstrong else return 0 ; Function to print the sum for each query ; Function to prsum of all armstrong numbers between [ L , R ] ; Function that pre computes the sum of all armstrong numbers ; Iterate over all Queries to print the sum ; Driver code ; Queries ; Function that print the the sum of all armstrong number in Range [ L , R ]
using System ; class GFG { static int [ ] pref = new int [ 100001 ] ; static int checkArmstrong ( int x ) { int n = x . ToString ( ) . Length ; int sum1 = 0 ; int temp = x ; while ( temp > 0 ) { int digit = temp % 10 ; sum1 += ( int ) Math . Pow ( digit , n ) ; temp /= 10 ; } if ( sum1 == x ) return x ; return 0 ; } static void preCompute ( ) { for ( int i = 1 ; i < 100001 ; i ++ ) { pref [ i ] = pref [ i - 1 ] + checkArmstrong ( i ) ; } } static void printSum ( int L , int R ) { Console . WriteLine ( pref [ R ] - pref [ L - 1 ] ) ; } static void printSumarmstrong ( int [ , ] arr , int Q ) { preCompute ( ) ; for ( int i = 0 ; i < Q ; i ++ ) { printSum ( arr [ i , 0 ] , arr [ i , 1 ] ) ; } } public static void Main ( string [ ] args ) { int Q = 2 ; int [ , ] arr = { { 1 , 13 } , { 121 , 211 } } ; printSumarmstrong ( arr , Q ) ; } }
Count of pairs in an array whose product is a perfect square | C # program to calculate the number of pairs with product is perfect square ; Prime [ ] array to calculate Prime Number ; Array k [ ] to store the value of k for each element in [ ] arr ; For value of k , Sieve function is implemented ; Initialize k [ i ] to i ; Prime Sieve ; If i is prime then remove all factors of prime from it ; Update that j is not prime ; Remove all square divisors i . e . if k [ j ] is divisible by i * i then divide it by i * i ; Function that return total count of pairs with perfect square product ; Map used to store the frequency of k ; Store the frequency of k ; The total number of pairs is the summation of ( fi * ( fi - 1 ) ) / 2 ; Driver code ; Size of [ ] arr ; To pre - compute the value of k ; Function that return total count of pairs with perfect square product
using System ; using System . Collections . Generic ; class GFG { static int [ ] prime = new int [ 100001 ] ; static int [ ] k = new int [ 100001 ] ; static void Sieve ( ) { for ( int i = 1 ; i < 100001 ; i ++ ) k [ i ] = i ; for ( int i = 2 ; i < 100001 ; i ++ ) { if ( prime [ i ] == 0 ) for ( int j = i ; j < 100001 ; j += i ) { prime [ j ] = 1 ; while ( k [ j ] % ( i * i ) == 0 ) k [ j ] /= ( i * i ) ; } } } static int countPairs ( int [ ] arr , int n ) { Dictionary < int , int > freq = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( freq . ContainsKey ( k [ arr [ i ] ] ) ) { freq [ k [ arr [ i ] ] ] = freq [ k [ arr [ i ] ] ] + 1 ; } else freq . Add ( k [ arr [ i ] ] , 1 ) ; } int sum = 0 ; foreach ( KeyValuePair < int , int > i in freq ) { sum += ( ( i . Value - 1 ) * i . Value ) / 2 ; } return sum ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 4 , 8 , 5 , 6 } ; int n = arr . Length ; Sieve ( ) ; Console . Write ( countPairs ( arr , n ) + " STRNEWLINE " ) ; } }
Sum of all Perfect Cubes lying in the range [ L , R ] for Q queries | C # program to find the sum of all perfect cubes in the given range ; Array to precompute the sum of cubes from 1 to 100010 so that for every query , the answer can be returned in O ( 1 ) . ; Function to check if a number is a perfect Cube or not ; Find floating point value of cube root of x . ; If cube root of x is cr return the x , else 0 ; Function to precompute the perfect Cubes upto 100000. ; Function to print the sum for each query ; Driver code ; To calculate the precompute function ; Calling the printSum function for every query
using System ; class GFG { public static long [ ] pref = new long [ 100010 ] ; static long isPerfectCube ( long x ) { double cr = Math . Round ( MathF . Cbrt ( x ) ) ; if ( cr * cr * cr == ( double ) x ) return x ; return 0 ; } static void compute ( ) { for ( long i = 1 ; i <= 100000 ; ++ i ) { pref [ i ] = pref [ i - 1 ] + isPerfectCube ( i ) ; } } static void printSum ( int L , int R ) { long sum = pref [ R ] - pref [ L - 1 ] ; Console . Write ( sum + " ▁ " ) ; } public static void Main ( ) { compute ( ) ; int Q = 4 ; int [ , ] arr = new int [ , ] { { 1 , 10 } , { 1 , 100 } , { 2 , 25 } , { 4 , 50 } } ; for ( int i = 0 ; i < Q ; i ++ ) { printSum ( arr [ i , 0 ] , arr [ i , 1 ] ) ; } } }
Total number of Subsets of size at most K | C # code to find total number of Subsets of size at most K ; Function to compute the value of Binomial Coefficient C ( n , k ) ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Function to calculate sum of nCj from j = 1 to k ; Calling the nCr function for each value of j ; Driver code
using System ; class GFG { public static int binomialCoeff ( int n , int k ) { int [ , ] C = new int [ n + 1 , k + 1 ] ; int i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= Math . Min ( i , k ) ; j ++ ) { if ( j == 0 j == i ) C [ i , j ] = 1 ; else C [ i , j ] = C [ i - 1 , j - 1 ] + C [ i - 1 , j ] ; } } return C [ n , k ] ; } public static int count ( int n , int k ) { int sum = 0 ; for ( int j = 1 ; j <= k ; j ++ ) { sum = sum + binomialCoeff ( n , j ) ; } return sum ; } public static void Main ( ) { int n = 3 , k = 2 ; Console . Write ( count ( n , k ) ) ; int n1 = 5 , k1 = 2 ; Console . Write ( count ( n1 , k1 ) ) ; } }
Tree Isomorphism Problem | An iterative C # program to solve tree isomorphism problem ; A binary tree node has data , pointer to left and right children ; Given a binary tree , print its nodes in reverse level order ; Both roots are NULL , trees isomorphic by definition ; Exactly one of the n1 and n2 is NULL , trees not isomorphic ; There are two possible cases for n1 and n2 to be isomorphic Case 1 : The subtrees rooted at these nodes have NOT been " Flipped " . Both of these subtrees have to be isomorphic . Case 2 : The subtrees rooted at these nodes have been " Flipped " ; Driver program to test above functions ; Let us create trees shown in above diagram
using System ; public class Node { public int data ; public Node left , right ; public Node ( int item ) { data = item ; left = right ; } } public class BinaryTree { public Node root1 , root2 ; public virtual bool isIsomorphic ( Node n1 , Node n2 ) { if ( n1 == null && n2 == null ) { return true ; } if ( n1 == null n2 == null ) { return false ; } if ( n1 . data != n2 . data ) { return false ; } return ( isIsomorphic ( n1 . left , n2 . left ) && isIsomorphic ( n1 . right , n2 . right ) ) || ( isIsomorphic ( n1 . left , n2 . right ) && isIsomorphic ( n1 . right , n2 . left ) ) ; } public static void Main ( string [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root1 = new Node ( 1 ) ; tree . root1 . left = new Node ( 2 ) ; tree . root1 . right = new Node ( 3 ) ; tree . root1 . left . left = new Node ( 4 ) ; tree . root1 . left . right = new Node ( 5 ) ; tree . root1 . right . left = new Node ( 6 ) ; tree . root1 . left . right . left = new Node ( 7 ) ; tree . root1 . left . right . right = new Node ( 8 ) ; tree . root2 = new Node ( 1 ) ; tree . root2 . left = new Node ( 3 ) ; tree . root2 . right = new Node ( 2 ) ; tree . root2 . right . left = new Node ( 4 ) ; tree . root2 . right . right = new Node ( 5 ) ; tree . root2 . left . right = new Node ( 6 ) ; tree . root2 . right . right . left = new Node ( 8 ) ; tree . root2 . right . right . right = new Node ( 7 ) ; if ( tree . isIsomorphic ( tree . root1 , tree . root2 ) == true ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }
Find smallest number with given number of digits and sum of digits under given constraints | C # implementation of the approach ; Function to find the number having sum of digits as s and d number of digits such that the difference between the maximum and the minimum digit the minimum possible ; To store the readonly number ; To store the value that is evenly distributed among all the digits ; To store the remaining sum that still remains to be distributed among d digits ; rem stores the value that still remains to be distributed To keep the difference of digits minimum last rem digits are incremented by 1 ; In the last rem digits one is added to the value obtained by equal distribution ; Driver function
using System ; class GFG { static String findNumber ( int s , int d ) { String num = " " ; int val = s / d ; int rem = s % d ; int i ; for ( i = 1 ; i <= d - rem ; i ++ ) { num = num + String . Join ( " " , val ) ; } if ( rem > 0 ) { val ++ ; for ( i = d - rem + 1 ; i <= d ; i ++ ) { num = num + String . Join ( " " , val ) ; } } return num ; } public static void Main ( String [ ] args ) { int s = 25 , d = 4 ; Console . Write ( findNumber ( s , d ) ) ; } }
Find Kth smallest value for b such that a + b = a | b | C # program to find k 'th smallest value for b such that a + b = a | b ; Function to find the kth smallest value for b ; res wiint store final answer ; skip when j ' th ▁ position ▁ ▁ has ▁ 1 ▁ in ▁ binary ▁ representation ▁ ▁ as ▁ in ▁ res , ▁ j ' th position wiint be 0. ; j 'th bit is set ; i 'th bit is set ; proceed to next bit ; Driver Code
using System ; class GFG { static int kthSmallest ( int a , int k ) { int res = 0 ; int j = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) { while ( j < 32 && ( a & ( 1 << j ) ) > 0 ) j ++ ; if ( ( k & ( 1 << i ) ) > 0 ) res |= ( 1 << j ) ; j ++ ; } return res ; } public static void Main ( ) { int a = 10 , k = 3 ; Console . WriteLine ( kthSmallest ( a , k ) ) ; } }
Find the sum of power of bit count raised to the power B | C # program for the above approach ; Function to calculate a ^ b mod m using fast - exponentiation method ; Function to get no of set bits in binary representation of positive integer n ; Function to calculate sum ; Itereate for all values of array A ; Calling fast - exponentiation and appending ans to sum ; Driver code .
using System ; class GFG { static int fastmod ( int baseval , int exp , int mod ) { if ( exp == 0 ) return 1 ; else if ( exp % 2 == 0 ) { int ans = fastmod ( baseval , exp / 2 , mod ) ; return ( ans % mod * ans % mod ) % mod ; } else return ( fastmod ( baseval , exp - 1 , mod ) % mod * baseval % mod ) % mod ; } static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } static int findPowerSum ( int n , int [ ] ar ) { int mod = ( int ) 1e9 + 7 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int baseval = countSetBits ( ar [ i ] ) ; int exp = ar [ i ] ; sum += fastmod ( baseval , exp , mod ) ; sum %= mod ; } return sum ; } public static void Main ( ) { int n = 3 ; int [ ] ar = { 1 , 2 , 3 } ; Console . WriteLine ( findPowerSum ( n , ar ) ) ; } }
Count the total number of triangles after Nth operation |  ; Method to return the total no . of Triangles ; For every subtriangle formed there are possibilities of generating ( curr * 3 ) + 2 ; Changing the curr value to Tri_count ; Driver code
using System ; class Gfg { public static int CountTriangles ( int n ) { int curr = 1 ; int Tri_count = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { Tri_count = ( curr * 3 ) + 2 ; curr = Tri_count ; } return Tri_count ; } public static void Main ( String [ ] args ) { int n = 10 ; Console . WriteLine ( CountTriangles ( n ) ) ; } }
In how many ways the ball will come back to the first boy after N turns | Function to return the number of sequences that get back to Bob ; Driver code
using System ; class GFG { static int numSeq ( int n ) { return ( int ) ( ( Math . Pow ( 3 , n ) + 3 * Math . Pow ( - 1 , n ) ) / 4 ) ; } public static void Main ( ) { int N = 10 ; Console . WriteLine ( numSeq ( N ) ) ; } }
Count subarrays such that remainder after dividing sum of elements by K gives count of elements | C # implementation of the approach ; Function to return the number of subarrays of the given array such that the remainder when dividing the sum of its elements by K is equal to the number of its elements ; To store prefix sum ; We are dealing with zero indexed array ; Taking modulus value ; Prefix sum ; To store the required answer , the left index and the right index ; To store si - i value ; Include sum ; Increment the right index ; If subarray has at least k elements ; Return the required answer ; Driver code ; Function call
using System ; using System . Collections . Generic ; class gfg { static int sub_arrays ( int [ ] a , int n , int k ) { int [ ] sum = new int [ n + 2 ] ; for ( int i = 0 ; i < n + 2 ; i ++ ) { sum [ i ] = 0 ; } for ( int i = 0 ; i < n ; i ++ ) { a [ i ] -- ; a [ i ] %= k ; sum [ i + 1 ] += sum [ i ] + a [ i ] ; sum [ i + 1 ] %= k ; } int ans = 0 , l = 0 , r = 0 ; Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n + 1 ; i ++ ) { if ( ! mp . ContainsKey ( sum [ i ] ) ) mp . Add ( sum [ i ] , 0 ) ; } int temp ; for ( int i = 0 ; i < n + 1 ; i ++ ) { ans += ( int ) mp [ sum [ i ] ] ; temp = ( int ) mp [ sum [ i ] ] + 1 ; mp [ sum [ i ] ] = temp ; r ++ ; if ( r - l >= k ) { temp = ( int ) mp [ sum [ l ] ] - 1 ; mp [ sum [ i ] ] = temp ; l ++ ; } } return ans ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 4 , 2 , 3 , 5 } ; int n = a . Length ; int k = 4 ; Console . Write ( sub_arrays ( a , n , k ) ) ; } }
Sum of all the prime numbers with the maximum position of set bit â ‰¤ D | C # implementation of the approach ; Function for Sieve of Eratosthenes ; Function to return the sum of the required prime numbers ; Maximum number of the required range ; Sieve of Eratosthenes ; To store the required sum ; If current element is prime ; Driver code
using System ; using System . Linq ; class GFG { static void sieve ( Boolean [ ] prime , int n ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) prime [ i ] = false ; } } } static int sumPrime ( int d ) { int maxVal = ( int ) ( Math . Pow ( 2 , d ) - 1 ) ; Boolean [ ] prime = new Boolean [ maxVal + 1 ] ; for ( int i = 0 ; i <= maxVal ; i ++ ) prime . SetValue ( true , i ) ; sieve ( prime , maxVal ) ; int sum = 0 ; for ( int i = 2 ; i <= maxVal ; i ++ ) { if ( prime [ i ] ) { sum += i ; } } return sum ; } public static void Main ( String [ ] args ) { int d = 8 ; Console . WriteLine ( sumPrime ( d ) ) ; } }
Check whether the exchange is possible or not | C # implementation of the approach ; Recursive function to return gcd of a and b ; Function that returns true if the exchange is possible ; Find the GCD of the array elements ; If the exchange is possible ; Driver code
using System ; class GFG { static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static bool isPossible ( int [ ] arr , int n , int p ) { int gcd = 0 ; for ( int i = 0 ; i < n ; i ++ ) gcd = __gcd ( gcd , arr [ i ] ) ; if ( p % gcd == 0 ) return true ; return false ; } public static void Main ( String [ ] args ) { int [ ] arr = { 6 , 9 } ; int n = arr . Length ; int p = 3 ; if ( isPossible ( arr , n , p ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Choose two elements from the given array such that their sum is not present in any of the arrays | C # implementation of the approach ; find maximum element in an array ; Function to find the numbers from the given arrays such that their sum is not present in any of the given array ; Find the maximum element from both the arrays ; Driver code
using System ; class GFG { static int max_element ( int [ ] a , int n ) { int m = int . MinValue ; for ( int i = 0 ; i < n ; i ++ ) m = Math . Max ( m , a [ i ] ) ; return m ; } static void findNum ( int [ ] a , int n , int [ ] b , int m ) { int x = max_element ( a , n ) ; int y = max_element ( b , m ) ; Console . Write ( x + " ▁ " + y ) ; } public static void Main ( ) { int [ ] a = { 3 , 2 , 2 } ; int n = a . Length ; int [ ] b = { 1 , 5 , 7 , 7 , 9 } ; int m = b . Length ; findNum ( a , n , b , m ) ; } }
Generate an array B [ ] from the given array A [ ] which satisfies the given conditions | C # implementation of the approach Utility function to print the array elements ; Utility function to print the array elements ; Function to generate and print the required array ; To switch the ceil and floor function alternatively ; For every element of the array ; If the number is odd then print the ceil or floor value after division by 2 ; Use the ceil and floor alternatively ; If arr [ i ] is even then it will be completely divisible by 2 ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void printArr ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } static void generateArr ( int [ ] arr , int n ) { bool flip = true ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] & 1 ) != 0 ) { if ( flip ^= true ) Console . Write ( ( int ) ( Math . Ceiling ( arr [ i ] / 2.0 ) ) + " ▁ " ) ; else Console . Write ( ( int ) ( Math . Floor ( arr [ i ] / 2.0 ) ) + " ▁ " ) ; } else { Console . Write ( arr [ i ] / 2 + " ▁ " ) ; } } } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , - 5 , - 7 , 9 , 2 , - 2 } ; int n = arr . Length ; generateArr ( arr , n ) ; } }
Number of K length subsequences with minimum sum | C # implementation of the approach ; Function to return the value of Binomial Coefficient C ( n , k ) ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Function to return the count of valid subsequences ; Sort the array ; Maximum among the minimum K elements ; Y will store the frequency of num in the minimum K elements ; cntX will store the frequency of num in the complete array ; Driver code
using System ; class GFG { static int binomialCoeff ( int n , int k ) { int [ , ] C = new int [ n + 1 , k + 1 ] ; int i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= Math . Min ( i , k ) ; j ++ ) { if ( j == 0 j == i ) C [ i , j ] = 1 ; else C [ i , j ] = C [ i - 1 , j - 1 ] + C [ i - 1 , j ] ; } } return C [ n , k ] ; } static int cntSubSeq ( int [ ] arr , int n , int k ) { Array . Sort ( arr ) ; int num = arr [ k - 1 ] ; int Y = 0 ; for ( int i = k - 1 ; i >= 0 ; i -- ) { if ( arr [ i ] == num ) Y ++ ; } int cntX = Y ; for ( int i = k ; i < n ; i ++ ) { if ( arr [ i ] == num ) cntX ++ ; } return binomialCoeff ( cntX , Y ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int n = arr . Length ; int k = 2 ; Console . WriteLine ( cntSubSeq ( arr , n , k ) ) ; } }
Find all even length binary sequences with same sum of first and second half bits | Iterative | C # code implementation for above approach ; Function to convert the number into binary and store the number into an array ; Function to check if the sum of the digits till the mid of the array and the sum of the digits from mid till n is the same , if they are same then print that binary ; Calculating the sum from 0 till mid and store in sum1 ; Calculating the sum from mid till n and store in sum2 ; If sum1 is same as sum2 print the binary ; Function to print sequence ; Creating the array ; Looping over powers of 2 ; Converting the number into binary first ; Checking if the sum of the first half of the array is same as the sum of the next half ; Driver Code
using System ; class GFG { static void convertToBinary ( int num , int [ ] a , int n ) { int pointer = n - 1 ; while ( num > 0 ) { a [ pointer ] = num % 2 ; num = num / 2 ; pointer -- ; } } static void checkforsum ( int [ ] a , int n ) { int sum1 = 0 ; int sum2 = 0 ; int mid = n / 2 ; for ( int i = 0 ; i < mid ; i ++ ) sum1 = sum1 + a [ i ] ; for ( int j = mid ; j < n ; j ++ ) sum2 = sum2 + a [ j ] ; if ( sum1 == sum2 ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( a [ i ] ) ; Console . WriteLine ( ) ; } } static void print_seq ( int m ) { int n = ( 2 * m ) ; int [ ] a = new int [ n ] ; for ( int j = 0 ; j < n ; j ++ ) { a [ j ] = 0 ; } for ( int i = 0 ; i < ( int ) Math . Pow ( 2 , n ) ; i ++ ) { convertToBinary ( i , a , n ) ; checkforsum ( a , n ) ; } } public static void Main ( String [ ] args ) { int m = 2 ; print_seq ( m ) ; } }