"Subtract 1 without arithmetic operators | PHP code to subtract one from a given number ; Flip all the set bits until we find a 1 ; flip the rightmost 1 bit ; Driver Code"
Solution in php:

< ? php function subtractOne ( $ x ) { $ m = 1 ; while ( ! ( $ x & $ m ) ) { $ x = $ x ^ $ m ; $ m <<= 1 ; } $ x = $ x ^ $ m ; return $ x ; } echo subtractOne ( 13 ) ; ? >


"Sum of Area of all possible square inside a rectangle | Function to calculate the sum of area of all possible squares that comes inside the rectangle ; Square with max size possible ; calculate total square of a given size ; calculate area of squares of a particular size ; total area ; increment size ; Driver Code"
Solution in php:

< ? php function calculateAreaSum ( $ l , $ b ) { $ size = 1 ; $ maxSize = min ( $ l , $ b ) ; $ totalArea = 0 ; for ( $ i = 1 ; $ i <= $ maxSize ; $ i ++ ) { $ totalSquares = ( $ l - $ size + 1 ) * ( $ b - $ size + 1 ) ; $ area = $ totalSquares * $ size * $ size ; $ totalArea += $ area ; $ size ++ ; } return $ totalArea ; } $ l = 4 ; $ b = 3 ; echo calculateAreaSum ( $ l , $ b ) ; ? >


"Sum of Fibonacci Numbers with alternate negatives | Computes value of first fibonacci numbers and stores their alternate sum ; Initialize result ; Add remaining terms ; For even terms ; For odd terms ; Return the alternating sum ; Get n ; Find the alternating sum"
Solution in php:

< ? php function calculateAlternateSum ( $ n ) { if ( $ n <= 0 ) return 0 ; $ fibo = array ( ) ; $ fibo [ 0 ] = 0 ; $ fibo [ 1 ] = 1 ; $ sum = pow ( $ fibo [ 0 ] , 2 ) + pow ( $ fibo [ 1 ] , 2 ) ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { $ fibo [ $ i ] = $ fibo [ $ i - 1 ] + $ fibo [ $ i - 2 ] ; if ( $ i % 2 == 0 ) $ sum -= $ fibo [ $ i ] ; else $ sum += $ fibo [ $ i ] ; } return $ sum ; } $ n = 8 ; echo ( " Alternating ▁ Fibonacci ▁ Sum ▁ upto ▁ " ) ; echo $ n ; echo " ▁ terms : ▁ " ; echo ( calculateAlternateSum ( $ n ) ) ; ? >


"Sum of all odd length palindromic numbers within the range [ L , R ] | Function that returns true if the given number is a palindrome ; Here we are generating a new number ( reverse_num ) by reversing the digits of original input number ; If the original input number ( num ) is equal to its reverse ( reverse_num ) then its palindrome else it is not . ; Function that returns true if the given number is of odd length ; Function to return the sum of all odd length palindromic numbers within the given range ; if number is palindrome and of odd length ; Driver code"
Solution in php:

< ? php function isPalindrome ( $ num ) { $ reverse_num = 0 ; $ remainder ; $ temp ; $ temp = $ num ; while ( $ temp != 0 ) { $ remainder = $ temp % 10 ; $ reverse_num = $ reverse_num * 10 + $ remainder ; $ temp = ( int ) ( $ temp / 10 ) ; } if ( $ reverse_num == $ num ) { return true ; } return false ; } function isOddLength ( $ num ) { $ count = 0 ; while ( $ num > 0 ) { $ num = ( int ) ( $ num / 10 ) ; $ count ++ ; } if ( $ count % 2 != 0 ) { return true ; } return false ; } function sumOfAllPalindrome ( $ L , $ R ) { $ sum = 0 ; if ( $ L <= $ R ) for ( $ i = $ L ; $ i <= $ R ; $ i ++ ) { if ( isPalindrome ( $ i ) && isOddLength ( $ i ) ) { $ sum += $ i ; } } return $ sum ; } $ L = 110 ; $ R = 1130 ; echo sumOfAllPalindrome ( $ L , $ R ) ; ? >


"Sum of numbers from 1 to N which are divisible by 3 or 4 | Function to calculate the sum of numbers divisible by 3 or 4 ; Driver Code"
Solution in php:


"Sum of the multiples of two numbers below N | Function to return the sum of all the integers below N which are multiples of either A or B ; If i is a multiple of a or b ; Driver code"
Solution in php:


