codeparrot/xlcost-text-to-code huggingface dataset:
Total pairs in an array such that the bitwise AND , bitwise OR and bitwise XOR of LSB is 1 | Function to find the count of required pairs ; To store the count of elements which give remainder 0 i . e . even values ; To store the count of elements which give remainder 1 i . e . odd values ; Driver code
< ? php function CalculatePairs ( $ a , $ n ) { $ cnt_zero = 0 ; $ cnt_one = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] % 2 == 0 ) $ cnt_zero += 1 ; else $ cnt_one += 1 ; } $ total_XOR_pairs = $ cnt_zero * $ cnt_one ; $ total_AND_pairs = ( $ cnt_one ) * ( $ cnt_one - 1 ) / 2 ; $ total_OR_pairs = $ cnt_zero * $ cnt_one + ( $ cnt_one ) * ( $ cnt_one - 1 ) / 2 ; echo ( " cntXOR ▁ = ▁ $ total _ XOR _ pairs STRNEWLINE " ) ; echo ( " cntAND ▁ = ▁ $ total _ AND _ pairs STRNEWLINE " ) ; echo ( " cntOR ▁ = ▁ $ total _ OR _ pairs STRNEWLINE " ) ; } $ a = array ( 1 , 3 , 4 , 2 ) ; $ n = count ( $ a ) ; CalculatePairs ( $ a , $ n ) ; ? >
Assign other value to a variable from two possible values | Function to alternate the values ; Driver Code
< ? php function alternate ( & $ a , & $ b , & $ x ) { $ x = $ a + $ b - $ x ; } $ a = -10 ; $ b = 15 ; $ x = $ a ; echo " x ▁ is ▁ : ▁ " , $ x ; alternate ( $ a , $ b , $ x ) ; echo " After change " ; echo " x is : " ? >
Highest power of two that divides a given number | PHP program to find highest power of 2 that divides n . ; Driver Code
< ? php function highestPowerOf2 ( $ n ) { return ( $ n & ( ~ ( $ n - 1 ) ) ) ; } $ n = 48 ; echo highestPowerOf2 ( $ n ) ; ? >
Check whether bitwise AND of a number with any subset of an array is zero or not | Function to check whether bitwise AND of a number with any subset of an array is zero or not ; variable to store the AND of all the elements ; find the AND of all the elements of the array ; if the AND of all the array elements and N is equal to zero ; Driver Code
< ? php function isSubsetAndZero ( $ array , $ length , $ N ) { $ arrAnd = $ array [ 0 ] ; for ( $ i = 1 ; $ i < $ length ; $ i ++ ) { $ arrAnd = $ arrAnd & $ array [ $ i ] ; } if ( ( $ arrAnd & $ N ) == 0 ) echo ( " YES " ) ; else echo ( " NO " ) ; } $ array = array ( 1 , 2 , 4 ) ; $ length = count ( $ array ) ; $ N = 3 ; isSubsetAndZero ( $ array , $ length , $ N ) ; ? >
Finding the Parity of a number Efficiently | Function to find the parity ; Rightmost bit of y holds the parity value if ( y & 1 ) is 1 then parity is odd else even ; Driver code
< ? php function findParity ( $ x ) { $ y = $ x ^ ( $ x >> 1 ) ; $ y = $ y ^ ( $ y >> 2 ) ; $ y = $ y ^ ( $ y >> 4 ) ; $ y = $ y ^ ( $ y >> 8 ) ; $ y = $ y ^ ( $ y >> 16 ) ; if ( $ y & 1 ) return 1 ; return 0 ; } ( findParity ( 9 ) == 0 ) ? print ( " Even ▁ Parity STRNEWLINE " ) : print ( " Odd ▁ Parity STRNEWLINE " ) ; ( findParity ( 13 ) == 0 ) ? print ( " Even ▁ Parity STRNEWLINE " ) : print ( " Odd ▁ Parity STRNEWLINE " ) ; ? >
Check if bits in range L to R of two numbers are complement of each other or not | function to check whether all the bits are set in the given range or not ; calculating a number ' num ' having ' r ' number of bits and bits in the range l to r are the only set bits ; new number which will only have one or more set bits in the range l to r and nowhere else ; if both are equal , then all bits are set in the given range ; else all bits are not set ; function to check whether all the bits in the given range of two numbers are complement of each other ; Driver Code
< ? php function allBitsSetInTheGivenRange ( $ n , $ l , $ r ) { $ num = ( ( 1 << $ r ) - 1 ) ^ ( ( 1 << ( $ l - 1 ) ) - 1 ) ; $ new_num = ( $ n & $ num ) ; if ( $ num == $ new_num ) return true ; return false ; } function bitsAreComplement ( $ a , $ b , $ l , $ r ) { $ xor_value = $ a ^ $ b ; return allBitsSetInTheGivenRange ( $ xor_value , $ l , $ r ) ; } $ a = 10 ; $ b = 5 ; $ l = 1 ; $ r = 3 ; if ( bitsAreComplement ( $ a , $ b , $ l , $ r ) ) echo " Yes " ; else echo " No " ; ? >
Sum of the series 2 ^ 0 + 2 ^ 1 + 2 ^ 2 + ... . . + 2 ^ n | function to calculate sum of series ; initialize sum as 0 ; loop to calculate sum of series ; calculate 2 ^ i and add it to sum ; Driver code
< ? php function calculateSum ( $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ sum = $ sum + ( 1 << $ i ) ; } return $ sum ; } $ n = 10 ; echo " Sum ▁ of ▁ the ▁ series ▁ of ▁ " . " power ▁ 2 ▁ is ▁ : ▁ " , calculateSum ( $ n ) ; ? >
Print all the combinations of N elements by changing sign such that their sum is divisible by M | Function to print all the combinations ; Iterate for all combinations ; Initially 100 in binary if n is 3 as 1 << ( 3 - 1 ) = 100 in binary ; Iterate in the array and assign signs to the array elements ; If the j - th bit from left is set take ' + ' sign ; Right shift to check if jth bit is set or not ; re - initialize ; Iterate in the array elements ; If the jth from left is set ; right shift ; Driver Code
< ? php function printCombinations ( $ a , $ n , $ m ) { for ( $ i = 0 ; $ i < ( 1 << $ n ) ; $ i ++ ) { $ sum = 0 ; $ num = 1 << ( $ n - 1 ) ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ i & $ num ) $ sum += $ a [ $ j ] ; else $ sum += ( -1 * $ a [ $ j ] ) ; $ num = $ num >> 1 ; } if ( $ sum % $ m == 0 ) { $ num = 1 << ( $ n - 1 ) ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( ( $ i & $ num ) ) echo " + " ▁ , ▁ $ a [ $ j ] ▁ , ▁ " " ; STRNEWLINE TABSYMBOL TABSYMBOL TABSYMBOL TABSYMBOL else STRNEWLINE TABSYMBOL TABSYMBOL TABSYMBOL TABSYMBOL TABSYMBOL echo ▁ " - " ▁ , ▁ $ a [ $ j ] ▁ , ▁ " " $ num = $ num >> 1 ; } echo " STRNEWLINE " ; } } } $ a = array ( 3 , 5 , 6 , 8 ) ; $ n = sizeof ( $ a ) ; $ m = 5 ; printCombinations ( $ a , $ n , $ m ) ; ? >
Same Number Of Set Bits As N | returns number of set bits in a number ; function ; __builtin_popcount function that count set bits in n ; Iterate from n - 1 to 1 ; check if the number of set bits equals to temp increment count ; Driver Code
< ? php function __builtin_popcount ( $ n ) { $ t = 0 ; while ( $ n > 0 ) { $ d = $ n % 2 ; $ n = intval ( $ n / 2 ) ; if ( $ d == 1 ) $ t ++ ; } return $ t ; } function smallerNumsWithSameSetBits ( $ n ) { $ temp = __builtin_popcount ( $ n ) ; $ count = 0 ; for ( $ i = $ n - 1 ; $ i > 0 ; $ i -- ) { if ( $ temp == __builtin_popcount ( $ i ) ) $ count ++ ; } return $ count ; } $ n = 4 ; echo ( smallerNumsWithSameSetBits ( $ n ) ) ; ? >
Multiply any Number with 4 using Bitwise Operator | function the return multiply a number with 4 using bitwise operator ; returning a number with multiply with 4 using2 bit shifting right ; Driver Code
< ? php function multiplyWith4 ( $ n ) { return ( $ n << 2 ) ; } $ n = 4 ; echo multiplyWith4 ( $ n ) , " STRNEWLINE " ; ? >
Set all even bits of a number | Sets even bits of n and returns modified number . ; Generate 101010. . .10 number and store in res . ; if bit is even then generate number and or with res ; return OR number ; Driver Code
< ? php function evenbitsetnumber ( $ n ) { $ count = 0 ; $ res = 0 ; for ( $ temp = $ n ; $ temp > 0 ; $ temp >>= 1 ) { if ( $ count % 2 == 1 ) $ res |= ( 1 << $ count ) ; $ count ++ ; } return ( $ n $ res ) ; } $ n = 10 ; echo evenbitsetnumber ( $ n ) ; ? >
Set all even bits of a number | return msb set number ; set all bits ; return msb increment n by 1 and shift by 1 ; return even seted number ; get msb here ; generate even bits like 101010. . ; if bits is odd then shift by 1 ; return even set bits number ; set all even bits here ; take or with even set bits number ; Driver code
< ? php function getmsb ( $ n ) { $ n |= $ n >> 1 ; $ n |= $ n >> 2 ; $ n |= $ n >> 4 ; $ n |= $ n >> 8 ; $ n |= $ n >> 16 ; return ( $ n + 1 ) >> 1 ; } function getevenbits ( $ n ) { $ n = getmsb ( $ n ) ; $ n |= $ n >> 2 ; $ n |= $ n >> 4 ; $ n |= $ n >> 8 ; $ n |= $ n >> 16 ; if ( $ n & 1 ) $ n = $ n >> 1 ; return $ n ; } function setallevenbits ( $ n ) { return $ n | getevenbits ( $ n ) ; } $ n = 10 ; echo setallevenbits ( $ n ) ; ? >
Set all odd bits of a number | set all odd bit ; res for store 010101. . number ; generate number form of 010101. . . till temp size ; if bit is odd , then generate number and or with res ; Driver code
< ? php function oddbitsetnumber ( $ n ) { $ count = 0 ; $ res = 0 ; for ( $ temp = $ n ; $ temp > 0 ; $ temp >>= 1 ) { if ( $ count % 2 == 0 ) $ res |= ( 1 << $ count ) ; $ count ++ ; } return ( $ n $ res ) ; } $ n = 10 ; echo oddbitsetnumber ( $ n ) ; ? >
Set all odd bits of a number | return MSB set number ; set all bits including MSB . ; return MSB ; Returns a number of same size ( MSB atsame position ) as n and all odd bits set ; generate odd bits like 010101. . ; if bits is even then shift by 1 ; return odd set bits number ; set all odd bits here ; take OR with odd set bits number ; Driver code
< ? php function getmsb ( $ n ) { $ n |= $ n >> 1 ; $ n |= $ n >> 2 ; $ n |= $ n >> 4 ; $ n |= $ n >> 8 ; $ n |= $ n >> 16 ; return ( $ n + 1 ) >> 1 ; } function getevenbits ( $ n ) { $ n = getmsb ( $ n ) ; $ n |= $ n >> 2 ; $ n |= $ n >> 4 ; $ n |= $ n >> 8 ; $ n |= $ n >> 16 ; if ( ( $ n & 1 ) == 0 ) $ n = $ n >> 1 ; return $ n ; } function setalloddbits ( $ n ) { return $ n | getevenbits ( $ n ) ; } $ n = 10 ; echo setalloddbits ( $ n ) ; ? >
Print numbers in the range 1 to n having bits in alternate pattern | function to print numbers in the range 1 to n having bits in alternate pattern ; first number having bits in alternate pattern ; display ; loop until n < curr_num ; generate next number having alternate bit pattern ; if true then break ; display ; generate next number having alternate bit pattern ; if true then break ; display ; Driver code
< ? php function printNumHavingAltBitPatrn ( $ n ) { $ curr_num = 1 ; echo $ curr_num . " " ; while ( 1 ) { $ curr_num <<= 1 ; if ( $ n < $ curr_num ) break ; echo $ curr_num . " " ; $ curr_num = ( ( $ curr_num ) << 1 ) ^ 1 ; if ( $ n < $ curr_num ) break ; echo $ curr_num . " " ; } } $ n = 50 ; printNumHavingAltBitPatrn ( $ n ) ; ? >
Smallest perfect power of 2 greater than n ( without using arithmetic operators ) | Function to find smallest perfect power of 2 greater than n ; To store perfect power of 2 ; bitwise left shift by 1 ; bitwise right shift by 1 ; Required perfect power of 2 ; Driver code
< ? php function perfectPowerOf2 ( $ n ) { $ per_pow = 1 ; while ( $ n > 0 ) { $ per_pow = $ per_pow << 1 ; $ n = $ n >> 1 ; } return $ per_pow ; } $ n = 128 ; echo " Perfect ▁ power ▁ of ▁ 2 ▁ greater ▁ than ▁ " . $ n . " : ▁ " . perfectPowerOf2 ( $ n ) ; ? >
Find Unique pair in an array with pairs of numbers | PHP program to find a unique pair in an array of pairs . ; XOR each element and get XOR of two unique elements ( ans ) ; Get a set bit of XOR ( We get the rightmost set bit ) ; Now divide elements in two sets by comparing rightmost set bit of XOR with bit at same position in each element . Initialize missing numbers ; XOR of first set in arr [ ] ; XOR of second set in arr [ ] ; Driver code
< ? php function findUniquePair ( $ arr , $ n ) { $ XOR = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ XOR = $ XOR ^ $ arr [ $ i ] ; $ set_bit_no = $ XOR & ~ ( $ XOR - 1 ) ; $ x = 0 ; $ y = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] & $ set_bit_no ) $ x = $ x ^ $ arr [ $ i ] ; else $ y = $ y ^ $ arr [ $ i ] ; } echo " The ▁ unique ▁ pair ▁ is ▁ " , " ( " , $ x , " ▁ " , $ y , " ) " ; } $ a = array ( 6 , 1 , 3 , 5 , 1 , 3 , 7 , 6 ) ; $ n = count ( $ a ) ; findUniquePair ( $ a , $ n ) ; ? >
Count unset bits of a number | An optimized PHP program to count unset bits in an integer . ; This makes sure two bits ( From MSB and including MSB ) are set ; This makes sure 4 bits ( From MSB and including MSB ) are set ; Count set bits in toggled number ; Driver code
< ? php function countUnsetBits ( $ n ) { $ x = $ n ; $ n |= $ n >> 1 ; $ n |= $ n >> 2 ; $ n |= $ n >> 4 ; $ n |= $ n >> 8 ; $ n |= $ n >> 16 ; $ t = log ( $ x ^ $ n , 2 ) ; return floor ( $ t ) ; } $ n = 17 ; echo countUnsetBits ( $ n ) ; ? >
Count total bits in a number | Function to get no of bits in binary representation of positive integer ; Driver Code
< ? php function countBits ( $ n ) { $ count = 0 ; while ( $ n ) { $ count ++ ; $ n >>= 1 ; } return $ count ; } $ i = 65 ; echo ( countBits ( $ i ) ) ; ? >
Toggle all bits after most significant bit | Function to toggle bits starting from MSB ; temporary variable to use XOR with one of a n ; Run loop until the only set bit in temp crosses MST of n . ; Toggle bit of n corresponding to current set bit in temp . ; Move set bit to next higher position . ; Driver code
< ? php function toggle ( & $ n ) { $ temp = 1 ; while ( $ temp <= $ n ) { $ n = $ n ^ $ temp ; $ temp = $ temp << 1 ; } } $ n = 10 ; toggle ( $ n ) ; echo $ n ; ? >
Find the n | Finds if the kth bit is set in the binary representation ; Returns the position of leftmost set bit in the binary representation ; Finds whether the integer in binary representation is palindrome or not ; One by one compare bits ; Compare left and right bits and converge ; Start from 1 , traverse through all the integers ; If we reach n , break the loop ; Driver code ; Function Call
< ? php function isKthBitSet ( $ x , $ k ) { return ( $ x & ( 1 << ( $ k - 1 ) ) ) ? 1 : 0 ; } function leftmostSetBit ( $ x ) { $ count = 0 ; while ( $ x ) { $ count ++ ; $ x = $ x >> 1 ; } return $ count ; } function isBinPalindrome ( $ x ) { $ l = leftmostSetBit ( $ x ) ; $ r = 1 ; while ( $ l > $ r ) { if ( isKthBitSet ( $ x , $ l ) != isKthBitSet ( $ x , $ r ) ) return 0 ; $ l -- ; $ r ++ ; } return 1 ; } function findNthPalindrome ( $ n ) { $ pal_count = 0 ; $ i = 0 ; for ( $ i = 1 ; $ i <= PHP_INT_MAX ; $ i ++ ) { if ( isBinPalindrome ( $ i ) ) { $ pal_count ++ ; } if ( $ pal_count == $ n ) break ; } return $ i ; } $ n = 9 ; echo ( findNthPalindrome ( $ n ) ) ; ? >
Print ' K ' th least significant bit of a number | Function returns 1 if set , 0 if not ; Driver code ; Function call
< ? php function LSB ( $ num , $ K ) { return ( $ num & ( 1 << ( $ K - 1 ) ) ) ; } $ num = 10 ; $ K = 4 ; $ r = LSB ( $ num , $ K ) ; if ( $ r ) echo '1' ; else echo '0' ; ? >
Check if two numbers are equal without using comparison operators | Finds if a and b are same ; Driver code
< ? php function areSame ( $ a , $ b ) { if ( ! ( $ a - $ b ) ) echo " Same " ; else echo " Not ▁ Same " ; } areSame ( 10 , 20 ) ; ? >
Toggle bits in the given range | function to toggle bits in the given range ; calculating a number ' num ' having ' r ' number of bits and bits in the range l to r are the only set bits ; toggle bits in the range l to r in ' n ' Besides this , we can calculate num as : $num = ( 1 << $r ) - $l . and return the number ; Driver Code
< ? php function toggleBitsFromLToR ( $ n , $ l , $ r ) { $ num = ( ( 1 << $ r ) - 1 ) ^ ( ( 1 << ( $ l - 1 ) ) - 1 ) ; return ( $ n ^ $ num ) ; } $ n = 50 ; $ l = 2 ; $ r = 5 ; echo toggleBitsFromLToR ( $ n , $ l , $ r ) ; ? >
Position of rightmost different bit | Function to find the position of rightmost set bit in ' n ' ; to handle edge case when n = 0. ; Function to find the position of rightmost different bit in the binary representations of ' m ' and ' n ' ; position of rightmost different bit ; Driver Code
< ? php function getRightMostSetBit ( $ n ) { if ( $ n == 0 ) return 0 ; return log ( $ n & - $ n , ( 2 ) ) + 1 ; } function posOfRightMostDiffBit ( $ m , $ n ) { return getRightMostSetBit ( $ m ^ $ n ) ; } $ m = 52 ; $ n = 4 ; echo posOfRightMostDiffBit ( $ m , $ n ) ; ? >
Closest ( or Next ) smaller and greater numbers with same number of set bits | Main Function to find next smallest number bigger than n ; Compute c0 and c1 ; If there is no bigger number with the same no . of 1 's ; input 1 ; input 2
< ? php function getNext ( $ n ) { $ c = $ n ; $ c0 = 0 ; $ c1 = 0 ; while ( ( ( $ c & 1 ) == 0 ) && ( $ c != 0 ) ) { $ c0 ++ ; $ c >>= 1 ; } while ( ( $ c & 1 ) == 1 ) { $ c1 ++ ; $ c >>= 1 ; } if ( $ c0 + $ c1 == 31 $ c0 + $ c1 == 0 ) return -1 ; return $ n + ( 1 << $ c0 ) + ( 1 << ( $ c1 - 1 ) ) - 1 ; } $ n = 5 ; echo getNext ( $ n ) ; $ n = 8 ; echo " STRNEWLINE " ; echo getNext ( $ n ) ; ? >
Count minimum bits to flip such that XOR of A and B equal to C | PHP code to count the Minimum bits in A and B ; If both A [ i ] and B [ i ] are equal ; If Both A and B are unequal ; N represent total count of Bits
< ? php function totalFlips ( $ A , $ B , $ C , $ N ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ N ; ++ $ i ) { if ( $ A [ $ i ] == $ B [ $ i ] && $ C [ $ i ] == '1' ) ++ $ count ; else if ( $ A [ $ i ] != $ B [ $ i ] && $ C [ $ i ] == '0' ) ++ $ count ; } return $ count ; } $ N = 5 ; $ a = "10100" ; $ b = "00010" ; $ c = "10011" ; echo totalFlips ( $ a , $ b , $ c , $ N ) ; ? >
Swap three variables without using temporary variable | Assign c ' s ▁ value ▁ to ▁ a , ▁ a ' s value to b and b 's value to c. ; Store XOR of all in a ; After this , b has value of a ; After this , c has value of b ; After this , a has value of c ; Driver code ; Calling Function
< ? php function swapThree ( & $ a , & $ b , & $ c ) { $ a = $ a ^ $ b ^ $ c ; $ b = $ a ^ $ b ^ $ c ; $ c = $ a ^ $ b ^ $ c ; $ a = $ a ^ $ b ^ $ c ; } $ a = 10 ; $ b = 20 ; $ c = 30 ; echo " Before ▁ swapping ▁ a ▁ = ▁ " , $ a , " , ▁ b ▁ = ▁ " , $ b , " , ▁ c ▁ = ▁ " , $ c , " STRNEWLINE " ; swapThree ( $ a , $ b , $ c ) ; echo " After ▁ swapping ▁ a ▁ = ▁ " , $ a , " , ▁ b ▁ = ▁ " , $ b , " , ▁ c ▁ = ▁ " , $ c , " STRNEWLINE " ; ? >
Find Two Missing Numbers | Set 2 ( XOR based solution ) | Function to find two missing numbers in range [ 1 , n ] . This function assumes that size of array is n - 2 and all array elements are distinct ; Get the XOR of all elements in arr [ ] and { 1 , 2 . . n } ; Get a set bit of XOR ( We get the rightmost set bit ) ; Now divide elements in two sets by comparing rightmost set bit of XOR with bit at same position in each element . ; XOR of first set in arr [ ] ; XOR of second set in arr [ ] ; XOR of first set in arr [ ] and { 1 , 2 , ... n } ; XOR of second set in arr [ ] and { 1 , 2 , ... n } ; Driver Code ; Range of numbers is 2 plus size of array
< ? php function findTwoMissingNumbers ( $ arr , $ n ) { $ XOR = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n - 2 ; $ i ++ ) $ XOR ^= $ arr [ $ i ] ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ XOR ^= $ i ; $ set_bit_no = $ XOR & ~ ( $ XOR - 1 ) ; $ x = 0 ; $ y = 0 ; for ( $ i = 0 ; $ i < $ n - 2 ; $ i ++ ) { if ( $ arr [ $ i ] & $ set_bit_no ) $ x = $ x ^ $ arr [ $ i ] ; else $ y = $ y ^ $ arr [ $ i ] ; } for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { if ( $ i & $ set_bit_no ) $ x = $ x ^ $ i ; else $ y = $ y ^ $ i ; } echo " Two ▁ Missing ▁ Numbers ▁ are STRNEWLINE " , $ x ; echo " STRNEWLINE " , $ y ; } $ arr = array ( 1 , 3 , 5 , 6 ) ; $ n = 2 + count ( $ arr ) ; findTwoMissingNumbers ( $ arr , $ n ) ; ? >
Find profession in a special family | Function to get no of set bits in binary representation of passed binary no . ; 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 . ; Count set bits in ' pos - 1' ; If set bit count is odd , then doctor , else engineer ; Driver Code
< ? php function countSetBits ( $ n ) { $ count = 0 ; while ( $ n ) { $ n &= ( $ n - 1 ) ; $ count ++ ; } return $ count ; } function findProffesion ( $ level , $ pos ) { $ c = countSetBits ( $ pos - 1 ) ; return ( $ c % 2 ) ? ' d ' : ' e ' ; } $ level = 3 ; $ pos = 4 ; if ( ( findProffesion ( $ level , $ pos ) == ' e ' ) == true ) echo " Engineer ▁ STRNEWLINE " ; else echo " Doctor ▁ STRNEWLINE " ; ? >
Implement rand3 ( ) using rand2 ( ) | 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 0 , 1 or 2 with equal probability 1 with 75 % ; returns 0 , 1 , 2 or 3 with 25 % probability ; Driver Code
< ? php function rand2 ( ) { return rand ( ) & 1 ; } function rand3 ( ) { $ r = 2 * rand2 ( ) + rand2 ( ) ; if ( $ r < 3 ) return $ r ; return rand3 ( ) ; } srand ( time ( NULL ) ) ; for ( $ i = 0 ; $ i < 100 ; $ i ++ ) echo rand3 ( ) ; ? >
Find a distinct pair ( x , y ) in given range such that x divides y | Function to return the possible pair ; ans1 , ans2 store value of x and y respectively ; Driver Code
< ? php function findpair ( $ l , $ r ) { $ ans1 = $ l ; $ ans2 = 2 * $ l ; echo ( $ ans1 . " , ▁ " . $ ans2 ) ; } $ l = 1 ; $ r = 10 ; findpair ( $ l , $ r ) ;
Maximum elements that can be made equal with k updates | Function to calculate the maximum number of equal elements possible with atmost K increment of values . Here we have done sliding window to determine that whether there are x number of elements present which on increment will become equal . The loop here will run in fashion like 0. . . x - 1 , 1. . . x , 2. . . x + 1 , ... . , n - x - 1. . . n - 1 ; It can be explained with the reasoning that if for some x number of elements we can update the values then the increment to the segment ( i to j having length -> x ) so that all will be equal is ( x * maxx [ j ] ) this is the total sum of segment and ( pre [ j ] - pre [ i ] ) is present sum So difference of them should be less than k if yes , then that segment length ( x ) can be possible return true ; sort the array in ascending order ; Initializing the prefix array and maximum array ; Calculating prefix sum of the array ; Calculating max value upto that position in the array ; Binary search applied for computation here ; printing result ; Driver code
< ? php function ElementsCalculationFunc ( $ pre , $ maxx , $ x , $ k , $ n ) { for ( $ i = 0 , $ j = $ x ; $ j <= $ n ; $ j ++ , $ i ++ ) { if ( $ x * $ maxx [ $ j ] - ( $ pre [ $ j ] - $ pre [ $ i ] ) <= $ k ) return true ; } return false ; } function MaxNumberOfElements ( $ a , $ n , $ k ) { sort ( $ a ) ; for ( $ i = 0 ; $ i <= $ n ; ++ $ i ) { $ pre [ $ i ] = 0 ; $ maxx [ $ i ] = 0 ; } for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ pre [ $ i ] = $ pre [ $ i - 1 ] + $ a [ $ i - 1 ] ; $ maxx [ $ i ] = max ( $ maxx [ $ i - 1 ] , $ a [ $ i - 1 ] ) ; } $ l = 1 ; $ r = $ n ; $ ans ; while ( $ l < $ r ) { $ mid = ( $ l + $ r ) / 2 ; if ( ElementsCalculationFunc ( $ pre , $ maxx , $ mid - 1 , $ k , $ n ) ) { $ ans = $ mid ; $ l = $ mid + 1 ; } else $ r = $ mid - 1 ; } echo $ ans , " STRNEWLINE " ; } $ arr = array ( 2 , 4 , 9 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; $ k = 3 ; MaxNumberOfElements ( $ arr , $ n , $ k ) ; #This  code is contributed by akt_mit. NEW_LINE ? >