codeparrot/xlcost-text-to-code huggingface dataset:
Count Set | CPP 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int CountSetBits ( int n ) { if ( n == 0 ) return 0 ; if ( ( n & 1 ) == 1 ) return 1 + CountSetBits ( n >> 1 ) ; else return CountSetBits ( n >> 1 ) ; } int main ( ) { int n = 21 ; cout << CountSetBits ( n ) << endl ; return 0 ; }
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
#include <bits/stdc++.h> NEW_LINE using namespace std ; 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 ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << pairORSum ( arr , n ) << endl ; return 0 ; }
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
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPrime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i < n ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } void countPrimePosition ( int arr [ ] , int n ) { int c0 = 0 , c1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 0 && isPrime ( i ) ) c0 ++ ; if ( arr [ i ] == 1 && isPrime ( i ) ) c1 ++ ; } cout << " Number ▁ of ▁ 0s ▁ = ▁ " << c0 << endl ; cout << " Number ▁ of ▁ 1s ▁ = ▁ " << c1 ; } int main ( ) { int arr [ ] = { 1 , 0 , 1 , 0 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; countPrimePosition ( arr , n ) ; return 0 ; }
Find a sub matrix with maximum XOR | C ++ program to implement the above approach ; Compute the xor of elements from ( 1 , 1 ) to ( i , j ) and store it in prefix_xor [ i ] [ j ] ; xor of submatrix from 1 , 1 to i , j is ( xor of submatrix from 1 , 1 to i - 1 , j ) ^ ( xor of submatrix from 1 , 1 to i , j - 1 ) ^ ( xor of submatrix from 1 , 1 to i - 1 , j - 1 ) ^ arr [ i ] [ j ] ; find the submatrix with maximum xor value ; we need four loops to find all the submatrix of a matrix ; xor of submatrix from i , j to i1 , j1 is ( xor of submatrix from 1 , 1 to i1 , j1 ) ^ ( xor of submatrix from 1 , 1 to i - 1 , j - 1 ) ^ ( xor of submatrix from 1 , 1 to i1 , j - 1 ) ^ ( xor of submatrix from 1 , 1 to i - 1 , j1 ) ; if the xor is greater than maximum value substitute it ; Driver code ; Find the prefix_xor ; Find submatrix with maximum bitwise xor
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  101 NEW_LINE void prefix ( int arr [ N ] [ N ] , int prefix_xor [ N ] [ N ] , int n ) { for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { prefix_xor [ i ] [ j ] = arr [ i ] [ j ] ^ prefix_xor [ i - 1 ] [ j ] ^ prefix_xor [ i ] [ j - 1 ] ^ prefix_xor [ i - 1 ] [ j - 1 ] ; } } } void Max_xor ( int prefix_xor [ N ] [ N ] , int n ) { int max_value = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { for ( int i1 = i ; i1 <= n ; i1 ++ ) { for ( int j1 = j ; j1 <= n ; j1 ++ ) { int x = 0 ; x ^= prefix_xor [ i1 ] [ j1 ] ; x ^= prefix_xor [ i - 1 ] [ j - 1 ] ; x ^= prefix_xor [ i1 ] [ j - 1 ] ; x ^= prefix_xor [ i - 1 ] [ j1 ] ; max_value = max ( max_value , x ) ; } } } } cout << max_value << endl ; } int main ( ) { int arr [ N ] [ N ] = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; int n = 4 ; int prefix_xor [ N ] [ N ] = { 0 } ; prefix ( arr , prefix_xor , n ) ; Max_xor ( prefix_xor , n ) ; return 0 ; }
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
#include <bits/stdc++.h> NEW_LINE using namespace std ; long multiplyByFifteen ( long n ) { long prod = ( n << 4 ) ; prod = prod - n ; return prod ; } int main ( ) { long n = 7 ; cout << multiplyByFifteen ( n ) ; return 0 ; }
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
#include <bits/stdc++.h> NEW_LINE using namespace std ; long multiplyByFifteen ( long n ) { long prod = ( n << 3 ) ; prod += ( n << 2 ) ; prod += ( n << 1 ) ; prod += n ; return prod ; } int main ( ) { long n = 7 ; cout << multiplyByFifteen ( n ) ; return 0 ; }
Find a number which give minimum sum when XOR with every number of array of integers | C ++ implementation of the 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
#include <bits/stdc++.h> NEW_LINE #include <cmath> NEW_LINE using namespace std ; void findX ( int arr [ ] , int n ) { int * itr = max_element ( arr , arr + n ) ; int p = log2 ( * itr ) + 1 ; int X = 0 ; for ( int i = 0 ; i < p ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ j ] & ( 1 << i ) ) { count ++ ; } } if ( count > ( n / 2 ) ) { X += 1 << i ; } } long long int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += ( X ^ arr [ i ] ) ; cout << " X ▁ = ▁ " << X << " , ▁ Sum ▁ = ▁ " << sum ; } int main ( ) { int arr [ ] = { 2 , 3 , 4 , 5 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findX ( arr , n ) ; return 0 ; }
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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int arr [ 32 ] ; void balTernary ( int ter ) { int carry = 0 , base = 10 ; int i = 32 ; while ( ter > 0 ) { int rem = ter % base ; 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 = ter / base ; } if ( carry == 1 ) arr [ i ] = 1 ; } int ternary ( int number ) { int ans = 0 , rem = 1 , base = 1 ; while ( number > 0 ) { rem = number % 3 ; ans = ans + rem * base ; number /= 3 ; base = base * 10 ; } return ans ; } int main ( ) { int number = 3056 ; int ter = ternary ( number ) ; memset ( arr , 0 , sizeof ( arr ) ) ; balTernary ( ter ) ; int i = 0 ; while ( arr [ i ] == 0 ) { i ++ ; } for ( int j = i ; j <= 32 ; j ++ ) { if ( arr [ j ] == -1 ) cout << ' Z ' ; else cout << arr [ j ] ; } return 0 ; }
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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minAND ( int arr [ ] , int n ) { int s = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { s = s & arr [ i ] ; } cout << ( s ) << endl ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 } ; int n = sizeof ( arr ) / sizeof ( int ) ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( int a [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) if ( a [ i ] ) return true ; return false ; } int main ( ) { int a [ ] = { 0 , 1 , 0 , 1 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; check ( a , n ) ? cout << " YES STRNEWLINE " : cout << " NO STRNEWLINE " ; return 0 ; }
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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; } void findArray ( int q [ ] , int n ) { int arr [ n ] , ans ; 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 ) ; } int main ( ) { int q [ ] = { 4 , 1 , 7 , 0 } ; int n = sizeof ( q ) / sizeof ( q [ 0 ] ) ; findArray ( q , n ) ; return 0 ; }
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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countX ( int n ) { string binary = bitset < 8 > ( n ) . to_string ( ) ; int count = 0 ; for ( int i = 0 ; i < binary . length ( ) ; i ++ ) { if ( binary . at ( i ) == '1' ) count ++ ; } int answer = ( int ) pow ( 2 , count ) ; return answer ; } int main ( ) { int n = 5 ; int answer = countX ( n ) ; cout << ( 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 program to test above function
#include <iostream> NEW_LINE using namespace std ; 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 ) { int 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 ; } int main ( ) { int n = 51 ; if ( isEqualBlock ( n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void binToDecimal3 ( int n ) { int a [ 64 ] = { 0 } ; 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 i = x - 3 ; i < x ; i ++ ) d += a [ i ] * pow ( 2 , p ++ ) ; cout << d << " ▁ " ; d = 0 ; p = 0 ; for ( int i = 0 ; i < 3 ; i ++ ) d += a [ i ] * pow ( 2 , p ++ ) ; cout << d ; } int main ( ) { int n = 86 ; binToDecimal3 ( n ) ; return 0 ; }
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
#include <bits/stdc++.h> NEW_LINE using namespace std ; 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 ) ) ; cout << first_3 << " ▁ " << last_3 ; } int main ( ) { int n = 86 ; binToDecimal3 ( n ) ; return 0 ; }
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
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPowerOfTwo ( int x ) { if ( x == 0 ) return false ; if ( ! ( x & ( x - 1 ) ) ) return true ; else return false ; } 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 ; } int main ( ) { int arr [ ] = { 5 , 6 , 9 , 3 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countNum ( arr , n ) ; return 0 ; }
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
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkEvenParity ( int x ) { int parity = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; parity ++ ; } if ( parity % 2 == 0 ) return true ; else return false ; } 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 ; } int main ( ) { int arr [ ] = { 2 , 4 , 3 , 5 , 9 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << sumlist ( arr , n ) ; return 0 ; }
Range Sum Queries and Update with Square Root | CPP program to calculate sum in an interval and update with square root ; Maximum size of input array ; structure for queries with members type , leftIndex , rightIndex of the query ; function for updating the value ; function for calculating the required sum between two indexes ; function to return answer to queries ; Declaring a Set ; inserting indexes of those numbers which are greater than 1 ; update query ; find the left index of query in the set using binary search ; if it crosses the right index of query or end of set , then break ; update the value of arr [ i ] to its square root ; if updated value becomes equal to 1 remove it from the set ; increment the index ; sum query ; Driver Code ; input array using 1 - based indexing ; declaring array of structure of type queries ; answer the Queries
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 100 ; int BIT [ MAX + 1 ] ; struct queries { int type , l , r ; } ; void update ( int x , int val , int n ) { for ( x ; x <= n ; x += x & - x ) { BIT [ x ] += val ; } } int sum ( int x ) { int s = 0 ; for ( x ; x > 0 ; x -= x & - x ) { s += BIT [ x ] ; } return s ; } void answerQueries ( int arr [ ] , queries que [ ] , int n , int q ) { set < int > s ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > 1 ) s . insert ( i ) ; update ( i , arr [ i ] , n ) ; } for ( int i = 0 ; i < q ; i ++ ) { if ( que [ i ] . type == 1 ) { while ( true ) { auto it = s . lower_bound ( que [ i ] . l ) ; if ( it == s . end ( ) * it > que [ i ] . r ) break ; que [ i ] . l = * it ; update ( * it , ( int ) sqrt ( arr [ * it ] ) - arr [ * it ] , n ) ; arr [ * it ] = ( int ) sqrt ( arr [ * it ] ) ; if ( arr [ * it ] == 1 ) s . erase ( * it ) ; que [ i ] . l ++ ; } } else { cout << ( sum ( que [ i ] . r ) - sum ( que [ i ] . l - 1 ) ) << endl ; } } } int main ( ) { int q = 4 ; int arr [ ] = { 0 , 4 , 5 , 1 , 2 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; queries que [ q + 1 ] ; que [ 0 ] . type = 2 , que [ 0 ] . l = 1 , que [ 0 ] . r = 5 ; que [ 1 ] . type = 1 , que [ 1 ] . l = 1 , que [ 1 ] . r = 2 ; que [ 2 ] . type = 1 , que [ 2 ] . l = 2 , que [ 2 ] . r = 4 ; que [ 3 ] . type = 2 , que [ 3 ] . l = 1 , que [ 3 ] . r = 5 ; answerQueries ( arr , que , n , q ) ; return 0 ; }
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
#include <iostream> NEW_LINE using namespace std ; 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 ; } int main ( ) { int A [ ] = { 5 , 6 , 2 , 8 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << findOddPair ( A , N ) << endl ; return 0 ; }
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
#include <iostream> NEW_LINE using namespace std ; 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 ; } int main ( ) { int A [ ] = { 5 , 4 , 7 , 2 , 1 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << findevenPair ( A , N ) << endl ; return 0 ; }
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
#include <iostream> NEW_LINE using namespace std ; 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 ; } int main ( ) { int a [ ] = { 5 , 4 , 7 , 2 , 1 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << findEvenPair ( a , n ) << endl ; return 0 ; }
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
#include <iostream> NEW_LINE using namespace std ; 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 ; } int main ( ) { int a [ ] = { 5 , 1 , 3 , 2 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << findevenPair ( a , n ) << endl ; return 0 ; }
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
#include <iostream> NEW_LINE using namespace std ; 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 ; } int main ( ) { int a [ ] = { 5 , 1 , 3 , 2 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << findevenPair ( a , n ) << endl ; return 0 ; }
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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calculate ( int X ) { int number_of_bits = 8 ; return ( ( 1 << number_of_bits ) - 1 ) ^ X ; } int main ( ) { int X = 4 ; cout << " Required ▁ Number ▁ is ▁ : ▁ " << calculate ( X ) << endl ; return 0 ; }
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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int xorGivenSetBits ( int arr [ ] , int n , int k ) { vector < int > v ; for ( int i = 0 ; i < n ; i ++ ) { if ( __builtin_popcount ( arr [ i ] ) == k ) { v . push_back ( arr [ i ] ) ; } } int result = v [ 0 ] ; for ( int i = 1 ; i < v . size ( ) ; i ++ ) result = result ^ v [ i ] ; return result ; } int main ( ) { int arr [ ] = { 2 , 13 , 1 , 19 , 7 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 3 ; cout << xorGivenSetBits ( arr , n , k ) ; return 0 ; }
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 .
#include <bits/stdc++.h> NEW_LINE using namespace std ; 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 ] ; } } int main ( ) { int arr [ ] = { 2 , 3 , 3 , 5 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; ReplaceElements ( arr , n ) ; for ( int i = 0 ; i < n ; ++ i ) { cout << arr [ i ] << " ▁ " ; } return 0 ; }
Find consecutive 1 s of length >= n in binary representation of a number | C ++ implementation of above approach ; Function to count the number of leading zeros ; Function to find the string of n consecutive 1 's ; Initialize position to return . ; Skip leading 0 's ; Set position after leading 0 's ; Count first group of 1 's. ; If length of consecutive 1 's  is greater than or equal to n ; Not enough 1 's  skip over to next group ; Update the position ; if no string is found ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countLeadingZeros ( int x ) { unsigned y ; int n ; n = 32 ; y = x >> 16 ; if ( y != 0 ) { n = n - 16 ; x = y ; } y = x >> 8 ; if ( y != 0 ) { n = n - 8 ; x = y ; } y = x >> 4 ; if ( y != 0 ) { n = n - 4 ; x = y ; } y = x >> 2 ; if ( y != 0 ) { n = n - 2 ; x = y ; } y = x >> 1 ; if ( y != 0 ) return n - 2 ; return n - x ; } int FindStringof1s ( unsigned x , int n ) { int k , p ; p = 0 ; while ( x != 0 ) { k = countLeadingZeros ( x ) ; x = x << k ; p = p + k ; k = countLeadingZeros ( ~ x ) ; if ( k >= n ) return p + 1 ; x = x << k ; p = p + k ; } return -1 ; } int main ( ) { int x = 35 ; int n = 2 ; cout << FindStringof1s ( x , n ) ; }
Assign other value to a variable from two possible values | CPP program to change value of x according to its current value . ; Function to alternate the values ; Main function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void alternate ( int & a , int & b , int & x ) { x = a ^ b ^ x ; } int main ( ) { int a = -10 ; int b = 15 ; int x = a ; cout << " x ▁ is ▁ : ▁ " << x ; alternate ( a , b , x ) ; cout << " After exchange " cout << " STRNEWLINE x ▁ is ▁ : ▁ " << x ; return 0 ; }