"Sum of two numbers where one number is represented as array of digits | Function to return the vector containing the answer ; Vector v is to store each digits sum and vector ans is to store the answer ; No carry in the beginning ; Start loop from the end and take element one by one ; Array index and last digit of number ; Maintain carry of summation ; Push the digit value into the array ; K value is greater then 0 ; Push digits of K one by one in the array ; Also maintain carry with summation ; Reverse the elements of vector v and store it in vector ans ; Driver code ; Print the answer"
Solution in php:

< ? php function addToArrayForm ( $ A , $ K ) { $ v = array ( ) ; $ ans = array ( ) ; $ rem = 0 ; $ i = 0 ; for ( $ i = count ( $ A ) - 1 ; $ i >= 0 ; $ i -- ) { $ my = $ A [ $ i ] + $ K % 10 + $ rem ; if ( $ my > 9 ) { $ rem = 1 ; array_push ( $ v , $ my % 10 ) ; } else { array_push ( $ v , $ my ) ; $ rem = 0 ; } $ K = floor ( $ K / 10 ) ; } while ( $ K > 0 ) { $ my = $ K % 10 + $ rem ; array_push ( $ v , $ my % 10 ) ; if ( $ my / 10 > 0 ) $ rem = 1 ; else $ rem = 0 ; $ K = floor ( $ K / 10 ) ; } if ( $ rem > 0 ) array_push ( $ v , $ rem ) ; for ( $ i = count ( $ v ) - 1 ; $ i >= 0 ; $ i -- ) array_push ( $ ans , $ v [ $ i ] ) ; return $ ans ; } $ A = array ( 2 , 7 , 4 ) ; $ K = 181 ; $ ans = addToArrayForm ( $ A , $ K ) ; for ( $ i = 0 ; $ i < count ( $ ans ) ; $ i ++ ) echo $ ans [ $ i ] ; ? >


"Ternary Search | Function to perform Ternary Search ; Find the mid1 and mid2 ; Check if key is present at any mid ; Since key is not present at mid , check in which region it is present then repeat the Search operation in that region ; The key lies in between l and mid1 ; The key lies in between mid2 and r ; The key lies in between mid1 and mid2 ; Key not found ; Get the array Sort the array if not sorted ; Starting index ; length of array ; Key to be searched in the array ; Search the key using ternarySearch ; Print the result ; Key to be searched in the array ; Search the key using ternarySearch ; Print the result"
Solution in php:

 < ? php function ternarySearch ( $ l , $ r , $ key , $ ar ) { if ( $ r >= $ l ) { $ mid1 = ( int ) ( $ l + ( $ r - $ l ) / 3 ) ; $ mid2 = ( int ) ( $ r - ( $ r - $ l ) / 3 ) ; if ( $ ar [ $ mid1 ] == $ key ) { return $ mid1 ; } if ( $ ar [ $ mid2 ] == $ key ) { return $ mid2 ; } if ( $ key < $ ar [ $ mid1 ] ) { return ternarySearch ( $ l , $ mid1 - 1 , $ key , $ ar ) ; } else if ( $ key > $ ar [ $ mid2 ] ) { return ternarySearch ( $ mid2 + 1 , $ r , $ key , $ ar ) ; } else { return ternarySearch ( $ mid1 + 1 , $ mid2 - 1 , $ key , $ ar ) ; } } return -1 ; } $ ar = array ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ) ; $ l = 0 ; $ r = 9 ; $ key = 5 ; $ p = ternarySearch ( $ l , $ r , $ key , $ ar ) ; echo " Index ▁ of ▁ " , $ key , " ▁ is ▁ " , ( int ) $ p , "
 " ; $ key = 50 ; $ p = ternarySearch ( $ l , $ r , $ key , $ ar ) ; echo " Index ▁ of ▁ " , $ key , " ▁ is ▁ " , ( int ) $ p , "
 " ; ? >


"Tetranacci Numbers | Function to print the N - th tetranacci number ; Initialize first four numbers to base cases ; declare a current variable ; Loop to add previous four numbers for each number starting from 4 and then assign first , second , third to second , third , fourth and curr to fourth respectively ; Driver code"
Solution in php:

< ? php function printTetra ( $ n ) { if ( $ n < 0 ) return ; $ first = 0 ; $ second = 1 ; $ third = 1 ; $ fourth = 2 ; $ curr ; if ( $ n == 0 ) echo $ first ; else if ( $ n == 1 $ n == 2 ) echo $ second ; else if ( $ n == 3 ) echo $ fourth ; else { for ( $ i = 4 ; $ i <= $ n ; $ i ++ ) { $ curr = $ first + $ second + $ third + $ fourth ; $ first = $ second ; $ second = $ third ; $ third = $ fourth ; $ fourth = $ curr ; } echo $ curr ; } } $ n = 10 ; printTetra ( $ n ) ; ? >


