codeparrot/xlcost-text-to-code huggingface dataset:
1 to n bit numbers with no consecutive 1 s in binary representation | Print all numbers upto n bits with no consecutive set bits . ; Let us first compute 2 raised to power n . ; loop 1 to n to check all the numbers ; A number i doesn ' t ▁ contain ▁ ▁ consecutive ▁ set ▁ bits ▁ if ▁ ▁ bitwise ▁ and ▁ of ▁ i ▁ and ▁ left ▁ ▁ shifted ▁ i ▁ do ' t contain a commons set bit . ; Driver code
< ? php function printNonConsecutive ( $ n ) { $ p = ( 1 << $ n ) ; for ( $ i = 1 ; $ i < $ p ; $ i ++ ) if ( ( $ i & ( $ i << 1 ) ) == 0 ) echo $ i . " " ; } $ n = 3 ; printNonConsecutive ( $ n ) ; ? >
Find the n | Efficient PHP program to find n - th palindrome ; Construct the nth binary palindrome with the given group number , aux_number and operation type ; No need to insert any bit in the middle ; Length of the final binary representation ; Fill first and last bit as 1 ; Start filling the a [ ] from middle , with the aux_num binary representation ; Get the auxiliary number 's ith bit and  fill around middle ; Insert bit 0 in the middle ; Length of the final binary representation ; Fill first and last bit as 1 ; Start filling the a [ ] from middle , with the aux_num binary representation ; Get the auxiliary number 's ith bit and fill  around middle ; else Insert bit 1 in the middle ; Length of the final binary representation ; Fill first and last bit as 1 ; Start filling the a [ ] from middle , with the aux_num binary representation ; Get the auxiliary number 's ith bit and fill  around middle ; Convert the number to decimal from binary ; Will return the nth binary palindrome number ; Add number of elements in all the groups , until the group of the nth number is found ; Total number of elements until this group ; Element 's offset position in the group ; Finding which bit to be placed in the middle and finding the number , which we will fill from the middle in both directions ; We need to fill this auxiliary number in binary form the middle in both directions ; $op = 0 ; Need to Insert 0 at middle ; $op = 1 ; Need to Insert 1 at middle ; Driver code ; Function Call
< ? php $ INT_SIZE = 32 ; function constructNthNumber ( $ group_no , $ aux_num , $ op ) { global $ INT_SIZE ; $ a = array_fill ( 0 , $ INT_SIZE , 0 ) ; $ num = 0 ; $ i = 0 ; $ len_f = 0 ; if ( $ op == 2 ) { $ len_f = 2 * $ group_no ; $ a [ $ len_f - 1 ] = $ a [ 0 ] = 1 ; while ( $ aux_num ) { $ a [ $ group_no + i ] = $ a [ $ group_no - 1 - $ i ] = $ aux_num & 1 ; $ aux_num = $ aux_num >> 1 ; $ i ++ ; } } else if ( $ op == 0 ) { $ len_f = 2 * $ group_no + 1 ; $ a [ $ len_f - 1 ] = $ a [ 0 ] = 1 ; $ a [ $ group_no ] = 0 ; while ( $ aux_num ) { $ a [ $ group_no + 1 + $ i ] = $ a [ $ group_no - 1 - $ i ] = $ aux_num & 1 ; $ aux_num = $ aux_num >> 1 ; $ i ++ ; } } { $ len_f = 2 * $ group_no + 1 ; $ a [ $ len_f - 1 ] = $ a [ 0 ] = 1 ; $ a [ $ group_no ] = 1 ; while ( $ aux_num ) { $ a [ $ group_no + 1 + $ i ] = $ a [ $ group_no - 1 - $ i ] = $ aux_num & 1 ; $ aux_num = $ aux_num >> 1 ; $ i ++ ; } } for ( $ i = 0 ; $ i < $ len_f ; $ i ++ ) $ num += ( 1 << $ i ) * $ a [ $ i ] ; return $ num ; } function getNthNumber ( $ n ) { $ group_no = 0 ; $ count_upto_group = 0 ; $ count_temp = 1 ; $ op = $ aux_num = 0 ; while ( $ count_temp < $ n ) { $ group_no ++ ; $ count_upto_group = $ count_temp ; $ count_temp += 3 * ( 1 << ( $ group_no - 1 ) ) ; } $ group_offset = $ n - $ count_upto_group - 1 ; if ( ( $ group_offset + 1 ) <= ( 1 << ( $ group_no - 1 ) ) ) { $ aux_num = $ group_offset ; } else { if ( ( ( $ group_offset + 1 ) - ( 1 << ( $ group_no - 1 ) ) ) % 2 ) else $ aux_num = ( int ) ( ( ( $ group_offset ) - ( 1 << ( $ group_no - 1 ) ) ) / 2 ) ; } return constructNthNumber ( $ group_no , $ aux_num , $ op ) ; } $ n = 9 ; print ( getNthNumber ( $ n ) ) ; ? >
Sum of numbers with exactly 2 bits set | To count number of set bits ; To calculate sum of numbers ; To count sum of number whose 2 bit are set ; Driver Code
< ? php function countSetBits ( $ n ) { $ count = 0 ; while ( $ n ) { $ n &= ( $ n - 1 ) ; $ count ++ ; } return $ count ; } function findSum ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) if ( countSetBits ( $ i ) == 2 ) $ sum += $ i ; return $ sum ; } $ n = 10 ; echo findSum ( $ n ) ; ? >
Toggle the last m bits | function to toggle the last m bits ; calculating a number ' num ' having ' m ' bits and all are set . ; toggle the last m bits and return the number ; Driver code
< ? php function toggleLastMBits ( $ n , $ m ) { $ num = ( 1 << $ m ) - 1 ; return ( $ n ^ $ num ) ; } { $ n = 107 ; $ m = 4 ; echo toggleLastMBits ( $ n , $ m ) ; return 0 ; } ? >
Previous smaller integer having one less number of set bits | function to find the position of rightmost set bit . ; function to find the previous smaller integer ; position of rightmost set bit of n ; turn off or unset the bit at position ' pos ' ; Driver Code
< ? php function getFirstSetBitPos ( $ n ) { return log ( $ n & - $ n ) + 1 ; } function previousSmallerInteger ( $ n ) { $ pos = getFirstSetBitPos ( $ n ) ; return ( $ n & ~ ( 1 << ( $ pos - 1 ) ) ) ; } $ n = 25 ; echo " Previous ▁ smaller ▁ Integer ▁ = ▁ " , previousSmallerInteger ( $ n ) ; ? >
Check if all bits of a number are set | function to check if all the bits are set or not in the binary representation of ' n ' ; all bits are not set ; loop till n becomes '0' ; if the last bit is not set ; right shift ' n ' by 1 ; all bits are set ; Driver Code
< ? php function areAllBitsSet ( $ n ) { if ( $ n == 0 ) return " No " ; while ( $ n > 0 ) { if ( ( $ n & 1 ) == 0 ) return " No " ; $ n = $ n >> 1 ; } return " Yes " ; } $ n = 7 ; echo areAllBitsSet ( $ n ) ; ? >
Check if all bits of a number are set | function to check if all the bits are set or not in the binary representation of ' n ' ; all bits are not set ; if true , then all bits are set ; else all bits are not set ; Driver Code
< ? php function areAllBitsSet ( $ n ) { if ( $ n == 0 ) return " No " ; if ( ( ( $ n + 1 ) & $ n ) == 0 ) return " Yes " ; return " No " ; } $ n = 7 ; echo areAllBitsSet ( $ n ) ; ? >
Next greater integer having one more number of set bits | function to find the position of rightmost set bit . Returns - 1 if there are no set bits ; function to find the next greater integer ; position of rightmost unset bit of n by passing ~ n as argument ; if n consists of unset bits , then set the rightmost unset bit ; n does not consists of unset bits ; Driver Code
< ? php function getFirstSetBitPos ( $ n ) { return ( log ( $ n & - $ n + 1 ) ) - 1 ; } function nextGreaterWithOneMoreSetBit ( $ n ) { $ pos = getFirstSetBitPos ( ~ $ n ) ; if ( $ pos > -1 ) return ( 1 << $ pos ) | $ n ; return ( ( $ n << 1 ) + 1 ) ; } $ n = 10 ; echo " Next ▁ greater ▁ integer ▁ = ▁ " , nextGreaterWithOneMoreSetBit ( $ n ) ; ? >
Count numbers whose sum with x is equal to XOR with x | Function to find total 0 bit in a number ; Function to find Count of non - negative numbers less than or equal to x , whose bitwise XOR and SUM with x are equal . ; count number of zero bit in x ; power of 2 to count ; Driver code ; Function call
< ? php function CountZeroBit ( $ x ) { $ count = 0 ; while ( $ x ) { if ( ! ( $ x & 1 ) ) $ count ++ ; $ x >>= 1 ; } return $ count ; } function CountXORandSumEqual ( $ x ) { $ count = CountZeroBit ( $ x ) ; return ( 1 << $ count ) ; } $ x = 10 ; echo CountXORandSumEqual ( $ x ) ; ? >
Find missing number in another array which is shuffled copy | Returns the missing number Size of arr2 [ ] is n - 1 ; Missing number ' mnum ' ; 1 st array is of size ' n ' ; 2 nd array is of size ' n ▁ - ▁ 1' ; Required missing number ; Driver Code
< ? php function missingNumber ( $ arr1 , $ arr2 , $ n ) { $ mnum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ mnum = $ mnum ^ $ arr1 [ $ i ] ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) $ mnum = $ mnum ^ $ arr2 [ $ i ] ; return $ mnum ; } $ arr1 = array ( 4 , 8 , 1 , 3 , 7 ) ; $ arr2 = array ( 7 , 4 , 3 , 1 ) ; $ n = count ( $ arr1 ) ; echo " Missing ▁ number ▁ = ▁ " , missingNumber ( $ arr1 , $ arr2 , $ n ) ; ? >
Change bits to make specific OR value | Returns max of three numbers ; Returns count of bits in N ; Returns bit at ' pos ' position ; Utility method to toggle bit at ' pos ' position ; method returns minimum number of bit flip to get T as OR value of A and B ; Loop over maximum number of bits among A , B and T ; T 's bit is set, try to toggle  bit of B, if not already ; if A 's bit is set,   flip that ; if B 's bit is set,  flip that ; if K is less than 0 then we can make A | B == T ; Loop over bits one more time to minimise A further ; If both bit are set , then Unset A 's bit to minimise it ; If A ' s ▁ bit ▁ is ▁ 1 ▁ and ▁ B ' s bit is 0 , toggle both ; Output changed value of A and B ; Driver Code
< ? php function maxDD ( $ a , $ b , $ c ) { return max ( $ a , ( max ( $ b , $ c ) ) ) ; } function bitCount ( $ N ) { $ cnt = 0 ; while ( $ N ) { $ cnt ++ ; $ N >>= 1 ; } return $ cnt ; } function at_position ( $ num , $ pos ) { $ bit = $ num & ( 1 << $ pos ) ; return $ bit ; } function toggle ( & $ num , $ pos ) { $ num ^= ( 1 << $ pos ) ; } function minChangeToReachTaregetOR ( $ A , $ B , $ K , $ T ) { $ maxlen = max ( bitCount ( $ A ) , bitCount ( $ B ) , bitCount ( $ T ) ) ; for ( $ i = $ maxlen - 1 ; $ i >= 0 ; $ i -- ) { $ bitA = at_position ( $ A , $ i ) ; $ bitB = at_position ( $ B , $ i ) ; $ bitT = at_position ( $ T , $ i ) ; if ( $ bitT ) { if ( ! $ bitA && ! $ bitB ) { toggle ( $ B , $ i ) ; $ K -- ; } } else { if ( $ bitA ) { toggle ( $ A , $ i ) ; $ K -- ; } if ( $ bitB ) { toggle ( $ B , $ i ) ; $ K -- ; } } } if ( $ K < 0 ) { echo " Not ▁ possible STRNEWLINE " ; return ; } for ( $ i = $ maxlen - 1 ; $ K > 0 && $ i >= 0 ; -- $ i ) { $ bitA = at_position ( $ A , $ i ) ; $ bitB = at_position ( $ B , $ i ) ; $ bitT = at_position ( $ T , $ i ) ; if ( $ bitT ) { if ( $ bitA && $ bitB ) { toggle ( $ A , $ i ) ; $ K -- ; } } if ( $ bitA && ! $ bitB && $ K >= 2 ) { toggle ( $ A , $ i ) ; toggle ( $ B , $ i ) ; $ K -= 2 ; } } echo $ A , " " ▁ , ▁ $ B ▁ , ▁ " " } $ A = 175 ; $ B = 66 ; $ K = 5 ; $ T = 100 ; minChangeToReachTaregetOR ( $ A , $ B , $ K , $ T ) ; ? >
Count smaller values whose XOR with x is greater than x | PHP program to find count of values whose XOR with x is greater than x and values are smaller than x ; Initialize result ; Traversing through all bits of x ; If current last bit of x is set then increment count by n . Here n is a power of 2 corresponding to position of bit ; Simultaneously calculate the 2 ^ n ; Replace x with x / 2 ; ; Driver code
< ? php function countValues ( $ x ) { $ count = 0 ; $ n = 1 ; while ( $ x != 0 ) { if ( $ x % 2 == 0 ) $ count += $ n ; $ n *= 2 ; $ x /= 2 ; $ x = ( int ) $ x ; } return $ count ; } $ x = 10 ; echo countValues ( $ x ) ; ? >
Construct an array from XOR of all elements of array except element at same index | function to construct new array ; calculate xor of array ; update array ; Driver code ; print result
< ? php function constructXOR ( & $ A , $ n ) { $ XOR = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ XOR ^= $ A [ $ i ] ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ A [ $ i ] = $ XOR ^ $ A [ $ i ] ; } $ A = array ( 2 , 4 , 1 , 3 , 5 ) ; $ n = sizeof ( $ A ) ; constructXOR ( $ A , $ n ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ A [ $ i ] . " ▁ " ; ? >
Count all pairs of an array which differ in K bits | Utility function to count total ones in a number ; Function to count pairs of K different bits ; initialize final answer ; Check for K differ bit ; Driver code
< ? php function bitCount ( $ n ) { $ count = 0 ; while ( $ n ) { if ( $ n & 1 ) ++ $ count ; $ n >>= 1 ; } return $ count ; } function countPairsWithKDiff ( $ arr , $ n , $ k ) { $ ans = 0 ; for ( $ i = 0 ; $ i < $ n - 1 ; ++ $ i ) { for ( $ j = $ i + 1 ; $ j < $ n ; ++ $ j ) { $ xoredNum = $ arr [ $ i ] ^ $ arr [ $ j ] ; if ( $ k == bitCount ( $ xoredNum ) ) ++ $ ans ; } } return $ ans ; } $ k = 2 ; $ arr = array ( 2 , 4 , 1 , 3 , 1 ) ; $ n = count ( $ arr ) ; echo " Total ▁ pairs ▁ for ▁ k ▁ = ▁ " , $ k , " ▁ are ▁ " , countPairsWithKDiff ( $ arr , $ n , $ k ) , " STRNEWLINE " ; ? >
Multiplication of two numbers with shift operator | Function for multiplication ; check for set bit and left shift n , count times ; increment of place value ( count ) ; Driver code
< ? php function multiply ( $ n , $ m ) { $ ans = 0 ; $ count = 0 ; while ( $ m ) { if ( $ m % 2 == 1 ) $ ans += $ n << $ count ; $ count ++ ; $ m /= 2 ; } return $ ans ; } $ n = 20 ; $ m = 13 ; echo multiply ( $ n , $ m ) ; ? >
Compare two integers without using any Comparison operator | function return true if A ^ B > 0 else false ; Driver Code
< ? php function EqualNumber ( $ A , $ B ) { return ( $ A ^ $ B ) ; } $ A = 5 ; $ B = 6 ; echo ( ( int ) ! ( EqualNumber ( $ A , $ B ) ) ) . " STRNEWLINE " ; ? >
Check if bits of a number has count of consecutive set bits in increasing order | Returns true if n has counts of consecutive 1 's are increasing order. ; Initialize previous count ; We traverse bits from right to left and check if counts are decreasing order . ; Ignore 0 s until we reach a set bit . ; Count current set bits ; Compare current with previous and update previous . ; Driver code
< ? php function areSetBitsIncreasing ( $ n ) { $ prev_count = PHP_INT_MAX ; while ( $ n > 0 ) { while ( $ n > 0 && $ n % 2 == 0 ) $ n = $ n / 2 ; $ curr_count = 1 ; while ( $ n > 0 and $ n % 2 == 1 ) { $ n = $ n / 2 ; $ curr_count ++ ; } if ( $ curr_count >= $ prev_count ) return false ; $ prev_count = $ curr_count ; } return true ; } $ n = 10 ; if ( areSetBitsIncreasing ( $ n ) ) echo " Yes " ; else echo " No " ; ? >
Check if a number has bits in alternate pattern | Set 1 | Returns true if n has alternate bit pattern else false ; Store last bit ; Traverse through remaining bits ; If current bit is same as previous ; Driver code
< ? php function findPattern ( $ n ) { $ prev = $ n % 2 ; $ n = $ n / 2 ; while ( $ n > 0 ) { $ curr = $ n % 2 ; if ( $ curr == $ prev ) return false ; $ prev = $ curr ; $ n = floor ( $ n / 2 ) ; } return true ; } $ n = 10 ; if ( findPattern ( $ n ) ) echo " Yes " ; else echo " No " ; return 0 ; ? >
XOR counts of 0 s and 1 s in binary representation | Returns XOR of counts 0 s and 1 s in binary representation of n . ; calculating count of zeros and ones ; Driver Code
< ? php function countXOR ( $ n ) { $ count0 = 0 ; $ count1 = 0 ; while ( $ n ) { ( $ n % 2 == 0 ) ? $ count0 ++ : $ count1 ++ ; $ n = intval ( $ n / 2 ) ; } return ( $ count0 ^ $ count1 ) ; } $ n = 31 ; echo countXOR ( $ n ) ; ? >
Bitwise and ( or & ) of a range | Find position of MSB in n . For example if n = 17 , then position of MSB is 4. If n = 7 , value of MSB is 3 ; Function to find Bit - wise & of all numbers from x to y . ; $res = 0 ; Initialize result ; Find positions of MSB in x and y ; If positions are not same , return ; Add 2 ^ msb_p1 to result ; subtract 2 ^ msb_p1 from x and y . ; Driver code
< ? php function msbPos ( $ n ) { $ msb_p = -1 ; while ( $ n > 0 ) { $ n = $ n >> 1 ; $ msb_p ++ ; } return $ msb_p ; } function andOperator ( $ x , $ y ) { while ( $ x > 0 && $ y > 0 ) { $ msb_p1 = msbPos ( $ x ) ; $ msb_p2 = msbPos ( $ y ) ; if ( $ msb_p1 != $ msb_p2 ) break ; $ msb_val = ( 1 << $ msb_p1 ) ; $ res = $ res + $ msb_val ; $ x = $ x - $ msb_val ; $ y = $ y - $ msb_val ; } return $ res ; } $ x = 10 ; $ y = 15 ; echo andOperator ( $ x , $ y ) ; ? >
Multiply a number with 10 without using multiplication operator | Function to find multiplication of n with 10 without using multiplication operator ; Driver Code
< ? php function multiplyTen ( $ n ) { return ( $ n << 1 ) + ( $ n << 3 ) ; } $ n = 50 ; echo multiplyTen ( $ n ) ; ? >
Equal Sum and XOR | function to count number of values less than equal to n that satisfy the given condition ; Traverse all numbers from 0 to n and increment result only when given condition is satisfied . ; Driver Code
< ? php function countValues ( $ n ) { $ countV = 0 ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) if ( ( $ n + $ i ) == ( $ n ^ $ i ) ) $ countV ++ ; return $ countV ; } $ n = 12 ; echo countValues ( $ n ) ; ? >
Equal Sum and XOR | function to count number of values less than equal to n that satisfy the given condition ; unset_bits keeps track of count of un - set bits in binary representation of n ; Return 2 ^ unset_bits ; Driver code
< ? php function countValues ( $ n ) { $ unset_bits = 0 ; while ( $ n ) { if ( ( $ n & 1 ) == 0 ) $ unset_bits ++ ; $ n = $ n >> 1 ; } return 1 << $ unset_bits ; } $ n = 12 ; echo countValues ( $ n ) ; ? >
Find profession in a special family | Returns ' e ' if profession of node at given level and position is engineer . Else doctor . The function assumes that given position and level have valid values . ; Base case ; Recursively find parent 's  profession. If parent is  a Doctor, this node will  be a doctor if it is at  odd position and an engineer  if at even position ; If parent is an engineer , then current node will be an engineer if at odd position and doctor if even position . ; Driver code
< ? php function findProffesion ( $ level , $ pos ) { if ( $ level == 1 ) return ' e ' ; if ( findProffesion ( $ level - 1 , ( $ pos + 1 ) / 2 ) == ' d ' ) return ( $ pos % 2 ) ? ' d ' : ' e ' ; return ( $ pos % 2 ) ? ' e ' : ' d ' ; } $ level = 4 ; $ pos = 2 ; if ( ( findProffesion ( $ level , $ pos ) == ' e ' ) == true ) echo " Engineer " ; else echo " Doctor " ; ? >
Print first n numbers with exactly two set bits | Prints first n numbers with two set bits ; Initialize higher of two sets bits ; Keep reducing n for every number with two set bits . ; Consider all lower set bits for current higher set bit ; Print current number ; If we have found n numbers ; Consider next lower bit for current higher bit . ; Increment higher set bit ; Driver code
< ? php function printTwoSetBitNums ( $ n ) { $ x = 1 ; while ( $ n > 0 ) { $ y = 0 ; while ( $ y < $ x ) { echo ( 1 << $ x ) + ( 1 << $ y ) , " ▁ " ; $ n -- ; if ( $ n == 0 ) return ; $ y ++ ; } $ x ++ ; } } printTwoSetBitNums ( 4 ) ; ? >
Generate 0 and 1 with 25 % and 75 % probability | Random Function to that returns 0 or 1 with equal probability ; rand ( ) function will generate odd or even number with equal probability . If rand ( ) generates odd number , the function will return 1 else it will return 0. ; Random Function to that returns 1 with 75 % probability and 0 with 25 % probability using Bitwise OR ; Driver Code Initialize random number generator ; This code is contributed m_kit
< ? php function rand50 ( ) { return rand ( ) & 1 ; } function rand75 ( ) { return rand50 ( ) | rand50 ( ) ; } srand ( time ( NULL ) ) ; for ( $ i = 0 ; $ i < 50 ; $ i ++ ) echo rand75 ( ) ; ? >
Find even occurring elements in an array of limited range | Function to find the even occurring elements in given array ; do for each element of array ; left - shift 1 by value of current element ; Toggle the bit everytime element gets repeated ; Traverse array again and use _xor to find even occurring elements ; left - shift 1 by value of current element ; Each 0 in _xor represents an even occurrence ; print the even occurring numbers ; set bit as 1 to avoid printing duplicates ; Driver code
< ? php function printRepeatingEven ( $ arr , $ n ) { $ _xor = 0 ; $ pos ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { $ pos = 1 << $ arr [ $ i ] ; $ _xor ^= $ pos ; } for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { $ pos = 1 << $ arr [ $ i ] ; if ( ! ( $ pos & $ _xor ) ) { echo $ arr [ $ i ] , " " ; $ _xor ^= $ pos ; } } } $ arr = array ( 9 , 12 , 23 , 10 , 12 , 12 , 15 , 23 , 14 , 12 , 15 ) ; $ n = sizeof ( $ arr ) ; printRepeatingEven ( $ arr , $ n ) ; ? >
Check if a number is Bleak | Function to get no of set bits in binary representation of passed binary no . ; Returns true if n is Bleak ; Check for all numbers ' x ' smaller than n . If x + countSetBits ( x ) becomes n , then n can 't be Bleak ; Driver code
< ? php function countSetBits ( $ x ) { $ count = 0 ; while ( $ x ) { $ x &= ( $ x - 1 ) ; $ count ++ ; } return $ count ; } function isBleak ( $ n ) { for ( $ x = 1 ; $ x < $ n ; $ x ++ ) if ( $ x + countSetBits ( $ x ) == $ n ) return false ; return true ; } if ( isBleak ( 3 ) ) echo " Yes STRNEWLINE " ; else echo " No STRNEWLINE " ; if ( isBleak ( 4 ) ) echo " Yes STRNEWLINE " ; else echo " No STRNEWLINE " ; ? >
Find the maximum subset XOR of a given set | Number of bits to represent int ; Function to return maximum XOR subset in set [ ] ; Initialize index of chosen elements ; Traverse through all bits of integer starting from the most significant bit ( MSB ) ; Initialize index of maximum element and the maximum element ; If i 'th bit of set[j]  is set and set[j] is  greater than max so far. ; If there was no element with i 'th  bit set, move to smaller i ; Put maximum element with i ' th ▁ bit ▁ ▁ set ▁ at ▁ index ▁ ' index ' ; Update maxInd and increment index ; Do XOR of set [ maxIndex ] with all numbers having i 'th bit as set. ; XOR set [ maxInd ] those numbers which have the i 'th bit set ; Increment index of chosen elements ; Final result is XOR of all elements ; Driver Code
< ? php $ INT_BITS = 32 ; function maxSubarrayXOR ( & $ set , $ n ) { global $ INT_BITS ; $ index = 0 ; for ( $ i = $ INT_BITS - 1 ; $ i >= 0 ; $ i -- ) { $ maxInd = $ index ; $ maxEle = 0 ; for ( $ j = $ index ; $ j < $ n ; $ j ++ ) { if ( ( $ set [ $ j ] & ( 1 << $ i ) ) != 0 && $ set [ $ j ] > $ maxEle ) { $ maxEle = $ set [ $ j ] ; $ maxInd = $ j ; } } if ( $ maxEle == 0 ) continue ; $ t = $ set [ $ index ] ; $ set [ $ index ] = $ set [ $ maxInd ] ; $ set [ $ maxInd ] = $ t ; $ maxInd = $ index ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ j != $ maxInd && ( $ set [ $ j ] & ( 1 << $ i ) ) != 0 ) $ set [ $ j ] = $ set [ $ j ] ^ $ set [ $ maxInd ] ; } $ index ++ ; } $ res = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ res ^= $ set [ $ i ] ; return $ res ; } $ set = array ( 9 , 8 , 5 ) ; $ n = sizeof ( $ set ) ; echo " Max ▁ subset ▁ XOR ▁ is ▁ " ; echo maxSubarrayXOR ( $ set , $ n ) ; ? >
Given a set , find XOR of the XOR 's of all subsets. | Returns XOR of all XOR 's of given subset ; XOR is 1 only when n is 1 , else 0 ; Driver Code
< ? php function findXOR ( $ Set , $ n ) { if ( $ n == 1 ) return $ Set [ 0 ] ; else return 0 ; } $ Set = array ( 1 , 2 , 3 ) ; $ n = count ( $ Set ) ; echo " XOR ▁ of ▁ XOR ' s ▁ of ▁ all ▁ subsets ▁ is ▁ " , findXOR ( $ Set , $ n ) ; ? >
Find XOR of two number without using XOR operator | Returns XOR of x and y ; Driver Code
< ? php function myXOR ( $ x , $ y ) { return ( $ x $ y ) & ( ~ $ x ~ $ y ) ; } $ x = 3 ; $ y = 5 ; echo " XOR ▁ is ▁ " , myXOR ( $ x , $ y ) ; ? >
Freivaldâ €™ s Algorithm to check if a matrix is product of two | PHP code to implement FreivaldaTMs Algorithm ; Function to check if ABx = Cx ; Generate a random vector ; Now comput B * r for evaluating expression A * ( B * r ) - ( C * r ) ; Now comput C * r for evaluating expression A * ( B * r ) - ( C * r ) ; Now comput A * ( B * r ) for evaluating expression A * ( B * r ) - ( C * r ) ; Finally check if value of expression A * ( B * r ) - ( C * r ) is 0 or not ; Runs k iterations Freivald . The value of k determines accuracy . Higher value means higher accuracy . ; Driver code
< ? php $ N = 2 ; function freivald ( $ a , $ b , $ c ) { global $ N ; $ r = array ( ) ; $ br = array ( ) ; $ cr = array ( ) ; $ axbr = array ( ) ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { $ r [ $ i ] = mt_rand ( ) % 2 ; $ br [ $ i ] = 0 ; $ cr [ $ i ] = 0 ; $ axbr [ $ i ] = 0 ; } for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { for ( $ j = 0 ; $ j < $ N ; $ j ++ ) $ br [ $ i ] = $ br [ $ i ] + $ b [ $ i ] [ $ j ] * $ r [ $ j ] ; } for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { for ( $ j = 0 ; $ j < $ N ; $ j ++ ) $ cr [ $ i ] = $ cr [ $ i ] + $ c [ $ i ] [ $ j ] * $ r [ $ j ] ; } for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { for ( $ j = 0 ; $ j < $ N ; $ j ++ ) $ axbr [ $ i ] = $ axbr [ $ i ] + $ a [ $ i ] [ $ j ] * $ br [ $ j ] ; } for ( $ i = 0 ; $ i < $ N ; $ i ++ ) if ( $ axbr [ $ i ] - $ cr [ $ i ] != 0 ) return false ; return true ; } function isProduct ( $ a , $ b , $ c , $ k ) { for ( $ i = 0 ; $ i < $ k ; $ i ++ ) if ( freivald ( $ a , $ b , $ c ) == false ) return false ; return true ; } $ a = array ( array ( 1 , 1 ) , array ( 1 , 1 ) ) ; $ b = array ( array ( 1 , 1 ) , array ( 1 , 1 ) ) ; $ c = array ( array ( 2 , 2 ) , array ( 2 , 2 ) ) ; $ k = 2 ; if ( isProduct ( $ a , $ b , $ c , $ k ) ) echo ( " Yes " ) ; else echo ( " No " ) ; ? >