codeparrot/xlcost-text-to-code huggingface dataset:
Program to convert a given number to words | C # program to print a given number in words . The program handles numbers from 0 to 9999 ; A function that prints given number in words ; Get number of digits in given number ; Base cases ; The first string is not used , it is to make array indexing simple ; The first string is not used , it is to make array indexing simple ; The first two string are not used , they are to make array indexing simple ; Used for debugging purpose only ; For single digit number ; Iterate while num is not ' \0' ; Code path for first 2 digits ; here len can be 3 or 4 ; Code path for last 2 digits ; Need to explicitly handle 10 - 19. Sum of the two digits is used as index of " two _ digits " array of strings ; Need to explicitely handle 20 ; Rest of the two digit numbers i . e . , 21 to 99 ; Driver Code
using System ; class GFG { static void convert_to_words ( char [ ] num ) { int len = num . Length ; if ( len == 0 ) { Console . WriteLine ( " empty ▁ string " ) ; return ; } if ( len > 4 ) { Console . WriteLine ( " Length ▁ more ▁ than ▁ " + "4 ▁ is ▁ not ▁ supported " ) ; return ; } string [ ] single_digits = new string [ ] { " zero " , " one " , " two " , " three " , " four " , " five " , " six " , " seven " , " eight " , " nine " } ; string [ ] two_digits = new string [ ] { " " , " ten " , " eleven " , " twelve " , " thirteen " , " fourteen " , " fifteen " , " sixteen " , " seventeen " , " eighteen " , " nineteen " } ; string [ ] tens_multiple = new string [ ] { " " , " " , " twenty " , " thirty " , " forty " , " fifty " , " sixty " , " seventy " , " eighty " , " ninety " } ; string [ ] tens_power = new string [ ] { " hundred " , " thousand " } ; Console . Write ( ( new string ( num ) ) + " : ▁ " ) ; if ( len == 1 ) { Console . WriteLine ( single_digits [ num [ 0 ] - '0' ] ) ; return ; } int x = 0 ; while ( x < num . Length ) { if ( len >= 3 ) { if ( num [ x ] - '0' != 0 ) { Console . Write ( single_digits [ num [ x ] - '0' ] + " ▁ " ) ; Console . Write ( tens_power [ len - 3 ] + " ▁ " ) ; } -- len ; } else { if ( num [ x ] - '0' == 1 ) { int sum = num [ x ] - '0' + num [ x + 1 ] - '0' ; Console . WriteLine ( two_digits [ sum ] ) ; return ; } else if ( num [ x ] - '0' == 2 && num [ x + 1 ] - '0' == 0 ) { Console . WriteLine ( " twenty " ) ; return ; } else { int i = ( num [ x ] - '0' ) ; if ( i > 0 ) Console . Write ( tens_multiple [ i ] + " ▁ " ) ; else Console . Write ( " " ) ; ++ x ; if ( num [ x ] - '0' != 0 ) Console . WriteLine ( single_digits [ num [ x ] - '0' ] ) ; } } ++ x ; } } public static void Main ( ) { convert_to_words ( "9923" . ToCharArray ( ) ) ; convert_to_words ( "523" . ToCharArray ( ) ) ; convert_to_words ( "89" . ToCharArray ( ) ) ; convert_to_words ( "8" . ToCharArray ( ) ) ; } }
Check if a number is multiple of 5 without using / and % operators | Assuming that integer takes 4 bytes , there can be maximum 10 digits in a integer ; Check the last character of string ; Driver Code
class GFG { static int MAX = 11 ; static bool isMultipleof5 ( int n ) { char [ ] str = new char [ MAX ] ; int len = str . Length ; if ( str [ len - 1 ] == '5' str [ len - 1 ] == '0' ) return true ; return false ; } static void Main ( ) { int n = 19 ; if ( isMultipleof5 ( n ) == true ) Console . WriteLine ( " { 0 } ▁ is ▁ " + " multiple ▁ of ▁ 5" , n ) ; else Console . WriteLine ( " { 0 } ▁ is ▁ not ▁ a ▁ " + " multiple ▁ of ▁ 5" , n ) ; } }
Count subsequences having odd Bitwise OR values in an array | Java implementation for the above approach ; Function to count the subsequences having odd bitwise OR value ; Stores count of odd elements ; Stores count of even elements ; Traverse the array arr [ ] ; If element is odd ; Return the final answer ; Driver Code ; Given array arr [ ]
using System ; class GFG { static int countSubsequences ( int [ ] arr ) { int odd = 0 ; int even = 0 ; for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( ( arr [ i ] & 1 ) != 0 ) odd ++ ; else even ++ ; } return ( ( 1 << odd ) - 1 ) * ( 1 << even ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 1 } ; Console . Write ( countSubsequences ( arr ) ) ; } }
Bitwise OR of Bitwise AND of all subsets of an Array for Q queries | C # program for the above approach ; Function to find the OR of AND of all subsets of the array for each query ; An array to store the bits ; Itearte for all the bits ; Iterate over all the numbers and store the bits in bits [ ] array ; Itearte over all the queries ; Replace the bits of the value at arr [ queries [ p ] [ 0 ] ] with the bits of queries [ p ] [ 1 ] ; Substitute the value in the array ; Find OR of the bits [ ] array ; Print the answer ; Driver Code ; Given Input ; Function Call
using System ; class GFG { static void Or_of_Ands_for_each_query ( int [ ] arr , int n , int [ , ] queries , int q ) { int [ ] bits = new int [ 32 ] ; for ( int i = 0 ; i < 32 ; i ++ ) { bits [ i ] = 0 ; } for ( int i = 0 ; i < 32 ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( ( ( 1 << i ) & arr [ j ] ) != 0 ) { bits [ i ] ++ ; } } } for ( int p = 0 ; p < q ; p ++ ) { for ( int i = 0 ; i < 32 ; i ++ ) { if ( ( ( 1 << i ) & arr [ queries [ p , 0 ] ] ) != 0 ) { bits [ i ] -- ; } if ( ( queries [ p , 1 ] & ( 1 << i ) ) != 0 ) { bits [ i ] ++ ; } } arr [ queries [ p , 0 ] ] = queries [ p , 1 ] ; int ans = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) { if ( bits [ i ] != 0 ) { ans |= ( 1 << i ) ; } } Console . WriteLine ( ans ) ; } } public static void Main ( String [ ] args ) { int n = 3 , q = 2 ; int [ ] arr = { 3 , 5 , 7 } ; int [ , ] queries = { { 1 , 2 } , { 2 , 1 } } ; Or_of_Ands_for_each_query ( arr , n , queries , q ) ; } }
Find XOR sum of Bitwise AND of all pairs from given two Arrays | C # algorithm for the above approach ; Function to calculate the XOR sum of all ANDS of all pairs on A [ ] and B [ ] ; variable to store anshu ; when there has been no AND of pairs before this ; Driver code ; Input ] ; Function call
using System ; using System . Collections . Generic ; class GFG { static int XorSum ( int [ ] A , int [ ] B , int N , int M ) { int ans = - 1 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( ans == - 1 ) ans = ( A [ i ] & B [ j ] ) ; else ans ^= ( A [ i ] & B [ j ] ) ; } } return ans ; } public static void Main ( ) { int [ ] A = { 3 , 5 } ; int [ ] B = { 2 , 3 } ; int N = A . Length ; int M = B . Length ; Console . Write ( XorSum ( A , B , N , M ) ) ; } }
Shortest path length between two given nodes such that adjacent nodes are at bit difference 2 | C # program for the above approach ; Function to count set bits in a number ; Stores count of set bits in xo ; Iterate over each bits of xo ; If current bit of xo is 1 ; Update count ; Update xo ; Function to find length of shortest path between the nodes a and b ; Stores XOR of a and b ; Stores the count of set bits in xorVal ; If cnt is an even number ; Driver code ; Given N ; Given a and b ; Function call
using System ; class GFG { static int countbitdiff ( int xo ) { int count = 0 ; while ( xo != 0 ) { if ( xo % 2 == 1 ) { count ++ ; } xo = xo / 2 ; } return count ; } static void shortestPath ( int n , int a , int b ) { int xorVal = a ^ b ; int cnt = countbitdiff ( xorVal ) ; if ( cnt % 2 == 0 ) Console . Write ( cnt / 2 ) ; else Console . Write ( " - 1" ) ; } public static void Main ( String [ ] args ) { int n = 15 ; int a = 15 , b = 3 ; shortestPath ( n , a , b ) ; } }
Modify a matrix by converting each element to XOR of its digits | C # program for the above approach ; Function to calculate Bitwise XOR of digits present in X ; Stores the Bitwise XOR ; While X is true ; Update Bitwise XOR of its digits ; Return the result ; Function to print matrix after converting each matrix element to XOR of its digits ; Traverse each row of arr [ ] [ ] ; Traverse each column of arr [ ] [ ] ; Function to convert the given matrix to required XOR matrix ; Traverse each row of arr [ ] [ ] ; Traverse each column of arr [ ] [ ] ; Store the current matrix element ; Find the xor of digits present in X ; Stores the XOR value ; Print resultant matrix ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int M = 3 ; static int N = 3 ; static int findXOR ( int X ) { int ans = 0 ; while ( X != 0 ) { ans ^= ( X % 10 ) ; X /= 10 ; } return ans ; } static void printXORmatrix ( int [ , ] arr ) { for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { Console . Write ( arr [ i , j ] + " ▁ " ) ; } Console . WriteLine ( ) ; } } static void convertXOR ( int [ , ] arr ) { for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { int X = arr [ i , j ] ; int temp = findXOR ( X ) ; arr [ i , j ] = temp ; } } printXORmatrix ( arr ) ; } static public void Main ( ) { int [ , ] arr = { { 27 , 173 , 5 } , { 21 , 6 , 624 } , { 5 , 321 , 49 } } ; convertXOR ( arr ) ; } }
Count subarrays made up of elements having exactly K set bits | C # program for the above approach ; Function to count the number of set bits in an integer N ; Stores the count of set bits ; While N is non - zero ; If the LSB is 1 , then increment ans by 1 ; Return the total set bits ; Function to count the number of subarrays having made up of elements having K set bits ; Stores the total count of resultant subarrays ; Traverse the given array ; If the current element has K set bits ; Otherwise ; Increment count of subarrays ; Return total count of subarrays ; Driver Code ; Function Call
using System ; class GFG { static int countSet ( int N ) { int ans = 0 ; while ( N > 0 ) { ans += N & 1 ; N >>= 1 ; } return ans ; } static int countSub ( int [ ] arr , int k ) { int ans = 0 ; int setK = 0 ; for ( int i = 0 ; i < 5 ; i ++ ) { if ( countSet ( arr [ i ] ) == k ) setK += 1 ; else setK = 0 ; ans += setK ; } return ans ; } public static void Main ( string [ ] args ) { int [ ] arr = { 4 , 2 , 1 , 5 , 6 } ; int K = 2 ; Console . WriteLine ( countSub ( arr , K ) ) ; } }
Count subarrays having odd Bitwise XOR | C # program for the above approach ; Function to count the number of subarrays of the given array having odd Bitwise XOR ; Stores number of odd numbers upto i - th index ; Stores number of required subarrays starting from i - th index ; Store the required result ; Find the number of subarrays having odd Bitwise XOR values starting at 0 - th index ; Check if current element is odd ; If the current value of odd is not zero , increment c_odd by 1 ; Find the number of subarrays having odd bitwise XOR value starting at ith index and add to result ; Add c_odd to result ; Print the result ; Driver Code ; Given array ; Stores the size of the array
using System ; class GFG { static void oddXorSubarray ( int [ ] a , int n ) { int odd = 0 ; int c_odd = 0 ; int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 != 0 ) { odd = ( odd == 0 ) ? 1 : 0 ; } if ( odd != 0 ) { c_odd ++ ; } } for ( int i = 0 ; i < n ; i ++ ) { result += c_odd ; if ( a [ i ] % 2 != 0 ) { c_odd = ( n - i - c_odd ) ; } } Console . WriteLine ( result ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 4 , 7 , 9 , 10 } ; int N = arr . Length ; oddXorSubarray ( arr , N ) ; } }
Sum of Bitwise OR of every array element paired with all other array elements | C # program for the above approach ; Function to print required sum for every valid index i ; Store the required sum for current array element ; Generate all possible pairs ( arr [ i ] , arr [ j ] ) ; Update the value of req_sum ; Print the required sum ; Driver Code ; Given array ; Size of the array ; Function Call
using System ; public class GFG { static void printORSumforEachElement ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int req_sum = 0 ; for ( int j = 0 ; j < N ; j ++ ) { req_sum += ( arr [ i ] arr [ j ] ) ; } Console . Write ( req_sum + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int N = arr . Length ; printORSumforEachElement ( arr , N ) ; } }
Sum of Bitwise OR of each array element of an array with all elements of another array | C # program for the above approach ; Function to compute sum of Bitwise OR of each element in arr1 [ ] with all elements of the array arr2 [ ] ; Declaring an array of size 32 to store the count of each bit ; Traverse the array arr1 [ ] ; Current bit position ; While num exceeds 0 ; Checks if i - th bit is set or not ; Increment the count at bit_position by one ; Increment bit_position ; Right shift the num by one ; Traverse in the arr2 [ ] ; Store the ith bit value ; Total required sum ; Traverse in the range [ 0 , 31 ] ; Check if current bit is set ; Increment the Bitwise sum by N * ( 2 ^ i ) ; Right shift num by one ; Left shift valee_at_that_bit by one ; Print the sum obtained for ith number in arr1 [ ] ; Driver Code ; Given arr1 [ ] ; Given arr2 [ ] ; Size of arr1 [ ] ; Size of arr2 [ ] ; Function Call
using System ; class GFG { static void Bitwise_OR_sum_i ( int [ ] arr1 , int [ ] arr2 , int M , int N ) { int [ ] frequency = new int [ 32 ] ; for ( int i = 0 ; i < 32 ; i ++ ) { frequency [ i ] = 0 ; } for ( int i = 0 ; i < N ; i ++ ) { int bit_position = 0 ; int num = arr1 [ i ] ; while ( num != 0 ) { if ( ( num & 1 ) != 0 ) { frequency [ bit_position ] += 1 ; } bit_position += 1 ; num >>= 1 ; } } for ( int i = 0 ; i < M ; i ++ ) { int num = arr2 [ i ] ; int value_at_that_bit = 1 ; int bitwise_OR_sum = 0 ; for ( int bit_position = 0 ; bit_position < 32 ; bit_position ++ ) { if ( ( num & 1 ) != 0 ) { bitwise_OR_sum += N * value_at_that_bit ; } else { bitwise_OR_sum += frequency [ bit_position ] * value_at_that_bit ; } num >>= 1 ; value_at_that_bit <<= 1 ; } Console . Write ( bitwise_OR_sum + " ▁ " ) ; } return ; } public static void Main ( ) { int [ ] arr1 = { 1 , 2 , 3 } ; int [ ] arr2 = { 1 , 2 , 3 } ; int N = arr1 . Length ; int M = arr2 . Length ; Bitwise_OR_sum_i ( arr1 , arr2 , M , N ) ; } }
Count pairs from given array with Bitwise OR equal to K | C # program for the above approach ; Function that counts the pairs from the array whose Bitwise OR is K ; Stores the required count of pairs ; Generate all possible pairs ; Perform OR operation ; If Bitwise OR is equal to K , increment count ; Print the total count ; Driver Code ; Function Call
using System ; class GFG { static void countPairs ( int [ ] arr , int k , int size ) { int count = 0 , x ; for ( int i = 0 ; i < size - 1 ; i ++ ) { for ( int j = i + 1 ; j < size ; j ++ ) { x = arr [ i ] | arr [ j ] ; if ( x == k ) count ++ ; } } Console . WriteLine ( count ) ; } public static void Main ( ) { int [ ] arr = { 2 , 38 , 44 , 29 , 62 } ; int K = 46 ; int N = arr . Length ; countPairs ( arr , K , N ) ; } }
Count nodes having Bitwise XOR of all edges in their path from the root equal to K | C # program for the above approach ; Initialize the adjacency list to represent the tree ; Marks visited / unvisited vertices ; Stores the required count of nodes ; DFS to visit each vertex ; Mark the current node as visited ; Update the counter xor is K ; Visit adjacent nodes ; Calculate Bitwise XOR of edges in the path ; Recursive call to dfs function ; Function to construct the tree and print required count of nodes ; Add edges ; Print answer ; Driver Code ; Given K and R ; Given edges ; Number of vertices ; Function call
using System ; using System . Collections . Generic ; class GFG { public class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static List < pair > [ ] adj = new List < pair > [ 100005 ] ; static int [ ] visited = new int [ 100005 ] ; static int ans = 0 ; static void dfs ( int node , int xorr , int k ) { visited [ node ] = 1 ; if ( node != 1 && xorr == k ) ans ++ ; foreach ( pair x in adj [ node ] ) { if ( visited [ x . first ] != 1 ) { int xorr1 = xorr ^ x . second ; dfs ( x . first , xorr1 , k ) ; } } } static void countNodes ( int N , int K , int R , int [ , ] edges ) { for ( int i = 0 ; i < N - 1 ; i ++ ) { int u = edges [ i , 0 ] ; int v = edges [ i , 1 ] , w = edges [ i , 2 ] ; adj [ u ] . Add ( new pair ( v , w ) ) ; adj [ v ] . Add ( new pair ( u , w ) ) ; } dfs ( R , 0 , K ) ; Console . Write ( ans + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int K = 0 , R = 1 ; for ( int i = 0 ; i < adj . Length ; i ++ ) adj [ i ] = new List < pair > ( ) ; int [ , ] edges = { { 1 , 2 , 3 } , { 1 , 3 , 1 } , { 2 , 4 , 3 } , { 2 , 5 , 4 } , { 3 , 6 , 1 } , { 3 , 7 , 2 } } ; int N = edges . GetLength ( 0 ) ; countNodes ( N , K , R , edges ) ; } }
Bitwise operations on Subarrays of size K | C # program to find the subarray with minimum XOR ; Function to find the minimum XOR of the subarray of size K ; K must be smaller than or equal to n ; Initialize the beginning index of result ; Compute XOR sum of first subarray of size K ; Initialize minimum XOR sum as current xor ; Traverse from ( k + 1 ) ' th ▁ ▁ element ▁ to ▁ n ' th element ; XOR with current item and first item of previous subarray ; Update result if needed ; Print the minimum XOR ; Driver Code ; Given array [ ] arr ; Given subarray size K ; Function call
using System ; class GFG { static void findMinXORSubarray ( int [ ] arr , int n , int k ) { if ( n < k ) return ; int res_index = 0 ; int curr_xor = 0 ; for ( int i = 0 ; i < k ; i ++ ) curr_xor ^= arr [ i ] ; int min_xor = curr_xor ; for ( int i = k ; i < n ; i ++ ) { curr_xor ^= ( arr [ i ] ^ arr [ i - k ] ) ; if ( curr_xor < min_xor ) { min_xor = curr_xor ; res_index = ( i - k + 1 ) ; } } Console . WriteLine ( min_xor ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 7 , 90 , 20 , 10 , 50 , 40 } ; int k = 3 ; int n = arr . Length ; findMinXORSubarray ( arr , n , k ) ; } }
Maximum number of consecutive 1 s after flipping all 0 s in a K length subarray | C # program for the above approach ; Function to find the maximum number of consecutive 1 's after flipping all zero in a K length subarray ; Initialize variable ; Iterate unil n - k + 1 as we have to go till i + k ; Iterate in the array in left direction till you get 1 else break ; Iterate in the array in right direction till you get 1 else break ; Compute the maximum length ; Return the length ; Driver code ; Array initialization ; Size of array
using System ; class GFG { static int findmax ( int [ ] arr , int n , int k ) { int trav , i ; int c = 0 , maximum = 0 ; for ( i = 0 ; i < n - k + 1 ; i ++ ) { trav = i - 1 ; c = 0 ; while ( trav >= 0 && arr [ trav ] == 1 ) { trav -- ; c ++ ; } trav = i + k ; while ( trav < n && arr [ trav ] == 1 ) { trav ++ ; c ++ ; } c += k ; if ( c > maximum ) maximum = c ; } return maximum ; } public static void Main ( ) { int k = 3 ; int [ ] arr = { 0 , 0 , 1 , 1 , 0 , 0 , 0 , 0 } ; int n = arr . Length ; int ans = findmax ( arr , n , k ) ; Console . WriteLine ( ans ) ; } }
Print all the leaf nodes of Binary Heap | C # implementation to print the leaf nodes of a Binary Heap ; Function to calculate height of the Binary heap with given the count of the nodes ; Function to find the leaf nodes of binary heap ; Calculate the height of the complete binary tree ; if the height if h - 1 , then there should not be any child nodes ; Function to print the leaf nodes ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int height ( int N ) { return ( int ) Math . Ceiling ( Math . Log ( N + 1 ) / Math . Log ( 2 ) ) - 1 ; } static void findLeafNodes ( int [ ] arr , int n ) { int h = height ( n ) ; List < int > arrlist = new List < int > ( ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( height ( i + 1 ) == h ) { arrlist . Add ( arr [ i ] ) ; } else if ( height ( i + 1 ) == h - 1 && n <= ( ( 2 * i ) + 1 ) ) { arrlist . Add ( arr [ i ] ) ; } else { break ; } } printLeafNodes ( arrlist ) ; } static void printLeafNodes ( List < int > arrlist ) { for ( int i = arrlist . Count - 1 ; i >= 0 ; i -- ) { Console . Write ( arrlist [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ; findLeafNodes ( arr , arr . Length ) ; } }
Count of elements which cannot form any pair whose sum is power of 2 | C # program to count of array elements which do not form a pair with sum equal to a power of 2 with any other array element ; Function to calculate and return the count of elements ; Stores the frequencies of every array element ; Stores the count of removals ; For every element , check if it can form a sum equal to any power of 2 with any other element ; Store Math . Pow ( 2 , j ) - a [ i ] ; Check if s is present in the array ; If frequency of s exceeds 1 ; If s has frequency 1 but is different from a [ i ] ; Pair possible ; If no pair possible for the current element ; Return the answer ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int powerOfTwo ( int [ ] a , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( a [ i ] ) ) { mp [ a [ i ] ] = mp [ a [ i ] ] + 1 ; } else { mp . Add ( a [ i ] , 1 ) ; } } int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { bool f = false ; for ( int j = 0 ; j < 31 ; j ++ ) { int s = ( 1 << j ) - a [ i ] ; if ( mp . ContainsKey ( s ) && ( mp [ s ] > 1 || mp [ s ] == 1 && s != a [ i ] ) ) f = true ; } if ( f == false ) count ++ ; } return count ; } public static void Main ( String [ ] args ) { int [ ] a = { 6 , 2 , 11 } ; int n = a . Length ; Console . Write ( powerOfTwo ( a , n ) ) ; } }
Construct the Array using given bitwise AND , OR and XOR | C # program for the above approach ; Function to find the array ; Loop through all bits in number ; If bit is set in AND then set it in every element of the array ; If bit is not set in AND ; But set in b ( OR ) ; Set bit position in first element ; If bit is not set in c then set it in second element to keep xor as zero for bit position ; Calculate AND , OR and XOR of array ; Check if values are equal or not ; If not , then array is not possible ; Driver code ; Given Bitwise AND , OR , and XOR ; Function Call
using System ; class GFG { static void findArray ( int n , int a , int b , int c ) { int [ ] arr = new int [ n + 1 ] ; for ( int bit = 30 ; bit >= 0 ; bit -- ) { int set = a & ( 1 << bit ) ; if ( set != 0 ) { for ( int i = 0 ; i < n ; i ++ ) arr [ i ] |= set ; } else { if ( ( b & ( 1 << bit ) ) != 0 ) { arr [ 0 ] |= ( 1 << bit ) ; if ( ( c & ( 1 << bit ) ) == 0 ) { arr [ 1 ] |= ( 1 << bit ) ; } } } } int aa = int . MaxValue , bb = 0 , cc = 0 ; for ( int i = 0 ; i < n ; i ++ ) { aa &= arr [ i ] ; bb |= arr [ i ] ; cc ^= arr [ i ] ; } if ( a == aa && b == bb && c == cc ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } else Console . WriteLine ( " - 1" ) ; } public static void Main ( String [ ] args ) { int n = 3 , a = 4 , b = 6 , c = 6 ; findArray ( n , a , b , c ) ; } }
Minimum XOR of OR and AND of any pair in the Array | C # program to for above approach ; Function to find the minimum value of XOR of AND and OR of any pair in the given array ; Sort the array ; Traverse the array arr [ ] ; Compare and Find the minimum XOR value of an array . ; Return the final answer ; Driver Code ; Given array arr [ ] ; Function Call
using System ; class GFG { static int maxAndXor ( int [ ] arr , int n ) { int ans = 9999999 ; Array . Sort ( arr ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { ans = Math . Min ( ans , arr [ i ] ^ arr [ i + 1 ] ) ; } return ans ; } public static void Main ( ) { int [ ] arr = new int [ ] { 1 , 2 , 3 , 4 , 5 } ; int N = arr . Length ; Console . Write ( maxAndXor ( arr , N ) ) ; } }