"Tetranacci Numbers | Function to print the N - th tetranacci number ; base cases ; Driver code"
Solution in php:


"Tetranacci Numbers | Function to return the N - th tetranacci number ; base cases ; base cases ; base cases ; function to print the nth tetranacci number ; Driver code"
Solution in php:

< ? php function printTetraRec ( $ n ) { if ( $ n == 0 ) return 0 ; if ( $ n == 1 $ n == 2 ) return 1 ; if ( $ n == 3 ) return 2 ; else return printTetraRec ( $ n - 1 ) + printTetraRec ( $ n - 2 ) + printTetraRec ( $ n - 3 ) + printTetraRec ( $ n - 4 ) ; } function printTetra ( $ n ) { echo printTetraRec ( $ n ) . " " ; } $ n = 10 ; printTetra ( $ n ) ; ? >


"Third last digit in 5 ^ N for given N | Function to find the element ; if n < 3 ; If n is even return 6 If n is odd return 1 ; Driver code"
Solution in php:

< ? php function findThirdDigit ( $ n ) { if ( $ n < 3 ) return 0 ; return $ n & 1 ? 1 : 6 ; } $ n = 7 ; echo findThirdDigit ( $ n ) ; ? >


"Toggling k | Php program to toggle k - th bit of n ; Driver code"
Solution in php:

< ? php function toggleKthBit ( $ n , $ k ) { return ( $ n ^ ( 1 << ( $ k - 1 ) ) ) ; } $ n = 5 ; $ k = 1 ; echo toggleKthBit ( $ n , $ k ) ; ? >


"Ways to split array into two groups of same XOR value | Return the count number of ways to split array into two groups such that each grouphas equal XOR value . ; We can split only if XOR is 0. Since XOR of all is 0 , we can consider all subsets as one group . ; Driver Code"
Solution in php:

< ? php function countgroup ( $ a , $ n ) { $ xs = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ xs = $ xs ^ $ a [ $ i ] ; if ( $ xs == 0 ) return ( 1 << ( $ n - 1 ) ) - 1 ; return 0 ; } $ a = array ( 1 , 2 , 3 ) ; $ n = count ( $ a ) ; echo countgroup ( $ a , $ n ) ; ? >


"Write a program to calculate pow ( x , n ) | Extended version of power function that can work for float x and negative y ; Driver Code"
Solution in php:

< ? php function power ( $ x , $ y ) { $ temp ; if ( $ y == 0 ) return 1 ; $ temp = power ( $ x , $ y / 2 ) ; if ( $ y % 2 == 0 ) return $ temp * $ temp ; else { if ( $ y > 0 ) return $ x * $ temp * $ temp ; else return ( $ temp * $ temp ) / $ x ; } } $ x = 2 ; $ y = -3 ; echo power ( $ x , $ y ) ; ? >


"Write a program to calculate pow ( x , n ) | Function to calculate x raised to the power y ; Driver Code"
Solution in php:

< ? php function power ( $ x , $ y ) { if ( $ y == 0 ) return 1 ; else if ( $ y % 2 == 0 ) return power ( $ x , ( int ) $ y / 2 ) * power ( $ x , ( int ) $ y / 2 ) ; else return $ x * power ( $ x , ( int ) $ y / 2 ) * power ( $ x , ( int ) $ y / 2 ) ; } $ x = 2 ; $ y = 3 ; echo power ( $ x , $ y ) ; ? >


"k | A function to generate prime factors of a given number n and return k - th prime factor ; Find the number of 2 's  that divide k ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; While i divides n , store i and divide n ; This condition is to handle the case where n is a prime number greater than 2 ; Driver Code"
Solution in php:


"k | Function to find k - th missing element ; interating over the array ; check if i - th and ( i + 1 ) - th element are not consecutive ; save their difference ; check for difference and given k ; if found ; Input array ; k - th missing element to be found in the array ; calling function to find missing element"
Solution in php:

< ? php function missingK ( & $ a , $ k , $ n ) { $ difference = 0 ; $ ans = 0 ; $ count = $ k ; $ flag = 0 ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { $ difference = 0 ; if ( ( $ a [ $ i ] + 1 ) != $ a [ $ i + 1 ] ) { $ difference += ( $ a [ $ i + 1 ] - $ a [ $ i ] ) - 1 ; if ( $ difference >= $ count ) { $ ans = $ a [ $ i ] + $ count ; $ flag = 1 ; break ; } else $ count -= $ difference ; } } if ( $ flag ) return $ ans ; else return -1 ; } $ a = array ( 1 , 5 , 11 , 19 ) ; $ k = 11 ; $ n = count ( $ a ) ; $ missing = missingK ( $ a , $ k , $ n ) ; echo $ missing ; ? >


