codeparrot/xlcost-text-to-code huggingface dataset:
Find number of subarrays with XOR value a power of 2 | C # Program to count number of subarrays with Bitwise - XOR as power of 2 ; Function to find number of subarrays ; Hash Map to store prefix XOR values ; When no element is selected ; Check for all the powers of 2 , till a MAX value ; Insert Current prefixxor in Hash Map ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int MAX = 10 ; static int findSubarray ( int [ ] array , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; mp . Add ( 0 , 1 ) ; int answer = 0 ; int preXor = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int value = 1 ; preXor ^= array [ i ] ; for ( int j = 1 ; j <= MAX ; j ++ ) { int Y = value ^ preXor ; if ( mp . ContainsKey ( Y ) ) { answer += mp [ Y ] ; } value *= 2 ; } if ( mp . ContainsKey ( preXor ) ) { mp [ preXor ] = mp [ preXor ] + 1 ; } else { mp . Add ( preXor , 1 ) ; } } return answer ; } public static void Main ( String [ ] args ) { int [ ] array = { 2 , 6 , 7 , 5 , 8 } ; int n = array . Length ; Console . WriteLine ( findSubarray ( array , n ) ) ; } }
Maximum XOR of Two Numbers in an Array | C # implementation of the approach ; Function to return the maximum xor ; Calculating xor of each pair ; Driver Code
using System ; class GFG { static int max_xor ( int [ ] arr , int n ) { int maxXor = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { maxXor = Math . Max ( maxXor , arr [ i ] ^ arr [ j ] ) ; } } return maxXor ; } public static void Main ( ) { int [ ] arr = { 25 , 10 , 2 , 8 , 5 , 3 } ; int n = arr . Length ; Console . WriteLine ( max_xor ( arr , n ) ) ; } }
Maximum XOR of Two Numbers in an Array | C # implementation of the above approach ; Function to return the maximum xor ; set the i 'th bit in mask  like 100000, 110000, 111000.. ; Just keep the prefix till i ' th ▁ bit ▁ neglecting ▁ all ▁ ▁ the ▁ bit ' s after i 'th bit ; find two pair in set such that a ^ b = newMaxx which is the highest possible bit can be obtained ; clear the set for next iteration ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int max_xor ( int [ ] arr , int n ) { int maxx = 0 , mask = 0 ; HashSet < int > se = new HashSet < int > ( ) ; for ( int i = 30 ; i >= 0 ; i -- ) { mask |= ( 1 << i ) ; for ( int j = 0 ; j < n ; ++ j ) { se . Add ( arr [ j ] & mask ) ; } int newMaxx = maxx | ( 1 << i ) ; foreach ( int prefix in se ) { if ( se . Contains ( newMaxx ^ prefix ) ) { maxx = newMaxx ; break ; } } se . Clear ( ) ; } return maxx ; } public static void Main ( String [ ] args ) { int [ ] arr = { 25 , 10 , 2 , 8 , 5 , 3 } ; int n = arr . Length ; Console . WriteLine ( max_xor ( arr , n ) ) ; } }
Count of triplets that satisfy the given equation | C # implementation of the approach ; Function to return the count of required triplets ; First element of the current sub - array ; XOR every element of the current sub - array ; If the XOR becomes 0 then update the count of triplets ; Driver code
using System ; class GFG { static int CountTriplets ( int [ ] arr , int n ) { int ans = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int first = arr [ i ] ; for ( int j = i + 1 ; j < n ; j ++ ) { first ^= arr [ j ] ; if ( first == 0 ) ans += ( j - i ) ; } } return ans ; } public static void Main ( ) { int [ ] arr = { 2 , 5 , 6 , 4 , 2 } ; int n = arr . Length ; Console . WriteLine ( CountTriplets ( arr , n ) ) ; } }
Find the Majority Element | Set 3 ( Bit Magic ) |  ; Number of bits in the integer ; Variable to calculate majority element ; Loop to iterate through all the bits of number ; Loop to iterate through all elements in array to count the total set bit at position i from right ; If the total set bits exceeds n / 2 , this bit should be present in majority Element . ; iterate through array get the count of candidate majority element ; Verify if the count exceeds n / 2 ; Driver Code
using System ; class GFG { static void findMajority ( int [ ] arr , int n ) { int len = 32 ; int number = 0 ; for ( int i = 0 ; i < len ; i ++ ) { int countt = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( arr [ j ] & ( 1 << i ) ) != 0 ) countt ++ ; } if ( countt > ( n / 2 ) ) number += ( 1 << i ) ; } int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] == number ) count ++ ; if ( count > ( n / 2 ) ) Console . Write ( number ) ; else Console . Write ( " Majority ▁ Element ▁ Not ▁ Present " ) ; } static public void Main ( ) { int [ ] arr = { 3 , 3 , 4 , 2 , 4 , 4 , 2 , 4 , 4 } ; int n = arr . Length ; findMajority ( arr , n ) ; } }
Count number of bits to be flipped to convert A to B | Set | C # implementation of the above approach ; Function to return the count of bits to be flipped to convert a to b ; To store the required count ; Loop until both of them become zero ; Store the last bits in a as well as b ; If the current bit is not same in both the integers ; Right shift both the integers by 1 ; Return the count ; Driver code
using System ; class GFG { static int countBits ( int a , int b ) { int count = 0 ; while ( a > 0 b > 0 ) { int last_bit_a = a & 1 ; int last_bit_b = b & 1 ; if ( last_bit_a != last_bit_b ) count ++ ; a = a >> 1 ; b = b >> 1 ; } return count ; } public static void Main ( String [ ] args ) { int a = 10 , b = 7 ; Console . WriteLine ( countBits ( a , b ) ) ; } }
Count Set | C # program to find number of set bist in a number ; Recursive function to find number of set bist in a number ; Base condition ; If Least significant bit is set ; If Least significant bit is not set ; Driver code ; Function call
using System ; class GFG { static int CountSetBits ( int n ) { if ( n == 0 ) return 0 ; if ( ( n & 1 ) == 1 ) return 1 + CountSetBits ( n >> 1 ) ; else return CountSetBits ( n >> 1 ) ; } public static void Main ( ) { int n = 21 ; Console . WriteLine ( CountSetBits ( n ) ) ; } }
Sum of Bitwise OR of all pairs in a given array | A Simple C # program to compute sum of bitwise OR of all pairs ; Returns value of " arr [ 0 ] ▁ | ▁ arr [ 1 ] ▁ + ▁ ▁ arr [ 0 ] ▁ | ▁ arr [ 2 ] ▁ + ▁ . . . ▁ arr [ i ] ▁ | ▁ arr [ j ] ▁ + ▁ ▁ . . . . . ▁ arr [ n - 2 ] ▁ | ▁ arr [ n - 1 ] " ; Consider all pairs ( arr [ i ] , arr [ j ) such that i < j ; Driver program to test above function
using System ; class GFG { static int pairORSum ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) ans += arr [ i ] | arr [ j ] ; return ans ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int n = arr . Length ; Console . Write ( pairORSum ( arr , n ) ) ; } }
Number of 0 s and 1 s at prime positions in the given array | C # implementation of the approach ; Function that returns true if n is prime ; Check from 2 to n ; Function to find the count of 0 s and 1 s at prime indices ; To store the count of 0 s and 1 s ; If current 0 is at prime position ; If current 1 is at prime position ; Driver code
using System ; class GFG { static bool isPrime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i < n ; i ++ ) { if ( ( n % i ) == 0 ) return false ; } return true ; } static void countPrimePosition ( int [ ] arr ) { int c0 = 0 , c1 = 0 ; int n = arr . Length ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] == 0 ) && ( isPrime ( i ) ) ) c0 ++ ; if ( ( arr [ i ] == 1 ) && ( isPrime ( i ) ) ) c1 ++ ; } Console . WriteLine ( " Number ▁ of ▁ 0s ▁ = ▁ " + c0 ) ; Console . WriteLine ( " Number ▁ of ▁ 1s ▁ = ▁ " + c1 ) ; } static public void Main ( ) { int [ ] arr = { 1 , 0 , 1 , 0 , 1 } ; countPrimePosition ( arr ) ; } }
Multiply a number by 15 without using * and / operators | C # implementation of the approach ; Function to return ( 15 * N ) without using ' * ' or ' / ' operator ; prod = 16 * n ; ( ( 16 * n ) - n ) = 15 * n ; Driver code
using System ; class GFG { static long multiplyByFifteen ( long n ) { long prod = ( n << 4 ) ; prod = prod - n ; return prod ; } public static void Main ( ) { long n = 7 ; Console . Write ( multiplyByFifteen ( n ) ) ; } }
Multiply a number by 15 without using * and / operators | C # implementation of the approach ; Function to return ( 15 * N ) without using ' * ' or ' / ' operator ; prod = 8 * n ; Add ( 4 * n ) ; Add ( 2 * n ) ; Add n ; ( 8 * n ) + ( 4 * n ) + ( 2 * n ) + n = ( 15 * n ) ; Driver code
using System ; class GFG { static long multiplyByFifteen ( long n ) { long prod = ( n << 3 ) ; prod += ( n << 2 ) ; prod += ( n << 1 ) ; prod += n ; return prod ; } public static void Main ( ) { long n = 7 ; Console . Write ( multiplyByFifteen ( n ) ) ; } }
Find a number which give minimum sum when XOR with every number of array of integers | C # implementation of above approach ; Function to find an integer X such that the sum of all the array elements after getting XORed with X is minimum ; Finding Maximum element of array ; Find Maximum number of bits required in the binary representation of maximum number so log2 is calculated ; Running loop from p times which is the number of bits required to represent all the elements of the array ; If the bits in same position are set then count ; If count becomes greater than half of size of array then we need to make that bit '0' by setting X bit to '1' ; Again using shift operation to calculate the required number ; Calculate minimized sum ; Print solution ; Driver Code
using System ; using System . Linq ; class GFG { public static void findX ( int [ ] a , int n ) { int itr = a . Max ( ) ; int p = ( int ) Math . Log ( itr , 2 ) + 1 ; int x = 0 ; for ( int i = 0 ; i < p ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( a [ j ] & ( 1 << i ) ) != 0 ) count ++ ; } if ( count > ( n / 2 ) ) { x += 1 << i ; } } long sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += ( x ^ a [ i ] ) ; Console . Write ( " X ▁ = ▁ " + x + " , ▁ Sum ▁ = ▁ " + sum ) ; } public static void Main ( String [ ] args ) { int [ ] a = { 2 , 3 , 4 , 5 , 6 } ; int n = a . Length ; findX ( a , n ) ; } }
Game Theory in Balanced Ternary Numeral System | ( Moving 3 k steps at a time ) | C # implementation of the approach ; Numbers are in range of pow ( 3 , 32 ) ; Conversion of ternary into balanced ternary as start iterating from Least Significant Bit ( i . e 0 th ) , if encountered 0 or 1 , safely skip and pass carry 0 further 2 , replace it to - 1 and pass carry 1 further 3 , replace it to 0 and pass carry 1 further ; Similar to binary conversion ; Driver code ; Moving on to first occupied bit ; Printing ; Print ' Z ' in place of - 1
using System ; class GFG { static int [ ] arr = new int [ 33 ] ; static void balTernary ( int ter ) { int carry = 0 , b = 10 ; int i = 32 ; while ( ter > 0 ) { int rem = ter % b ; rem = rem + carry ; if ( rem == 0 ) { arr [ i -- ] = 0 ; carry = 0 ; } else if ( rem == 1 ) { arr [ i -- ] = 1 ; carry = 0 ; } else if ( rem == 2 ) { arr [ i -- ] = - 1 ; carry = 1 ; } else if ( rem == 3 ) { arr [ i -- ] = 0 ; carry = 1 ; } ter = ( int ) ( ter / b ) ; } if ( carry == 1 ) arr [ i ] = 1 ; } static int ternary ( int number ) { int ans = 0 , rem = 1 , b = 1 ; while ( number > 0 ) { rem = number % 3 ; ans = ans + rem * b ; number = ( int ) ( number / 3 ) ; b = b * 10 ; } return ans ; } public static void Main ( String [ ] args ) { int number = 3056 ; int ter = ternary ( number ) ; balTernary ( ter ) ; int i = 0 ; while ( arr [ i ] == 0 ) { i ++ ; } for ( int j = i ; j <= 32 ; j ++ ) { if ( arr [ j ] == - 1 ) Console . Write ( ' Z ' ) ; else Console . Write ( arr [ j ] ) ; } } }
Minimum value among AND of elements of every subset of an array | C # program for the above approach ; Find AND of whole array ; Print the answer ; Driver code
class GFG { static void minAND ( int [ ] arr , int n ) { int s = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { s = s & arr [ i ] ; } System . Console . WriteLine ( s ) ; } static void Main ( ) { int [ ] arr = { 1 , 2 , 3 } ; int n = arr . Length ; minAND ( arr , n ) ; } }
Modify a binary array to Bitwise AND of all elements as 1 | C # implementation of the above approach ; Function to check if it is possible or not ; Driver code
using System ; class GFG { static bool check ( int [ ] a , int n ) { for ( int i = 0 ; i < n ; i ++ ) if ( a [ i ] == 1 ) return true ; return false ; } public static void Main ( ) { int [ ] a = { 0 , 1 , 0 , 1 } ; int n = a . Length ; if ( check ( a , n ) == true ) Console . Write ( " YES STRNEWLINE " ) ; else Console . Write ( " NO STRNEWLINE " ) ; } }
Find array using different XORs of elements in groups of size 4 | C # implementation of the approach ; Utility function to print the contents of the array ; Function to find the required array ; Print the array ; Driver code
using System ; class GFG { static void printArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } static void findArray ( int [ ] q , int n ) { int ans ; int [ ] arr = new int [ n ] ; for ( int k = 0 , j = 0 ; j < n / 4 ; j ++ ) { ans = q [ k ] ^ q [ k + 3 ] ; arr [ k + 1 ] = q [ k + 1 ] ^ ans ; arr [ k + 2 ] = q [ k + 2 ] ^ ans ; arr [ k ] = q [ k ] ^ ( ( arr [ k + 1 ] ) ^ ( arr [ k + 2 ] ) ) ; arr [ k + 3 ] = q [ k + 3 ] ^ ( arr [ k + 1 ] ^ arr [ k + 2 ] ) ; k += 4 ; } printArray ( arr , n ) ; } public static void Main ( ) { int [ ] q = { 4 , 1 , 7 , 0 } ; int n = q . Length ; findArray ( q , n ) ; } }
Count of values of x <= n for which ( n XOR x ) = ( n | C # implementation of the approach ; Function to return the count of valid values of x ; Convert n into binary String ; To store the count of 1 s ; If current bit is 1 ; Calculating answer ; Driver code
using System ; class GFG { static int countX ( int n ) { string binary = Convert . ToString ( n , 2 ) ; int count = 0 ; for ( int i = 0 ; i < binary . Length ; i ++ ) { if ( binary [ i ] == '1' ) count ++ ; } int answer = ( int ) Math . Pow ( 2 , count ) ; return answer ; } public static void Main ( ) { int n = 5 ; int answer = countX ( n ) ; Console . WriteLine ( answer ) ; } }
Check if the binary representation of a number has equal number of 0 s and 1 s in blocks | C # program to check if a number has same counts of 0 s and 1 s in every block . ; function to convert decimal to binary ; Count same bits in last block ; If n is 0 or it has all 1 s , then it is not considered to have equal number of 0 s and 1 s in blocks . ; Count same bits in all remaining blocks . ; Driver code
using System ; class GFG { static bool isEqualBlock ( int n ) { int first_bit = n % 2 ; int first_count = 1 ; n = n / 2 ; while ( n % 2 == first_bit && n > 0 ) { n = n / 2 ; first_count ++ ; } if ( n == 0 ) return false ; while ( n > 0 ) { first_bit = n % 2 ; int curr_count = 1 ; n = n / 2 ; while ( n % 2 == first_bit ) { n = n / 2 ; curr_count ++ ; } if ( curr_count != first_count ) return false ; } return true ; } public static void Main ( ) { int n = 51 ; if ( isEqualBlock ( n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
First and Last Three Bits | C # implementation of the approach ; Function to print the first and last 3 bits equivalent decimal number ; Converting n to binary ; Length of the array has to be at least 3 ; Convert first three bits to decimal ; Print the decimal ; Convert last three bits to decimal ; Print the decimal ; Driver code
using System ; class GFG { static void binToDecimal3 ( int n ) { int [ ] a = new int [ 64 ] ; int x = 0 , i ; for ( i = 0 ; n > 0 ; i ++ ) { a [ i ] = n % 2 ; n /= 2 ; } x = ( i < 3 ) ? 3 : i ; int d = 0 , p = 0 ; for ( int j = x - 3 ; j < x ; j ++ ) d += a [ j ] * ( int ) Math . Pow ( 2 , p ++ ) ; int d1 = d ; d = 0 ; p = 0 ; for ( int k = 0 ; k < 3 ; k ++ ) d += a [ k ] * ( int ) Math . Pow ( 2 , p ++ ) ; Console . WriteLine ( d1 + " ▁ " + d ) ; } static void Main ( ) { int n = 86 ; binToDecimal3 ( n ) ; } }
First and Last Three Bits | C # implementation of the approach ; Function to print the first and last 3 bits equivalent decimal number ; Number formed from last three bits ; Let us get first three bits in n ; Number formed from first three bits ; Printing result ; Driver code
using System ; class GFG { static void binToDecimal3 ( int n ) { int last_3 = ( ( n & 4 ) + ( n & 2 ) + ( n & 1 ) ) ; n = n >> 3 ; while ( n > 7 ) n = n >> 1 ; int first_3 = ( ( n & 4 ) + ( n & 2 ) + ( n & 1 ) ) ; Console . WriteLine ( first_3 + " ▁ " + last_3 ) ; } static public void Main ( ) { int n = 86 ; binToDecimal3 ( n ) ; } }
Count of numbers which can be made power of 2 by given operation | C # implementation of the approach ; Function that returns true if x is a power of 2 ; If x & ( x - 1 ) = 0 then x is a power of 2 ; Function to return the required count ; If a [ i ] or ( a [ i ] + 1 ) is a power of 2 ; Driver code
using System ; class GFG { static bool isPowerOfTwo ( int x ) { if ( x == 0 ) return false ; if ( ( x & ( x - 1 ) ) == 0 ) return true ; else return false ; } static int countNum ( int [ ] a , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPowerOfTwo ( a [ i ] ) || isPowerOfTwo ( a [ i ] + 1 ) ) count ++ ; } return count ; } public static void Main ( ) { int [ ] arr = { 5 , 6 , 9 , 3 , 1 } ; int n = arr . Length ; Console . WriteLine ( countNum ( arr , n ) ) ; } }
Sum of elements from an array having even parity | C # program to find the sum of the elements from an array which have even parity ; Function that returns true if x has even parity ; We basically count set bits https : www . geeksforgeeks . org / count - set - bits - in - an - integer / ; Function to return the sum of the elements from an array which have even parity ; If a [ i ] has even parity ; Driver code
using System ; class GFG { static bool checkEvenParity ( int x ) { int parity = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; parity ++ ; } if ( parity % 2 == 0 ) return true ; else return false ; } static long sumlist ( int [ ] a , int n ) { long sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( checkEvenParity ( a [ i ] ) ) sum += a [ i ] ; } return sum ; } public static void Main ( ) { int [ ] arr = { 2 , 4 , 3 , 5 , 9 } ; int n = arr . Length ; Console . WriteLine ( sumlist ( arr , n ) ) ; } }
Number of pairs with Bitwise OR as Odd number | C # program to count pairs with odd OR ; Function to count pairs with odd OR ; find OR operation check odd or odd ; return count of odd pair ; Driver Code
using System ; public class GFG { static int findOddPair ( int [ ] A , int N ) { int oddPair = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( ( A [ i ] A [ j ] ) % 2 != 0 ) oddPair ++ ; } } return oddPair ; } static public void Main ( ) { int [ ] A = { 5 , 6 , 2 , 8 } ; int N = A . Length ; Console . WriteLine ( findOddPair ( A , N ) ) ; } }
Count pairs with Bitwise XOR as EVEN number | C # program to count pairs with XOR giving a even number ; Function to count number of even pairs ; variable for counting even pairs ; find all pairs ; find XOR operation check even or even ; return number of even pair ; Driver Code ; calling function findevenPair and print number of even pair
using System ; class GFG { static int findevenPair ( int [ ] A , int N ) { int i , j ; int evenPair = 0 ; for ( i = 0 ; i < N ; i ++ ) { for ( j = i + 1 ; j < N ; j ++ ) { if ( ( A [ i ] ^ A [ j ] ) % 2 == 0 ) evenPair ++ ; } } return evenPair ; } public static void Main ( ) { int [ ] A = { 5 , 4 , 7 , 2 , 1 } ; int N = A . Length ; Console . WriteLine ( findevenPair ( A , N ) ) ; } }
Count pairs with Bitwise XOR as EVEN number | C # program to count pairs with XOR giving a even number ; Function to count number of even pairs ; find all pairs ; return number of even pair ; Driver Code ; calling function findEvenPair and print number of even pair
using System ; public class GFG { static int findEvenPair ( int [ ] A , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] % 2 != 0 ) count ++ ; } int totalPairs = ( N * ( N - 1 ) / 2 ) ; int oddEvenPairs = count * ( N - count ) ; return totalPairs - oddEvenPairs ; } public static void Main ( ) { int [ ] a = { 5 , 4 , 7 , 2 , 1 } ; int n = a . Length ; Console . Write ( findEvenPair ( a , n ) ) ; } }
Count pairs with Bitwise | C # program to count pair with bitwise - AND as even number ; Function to count number of pairs EVEN bitwise AND ; variable for counting even pairs ; find all pairs ; find AND operation to check evenpair ; return number of even pair ; Driver Code
using System ; class GFG { static int findevenPair ( int [ ] A , int N ) { int i , j ; int evenPair = 0 ; for ( i = 0 ; i < N ; i ++ ) { for ( j = i + 1 ; j < N ; j ++ ) { if ( ( A [ i ] & A [ j ] ) % 2 == 0 ) evenPair ++ ; } } return evenPair ; } public static void Main ( ) { int [ ] a = { 5 , 1 , 3 , 2 } ; int n = a . Length ; Console . WriteLine ( findevenPair ( a , n ) ) ; } }
Count pairs with Bitwise | C # program to count pair with bitwise - AND as even number ; Function to count number of pairs with EVEN bitwise AND ; count odd numbers ; count odd pairs ; return number of even pair ; Driver Code
using System ; public class GFG { static int findevenPair ( int [ ] A , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) if ( A [ i ] % 2 != 0 ) count ++ ; int oddCount = count * ( count - 1 ) / 2 ; return ( N * ( N - 1 ) / 2 ) - oddCount ; } static public void Main ( ) { int [ ] a = { 5 , 1 , 3 , 2 } ; int n = a . Length ; Console . WriteLine ( findevenPair ( a , n ) ) ; } }
Find a value whose XOR with given number is maximum | C # implementation of the above approach ; Function To Calculate Answer ; Find number of bits in the given integer ; XOR the given integer with poe ( 2 , number_of_bits - 1 and print the result ; Driver Code
using System ; class GFG { static int calculate ( int X ) { int number_of_bits = 8 ; return ( ( 1 << number_of_bits ) - 1 ) ^ X ; } public static void Main ( ) { int X = 4 ; Console . WriteLine ( " Required ▁ Number ▁ is ▁ : ▁ " + calculate ( X ) ) ; } }
XOR of all elements of array with set bits equal to K | C # program to find Xor of all elements with set bits equal to K ; Function to find Xor of desired elements ; Initialize vector ; push required elements ; Initialize result with first element of vector ; Driver code
using System ; using System . Collections ; using System . Linq ; class GFG { static int xorGivenSetBits ( int [ ] arr , int n , int k ) { ArrayList v = new ArrayList ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( Convert . ToString ( arr [ i ] , 2 ) . Count ( c => c == '1' ) == k ) { v . Add ( arr [ i ] ) ; } } int result = ( int ) v [ 0 ] ; for ( int i = 1 ; i < v . Count ; i ++ ) result = result ^ ( int ) v [ i ] ; return result ; } static void Main ( ) { int [ ] arr = { 2 , 13 , 1 , 19 , 7 } ; int n = arr . Length ; int k = 3 ; Console . WriteLine ( xorGivenSetBits ( arr , n , k ) ) ; } }
Replace every element of the array with BitWise XOR of all other | C # program to Replace every element by the bitwise xor of all other elements ; Function to replace the elements ; Calculate the xor of all the elements ; Replace every element by the xor of all other elements ; Driver code ; Print the modified array .
using System ; public class GFG { static void ReplaceElements ( int [ ] arr , int n ) { int X = 0 ; for ( int i = 0 ; i < n ; ++ i ) { X ^= arr [ i ] ; } for ( int i = 0 ; i < n ; ++ i ) { arr [ i ] = X ^ arr [ i ] ; } } static public void Main ( ) { int [ ] arr = { 2 , 3 , 3 , 5 , 5 } ; int n = arr . Length ; ReplaceElements ( arr , n ) ; for ( int i = 0 ; i < n ; ++ i ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } }