codeparrot/xlcost-text-to-code huggingface dataset:
Minimum Cost Path to visit all nodes situated at the Circumference of Circular Road | C # implementation to find the Minimum Cost Path to visit all nodes situated at the Circumference of Circular Road ; Function to find the minimum cost ; Sort the given array ; Initialise a new array of double size ; Fill the array elements ; Find the minimum path cost ; Return the readonly result ; Driver code
using System ; class GFG { static int minCost ( int [ ] arr , int n , int circumference ) { Array . Sort ( arr ) ; int [ ] arr2 = new int [ 2 * n ] ; for ( int i = 0 ; i < n ; i ++ ) { arr2 [ i ] = arr [ i ] ; arr2 [ i + n ] = arr [ i ] + circumference ; } int res = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) res = Math . Min ( res , arr2 [ i + ( n - 1 ) ] - arr2 [ i ] ) ; return res ; } public static void Main ( String [ ] args ) { int [ ] arr = { 19 , 3 , 6 } ; int n = arr . Length ; int circumference = 20 ; Console . WriteLine ( minCost ( arr , n , circumference ) ) ; } }
Unique element in an array where all elements occur K times except one | Set 2 | C # program for the above approach ; Function to find single occurrence element ; By shifting 1 to left ith time and taking and with 1 will give us that ith bit of a [ j ] is 1 or 0 ; Taking modulo of p with k ; Generate result ; Loop for negative numbers ; Check if the calculated value res is present in array , then mark c = 1 and if c = 1 return res else res must be - ve ; Driver code ; Function call
using System ; class GFG { public static int findunique ( int [ ] a , int k ) { int res = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) { int p = 0 ; for ( int j = 0 ; j < a . Length ; j ++ ) { p += ( Math . Abs ( a [ j ] ) & ( 1 << i ) ) != 0 ? 1 : 0 ; } p %= k ; res += ( int ) Math . Pow ( 2 , i ) * p ; } int c = 0 ; for ( int x = 0 ; x < a . Length ; x ++ ) if ( a [ x ] == res ) { c = 1 ; break ; } return c == 1 ? res : - res ; } public static void Main ( string [ ] args ) { int [ ] a = { 12 , 12 , 2 , 2 , 3 } ; int k = 2 ; Console . Write ( findunique ( a , k ) ) ; } }
Find minimum GCD of all pairs in an array | C # program to find the minimum GCD of any pair in the array ; Function returns the minimum GCD of any pair ; Finding GCD of all the elements in the array . ; Driver code
using System ; class GFG { static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; else return __gcd ( b , a % b ) ; } static int MinimumGCD ( int [ ] arr , int n ) { int g = 0 ; for ( int i = 0 ; i < n ; i ++ ) { g = __gcd ( g , arr [ i ] ) ; } return g ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 6 , 8 , 3 } ; int N = arr . Length ; Console . WriteLine ( MinimumGCD ( arr , N ) ) ; } }
Minimum steps to reach the Nth stair in jumps of perfect power of 2 | C # program for the above approach ; Function to count the number of jumps required to reach Nth stairs . ; Till N becomes 0 ; Removes the set bits from the right to left ; Driver Code ; Number of stairs ; Function Call
using System ; class GFG { static int stepRequired ( int N ) { int cnt = 0 ; while ( N > 0 ) { N = N & ( N - 1 ) ; cnt ++ ; } return cnt ; } public static void Main ( String [ ] args ) { int N = 23 ; Console . Write ( stepRequired ( N ) ) ; } }
Count of total subarrays whose sum is a Fibonacci Numbers | C # program for the above approach ; Function to check whether a number is perfect square or not ; Function to check whether a number is fibonacci number or not ; If 5 * n * n + 4 or 5 * n * n - 5 is a perfect square , then the number is Fibonacci ; Function to count the subarray with sum fibonacci number ; Traverse the array [ ] arr to find the sum of each subarray ; To store the sum ; Check whether sum of subarray between [ i , j ] is fibonacci or not ; Driver Code ; Function Call
using System ; class GFG { static bool isPerfectSquare ( int x ) { int s = ( int ) Math . Sqrt ( x ) ; return ( s * s == x ) ; } static bool isFibonacci ( int n ) { return isPerfectSquare ( 5 * n * n + 4 ) || isPerfectSquare ( 5 * n * n - 4 ) ; } static void fibonacciSubarrays ( int [ ] arr , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; ++ i ) { int sum = 0 ; for ( int j = i ; j < n ; ++ j ) { sum += arr [ j ] ; if ( isFibonacci ( sum ) ) { ++ count ; } } } Console . Write ( count ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 6 , 7 , 8 , 9 } ; int n = arr . Length ; fibonacciSubarrays ( arr , n ) ; } }
Count the number of ways to fill K boxes with N distinct items | C # program to calculate the above formula ; To store the factorials of all numbers ; Function to calculate factorial of all numbers ; Calculate x to the power y in O ( log n ) time ; Function to find inverse mod of a number x ; Calculate ( n C r ) ; Loop to compute the formula evaluated ; Add even power terms ; Subtract odd power terms ; Choose the k boxes which were used ; Driver Code
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static long mod = 1000000007 ; static long [ ] factorial = new long [ 100005 ] ; static void StoreFactorials ( int n ) { factorial [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { factorial [ i ] = ( i * factorial [ i - 1 ] ) % mod ; } } static long Power ( long x , long y ) { long ans = 1 ; while ( y > 0 ) { if ( y % 2 == 1 ) { ans = ( ans * x ) % mod ; } x = ( x * x ) % mod ; y /= 2 ; } return ans ; } static long invmod ( long x ) { return Power ( x , mod - 2 ) ; } static long nCr ( int n , int r ) { return ( factorial [ n ] * invmod ( ( factorial [ r ] * factorial [ n - r ] ) % mod ) ) % mod ; } static long CountWays ( int n , int k ) { StoreFactorials ( n ) ; long ans = 0 ; for ( int i = k ; i >= 0 ; i -- ) { if ( i % 2 == k % 2 ) { ans = ( ans + ( Power ( i , n ) * nCr ( k , i ) ) % mod ) % mod ; } else { ans = ( ans + mod - ( Power ( i , n ) * nCr ( k , i ) ) % mod ) % mod ; } } ans = ( ans * nCr ( n , k ) ) % mod ; return ans ; } public static void Main ( string [ ] args ) { int N = 5 ; int K = 5 ; Console . Write ( CountWays ( N , K ) + " STRNEWLINE " ) ; } }
Program to print numbers from N to 1 in reverse order | C # program to print all numbers between 1 to N in reverse order ; Recursive function to print from N to 1 ; If N is less than 1 then return static void function ; Recursive call of the function ; Driver code
using System ; class GFG { static void PrintReverseOrder ( int N ) { if ( N <= 0 ) { return ; } else { Console . Write ( N + " ▁ " ) ; PrintReverseOrder ( N - 1 ) ; } } public static void Main ( ) { int N = 5 ; PrintReverseOrder ( N ) ; } }
Find the XOR of first half and second half elements of an array | C # program to find the xor of the first half elements and second half elements of an array ; Function to find the xor of the first half elements and second half elements of an array ; xor of elements in FirstHalfXOR ; xor of elements in SecondHalfXOR ; Driver Code ; Function call
using System ; class GFG { static void XOROfElements ( int [ ] arr , int n ) { int FirstHalfXOR = 0 ; int SecondHalfXOR = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i < n / 2 ) FirstHalfXOR ^= arr [ i ] ; else SecondHalfXOR ^= arr [ i ] ; } Console . Write ( FirstHalfXOR + " , " + SecondHalfXOR + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 20 , 30 , 50 , 10 , 55 , 15 , 42 } ; int N = arr . Length ; XOROfElements ( arr , N ) ; } }
Minimum volume of cone that can be circumscribed about a sphere of radius R | C # program to find the minimum volume of the cone that can be circumscribed about a sphere of radius R ; Function to find the volume of the cone ; r = radius of cone h = height of cone Volume of cone = ( 1 / 3 ) * ( 3.14 ) * ( r * r ) * ( h ) we get radius of cone from the derivation is root ( 2 ) times multiple of R we get height of cone from the derivation is 4 times multiple of R ; Driver code
using System ; class GFG { static double Volume_of_cone ( double R ) { double V = ( double ) ( ( 1 / 3.0 ) * ( 3.14 ) * ( 2 * ( R * R ) ) * ( 4 * R ) ) ; return V ; } public static void Main ( ) { double R = 10.0 ; Console . Write ( Volume_of_cone ( R ) ) ; } }
Program to find Surface Area and Volume of Octagonal Prism | C # program to find the surface area and volume of octagonal prism ; Function to find the volume of octagonal prism ; Formula to calculate volume = ( area * h ) ; Display volume ; Function to find the surface area of octagonal prism ; Formula to calculate surface area ; Driver Code
using System ; class GFG { static void find_volume ( double area , double h ) { double Volume = ( area * h ) ; Console . WriteLine ( " Volume : ▁ " + Volume ) ; } static void find_Surface_area ( double area , double a , double h ) { double Surface_area = ( 2 * area ) + ( 8 * a * h ) ; Console . WriteLine ( " Surface ▁ area : ▁ " + Surface_area ) ; } public static void Main ( string [ ] args ) { double h = 1 ; double a = 6 ; double d = 2 ; double area = 2 * a * d ; find_Surface_area ( area , a , h ) ; find_volume ( area , h ) ; } }
Count of pairs upto N such whose LCM is not equal to their product for Q queries | C # program to find the count of pairs from 1 to N such that their LCM is not equal to their product ; To store Euler 's Totient Function ; To store prefix sum table ; Compute Totients of all numbers smaller than or equal to N ; Make phi [ 1 ] = 0 since 1 cannot form any pair ; Initialise all remaining phi [ ] with i ; Compute remaining phi ; If phi [ p ] is not computed already , then number p is prime ; phi of prime number is p - 1 ; Update phi of all multiples of p ; Add the contribution of p to its multiple i by multiplying it with ( 1 - 1 / p ) ; Function to store prefix sum table ; Prefix Sum of all Euler 's Totient Values ; Total number of pairs that can be formed ; Driver Code ; Function call to compute all phi ; Function call to store all prefix sum
using System ; class GFG { static readonly int N = 100005 ; static int [ ] phi = new int [ N ] ; static int [ ] pref = new int [ N ] ; static void precompute ( ) { phi [ 1 ] = 0 ; for ( int i = 2 ; i < N ; i ++ ) phi [ i ] = i ; for ( int p = 2 ; p < N ; p ++ ) { if ( phi [ p ] == p ) { phi [ p ] = p - 1 ; for ( int i = 2 * p ; i < N ; i += p ) { phi [ i ] = ( phi [ i ] / p ) * ( p - 1 ) ; } } } } static void prefix ( ) { for ( int i = 1 ; i < N ; i ++ ) pref [ i ] = pref [ i - 1 ] + phi [ i ] ; } static void find_pairs ( int n ) { int total = ( n * ( n - 1 ) ) / 2 ; int ans = total - pref [ n ] ; Console . Write ( " Number ▁ of ▁ pairs ▁ from ▁ 1 ▁ to ▁ " + n + " ▁ are ▁ " + ans + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { precompute ( ) ; prefix ( ) ; int [ ] q = { 5 , 7 } ; int n = q . Length ; for ( int i = 0 ; i < n ; i ++ ) { find_pairs ( q [ i ] ) ; } } }
Program to balance the given Chemical Equation | C # program to balance the given Chemical Equation ; Function to calculate GCD ; Function to calculate b1 , b2 and b3 ; Variable declaration ; temp variable to store gcd ; Computing GCD ; Driver code
using System ; public class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static void balance ( int x , int y , int p , int q ) { int b1 , b2 , b3 ; if ( p % x == 0 && q % y == 0 ) { b1 = p / x ; b2 = q / y ; b3 = 1 ; } else { p = p * y ; q = q * x ; b3 = x * y ; int temp = gcd ( p , gcd ( q , b3 ) ) ; b1 = p / temp ; b2 = q / temp ; b3 = b3 / temp ; } Console . Write ( b1 + " ▁ " + b2 + " ▁ " + b3 + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int x = 2 , y = 3 , p = 4 , q = 5 ; balance ( x , y , p , q ) ; } }
Find the Maximum Alternate Subsequence Sum from a given array | C # program to find the maximum alternating subsequence sum for a given array ; Function to find maximum alternating subsequence sum ; Initialize sum to 0 ; Calculate the sum ; Return the final result ; Driver Code ; Array initialization ; Length of array
using System ; class GFG { static int maxAlternatingSum ( int [ ] arr , int n ) { int max_sum = 0 ; int i = 0 ; while ( i < n ) { int current_max = arr [ i ] ; int k = i ; while ( k < n && ( ( arr [ i ] > 0 && arr [ k ] > 0 ) || ( arr [ i ] < 0 && arr [ k ] < 0 ) ) ) { current_max = Math . Max ( current_max , arr [ k ] ) ; k += 1 ; } max_sum += current_max ; i = k ; } return max_sum ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , - 1 , - 2 } ; int n = arr . Length ; Console . Write ( maxAlternatingSum ( arr , n ) ) ; } }
Minimum length subarray containing all unique elements after Q operations | C # implementation to find the minimum size subarray containing all unique elements after processing the array for K queries of ranges ; Function to find minimum size subarray of all array elements ; Updating the array after processing each query ; Making it to 0 - indexing ; Prefix sum array concept is used to obtain the count array ; Iterating over the array to get the readonly array ; Variable to get count of all unique elements ; Hash to maintain perviously occurred elements ; Loop to find the all unique elements ; Array to maintain counter of encountered elements ; Variable to store answer ; Using two pointers approach ; Increment counter if occurred once ; When all unique elements are found ; Update answer with minimum size ; Decrement count of elements from left ; Decrement counter ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int subarrayLength ( int [ ] A , int [ , ] R , int N , int M ) { for ( int i = 0 ; i < M ; ++ i ) { int l = R [ i , 0 ] , r = R [ i , 1 ] + 1 ; l -- ; r -- ; A [ l ] ++ ; if ( r < N ) A [ r ] -- ; } for ( int i = 1 ; i < N ; ++ i ) { A [ i ] += A [ i - 1 ] ; } int count = 0 ; HashSet < int > s = new HashSet < int > ( ) ; for ( int i = 0 ; i < N ; ++ i ) { if ( ! s . Contains ( A [ i ] ) ) count ++ ; s . Add ( A [ i ] ) ; } int [ ] repeat = new int [ count + 1 ] ; int ans = N ; int counter = 0 , left = 0 , right = 0 ; while ( right < N ) { int cur_element = A [ right ] ; repeat [ cur_element ] += 1 ; if ( repeat [ cur_element ] == 1 ) ++ counter ; while ( counter == count ) { ans = Math . Min ( ans , right - left + 1 ) ; cur_element = A [ left ] ; repeat [ cur_element ] -= 1 ; ++ left ; if ( repeat [ cur_element ] == 0 ) -- counter ; } ++ right ; } return ans ; } public static void Main ( String [ ] args ) { int N = 8 , queries = 6 ; int [ , ] Q = { { 1 , 4 } , { 3 , 4 } , { 4 , 5 } , { 5 , 5 } , { 7 , 8 } , { 8 , 8 } } ; int [ ] A = new int [ N ] ; Console . Write ( subarrayLength ( A , Q , N , queries ) ) ; } }
Elements of Array which can be expressed as power of prime numbers | C # program to print all elements of Array which can be expressed as power of prime numbers ; Function to mark all the exponent of prime numbers ; If number is prime then marking all of its exponent true ; Function to display all required elements ; Function to print the required numbers ; To find the largest number ; Function call to mark all the Exponential prime nos . ; Function call ; Driver code
using System ; class GFG { static void ModifiedSieveOfEratosthenes ( int N , bool [ ] Expo_Prime ) { bool [ ] primes = new bool [ N ] ; for ( int i = 0 ; i < N ; i ++ ) primes [ i ] = true ; for ( int i = 2 ; i < N ; i ++ ) { if ( primes [ i ] ) { int no = i ; while ( no <= N ) { Expo_Prime [ no ] = true ; no *= i ; } for ( int j = i * i ; j < N ; j += i ) primes [ j ] = false ; } } } static void Display ( int [ ] arr , bool [ ] Expo_Prime , int n ) { for ( int i = 0 ; i < n ; i ++ ) if ( Expo_Prime [ arr [ i ] ] ) Console . Write ( arr [ i ] + " ▁ " ) ; } static void FindExpoPrime ( int [ ] arr , int n ) { int max = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( max < arr [ i ] ) max = arr [ i ] ; } bool [ ] Expo_Prime = new bool [ max + 1 ] ; ModifiedSieveOfEratosthenes ( max + 1 , Expo_Prime ) ; Display ( arr , Expo_Prime , n ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 4 , 6 , 9 , 16 , 1 , 3 , 12 , 36 , 625 , 1000 } ; int n = arr . Length ; FindExpoPrime ( arr , n ) ; } }
Elements of Array which can be expressed as power of some integer to given exponent K | C # implementation to print elements of the Array which can be expressed as power of some integer to given exponent K ; Method returns Nth power of A ; Smaller eps , denotes more accuracy ; Initializing difference between two roots by int . MaxValue ; x ^ K denotes current value of x ; loop untill we reach desired accuracy ; calculating current value from previous value by newton 's method ; Function to check whether its k root is an integer or not ; Function to find the numbers ; Driver code
using System ; class GFG { static double nthRoot ( long A , long N ) { double xPre = 7 ; double eps = 1e-3 ; double delX = int . MaxValue ; double xK = 0 ; while ( delX > eps ) { xK = ( ( N - 1.0 ) * xPre + ( double ) A / Math . Pow ( xPre , N - 1 ) ) / ( double ) N ; delX = Math . Abs ( xK - xPre ) ; xPre = xK ; } return xK ; } static bool check ( long no , int k ) { double kth_root = nthRoot ( no , k ) ; long num = ( long ) kth_root ; if ( Math . Abs ( num - kth_root ) < 1e-4 ) return true ; return false ; } static void printExpo ( long [ ] arr , int n , int k ) { for ( int i = 0 ; i < n ; i ++ ) { if ( check ( arr [ i ] , k ) ) Console . Write ( arr [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int K = 6 ; long [ ] arr = { 46656 , 64 , 256 , 729 , 16 , 1000 } ; int n = arr . Length ; printExpo ( arr , n , K ) ; } }
Count of subsequences whose product is a difference of square of two integers | C # implementation to count the number of contiguous subsequences whose product can be expressed as the square of difference of two integers ; Function to count the number of contiguous subsequences whose product can be expressed as square of difference of two integers ; Iterating through the array ; Check if that number can be expressed as the square of difference of two numbers ; Variable to compute the product ; Finding the remaining subsequences ; Check if that number can be expressed as the square of difference of two numbers ; Return the number of subsequences ; Driver code
using System ; class GFG { static int CntcontSubs ( int [ ] a , int n ) { int c = 0 , d = 0 , i , sum = 1 , j ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 != 0 a [ i ] % 4 == 0 ) d ++ ; sum = a [ i ] ; for ( j = i + 1 ; j < n ; j ++ ) { sum = sum * a [ j ] ; if ( sum % 2 != 0 sum % 4 == 0 ) c ++ ; } sum = 1 ; } return c + d ; } static void Main ( ) { int [ ] arr = { 5 , 4 , 2 , 9 , 8 } ; int n = arr . Length ; Console . Write ( CntcontSubs ( arr , n ) ) ; } }
Count of subsequences whose product is a difference of square of two integers | C # implementation to count all the contiguous subsequences whose product is expressed as the square of the difference of two integers ; Function to count all the contiguous subsequences whose product is expressed as the square of the difference of two integers ; Creating vectors to store the remainders and the subsequences ; Iterating through the array ; Finding the remainder when the element is divided by 4 ; Bringing all the elements in the range [ 0 , 3 ] ; If the remainder is 2 , store the index of the ; If the remainder is 2 , store the index of the ; Finding the total number of subsequences ; If there are no numbers which yield the remainder 2 ; Iterating through the vector ; If the element is 2 , find the nearest 2 or 0 and find the number of elements between them ; Returning the count ; Driver code
using System ; using System . Collections . Generic ; class GFG { class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int CntcontSubs ( int [ ] a , int n ) { List < pair > vect = new List < pair > ( ) ; vect . Add ( new pair ( 0 , 2 ) ) ; List < int > two = new List < int > ( ) ; List < int > zero = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = a [ i ] % 4 ; if ( a [ i ] < 0 ) a [ i ] = a [ i ] + 4 ; if ( a [ i ] == 2 ) two . Add ( i + 1 ) ; if ( a [ i ] == 0 ) zero . Add ( i + 1 ) ; if ( a [ i ] == 0 a [ i ] == 2 ) vect . Add ( new pair ( i + 1 , a [ i ] ) ) ; } vect . Add ( new pair ( n + 1 , 2 ) ) ; int total = ( n * ( n + 1 ) ) / 2 ; if ( two . Count == 0 ) return total ; else { int sum = 0 ; int sz = vect . Count ; for ( int i = 1 ; i + 1 < sz ; i ++ ) { if ( vect [ i ] . second == 2 ) { sum += ( vect [ i ] . first - vect [ i - 1 ] . first ) * ( vect [ i + 1 ] . first - vect [ i ] . first ) - 1 ; } } return total - sum - two . Count ; } } public static void Main ( String [ ] args ) { int [ ] a = { 5 , 4 , 2 , 9 , 8 } ; int n = a . Length ; Console . Write ( CntcontSubs ( a , n ) ) ; } }
Count of possible subarrays and subsequences using given length of Array | C # implementation to count the subarray and subsequence of given length of the array ; Function to count the subarray for the given array ; Function to count the subsequence for the given array length ; Driver Code
using System ; class GFG { static int countSubarray ( int n ) { return ( ( n ) * ( n + 1 ) ) / 2 ; } static int countSubsequence ( int n ) { return ( int ) Math . Pow ( 2 , n ) ; } public static void Main ( String [ ] args ) { int n = 5 ; Console . Write ( ( countSubarray ( n ) ) + " STRNEWLINE " ) ; Console . Write ( ( countSubsequence ( n ) ) + " STRNEWLINE " ) ; } }
Program for finding the Integral of a given function using Boole 's Rule | C # program to implement Boole 's Rule on the given function ; Function to return the value of f ( x ) for the given value of x ; Function to computes the integrand of y at the given intervals of x with step size h and the initial limit a and final limit b ; Number of intervals ; Computing the step size ; Substituing a = 0 , b = 4 and h = 1 ; Driver code
using System ; class GFG { static float y ( float x ) { return ( 1 / ( 1 + x ) ) ; } static float BooleRule ( float a , float b ) { int n = 4 ; int h ; h = ( int ) ( ( b - a ) / n ) ; float sum = 0 ; float bl = ( 7 * y ( a ) + 32 * y ( a + h ) + 12 * y ( a + 2 * h ) + 32 * y ( a + 3 * h ) + 7 * y ( a + 4 * h ) ) * 2 * h / 45 ; sum = sum + bl ; return sum ; } public static void Main ( string [ ] args ) { Console . Write ( ( " f ( x ) ▁ = ▁ " + System . Math . Round ( BooleRule ( 0 , 4 ) , 4 ) ) ) ; } }
Number of subsets with same AND , OR and XOR values in an Array | C # program to find the number of subsets with equal bitwise AND , OR and XOR values ; Function to find the number of subsets with equal bitwise AND , OR and XOR values ; Precompute the modded powers of two for subset counting ; Loop to iterate and find the modded powers of two for subset counting ; Map to store the frequency of each element ; Loop to compute the frequency ; For every element > 0 , the number of subsets formed using this element only is equal to 2 ^ ( frequency [ element ] - 1 ) . And for 0 , we have to find all the subsets , so 2 ^ ( frequency [ element ] ) - 1 ; If element is greater than 0 ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int mod = 1000000007 ; static int countSubsets ( int [ ] a , int n ) { int answer = 0 ; int [ ] powerOfTwo = new int [ 100005 ] ; powerOfTwo [ 0 ] = 1 ; for ( int i = 1 ; i < 100005 ; i ++ ) powerOfTwo [ i ] = ( powerOfTwo [ i - 1 ] * 2 ) % mod ; Dictionary < int , int > frequency = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( frequency . ContainsKey ( a [ i ] ) ) { frequency [ a [ i ] ] = frequency [ a [ i ] ] + 1 ; } else { frequency . Add ( a [ i ] , 1 ) ; } foreach ( KeyValuePair < int , int > el in frequency ) { if ( el . Key != 0 ) answer = ( answer % mod + powerOfTwo [ el . Value - 1 ] ) % mod ; else answer = ( answer % mod + powerOfTwo [ el . Value ] - 1 + mod ) % mod ; } return answer ; } public static void Main ( String [ ] args ) { int N = 6 ; int [ ] A = { 1 , 3 , 2 , 1 , 2 , 1 } ; Console . Write ( countSubsets ( A , N ) ) ; } }