"k | Function to find the sum of minimum of all subarrays ; Insert all the elements in a set ; Find the maximum and minimum element ; Traverse from the minimum to maximum element ; Check if " i " is missing ; Check if it is kth missing ; If no kth element is missing ; Driver code"
Solution in php:

< ? php function findKth ( $ arr , $ n , $ k ) { $ missing = array ( ) ; $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) array_push ( $ missing , $ arr [ $ i ] ) ; $ missing = array_unique ( $ missing ) ; $ maxm = max ( $ arr ) ; $ minm = min ( $ arr ) ; for ( $ i = $ minm + 1 ; $ i < $ maxm ; $ i ++ ) { if ( ! in_array ( $ i , $ missing , false ) ) $ count += 1 ; if ( $ count == $ k ) return $ i ; } return -1 ; } $ arr = array ( 2 , 10 , 9 , 4 ) ; $ n = sizeof ( $ arr ) ; $ k = 5 ; echo findKth ( $ arr , $ n , $ k ) ; ? >


"k | PHP program to find k - th element in the Odd - Even sequence . ; insert all the odd numbers from 1 to n . ; insert all the even numbers from 1 to n . ; Driver code"
Solution in php:


"k | PHP program to find k - th prime factor using Sieve Of Eratosthenes . This program is efficient when we have a range of numbers . ; Using SieveOfEratosthenes to find smallest prime factor of all the numbers . For example , if MAX is 10 , s [ 2 ] = s [ 4 ] = s [ 6 ] = s [ 10 ] = 2 s [ 3 ] = s [ 9 ] = 3 s [ 5 ] = 5 s [ 7 ] = 7 ; Create a boolean array " prime [ 0 . . MAX ] " and initialize all entries in it as false . ; Initializing smallest factor equal to 2 for all the even numbers ; For odd numbers less then equal to n ; s ( i ) for a prime is the number itself ; For all multiples of current prime number ; i is the smallest prime factor for number " i * j " . ; Function to generate prime factors and return its k - th prime factor . s [ i ] stores least prime factor of i . ; Keep dividing n by least prime factor while either n is not 1 or count of prime factors is not k . ; To keep track of count of prime factors ; Divide n to find next prime factor ; s [ i ] is going to store prime factor of i ."
Solution in php:

< ? php $ MAX = 10001 ; function sieveOfEratosthenes ( & $ s ) { global $ MAX ; $ prime = array_fill ( 0 , $ MAX + 1 , false ) ; for ( $ i = 2 ; $ i <= $ MAX ; $ i += 2 ) $ s [ $ i ] = 2 ; for ( $ i = 3 ; $ i <= $ MAX ; $ i += 2 ) { if ( $ prime [ $ i ] == false ) { $ s [ $ i ] = $ i ; for ( $ j = $ i ; $ j * $ i <= $ MAX ; $ j += 2 ) { if ( $ prime [ $ i * $ j ] == false ) { $ prime [ $ i * $ j ] = true ; $ s [ $ i * $ j ] = $ i ; } } } } } function kPrimeFactor ( $ n , $ k , $ s ) { while ( $ n > 1 ) { if ( $ k == 1 ) return $ s [ $ n ] ; $ k -- ; $ n = ( int ) ( $ n / $ s [ $ n ] ) ; } return -1 ; } $ s = array_fill ( 0 , $ MAX + 1 , -1 ) ; sieveOfEratosthenes ( $ s ) ; $ n = 12 ; $ k = 3 ; print ( kPrimeFactor ( $ n , $ k , $ s ) . " " ) ; $ n = 14 ; $ k = 3 ; print ( kPrimeFactor ( $ n , $ k , $ s ) ) ; ? >


"k | Returns k - th distinct element in arr . ; Check if current element is present somewhere else . ; If element is unique ; Driver Code"
Solution in php:

< ? php function printKDistinct ( $ arr , $ n , $ k ) { $ dist_count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ j ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) if ( $ i != $ j && $ arr [ $ j ] == $ arr [ $ i ] ) break ; if ( $ j == $ n ) $ dist_count ++ ; if ( $ dist_count == $ k ) return $ arr [ $ i ] ; } return -1 ; } $ ar = array ( 1 , 2 , 1 , 3 , 4 , 2 ) ; $ n = sizeof ( $ ar ) / sizeof ( $ ar [ 0 ] ) ; $ k = 2 ; echo printKDistinct ( $ ar , $ n , $ k ) ; ? >