codeparrot/xlcost-text-to-code huggingface dataset:
Check if all the set bits of the binary representation of N are at least K places away | C ++ program to check if all the set bits of the binary representation of N are at least K places away . ; Initialize check and count with 0 ; The i - th bit is a set bit ; This is the first set bit so , start calculating all the distances between consecutive bits from here ; If count is less than K return false ; Adding the count as the number of zeroes increase between set bits ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool CheckBits ( int N , int K ) { int check = 0 ; int count = 0 ; for ( int i = 31 ; i >= 0 ; i -- ) { if ( ( 1 << i ) & N ) { if ( check == 0 ) { check = 1 ; } else { if ( count < K ) { return false ; } } count = 0 ; } else { count ++ ; } } return true ; } int main ( ) { int N = 5 ; int K = 1 ; if ( CheckBits ( N , K ) ) { cout << " YES " ; } else { cout << " NO " ; } return 0 ; }
Minimize bits to be flipped in X and Y such that their Bitwise OR is equal to Z | C ++ Program to minimize number of bits to be fipped in X or Y such that their bitwise or is equal to minimize ; This function returns minimum number of bits to be flipped in X and Y to make X | Y = Z ; If current bit in Z is set and is also set in either of X or Y or both ; If current bit in Z is set and is unset in both X and Y ; Set that bit in either X or Y ; If current bit in Z is unset and is set in both X and Y ; Unset the bit in both X and Y ; If current bit in Z is unset and is set in either X or Y ; Unset that set bit ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumFlips ( int X , int Y , int Z ) { int res = 0 ; while ( X > 0 Y > 0 Z > 0 ) { if ( ( ( X & 1 ) || ( Y & 1 ) ) && ( Z & 1 ) ) { X = X >> 1 ; Y = Y >> 1 ; Z = Z >> 1 ; continue ; } else if ( ! ( X & 1 ) && ! ( Y & 1 ) && ( Z & 1 ) ) { res ++ ; } else if ( ( X & 1 ) || ( Y & 1 ) == 1 ) { if ( ( X & 1 ) && ( Y & 1 ) && ! ( Z & 1 ) ) { res += 2 ; } else if ( ( ( X & 1 ) || ( Y & 1 ) ) && ! ( Z & 1 ) ) { res ++ ; } } X = X >> 1 ; Y = Y >> 1 ; Z = Z >> 1 ; } return res ; } int main ( ) { int X = 5 , Y = 8 , Z = 6 ; cout << minimumFlips ( X , Y , Z ) ; return 0 ; }
Count subsequences with same values of Bitwise AND , OR and XOR | function for finding count of possible subsequence ; creating a map to count the frequency of each element ; store frequency of each element ; iterate through the map ; add all possible combination for key equal zero ; add all ( odd number of elements ) possible combination for key other than zero ; driver function
int countSubseq ( int arr [ ] , int n ) { int count = 0 ; unordered_map < int , int > mp ; for ( int i = 0 ; i < n ; i ++ ) mp [ arr [ i ] ] ++ ; for ( auto i : mp ) { if ( i . first == 0 ) count += pow ( 2 , i . second ) - 1 ; else count += pow ( 2 , i . second - 1 ) ; } return count ; } int main ( ) { int arr [ ] = { 2 , 2 , 2 , 5 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countSubseq ( arr , n ) ; return 0 ; }
Choose an integer K such that maximum of the xor values of K with all Array elements is minimized | C ++ implementation to find Minimum possible value of the maximum xor in an array by choosing some integer ; Function to calculate Minimum possible value of the Maximum XOR in an array ; base case ; Divide elements into two sections ; Traverse all elements of current section and divide in two groups ; Check if one of the sections is empty ; explore both the possibilities using recursion ; Function to calculate minimum XOR value ; Start recursion from the most significant pos position ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calculate ( vector < int > & section , int pos ) { if ( pos < 0 ) return 0 ; vector < int > on_section , off_section ; for ( auto el : section ) { if ( ( ( el >> pos ) & 1 ) == 0 ) off_section . push_back ( el ) ; else on_section . push_back ( el ) ; } if ( off_section . size ( ) == 0 ) return calculate ( on_section , pos - 1 ) ; if ( on_section . size ( ) == 0 ) return calculate ( off_section , pos - 1 ) ; return min ( calculate ( off_section , pos - 1 ) , calculate ( on_section , pos - 1 ) ) + ( 1 << pos ) ; } int minXorValue ( int a [ ] , int n ) { vector < int > section ; for ( int i = 0 ; i < n ; i ++ ) section . push_back ( a [ i ] ) ; return calculate ( section , 30 ) ; } int main ( ) { int N = 4 ; int A [ N ] = { 3 , 2 , 5 , 6 } ; cout << minXorValue ( A , N ) ; return 0 ; }
Program to find the Nth natural number with exactly two bits set | C ++ Code to find the Nth number with exactly two bits set ; Function to find the Nth number with exactly two bits set ; Keep incrementing until we reach the partition of ' N ' stored in bit_L ; set the rightmost bit based on bit_R ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findNthNum ( long long int N ) { long long int bit_L = 1 , last_num = 0 ; while ( bit_L * ( bit_L + 1 ) / 2 < N ) { last_num = last_num + bit_L ; bit_L ++ ; } int bit_R = N - last_num - 1 ; cout << ( 1 << bit_L ) + ( 1 << bit_R ) << endl ; } int main ( ) { long long int N = 13 ; findNthNum ( N ) ; return 0 ; }
XOR of all Prime numbers in an Array at positions divisible by K | C ++ program to find XOR of all Prime numbers in an Array at positions divisible by K ; 0 and 1 are not prime numbers ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Function to find the required XOR ; To store XOR of the primes ; Traverse the array ; If the number is a prime ; If index is divisible by k ; Print the xor ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  1000005 NEW_LINE void SieveOfEratosthenes ( vector < bool > & prime ) { prime [ 1 ] = false ; prime [ 0 ] = false ; for ( int p = 2 ; p * p < MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i < MAX ; i += p ) prime [ i ] = false ; } } } void prime_xor ( int arr [ ] , int n , int k ) { vector < bool > prime ( MAX , true ) ; SieveOfEratosthenes ( prime ) ; long long int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { if ( ( i + 1 ) % k == 0 ) { ans ^= arr [ i ] ; } } } cout << ans << endl ; } int main ( ) { int arr [ ] = { 2 , 3 , 5 , 7 , 11 , 8 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 2 ; prime_xor ( arr , n , K ) ; return 0 ; }
Find XOR of all elements in an Array | C ++ program to find the XOR of all elements in the array ; Function to find the XOR of all elements in the array ; Resultant variable ; Iterating through every element in the array ; Find XOR with the result ; Return the XOR ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int xorOfArray ( int arr [ ] , int n ) { int xor_arr = 0 ; for ( int i = 0 ; i < n ; i ++ ) { xor_arr = xor_arr ^ arr [ i ] ; } return xor_arr ; } int main ( ) { int arr [ ] = { 3 , 9 , 12 , 13 , 15 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << xorOfArray ( arr , n ) << endl ; return 0 ; }
Count of even set bits between XOR of two arrays | C ++ program for the above approach ; Function that count the XOR of B [ ] with all the element in A [ ] having even set bit ; Count the set bits in A [ i ] ; check for even or Odd ; To store the count of element for B [ ] such that XOR with all the element in A [ ] having even set bit ; Count set bit for B [ i ] ; check for Even or Odd ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countEvenBit ( int A [ ] , int B [ ] , int n , int m ) { int i , j , cntOdd = 0 , cntEven = 0 ; for ( i = 0 ; i < n ; i ++ ) { int x = __builtin_popcount ( A [ i ] ) ; if ( x & 1 ) { cntEven ++ ; } else { cntOdd ++ ; } } int CountB [ m ] ; for ( i = 0 ; i < m ; i ++ ) { int x = __builtin_popcount ( B [ i ] ) ; if ( x & 1 ) { CountB [ i ] = cntEven ; } else { CountB [ i ] = cntOdd ; } } for ( i = 0 ; i < m ; i ++ ) { cout << CountB [ i ] << ' ▁ ' ; } } int main ( ) { int A [ ] = { 4 , 2 , 15 , 9 , 8 , 8 } ; int B [ ] = { 3 , 4 , 22 } ; countEvenBit ( A , B , 6 , 3 ) ; return 0 ; }
Check if a Number is Odd or Even using Bitwise Operators | C ++ program to check for even or odd using Bitwise XOR operator ; Returns true if n is even , else odd ; n ^ 1 is n + 1 , then even , else odd ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool isEven ( int n ) { if ( n ^ 1 == n + 1 ) return true ; else return false ; } int main ( ) { int n = 100 ; isEven ( n ) ? cout << " Even " : cout << " Odd " ; return 0 ; }
Bitwise OR ( | ) of all even number from 1 to N | C ++ implementation of the above approach ; Function to return the bitwise OR of all the even numbers upto N ; Initialize result as 2 ; Driver code
#include <iostream> NEW_LINE using namespace std ; int bitwiseOrTillN ( int n ) { int result = 2 ; for ( int i = 4 ; i <= n ; i = i + 2 ) { result = result | i ; } return result ; } int main ( ) { int n = 10 ; cout << bitwiseOrTillN ( n ) ; return 0 ; }
Bitwise OR ( | ) of all even number from 1 to N | C ++ implementation of the above approach ; Function to return the bitwise OR of all even numbers upto N ; For value less than 2 ; Count total number of bits in bitwise or all bits will be set except last bit ; Compute 2 to the power bitCount and subtract 2 ; Driver code
#include <iostream> NEW_LINE #include <math.h> NEW_LINE using namespace std ; int bitwiseOrTillN ( int n ) { if ( n < 2 ) return 0 ; int bitCount = log2 ( n ) + 1 ; return pow ( 2 , bitCount ) - 2 ; } int main ( ) { int n = 10 ; cout << bitwiseOrTillN ( n ) ; return 0 ; }
Generating N | C ++ program Generating N - bit Gray Code starting from K ; Function to Generating N - bit Gray Code starting from K ; Generate gray code of corresponding binary number of integer i . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void genSequence ( int n , int val ) { for ( int i = 0 ; i < ( 1 << n ) ; i ++ ) { int x = i ^ ( i >> 1 ) ^ val ; cout << x << " ▁ " ; } } int main ( ) { int n = 3 , k = 2 ; genSequence ( n , k ) ; return 0 ; }
Winner in the Rock | C ++ implementation of the approach ; Function to return the winner of the game ; Both the players chose to play the same move ; Player A wins the game ; Function to perform the queries ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string winner ( string moves ) { map < char , int > data ; data [ ' R ' ] = 0 ; data [ ' P ' ] = 1 ; data [ ' S ' ] = 2 ; if ( moves [ 0 ] == moves [ 1 ] ) { return " Draw " ; } if ( ( ( data [ moves [ 0 ] ] | 1 << ( 2 ) ) - ( data [ moves [ 1 ] ] | 0 << ( 2 ) ) ) % 3 ) { return " A " ; } return " B " ; } void performQueries ( string arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) cout << winner ( arr [ i ] ) << endl ; } int main ( ) { string arr [ ] = { " RS " , " SR " , " SP " , " PP " } ; int n = sizeof ( arr ) / sizeof ( string ) ; performQueries ( arr , n ) ; return 0 ; }
Find the minimum spanning tree with alternating colored edges | C ++ program for the above approach ; Initializing dp of size = ( 2 ^ 18 ) * 18 * 2. ; Recursive Function to calculate Minimum Cost with alternate colour edges ; Base case ; If already calculated ; Masking previous edges as explained in above formula . ; Function to Adjacency List Representation of a Graph ; Function to getCost for the Minimum Spanning Tree Formed ; Assigning maximum possible value . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int graph [ 18 ] [ 18 ] [ 2 ] ; long long dp [ 1 << 18 ] [ 18 ] [ 2 ] ; long long minCost ( int n , int m , int mask , int prev , int col ) { if ( mask == ( ( 1 << n ) - 1 ) ) { return 0 ; } if ( dp [ mask ] [ prev ] [ col == 1 ] != 0 ) { return dp [ mask ] [ prev ] [ col == 1 ] ; } long long ans = 1e9 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < 2 ; j ++ ) { if ( graph [ prev ] [ i ] [ j ] && ! ( mask & ( 1 << i ) ) && ( j != col ) ) { long long z = graph [ prev ] [ i ] [ j ] + minCost ( n , m , mask | ( 1 << i ) , i , j ) ; ans = min ( z , ans ) ; } } } return dp [ mask ] [ prev ] [ col == 1 ] = ans ; } void makeGraph ( vector < pair < pair < int , int > , pair < int , char > > > & vp , int m ) { for ( int i = 0 ; i < m ; i ++ ) { int a = vp [ i ] . first . first - 1 ; int b = vp [ i ] . first . second - 1 ; int cost = vp [ i ] . second . first ; char color = vp [ i ] . second . second ; graph [ a ] [ b ] [ color == ' W ' ] = cost ; graph [ b ] [ a ] [ color == ' W ' ] = cost ; } } int getCost ( int n , int m ) { long long ans = 1e9 ; for ( int i = 0 ; i < n ; i ++ ) { ans = min ( ans , minCost ( n , m , 1 << i , i , 2 ) ) ; } if ( ans != 1e9 ) { return ans ; } else { return -1 ; } } int main ( ) { int n = 3 , m = 4 ; vector < pair < pair < int , int > , pair < int , char > > > vp = { { { 1 , 2 } , { 2 , ' B ' } } , { { 1 , 2 } , { 3 , ' W ' } } , { { 2 , 3 } , { 4 , ' W ' } } , { { 2 , 3 } , { 5 , ' B ' } } } ; makeGraph ( vp , m ) ; cout << getCost ( n , m ) << ' ' ; return 0 ; }
Maximum number of consecutive 1 's in binary representation of all the array elements | C ++ implementation of the approach ; Function to return the count of maximum consecutive 1 s in the binary representation of x ; Initialize result ; Count the number of iterations to reach x = 0. ; This operation reduces length of every sequence of 1 s by one ; Function to return the count of maximum consecutive 1 s in the binary representation among all the elements of arr [ ] ; To store the answer ; For every element of the array ; Count of maximum consecutive 1 s in the binary representation of the current element ; Update the maximum count so far ; Driver code
#include <iostream> NEW_LINE using namespace std ; int maxConsecutiveOnes ( int x ) { int count = 0 ; while ( x != 0 ) { x = ( x & ( x << 1 ) ) ; count ++ ; } return count ; } int maxOnes ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int currMax = maxConsecutiveOnes ( arr [ i ] ) ; ans = max ( ans , currMax ) ; } return ans ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << maxOnes ( arr , n ) ; return 0 ; }
Maximum Bitwise OR pair from a range | C ++ implementation of the approach ; Function to return the maximum bitwise OR possible among all the possible pairs ; Check for every possible pair ; Maximum among all ( i , j ) pairs ; Driver code
#include <climits> NEW_LINE #include <iostream> NEW_LINE using namespace std ; int maxOR ( int L , int R ) { int maximum = INT_MIN ; for ( int i = L ; i < R ; i ++ ) for ( int j = i + 1 ; j <= R ; j ++ ) maximum = max ( maximum , ( i j ) ) ; return maximum ; } int main ( ) { int L = 4 , R = 5 ; cout << maxOR ( L , R ) ; return 0 ; }
Maximum Bitwise OR pair from a range | C ++ implementation of the approach ; Function to return the maximum bitwise OR possible among all the possible pairs ; If there is only a single value in the range [ L , R ] ; Loop through each bit from MSB to LSB ; MSBs where the bits differ , all bits from that bit are set ; If MSBs are same , then ans bit is same as that of bit of right or left limit ; Driver code
#include <iostream> NEW_LINE using namespace std ; const int MAX = 64 ; int maxOR ( int L , int R ) { if ( L == R ) { return L ; } int ans = 0 ; for ( int i = MAX - 1 ; i >= 0 ; i -- ) { int p , lbit , rbit ; p = 1 << i ; if ( ( rbit == 1 ) && ( lbit == 0 ) ) { ans += ( p << 1 ) - 1 ; break ; } if ( rbit == 1 ) { ans += p ; } } return ans ; } int main ( ) { int L = 4 , R = 5 ; cout << maxOR ( L , R ) ; return 0 ; }
Longest subsequence with a given AND value | O ( N ) | C ++ implementation of the approach ; Function to return the required length ; To store the filtered numbers ; Filtering the numbers ; If there are no elements to check ; Find the AND of all the filtered elements ; Check if the AND is equal to m ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findLen ( int * arr , int n , int m ) { vector < int > filter ; for ( int i = 0 ; i < n ; i ++ ) if ( ( arr [ i ] & m ) == m ) filter . push_back ( arr [ i ] ) ; if ( filter . size ( ) == 0 ) return 0 ; int c_and = filter [ 0 ] ; for ( int i = 1 ; i < filter . size ( ) ; i ++ ) c_and &= filter [ i ] ; if ( c_and == m ) return filter . size ( ) ; return 0 ; } int main ( ) { int arr [ ] = { 7 , 3 , 3 , 1 , 3 } ; int n = sizeof ( arr ) / sizeof ( int ) ; int m = 3 ; cout << findLen ( arr , n , m ) ; return 0 ; }
Longest sub | C ++ implementation of the approach ; Function to return the required length ; To store the filtered numbers ; Filtering the numbers ; If there are no elements to check ; Find the OR of all the filtered elements ; Check if the OR is equal to m ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findLen ( int * arr , int n , int m ) { vector < int > filter ; for ( int i = 0 ; i < n ; i ++ ) if ( ( arr [ i ] m ) == m ) filter . push_back ( arr [ i ] ) ; if ( filter . size ( ) == 0 ) return 0 ; int c_or = filter [ 0 ] ; for ( int i = 1 ; i < filter . size ( ) ; i ++ ) c_or |= filter [ i ] ; if ( c_or == m ) return filter . size ( ) ; return 0 ; } int main ( ) { int arr [ ] = { 7 , 3 , 3 , 1 , 3 } ; int n = sizeof ( arr ) / sizeof ( int ) ; int m = 3 ; cout << findLen ( arr , n , m ) ; return 0 ; }
Program to toggle K | C ++ program to toggle K - th bit of a number N ; Function to toggle the kth bit of n ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int toggleBit ( int n , int k ) { return ( n ^ ( 1 << ( k - 1 ) ) ) ; } int main ( ) { int n = 5 , k = 2 ; cout << toggleBit ( n , k ) << endl ; return 0 ; }
Program to clear K | C ++ program to clear K - th bit of a number N ; Function to clear the kth bit of n ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int clearBit ( int n , int k ) { return ( n & ( ~ ( 1 << ( k - 1 ) ) ) ) ; } int main ( ) { int n = 5 , k = 1 ; cout << clearBit ( n , k ) << endl ; return 0 ; }
Maximize the Expression | Bit Manipulation | C ++ implementation of the approach ; Function to return the value of the maximized expression ; int can have 32 bits ; Consider the ith bit of D to be 1 ; Calculate the value of ( B AND bitOfD ) ; Check if bitOfD satisfies ( B AND D = D ) ; Check if bitOfD can maximize ( A XOR D ) ; Note that we do not need to consider ith bit of D to be 0 because if above condition are not satisfied then value of result will not change which is similar to considering bitOfD = 0 as result XOR 0 = result ; Driver code
#include <iostream> NEW_LINE using namespace std ; #define MAX  32 NEW_LINE int maximizeExpression ( int a , int b ) { int result = a ; for ( int bit = MAX - 1 ; bit >= 0 ; bit -- ) { int bitOfD = 1 << bit ; int x = b & bitOfD ; if ( x == bitOfD ) { int y = result & bitOfD ; if ( y == 0 ) { result = result ^ bitOfD ; } } } return result ; } int main ( ) { int a = 11 , b = 14 ; cout << maximizeExpression ( a , b ) ; return 0 ; }
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
#include <bits/stdc++.h> NEW_LINE #define ll  long long int NEW_LINE #define MAX  10 NEW_LINE using namespace std ; int findSubarray ( int array [ ] , int n ) { unordered_map < int , int > mp ; mp . insert ( { 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 . find ( Y ) != mp . end ( ) ) { answer += mp [ Y ] ; } value *= 2 ; } if ( mp . find ( preXor ) != mp . end ( ) ) { mp [ preXor ] ++ ; } else { mp . insert ( { preXor , 1 } ) ; } } return answer ; } int main ( ) { int array [ ] = { 2 , 6 , 7 , 5 , 8 } ; int n = sizeof ( array ) / sizeof ( array [ 0 ] ) ; cout << findSubarray ( array , n ) << endl ; return 0 ; }
Maximum XOR of Two Numbers in an Array | C ++ implementation ; Function to return the maximum xor ; Calculating xor of each pair ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; 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 = max ( maxXor , arr [ i ] ^ arr [ j ] ) ; } } return maxXor ; } int main ( ) { int arr [ ] = { 25 , 10 , 2 , 8 , 5 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << max_xor ( arr , n ) << endl ; return 0 ; }
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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int max_xor ( int arr [ ] , int n ) { int maxx = 0 , mask = 0 ; set < int > se ; for ( int i = 30 ; i >= 0 ; i -- ) { mask |= ( 1 << i ) ; for ( int i = 0 ; i < n ; ++ i ) { se . insert ( arr [ i ] & mask ) ; } int newMaxx = maxx | ( 1 << i ) ; for ( int prefix : se ) { if ( se . count ( newMaxx ^ prefix ) ) { maxx = newMaxx ; break ; } } se . clear ( ) ; } return maxx ; } int main ( ) { int arr [ ] = { 25 , 10 , 2 , 8 , 5 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << max_xor ( arr , n ) << endl ; return 0 ; }
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
#include <bits/stdc++.h> NEW_LINE using namespace std ; 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 ; } int main ( ) { int arr [ ] = { 2 , 5 , 6 , 4 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << CountTriplets ( arr , n ) ; return 0 ; }
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 Program
#include <iostream> NEW_LINE using namespace std ; void findMajority ( int arr [ ] , int n ) { int len = sizeof ( int ) * 8 ; int number = 0 ; for ( int i = 0 ; i < len ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ j ] & ( 1 << i ) ) count ++ ; } if ( count > ( n / 2 ) ) number += ( 1 << i ) ; } int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] == number ) count ++ ; if ( count > ( n / 2 ) ) cout << number ; else cout << " Majority ▁ Element ▁ Not ▁ Present " ; } int main ( ) { int arr [ ] = { 3 , 3 , 4 , 2 , 4 , 4 , 2 , 4 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findMajority ( arr , n ) ; return 0 ; }
Count number of bits to be flipped to convert A to B | Set | C ++ implementation of the 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countBits ( int a , int b ) { int count = 0 ; while ( a b ) { 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 ; } int main ( ) { int a = 10 , b = 7 ; cout << countBits ( a , b ) ; return 0 ; }