codeparrot/xlcost-text-to-code huggingface dataset:
Find triplets in an array whose AND is maximum | CPP program to find triplet with maximum bitwise AND . ; Flag Array initially set to true for all numbers ; 2D array for bit representation of all the numbers . Initially all bits are set to 0. ; Finding bit representation of every number and storing it in bits array . ; Checking last bit of the number ; Dividing number by 2. ; maximum And number initially 0. ; Traversing the 2d binary representation . 0 th index represents 32 th bits while 32 th index represents 0 th bit . ; If cnt greater than 3 then ( 32 - i ) th bits of the number will be set . ; Setting flags of the numbers whose ith bit is not set . ; Counting the numbers whose flag are true . ; Driver code
#include " cmath " NEW_LINE #include " cstring " NEW_LINE #include " iostream " NEW_LINE using namespace std ; int maxTriplet ( int a [ ] , int n ) { bool f [ n ] ; memset ( f , true , sizeof ( f ) ) ; int bits [ n ] [ 33 ] ; memset ( bits , 0 , sizeof ( bits ) ) ; for ( int i = 0 ; i < n ; ++ i ) { int num = a [ i ] ; int j = 32 ; while ( num ) { if ( num & 1 ) { bits [ i ] [ j ] = 1 ; } j -- ; num >>= 1 ; } } long long ans = 0 ; for ( long long i = 0 ; i <= 32 ; ++ i ) { int cnt = 0 ; for ( int j = 0 ; j < n ; ++ j ) { if ( bits [ j ] [ i ] and f [ j ] ) { cnt ++ ; } } if ( cnt >= 3 ) { ans += pow ( 2LL , 32 - i ) ; for ( int j = 0 ; j < n ; ++ j ) { if ( ! bits [ j ] [ i ] ) { f [ j ] = false ; } } } } int cnt = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( f [ i ] ) { cnt ++ ; } } long long NumberOfTriplets = ( cnt * ( cnt - 1 ) * ( cnt - 2 ) ) / 6 ; cout << NumberOfTriplets << " ▁ " << ans ; } int main ( int argc , char const * argv [ ] ) { int a [ ] = { 4 , 11 , 10 , 15 , 26 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; maxTriplet ( a , n ) ; return 0 ; }
Find bitwise OR of all possible sub | C ++ program to find OR of all the sub - arrays ; function to return OR of sub - arrays ; Driver program ; print OR of all subarrays
#include <bits/stdc++.h> NEW_LINE using namespace std ; int OR ( int a [ ] , int n ) { int ans = a [ 0 ] ; for ( int i = 1 ; i < n ; ++ i ) ans |= a [ i ] ; return ans ; } int main ( ) { int a [ ] = { 1 , 4 , 6 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << OR ( a , n ) ; return 0 ; }
Maximum set bit sum in array without considering adjacent elements | C ++ program to maximum set bit sum in array without considering adjacent elements ; Function to count total number of set bits in an integer ; Maximum sum of set bits ; Calculate total number of set bits for every element of the array ; find total set bits for each number and store back into the array ; current max excluding i ; current max including i ; return max of incl and excl ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int bit ( int n ) { int count = 0 ; while ( n ) { count ++ ; n = n & ( n - 1 ) ; } return count ; } int maxSumOfBits ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = bit ( arr [ i ] ) ; } int incl = arr [ 0 ] ; int excl = 0 ; int excl_new ; for ( int i = 1 ; i < n ; i ++ ) { excl_new = ( incl > excl ) ? incl : excl ; incl = excl + arr [ i ] ; excl = excl_new ; } return ( ( incl > excl ) ? incl : excl ) ; } int main ( ) { int arr [ ] = { 1 , 2 , 4 , 5 , 6 , 7 , 20 , 25 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxSumOfBits ( arr , n ) ; return 0 ; }
Increment a number without using ++ or + | CPP program to increment an unsigned char using bitwise operators . ; function that increment the value . ; Invert bits and apply negative sign ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; char increment ( unsigned char i ) { i = - ( ~ i ) ; return i ; } int main ( ) { char n = ' a ' ; cout << increment ( n ) ; return 0 ; }
Count pairs with Bitwise XOR as ODD number | C ++ program to count pairs with XOR giving a odd number ; Function to count number of odd pairs ; find all pairs ; return number of odd pair ; Driver Code ; calling function findOddPair and print number of odd pair
#include <iostream> NEW_LINE using namespace std ; int findOddPair ( int A [ ] , int N ) { int i , count = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( A [ i ] % 2 == 0 ) count ++ ; } return count * ( N - count ) ; } int main ( ) { int a [ ] = { 5 , 4 , 7 , 2 , 1 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << findOddPair ( a , n ) << endl ; return 0 ; }
Bitwise OR ( or | ) of a range | C ++ Program to find the bitwise OR of all the integers in range L - R ; Returns the Most Significant Bit Position ( MSB ) ; Returns the Bitwise OR of all integers between L and R ; Find the MSB position in L ; Find the MSB position in R ; Add this value until msb_p1 and msb_p2 are same ; ; Calculate msb_p1 and msb_p2 ; Find the max of msb_p1 and msb_p2 ; Set all the bits from msb_p1 upto 0 th bit in the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MSBPosition ( long long int N ) { int msb_p = -1 ; while ( N ) { N = N >> 1 ; msb_p ++ ; } return msb_p ; } long long int findBitwiseOR ( long long int L , long long int R ) { long long int res = 0 ; int msb_p1 = MSBPosition ( L ) ; int msb_p2 = MSBPosition ( R ) ; while ( msb_p1 == msb_p2 ) { long long int res_val = ( 1 << msb_p1 ) ; res += res_val ; L -= res_val ; R -= res_val ; msb_p1 = MSBPosition ( L ) ; msb_p2 = MSBPosition ( R ) ; } msb_p1 = max ( msb_p1 , msb_p2 ) ; for ( int i = msb_p1 ; i >= 0 ; i -- ) { long long int res_val = ( 1 << i ) ; res += res_val ; } return res ; } int main ( ) { int L = 12 , R = 18 ; cout << findBitwiseOR ( L , R ) << endl ; return 0 ; }
Maximize the bitwise OR of an array | C ++ program to maximize the Bitwise OR Sum in given array ; Function to maximize the bitwise OR sum ; Compute x ^ k ; Find prefix bitwise OR ; Find suffix bitwise OR ; Find maximum OR value ; Drivers code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxOR ( long long arr [ ] , int n , int k , int x ) { long long preSum [ n + 1 ] , suffSum [ n + 1 ] ; long long res , pow = 1 ; for ( int i = 0 ; i < k ; i ++ ) pow *= x ; preSum [ 0 ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) preSum [ i + 1 ] = preSum [ i ] | arr [ i ] ; suffSum [ n ] = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) suffSum [ i ] = suffSum [ i + 1 ] | arr [ i ] ; res = 0 ; for ( int i = 0 ; i < n ; i ++ ) res = max ( res , preSum [ i ] | ( arr [ i ] * pow ) suffSum [ i + 1 ] ) ; return res ; } int main ( ) { long long arr [ ] = { 1 , 2 , 4 , 8 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 2 , x = 3 ; cout << maxOR ( arr , n , k , x ) << " STRNEWLINE " ; return 0 ; }
How to turn on a particular bit in a number ? | CPP program to turn on a particular bit ; Returns a number that has all bits same as n except the k 'th bit which is made 1 ; k must be greater than 0 ; Do | of n with a number with all unset bits except the k 'th bit ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; int turnOnK ( int n , int k ) { if ( k <= 0 ) return n ; return ( n | ( 1 << ( k - 1 ) ) ) ; } int main ( ) { int n = 4 ; int k = 2 ; cout << turnOnK ( n , k ) ; return 0 ; }
Minimum sum of two numbers formed from digits of an array | C ++ program to find minimum sum of two numbers formed from all digits in a given array . ; Returns sum of two numbers formed from all digits in a [ ] ; sort the elements ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minSum ( int a [ ] , int n ) { sort ( a , a + n ) ; int num1 = 0 ; int num2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) num1 = num1 * 10 + a [ i ] ; else num2 = num2 * 10 + a [ i ] ; } return num2 + num1 ; } int main ( ) { int arr [ ] = { 5 , 3 , 0 , 7 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " The ▁ required ▁ sum ▁ is ▁ " << minSum ( arr , n ) << endl ; return 0 ; }
Find value of k | CPP program to find k - th bit from right ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printKthBit ( unsigned int n , unsigned int k ) { cout << ( ( n & ( 1 << ( k - 1 ) ) ) >> ( k - 1 ) ) ; } int main ( ) { unsigned int n = 13 , k = 2 ; printKthBit ( n , k ) ; return 0 ; }
Disjoint Set Union on trees | Set 1 | CPP code to find maximum subtree such that all nodes are even in weight ; Structure for Edge ; ' id ' : stores parent of a node . ' sz ' : stores size of a DSU tree . ; Function to assign root ; Function to find Union ; Utility function for Union ; Edge between ' u ' and ' v ' ; 0 - indexed nodes ; If weights of both ' u ' and ' v ' are even then we make union of them . ; Function to find maximum size of DSU tree ; Driver code ; Weights of nodes ; Number of nodes in a tree ; Initializing every node as a tree with single node . ; Find maximum size of DSU tree .
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  100010 NEW_LINE struct Edge { int u , v ; } ; int id [ N ] , sz [ N ] ; int Root ( int idx ) { int i = idx ; while ( i != id [ i ] ) id [ i ] = id [ id [ i ] ] , i = id [ i ] ; return i ; } void Union ( int a , int b ) { int i = Root ( a ) , j = Root ( b ) ; if ( i != j ) { if ( sz [ i ] >= sz [ j ] ) { id [ j ] = i , sz [ i ] += sz [ j ] ; sz [ j ] = 0 ; } else { id [ i ] = j , sz [ j ] += sz [ i ] ; sz [ i ] = 0 ; } } } void UnionUtil ( struct Edge e [ ] , int W [ ] , int q ) { for ( int i = 0 ; i < q ; i ++ ) { int u , v ; u = e [ i ] . u , v = e [ i ] . v ; u -- , v -- ; if ( W [ u ] % 2 == 0 && W [ v ] % 2 == 0 ) Union ( u , v ) ; } } int findMax ( int n , int W [ ] ) { int maxi = 0 ; for ( int i = 1 ; i <= n ; i ++ ) if ( W [ i ] % 2 == 0 ) maxi = max ( maxi , sz [ i ] ) ; return maxi ; } int main ( ) { int W [ ] = { 1 , 2 , 6 , 4 , 2 , 0 , 3 } ; int n = sizeof ( W ) / sizeof ( W [ 0 ] ) ; for ( int i = 0 ; i < n ; i ++ ) id [ i ] = i , sz [ i ] = 1 ; Edge e [ ] = { { 1 , 2 } , { 1 , 3 } , { 2 , 4 } , { 2 , 5 } , { 4 , 6 } , { 6 , 7 } } ; int q = sizeof ( e ) / sizeof ( e [ 0 ] ) ; UnionUtil ( e , W , q ) ; int maxi = findMax ( n , W ) ; printf ( " Maximum ▁ size ▁ of ▁ the ▁ subtree ▁ with ▁ " ) ; printf ( " even ▁ weighted ▁ nodes ▁ = ▁ % d STRNEWLINE " , maxi ) ; return 0 ; }
Odd numbers in N | CPP code to find the count of odd numbers in n - th row of Pascal 's Triangle ; Function to get no of set bits in binary representation of positive integer n ; Count number of 1 's in binary  representation of n. ; Number of odd numbers in n - th row is 2 raised to power the count . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSetBits ( int n ) { unsigned int count = 0 ; while ( n ) { count += n & 1 ; n >>= 1 ; } return count ; } int countOfOddsPascal ( int n ) { int c = countSetBits ( n ) ; return pow ( 2 , c ) ; } int main ( ) { int n = 20 ; cout << countOfOddsPascal ( n ) ; return 0 ; }
Queries on XOR of XORs of all subarrays | CPP Program to answer queries on XOR of XORs of all subarray ; Output for each query ; If number of element is even . ; If number of element is odd . ; if l is even ; if l is odd ; Wrapper Function ; Evaluating prefixodd and prefixeven ; Driven Program
#include <bits/stdc++.h> NEW_LINE #define N  100 NEW_LINE using namespace std ; void ansQueries ( int prefeven [ ] , int prefodd [ ] , int l , int r ) { if ( ( r - l + 1 ) % 2 == 0 ) cout << "0" ; else { if ( l % 2 == 0 ) cout << ( prefeven [ r ] ^ prefeven [ l - 1 ] ) ; else cout << ( prefodd [ r ] ^ prefodd [ l - 1 ] ) ; } cout << endl ; } void wrapper ( int arr [ ] , int n , int l [ ] , int r [ ] , int q ) { int prefodd [ N ] = { 0 } , prefeven [ N ] = { 0 } ; for ( int i = 1 ; i <= n ; i ++ ) { if ( ( i ) % 2 == 0 ) { prefeven [ i ] = arr [ i - 1 ] ^ prefeven [ i - 1 ] ; prefodd [ i ] = prefodd [ i - 1 ] ; } else { prefeven [ i ] = prefeven [ i - 1 ] ; prefodd [ i ] = prefodd [ i - 1 ] ^ arr [ i - 1 ] ; } } int i = 0 ; while ( i != q ) { query ( prefeven , prefodd , l [ i ] , r [ i ] ) ; i ++ ; } } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int l [ ] = { 1 , 1 , 2 } ; int r [ ] = { 2 , 3 , 4 } ; int q = sizeof ( l ) / sizeof ( l [ 0 ] ) ; ansQueries ( arr , n , l , r , q ) ; return 0 ; }
Variation in Nim Game | CPP program for the variation in nim game ; Function to return final grundy Number ( G ) of game ; if pile size is odd ; We XOR pile size + 1 ; We XOR pile size - 1 ; driver program ; Game with 3 piles ; pile with different sizes ; Function to return result of game ; if ( res == 0 ) if G is zero ; else if G is non zero
#include <bits/stdc++.h> NEW_LINE using namespace std ; int solve ( int p [ ] , int n ) { int G = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( p [ i ] & 1 ) G ^= ( p [ i ] + 1 ) ; G ^= ( p [ i ] - 1 ) ; } return G ; } int main ( ) { int n = 3 ; int p [ 3 ] = { 32 , 49 , 58 } ; int res = solve ( p , n ) ; cout << " Player ▁ 2 ▁ wins " ; cout << " Player ▁ 1 ▁ wins " ; return 0 ; }
Maximum AND value of a pair in an array | CPP Program to find maximum XOR value of a pair ; Utility function to check number of elements having set msb as of pattern ; Function for finding maximum and value pair ; iterate over total of 30 bits from msb to lsb ; find the count of element having set msb ; if count >= 2 set particular bit in result ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int checkBit ( int pattern , int arr [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( ( pattern & arr [ i ] ) == pattern ) count ++ ; return count ; } int maxAND ( int arr [ ] , int n ) { int res = 0 , count ; for ( int bit = 31 ; bit >= 0 ; bit -- ) { count = checkBit ( res | ( 1 << bit ) , arr , n ) ; if ( count >= 2 ) res |= ( 1 << bit ) ; } return res ; } int main ( ) { int arr [ ] = { 4 , 8 , 6 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Maximum ▁ AND ▁ Value ▁ = ▁ " << maxAND ( arr , n ) ; return 0 ; }
Minimum flips to make all 1 s in left and 0 s in right | Set 1 ( Using Bitmask ) | C ++ program to find of flips till that all 1 s in lef ; Function to count minimum number of flips ; This is converting string s into integer of base 2 ( if s = '100' then num = 4 ) ; Initialize minXor with n that can be maximum number of flips ; right shift 1 by ( n - 1 ) bits ; Calculate bitwise XOR of num and mask ; Math . min ( a , b ) returns minimum of a and b return minimum number of flips till that digit ; Function to count number of 1 s ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countones ( long n ) ; int findMiniFlip ( int nums [ ] , int n ) { string s = " " ; for ( int i = 0 ; i < n ; i ++ ) s += nums [ i ] ; char * end ; char tmp [ s . length ( ) ] ; strcpy ( tmp , s . c_str ( ) ) ; long num = strtol ( tmp , & end , 2 ) ; int minXor = n ; long mask = ( 1 << ( n - 1 ) ) ; while ( n - 1 > 0 ) { long temp = ( num ^ mask ) ; minXor = min ( minXor , countones ( temp ) ) ; n -- ; mask = ( mask | ( 1 << ( n - 1 ) ) ) ; } return minXor ; } int countones ( long n ) { int c = 0 ; while ( n > 0 ) { n = n & ( n - 1 ) ; c ++ ; } return c ; } int main ( ) { int nums [ ] = { 1 , 0 , 1 , 1 , 0 , 0 , 0 } ; int size = sizeof ( nums ) / sizeof ( nums [ 0 ] ) ; int n = findMiniFlip ( nums , size ) ; cout << n ; }
Check if a number is power of 8 or not | C ++ program to check if a number is power of 8 ; function to check if power of 8 ; calculate log8 ( n ) ; check if i is an integer or not ; driver function
#include <cmath> NEW_LINE #include <iostream> NEW_LINE using namespace std ; bool checkPowerof8 ( int n ) { double i = log ( n ) / log ( 8 ) ; return ( i - trunc ( i ) < 0.000001 ) ; } int main ( ) { int n = 65 ; checkPowerof8 ( n ) ? cout << " Yes " : cout << " No " ; return 0 ; }
Find the n | C ++ program to find n - th palindrome ; utility function which is used to convert binary string into integer ; convert binary string into integer ; function to find nth binary palindrome number ; stores the binary palindrome string ; base case ; add 2 nd binary palindrome string ; runs till the nth binary palindrome number ; remove curr binary palindrome string from queue ; if n == 0 then we find the n 'th binary palindrome  so we return our answer ; calculate length of curr binary palindrome string ; if length is even . so it is our first case we have two choices ; if length is odd . so it is our second case we have only one choice ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int convertStringToInt ( string s ) { int num = 0 ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { num = num * 2 + ( s [ i ] - '0' ) ; } return num ; } int getNthNumber ( int n ) { queue < string > q ; if ( n == 1 ) return 1 ; n -- ; q . push ( "11" ) ; while ( ! q . empty ( ) ) { string curr = q . front ( ) ; q . pop ( ) ; n -- ; if ( ! n ) { return convertStringToInt ( curr ) ; } int mid = curr . size ( ) / 2 ; if ( curr . size ( ) % 2 == 0 ) { string s0 = curr , s1 = curr ; s0 . insert ( mid , "0" ) ; s1 . insert ( mid , "1" ) ; q . push ( s0 ) ; q . push ( s1 ) ; } else { string ch ( 1 , curr [ mid ] ) ; string temp = curr ; temp . insert ( mid , ch ) ; q . push ( temp ) ; } } return 0 ; } int main ( ) { int n = 9 ; cout << getNthNumber ( n ) ; }
Check if binary representation of a given number and its complement are anagram | An efficient C ++ program to check if binary representations of a number and it 's complement are anagram. ; Returns true if binary representations of a and b are anagram . ; _popcnt64 ( a ) gives number of 1 's present  in binary representation of a. If number  of 1s is half of total bits, return true. ; Driver code
#include <bits/stdc++.h> NEW_LINE #define ull  unsigned long long int NEW_LINE using namespace std ; const int ULL_SIZE = 8 * sizeof ( ull ) ; bool bit_anagram_check ( ull a ) { return ( _popcnt64 ( a ) == ( ULL_SIZE >> 1 ) ) ; } int main ( ) { ull a = 4294967295 ; cout << bit_anagram_check ( a ) << endl ; return 0 ; }
Sum of numbers with exactly 2 bits set | C ++ program to find sum of numbers upto n whose 2 bits are set ; To calculate sum of numbers ; Find numbers whose 2 bits are set ; If number is greater then n we don 't include this in sum ; Return sum of numbers ; Driver program to test findSum ( )
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSum ( int n ) { int sum = 0 ; for ( int i = 1 ; ( 1 << i ) < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { int num = ( 1 << i ) + ( 1 << j ) ; if ( num <= n ) sum += num ; } } return sum ; } int main ( ) { int n = 10 ; cout << findSum ( n ) ; return 0 ; }
Position of rightmost different bit | C ++ implementation to find the position of rightmost different bit in two number . ; function to find rightmost different bit in two numbers . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int posOfRightMostDiffBit ( int m , int n ) { return ffs ( m ^ n ) ; } int main ( ) { int m = 52 , n = 4 ; cout << " Position ▁ = ▁ " << posOfRightMostDiffBit ( m , n ) ; return 0 ; }
Set the K | C ++ implementation to set the kth bit of the given number ; function to set the kth bit ; kth bit of n is being set by this operation ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; int setKthBit ( int n , int k ) { return ( ( 1 << k ) n ) ; } int main ( ) { int n = 10 , k = 2 ; cout << " Kth ▁ bit ▁ set ▁ number ▁ = ▁ " << setKthBit ( n , k ) ; return 0 ; }
Reverse an array without using subtract sign â €˜ | C ++ program to reverse an array without using " - " sign ; Function to reverse array ; Trick to assign - 1 to a variable ; Reverse array in simple manner ; Swap ith index value with ( n - i - 1 ) th index value ; Drivers code ; print the reversed array
#include <bits/stdc++.h> NEW_LINE using namespace std ; void reverseArray ( int arr [ ] , int n ) { int x = ( INT_MIN / INT_MAX ) ; for ( int i = 0 ; i < n / 2 ; i ++ ) swap ( arr [ i ] , arr [ n + ( x * i ) + x ] ) ; } int main ( ) { int arr [ ] = { 5 , 3 , 7 , 2 , 1 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; reverseArray ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; return 0 ; }
Reverse an array without using subtract sign â €˜ | C ++ program to reverse an array without using " - " sign ; Function to reverse array ; Reverse array in simple manner ; Swap ith index value with ( n - i - 1 ) th index value Note : A - B = A + ~ B + 1 So n - i = n + ~ i + 1 then n - i - 1 = ( n + ~ i + 1 ) + ~ 1 + 1 ; Driver code ; print the reversed array
#include <bits/stdc++.h> NEW_LINE using namespace std ; void reverseArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n / 2 ; i ++ ) swap ( arr [ i ] , arr [ ( n + ~ i + 1 ) + ~ 1 + 1 ] ) ; } int main ( ) { int arr [ ] = { 5 , 3 , 7 , 2 , 1 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; reverseArray ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; return 0 ; }
Maximum XOR value of a pair from a range | C / C ++ program to get maximum xor value of two numbers in a range ; method to get maximum xor value in range [ L , R ] ; get xor of limits ; loop to get msb position of L ^ R ; construct result by adding 1 , msbPos times ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxXORInRange ( int L , int R ) { int LXR = L ^ R ; int msbPos = 0 ; while ( LXR ) { msbPos ++ ; LXR >>= 1 ; } int maxXOR = 0 ; int two = 1 ; while ( msbPos -- ) { maxXOR += two ; two <<= 1 ; } return maxXOR ; } int main ( ) { int L = 8 ; int R = 20 ; cout << maxXORInRange ( L , R ) << endl ; return 0 ; }
Numbers whose bitwise OR and sum with N are equal | C ++ program to count numbers whose bitwise OR and sum with N are equal ; Function to find total 0 bit in a number ; Function to find Count of non - negative numbers less than or equal to N , whose bitwise OR and SUM with N are equal . ; count number of zero bit in N ; power of 2 to count ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; unsigned int CountZeroBit ( int n ) { unsigned int count = 0 ; while ( n ) { if ( ! ( n & 1 ) ) count ++ ; n >>= 1 ; } return count ; } int CountORandSumEqual ( int N ) { int count = CountZeroBit ( N ) ; return ( 1 << count ) ; } int main ( ) { int N = 10 ; cout << CountORandSumEqual ( N ) ; return 0 ; }
Count smaller numbers whose XOR with n produces greater value | C ++ program to count numbers whose XOR with n produces a value more than n . ; Position of current bit in n ; int count = 0 ; Initialize result ; If current bit is 0 , then there are 2 ^ k numbers with current bit 1 and whose XOR with n produces greater value ; Increase position for next bit ; Reduce n to find next bit ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countNumbers ( int n ) { int k = 0 ; while ( n > 0 ) { if ( ( n & 1 ) == 0 ) count += pow ( 2 , k ) ; k += 1 ; n >>= 1 ; } return count ; } int main ( ) { int n = 11 ; cout << countNumbers ( n ) << endl ; return 0 ; }
Count all pairs with given XOR | C ++ program to Count all pair with given XOR value x ; Returns count of pairs in arr [ 0. . n - 1 ] with XOR value equals to x . ; create empty set that stores the visiting element of array . Refer below post for details of unordered_set https : www . geeksforgeeks . org / unorderd_set - stl - uses / ; If there exist an element in set s with XOR equals to x ^ arr [ i ] , that means there exist an element such that the XOR of element with arr [ i ] is equal to x , then increment count . ; Make element visited ; return total count of pairs with XOR equal to x ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int xorPairCount ( int arr [ ] , int n , int x ) { unordered_set < int > s ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . find ( x ^ arr [ i ] ) != s . end ( ) ) result ++ ; s . insert ( arr [ i ] ) ; } return result ; } int main ( ) { int arr [ ] = { 5 , 4 , 10 , 15 , 7 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int x = 5 ; cout << " Count ▁ of ▁ pairs ▁ with ▁ given ▁ XOR ▁ = ▁ " << xorPairCount ( arr , n , x ) ; return 0 ; }