codeparrot/xlcost-text-to-code huggingface dataset:
Maximum number of Armstrong Numbers present in a subarray of size K | C # program for above approach ; Function to calculate the value of x raised to the power y in O ( log y ) ; Base Case ; If the power y is even ; Otherwise ; Function to calculate the order of the number , i . e . count of digits ; Stores the total count of digits ; Iterate until num is 0 ; Function to check a number is an Armstrong Number or not ; Find the order of the number ; Check for Armstrong Number ; If Armstrong number condition is satisfied ; Utility function to find the maximum sum of a subarray of size K ; If k is greater than N ; Find the sum of first subarray of size K ; Find the sum of the remaining subarray ; Return the maximum sum of subarray of size K ; Function to find all the Armstrong Numbers in the array ; Traverse the array arr [ ] ; If arr [ i ] is an Armstrong Number , then replace it by 1. Otherwise , 0 ; Return the resultant count ; Driver Code
using System ; class GFG { static int power ( int x , int y ) { if ( y == 0 ) return 1 ; if ( y % 2 == 0 ) return power ( x , y / 2 ) * power ( x , y / 2 ) ; return x * power ( x , y / 2 ) * power ( x , y / 2 ) ; } static int order ( int num ) { int count = 0 ; while ( num > 0 ) { count ++ ; num = num / 10 ; } return count ; } static int isArmstrong ( int N ) { int r = order ( N ) ; int temp = N , sum = 0 ; while ( temp > 0 ) { int d = temp % 10 ; sum += power ( d , r ) ; temp = temp / 10 ; } if ( sum == N ) return 1 ; return 0 ; } static int maxSum ( int [ ] arr , int N , int K ) { if ( N < K ) { return - 1 ; } int res = 0 ; for ( int i = 0 ; i < K ; i ++ ) { res += arr [ i ] ; } int curr_sum = res ; for ( int i = K ; i < N ; i ++ ) { curr_sum += arr [ i ] - arr [ i - K ] ; res = Math . Max ( res , curr_sum ) ; } return res ; } static int maxArmstrong ( int [ ] arr , int N , int K ) { for ( int i = 0 ; i < N ; i ++ ) { arr [ i ] = isArmstrong ( arr [ i ] ) ; } return maxSum ( arr , N , K ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 28 , 2 , 3 , 6 , 153 , 99 , 828 , 24 } ; int K = 6 ; int N = arr . Length ; Console . Write ( maxArmstrong ( arr , N , K ) ) ; } }
Count N | C # program for the above approach ; Function to calculate the power of n ^ k % p ; Update x if it is more than or equal to p ; In case x is divisible by p ; ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Function to count the number of arrays satisfying required conditions ; Calculating N ^ K ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int power ( int x , int y , int p ) { int res = 1 ; x = x % p ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( ( y & 1 ) != 0 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } static int countArrays ( int n , int k ) { int mod = 1000000007 ; int ans = power ( n , k , mod ) ; return ans ; } public static void Main ( ) { int n = 3 , k = 5 ; int ans = countArrays ( n , k ) ; Console . Write ( ans ) ; } }
Count ways to replace ' ? ' in a Binary String to make the count of 0 s and 1 s same as that of another string | C # program for the above approach ; Function to find the factorial of the given number N ; Stores the factorial ; Iterate over the range [ 2 , N ] ; Return the resultant result ; Function to find the number of ways of choosing r objects from n distinct objects ; Function to find the number of ways to replace ' ? ' in string t to get the same count of 0 s and 1 s in the string S and T ; Traverse the string s ; If the current character is 1 ; Update the value of the sum1 ; Otherwise ; Traverse the string t ; If the current character is 1 , then update the value of sum2 ; If the current character is 0 ; Otherwise , update the value of K ; Check if P is greater than K or if K - P is odd ; Print the count of ways ; Driver code
using System ; class GFG { static int fact ( int n ) { int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { res = res * i ; } return res ; } static int nCr ( int n , int r ) { return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; } static void countWays ( string s , string t ) { int n = s . Length ; int sum1 = 0 , sum2 = 0 , K = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '1' ) { sum1 ++ ; } else sum1 -- ; } int m = t . Length ; for ( int i = 0 ; i < m ; i ++ ) { if ( t [ i ] == '1' ) { sum2 ++ ; } else if ( t [ i ] == '0' ) { sum2 -- ; } else K ++ ; } int P = Math . Abs ( sum1 - sum2 ) ; if ( ( P > K ) || ( ( K - P ) % 2 ) != 0 ) { Console . WriteLine ( 0 ) ; return ; } Console . WriteLine ( nCr ( K , ( P + K ) / 2 ) ) ; } static public void Main ( ) { string S1 = "1010" ; string S2 = "10 ? ? " ; countWays ( S1 , S2 ) ; } }
Count pairs of indices having sum of indices same as the sum of elements at those indices | C # program for the above approach ; Function to find all possible pairs of the given array such that the sum of arr [ i ] + arr [ j ] is i + j ; Stores the total count of pairs ; Iterate over the range ; Iterate over the range ; Print the total count ; Driver Code
using System ; class GFG { static void countPairs ( int [ ] arr , int N ) { int answer = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( arr [ i ] + arr [ j ] == i + j ) { answer ++ ; } } } Console . Write ( answer ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 0 , 1 , 2 , 3 , 4 , 5 } ; int N = arr . Length ; countPairs ( arr , N ) ; } }
Distribute R , B beans such that each packet has at least 1 R and 1 B bean with absolute difference at most D | C # program for the above approach ; Function to check if it is possible to distribute R red and B blue beans in packets such that the difference between the beans in each packet is atmost D ; Check for the condition to distributing beans ; Print the answer ; Distribution is not possible ; Driver code
using System ; class GFG { static void checkDistribution ( int R , int B , int D ) { if ( Math . Max ( R , B ) <= Math . Min ( R , B ) * ( D + 1 ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } static public void Main ( ) { int R = 1 , B = 1 , D = 0 ; checkDistribution ( R , B , D ) ; } }
Generate a sequence with product N such that for every pair of indices ( i , j ) and i < j , arr [ j ] is divisible by arr [ i ] | C # program for the above approach ; Function to calculate the prime factors of N with their count ; Initialize a vector , say v ; Initialize the count ; Count the number of divisors ; Divide the value of N by 2 ; For factor 2 divides it ; Find all prime factors ; Count their frequency ; Push it to the vector ; Push N if it is not 1 ; Function to print the array that have the maximum size ; Stores the all prime factor and their powers ; Traverse the vector and find the maximum power of prime factor ; If max size is less than 2 ; Otherwise ; Print the maximum size of sequence ; Print the readonly sequence ; Print the prime factor ; Print the last value of the sequence ; Driver Code
using System ; using System . Collections . Generic ; public class GFG { class pair { public int first ; public int second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static List < pair > primeFactor ( int N ) { List < pair > v = new List < pair > ( ) ; int count = 0 ; while ( ( N % 2 ) == 0 ) { N >>= 1 ; count ++ ; } if ( count != 0 ) v . Add ( new pair ( 2 , count ) ) ; for ( int i = 3 ; i <= Math . Sqrt ( N ) ; i += 2 ) { count = 0 ; while ( N % i == 0 ) { count ++ ; N = N / i ; } if ( count != 0 ) { v . Add ( new pair ( i , count ) ) ; } } if ( N > 2 ) v . Add ( new pair ( N , 1 ) ) ; return v ; } static void printAnswer ( int n ) { List < pair > v = primeFactor ( n ) ; int maxi_size = 0 , prime_factor = 0 ; for ( int i = 0 ; i < v . Count ; i ++ ) { if ( maxi_size < v [ i ] . second ) { maxi_size = v [ i ] . second ; prime_factor = v [ i ] . first ; } } if ( maxi_size < 2 ) { Console . Write ( 1 << ' ▁ ' ) ; } else { int product = 1 ; Console . Write ( maxi_size + " STRNEWLINE " ) ; for ( int i = 0 ; i < maxi_size - 1 ; i ++ ) { Console . Write ( prime_factor + " ▁ " ) ; product *= prime_factor ; } Console . Write ( ( n / product ) ) ; } } public static void Main ( String [ ] args ) { int N = 360 ; printAnswer ( N ) ; } }
Check if it is possible to reach destination in even number of steps in an Infinite Matrix | C # program for the above approach ; Function to check destination can be reached from source in even number of steps ; Coordinates differences ; Minimum number of steps required ; Minsteps is even ; Minsteps is odd ; Driver code ; Given Input ; Function Call
using System ; class GFG { static void IsEvenPath ( int [ ] Source , int [ ] Destination ) { int x_dif = Math . Abs ( Source [ 0 ] - Destination [ 0 ] ) ; int y_dif = Math . Abs ( Source [ 1 ] - Destination [ 1 ] ) ; int minsteps = x_dif + y_dif ; if ( minsteps % 2 == 0 ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } public static void Main ( string [ ] args ) { int [ ] Source = { 2 , 1 } ; int [ ] Destination = { 1 , 4 } ; IsEvenPath ( Source , Destination ) ; } }
Find the closest Fraction to given fraction having minimum absolute difference | C # code for the above approach ; Function to find the absolute value of x ; Function to find the fraction with minimum absolute difference ; Initialize the answer variables ; Iterate over the range ; Nearest fraction ; x / y - d / i < x / y - A / B ( B * x - y * A ) * ( i * y ) > ( i * x - y * d ) * ( B * y ) ; Check for d + 1 ; Print the answer ; Driver Code
using System ; public class GFG { static long ABS ( long x ) { return Math . Max ( x , - x ) ; } static void findFraction ( long x , long y , long n ) { long A = - 1 , B = - 1 ; for ( long i = 1 ; i <= n ; i ++ ) { long d = ( i * x ) / y ; if ( d >= 0 && ( A == - 1 || ABS ( B * x - y * A ) * ABS ( i * y ) > ABS ( i * x - y * d ) * ABS ( B * y ) ) ) A = d ; B = i ; d ++ ; if ( d >= 0 && ( A == - 1 || ABS ( B * x - y * A ) * ABS ( i * y ) > ABS ( i * x - y * d ) * ABS ( B * y ) ) ) A = d ; B = i ; } A -- ; B -- ; Console . Write ( A + " / " + B ) ; } static public void Main ( ) { long x = 3 ; long y = 7 ; long n = 6 ; findFraction ( x , y , n ) ; } }
Represent a number N in base | C # Program for above approach ; Function to convert N to equivalent representation in base - 2 ; Stores the required answer ; Iterate until N is not equal to zero ; If N is Even ; Add char '0' in front of string ; Add char '1' in front of string ; Decrement N by 1 ; Divide N by - 2 ; If string is empty , that means N is zero ; Put '0' in string s ; Driver Code ; Given Input ; Function Call
using System ; using System . Collections . Generic ; class GFG { static string BaseConversion ( int N ) { string s = " " ; while ( N != 0 ) { if ( N % 2 == 0 ) { s = "0" + s ; } else { s = "1" + s ; N -- ; } N /= - 2 ; } if ( s == " " ) { s = "0" ; } return s ; } public static void Main ( ) { int N = - 9 ; Console . Write ( BaseConversion ( N ) ) ; } }
Find instances at end of time frame after auto scaling | C # program for above approach ; Function to find the number of instances after compintion ; Traverse the array , arr [ ] ; If current element is less than 25 ; Divide instances by 2 ; If the current element is greater than 60 ; Double the instances ; Print the instances at the end of the traversal ; Driver Code ; Function Call
using System ; class GFG { static void finalInstances ( int instances , int [ ] arr ) { int i = 0 ; while ( i < arr . Length ) { if ( arr [ i ] < 25 && instances > 1 ) { instances = ( instances / 2 ) ; i = i + 10 ; } else if ( arr [ i ] > 60 && instances <= Math . Pow ( 10 , 8 ) ) { instances = instances * 2 ; i = i + 10 ; } i = i + 1 ; } Console . Write ( instances ) ; } static void Main ( ) { int instances = 2 ; int [ ] arr = { 25 , 23 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 76 , 80 } ; finalInstances ( instances , arr ) ; } }
Modify array by making all array elements equal to 0 by subtracting K ^ i from an array element in every i | C # program for the above approach ; Function to check whether all array elements can be made zero or not ; Stores if a power of K has already been subtracted or not ; Stores all the Kth power ; Iterate until X is less than INT_MAX ; Traverse the array arr [ ] ; Iterate over the range [ 0 , M ] ; If MP [ V [ j ] ] is 0 and V [ j ] is less than or equal to arr [ i ] ; If arr [ i ] is not 0 ; If i is less than N ; Otherwise , ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static string isMakeZero ( int [ ] arr , int N , int K ) { Dictionary < int , int > MP = new Dictionary < int , int > ( ) ; List < int > V = new List < int > ( ) ; int X = 1 ; int i ; while ( X > 0 && X < Int32 . MaxValue ) { V . Add ( X ) ; X *= K ; } for ( i = 0 ; i < N ; i ++ ) { for ( int j = V . Count - 1 ; j >= 0 ; j -- ) { if ( MP . ContainsKey ( V [ j ] ) == false && V [ j ] <= arr [ i ] ) { arr [ i ] -= V [ j ] ; MP [ V [ j ] ] = 1 ; } } if ( arr [ i ] != 0 ) break ; } if ( i < N ) return " No " ; else return " Yes " ; } public static void Main ( String [ ] args ) { int [ ] arr = { 8 , 0 , 3 , 4 , 80 } ; int N = arr . Length ; int K = 2 ; Console . WriteLine ( isMakeZero ( arr , N , K ) ) ; } }
Check whether the product of every subsequence is a perfect square or not | C # program for the above approach ; Function to check if the product of every subsequence of the array is a perfect square or not ; Traverse the given array ; If arr [ i ] is a perfect square or not ; Return " Yes " ; Driver Code
using System ; class GFG { static String perfectSquare ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int p = ( int ) Math . Sqrt ( arr [ i ] ) ; if ( p * p != arr [ i ] ) { return " No " ; } } return " Yes " ; } public static void Main ( ) { int [ ] arr = { 1 , 4 , 100 } ; int N = arr . Length ; Console . WriteLine ( perfectSquare ( arr , N ) ) ; } }
Count of subarrays with average K | C # implementation of above approach ; Function to count subarray having average exactly equal to K ; To Store the final answer ; Calculate all subarrays ; Calculate required average ; Check if average is equal to k ; Required average found ; Increment res ; Driver code ; Given Input ; Function Call
using System ; using System . Collections . Generic ; class GFG { static int countKAverageSubarrays ( int [ ] arr , int n , int k ) { int res = 0 ; for ( int L = 0 ; L < n ; L ++ ) { int sum = 0 ; for ( int R = L ; R < n ; R ++ ) { sum += arr [ R ] ; int len = ( R - L + 1 ) ; if ( sum % len == 0 ) { int avg = sum / len ; if ( avg == k ) res ++ ; } } } return res ; } public static void Main ( ) { int K = 6 ; int [ ] arr = { 12 , 5 , 3 , 10 , 4 , 8 , 10 , 12 , - 6 , - 1 } ; int N = arr . Length ; Console . Write ( countKAverageSubarrays ( arr , N , K ) ) ; } }
Maximize minimum array element possible by exactly K decrements | C # program for the above approach ; Function to find the maximized minimum element of the array after performing given operation exactly K times ; Stores the minimum element ; Traverse the given array ; Update the minimum element ; Stores the required operations to make all elements equal to the minimum element ; Update required operations ; If reqOperations < K ; Decrement the value of K by reqOperations ; Update minElement ; Return minimum element ; Driver Code
using System ; class GFG { static int minimumElement ( int [ ] arr , int N , int K ) { int minElement = arr [ 0 ] ; for ( int i = 0 ; i < N ; ++ i ) { minElement = Math . Min ( minElement , arr [ i ] ) ; } int reqOperations = 0 ; for ( int i = 0 ; i < N ; ++ i ) { reqOperations += arr [ i ] - minElement ; } if ( reqOperations < K ) { K -= reqOperations ; minElement -= ( K + N - 1 ) / N ; } return minElement ; } public static void Main ( string [ ] args ) { int [ ] arr = { 10 , 10 , 10 , 10 } ; int K = 7 ; int N = arr . Length ; Console . Write ( minimumElement ( arr , N , K ) ) ; } }
Split the fraction into sum of multiple fractions having numerator as 1 | C # program for the above approach ; Function to split the fraction into distinct unit fraction ; To store answer ; While numerator is positive ; Finding x = ceil ( d / n ) ; Add 1 / x to list of ans ; Update fraction ; Driver code ; Given Input ; Function Call ; Print Answer
using System ; using System . Collections . Generic ; class GFG { static List < string > FractionSplit ( long n , long d ) { List < string > UnitFactions = new List < string > ( ) ; while ( n > 0 ) { long x = ( d + n - 1 ) / n ; string s = "1 / " + x . ToString ( ) ; UnitFactions . Add ( s ) ; n = n * x - d ; d = d * x ; } return UnitFactions ; } public static void Main ( string [ ] args ) { long n = 13 , d = 18 ; List < string > res = FractionSplit ( n , d ) ; foreach ( string s in res ) Console . Write ( s + " , ▁ " ) ; } }
Find permutation of [ 1 , N ] such that ( arr [ i ] != i + 1 ) and sum of absolute difference between arr [ i ] and ( i + 1 ) is minimum | Function to generate the permutation of the first N natural numbers having sum of absolute difference between element and indices as minimum ; Initialize array arr [ ] from 1 to N ; Swap alternate positions ; Check N is greater than 1 and N is odd ; Swapping last two positions ; Print the permutation ; Driver code
using System ; class GFG { static void findPermutation ( int N ) { int [ ] arr = new int [ N ] ; int temp ; for ( int i = 0 ; i < N ; i ++ ) { arr [ i ] = i + 1 ; } for ( int i = 1 ; i < N ; i += 2 ) { temp = arr [ i ] ; arr [ i ] = arr [ i - 1 ] ; arr [ i - 1 ] = temp ; } if ( N % 2 == 1 && N > 1 ) { temp = arr [ N - 1 ] ; arr [ N - 1 ] = arr [ N - 2 ] ; arr [ N - 2 ] = temp ; } for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } public static void Main ( ) { int N = 7 ; findPermutation ( N ) ; } }
Find Array obtained after adding terms of AP for Q queries | C # program for the above approach ; Function to find array after performing the given query to the array elements ; Traverse the given query ; Traverse the given array ; Update the value of A [ i ] ; Update the value of curr ; Print the array elements ; Driver code ; Function Call
using System ; class GFG { public static void addAP ( int [ ] A , int Q , int [ , ] operations ) { for ( int j = 0 ; j < 2 ; ++ j ) { int L = operations [ j , 0 ] , R = operations [ j , 1 ] , a = operations [ j , 2 ] , d = operations [ j , 3 ] ; int curr = a ; for ( int i = L - 1 ; i < R ; i ++ ) { A [ i ] += curr ; curr += d ; } } for ( int i = 0 ; i < 4 ; ++ i ) Console . Write ( A [ i ] + " ▁ " ) ; } public static void Main ( string [ ] args ) { int [ ] A = { 5 , 4 , 2 , 8 } ; int Q = 2 ; int [ , ] query = { { 1 , 2 , 1 , 3 } , { 1 , 4 , 4 , 1 } } ; addAP ( A , Q , query ) ; } }
Maximize the number N by inserting given digit at any position | C # program for above approach ; Function to find the maximum value of N after inserting the digit K ; Convert it into N to string ; Stores the maximum value of N after inserting K ; Iterate till all digits that are not less than K ; Add the current digit to the string result ; Add digit ' K ' to result ; Iterate through all remaining characters ; Add current digit to result ; Print the maximum number formed ; Driver Code
using System ; class GFG { public static void maximizeNumber ( int N , int K ) { String s = N . ToString ( ) ; int L = s . Length ; string result = " " ; int i = 0 ; while ( ( i < L ) && ( K <= ( ( int ) s [ i ] - ( int ) '0' ) ) ) { result += ( s [ i ] ) ; ++ i ; } result += ( ( char ) ( K + ( int ) '0' ) ) ; while ( i < L ) { result += ( s [ i ] ) ; ++ i ; } Console . Write ( result ) ; } static void Main ( ) { int N = 6673 , K = 6 ; maximizeNumber ( N , K ) ; } }
Count of N | C # program for the above approach ; Function to find the value of X to the power Y ; Stores the value of X ^ Y ; If y is odd , multiply x with result ; Update the value of y and x ; Return the result ; Function to count number of arrays having element over the range [ 0 , 2 ^ K - 1 ] with Bitwise AND value 0 having maximum possible sum ; Print the value of N ^ K ; Driver Code
using System ; class GFG { static int power ( int x , int y ) { int res = 1 ; while ( y > 0 ) { if ( y % 2 != 0 ) res = res * x ; y = y >> 1 ; x = x * x ; } return res ; } static void countArrays ( int N , int K ) { Console . WriteLine ( ( int ) ( power ( N , K ) ) ) ; } public static void Main ( ) { int N = 5 , K = 6 ; countArrays ( N , K ) ; } }
Minimum absolute value of ( K – arr [ i ] ) for all possible values of K over the range [ 0 , N – 1 ] | C # program for the above approach ; Functinion to find the minimum absolute value of ( K - arr [ i ] ) for each possible value of K over the range [ 0 , N - 1 ] | ; Traverse the given array arr [ ] ; Stores the mininimum distance to any array element arr [ i ] ; Check if there is no safe position smaller than i ; Check if the current position is between two safe positions ; Take the minimum of two distances ; Check if the current index is a safe position ; Check if there is no safe position greater than i ; Print the minimum distance ; driver code
using System ; class GFG { public static void minimumDistance ( int [ ] arr , int N ) { int ind = 0 ; int prev = arr [ ind ] ; int s = arr . Length ; for ( int i = 0 ; i < N ; i ++ ) { int distance = Int32 . MaxValue ; if ( i < arr [ 0 ] ) { distance = arr [ 0 ] - i ; } else if ( i >= prev && ind + 1 < s && i <= arr [ ind + 1 ] ) { distance = Math . Min ( i - prev , arr [ ind + 1 ] - i ) ; if ( i == arr [ ind + 1 ] ) { distance = 0 ; prev = arr [ ind + 1 ] ; ind ++ ; } } else { distance = i - prev ; } Console . Write ( distance + " ▁ " ) ; } } public static void Main ( string [ ] args ) { int N = 5 ; int [ ] arr = { 0 , 4 } ; minimumDistance ( arr , N ) ; } }
Count of pairs in Array such that bitwise AND of XOR of pair and X is 0 | C # program for the above approach ; Function to find the number of pairs that satisfy the given criteria i . e . , i < j and ( arr [ i ] ^ arr [ j ] ) & X is 0 ; Stores the resultant count of pairs ; Iterate over the range [ 0 , N ) ; Iterate over the range ; Check for the given condition ; Return the resultant count ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int countOfPairs ( int [ ] arr , int N , int X ) { int count = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( ( ( arr [ i ] ^ arr [ j ] ) & X ) == 0 ) count ++ ; } } return count ; } public static void Main ( ) { int [ ] arr = { 3 , 2 , 5 , 4 , 6 , 7 } ; int X = 6 ; int N = arr . Length ; Console . Write ( countOfPairs ( arr , N , X ) ) ; } }
Count of pairs in Array such that bitwise AND of XOR of pair and X is 0 | C # program for the above approach ; Function to find the number of pairs that satisfy the given criteria i . e . , i < j and ( arr [ i ] ^ arr [ j ] ) & X is 0 ; Stores the resultant count of pairs ; Initializing the map M ; Populating the map ; Count number of pairs for every element in map using mathematical concept of combination ; As nC2 = n * ( n - 1 ) / 2 ; Return the resultant count ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int countOfPairs ( int [ ] arr , int N , int X ) { int count = 0 ; Dictionary < int , int > M = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( M . ContainsKey ( arr [ i ] & X ) ) M [ ( arr [ i ] & X ) ] ++ ; else M . Add ( arr [ i ] & X , 1 ) ; } foreach ( KeyValuePair < int , int > entry in M ) { int p = entry . Value ; count += p * ( p - 1 ) / 2 ; } return count ; } public static void Main ( ) { int [ ] arr = { 3 , 2 , 5 , 4 , 6 , 7 } ; int X = 6 ; int N = arr . Length ; Console . Write ( countOfPairs ( arr , N , X ) ) ; } }