"Perfect cubes in a range | An efficient solution to print perfect cubes between a and b ; Find cube root of both a and b ; Print cubes between acrt and bcrt ; Driver code"
How can the above be solved in PHP?

 < ? php function printCubes ( $ a , $ b ) { $ acrt = ( int ) pow ( $ a , 1 / 3 ) ; $ bcrt = ( int ) pow ( $ b , 1 / 3 ) ; for ( $ i = $ acrt ; $ i <= $ bcrt ; $ i ++ ) if ( $ i * $ i * $ i >= $ a && $ i * $ i * $ i <= $ b ) echo $ i * $ i * $ i , " ▁ " ; } $ a = 24 ; $ b = 576 ; echo " Perfect ▁ cubes ▁ in ▁ given ▁ range :
 " , printCubes ( $ a , $ b ) ; ? >


"Perfect power ( 1 , 4 , 8 , 9 , 16 , 25 , 27 , ... ) | Function that keeps all the odd power numbers upto n ; We need exclude perfect squares . ; sort the vector ; Return sum of odd and even powers . ; Driver Code"
How can the above be solved in PHP?

< ? php function powerNumbers ( $ n ) { $ v = array ( ) ; for ( $ i = 2 ; $ i * $ i * $ i <= $ n ; $ i ++ ) { $ j = $ i * $ i ; while ( $ j * $ i <= $ n ) { $ j *= $ i ; $ s = sqrt ( $ j ) ; if ( $ s * $ s != $ j ) array_push ( $ v , $ j ) ; } } sort ( $ v ) ; $ uni = array_unique ( $ v ) ; for ( $ i = 0 ; $ i < count ( $ uni ) ; $ i ++ ) { $ key = array_search ( $ uni [ $ i ] , $ v ) ; unset ( $ v [ $ key ] ) ; } return count ( $ v ) + 3 + intval ( sqrt ( $ n ) ) ; } echo ( powerNumbers ( 50 ) ) ; ? >


"Perimeter and Area of Varignon 's Parallelogram | Function to find the perimeter ; Function to find the area ; Driver code"
How can the above be solved in PHP?

 < ? php function per ( $ a , $ b ) { return ( $ a + $ b ) ; } function area ( $ s ) { return ( $ s / 2 ) ; } $ a = 7 ; $ b = 8 ; $ s = 10 ; echo ( per ( $ a , $ b ) " " ) ; echo "
 " ; echo ( area ( $ s ) ) ; ? >


"Permutations of n things taken r at a time with k things together | Function to find factorial of a number ; Function to calculate p ( n , r ) ; Function to find the number of permutations of n different things taken r at a time with k things grouped together ; Driver code"
How can the above be solved in PHP?

< ? php function factorial ( $ n ) { $ fact = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ fact = $ fact * $ i ; return $ fact ; } function npr ( $ n , $ r ) { $ pnr = factorial ( $ n ) / factorial ( $ n - $ r ) ; return $ pnr ; } function countPermutations ( $ n , $ r , $ k ) { return factorial ( $ k ) * ( $ r - $ k + 1 ) * npr ( $ n - $ k , $ r - $ k ) ; } $ n = 8 ; $ r = 5 ; $ k = 2 ; echo countPermutations ( $ n , $ r , $ k ) ; ? >


"Permutations of string such that no two vowels are adjacent | Factorial of a number ; Function to find c ( n , r ) ; Function to count permutations of string such that no two vowels are adjacent ; Finding the frequencies of the characters ; finding the no . of vowels and consonants in given word ; finding places for the vowels ; ways to fill consonants 6 ! / 2 ! ; ways to put vowels 7 C5 x 5 ! ; Driver code"
How can the above be solved in PHP?

 < ? php function factorial ( $ n ) { $ fact = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ fact = $ fact * $ i ; return $ fact ; } function ncr ( $ n , $ r ) { return factorial ( $ n ) / ( factorial ( $ r ) * factorial ( $ n - $ r ) ) ; } function countWays ( $ str ) { $ freq = array_fill ( 0 , 26 , NULL ) ; $ nvowels = 0 ; $ nconsonants = 0 ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) ++ $ freq [ ord ( $ str [ $ i ] ) - ord ( ' a ' ) ] ; for ( $ i = 0 ; $ i < 26 ; $ i ++ ) { if ( $ i == 0 $ i == 4 $ i == 8 $ i == 14 $ i == 20 ) $ nvowels += $ freq [ $ i ] ; else $ nconsonants += $ freq [ $ i ] ; } $ vplaces = $ nconsonants + 1 ; $ cways = factorial ( $ nconsonants ) ; for ( $ i = 0 ; $ i < 26 ; $ i ++ ) { if ( $ i != 0 && $ i != 4 && $ i != 8 && $ i != 14 && $ i != 20 && $ freq [ $ i ] > 1 ) { $ cways = $ cways / factorial ( $ freq [ $ i ] ) ; } } $ vways = ncr ( $ vplaces , $ nvowels ) * factorial ( $ nvowels ) ; for ( $ i = 0 ; $ i < 26 ; $ i ++ ) { if ( $ i == 0 $ i == 4 $ i == 8 $ i == 14 $ i == 20 && $ freq [ $ i ] > 1 ) { $ vways = $ vways / factorial ( $ freq [ $ i ] ) ; } } return $ cways * $ vways ; } $ str = " permutation " ; echo countWays ( $ str ) . "
 " ; return 0 ; ? >


"Permutations to arrange N persons around a circular table | Function to find no . of permutations ; Driver Code"
How can the above be solved in PHP?

< ? php function Circular ( $ n ) { $ Result = 1 ; while ( $ n > 0 ) { $ Result = $ Result * $ n ; $ n -- ; } return $ Result ; } $ n = 4 ; echo Circular ( $ n - 1 ) ; ? >


"Perpendicular distance between a point and a Line in 2 D | Function to find distance ; Driver Code"
How can the above be solved in PHP?

< ? php function shortest_distance ( $ x1 , $ y1 , $ a , $ b , $ c ) { $ d = abs ( ( $ a * $ x1 + $ b * $ y1 + $ c ) ) / ( sqrt ( $ a * $ a + $ b * $ b ) ) ; echo " Perpendicular ▁ distance ▁ is ▁ " , $ d ; } $ x1 = 5 ; $ y1 = 6 ; $ a = -2 ; $ b = 3 ; $ c = 4 ; shortest_distance ( $ x1 , $ y1 , $ a , $ b , $ c ) ; ? >


"Pierpont Prime | PHP program to print Pierpont prime numbers smaller than n . ; Finding all numbers having factor power of 2 and 3 Using sieve ; Storing number of the form 2 ^ i . 3 ^ k + 1. ; Finding prime number using sieve of Eratosthenes . Reusing same array as result of above computations in v . ; Printing n pierpont primes smaller than n ; Driver Code"
How can the above be solved in PHP?

< ? php function printPierpont ( $ n ) { $ arr = array_fill ( 0 , $ n + 1 , false ) ; $ two = 1 ; $ three = 1 ; while ( $ two + 1 < $ n ) { $ arr [ $ two ] = true ; while ( $ two * $ three + 1 < $ n ) { $ arr [ $ three ] = true ; $ arr [ $ two * $ three ] = true ; $ three *= 3 ; } $ three = 1 ; $ two *= 2 ; } $ v ; $ x = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] ) $ v [ $ x ++ ] = $ i + 1 ; $ arr1 = array_fill ( 0 , count ( $ arr ) , false ) ; for ( $ p = 2 ; $ p * $ p < $ n ; $ p ++ ) { if ( $ arr1 [ $ p ] == false ) for ( $ i = $ p * 2 ; $ i < $ n ; $ i += $ p ) $ arr1 [ $ i ] = true ; } for ( $ i = 0 ; $ i < $ x ; $ i ++ ) if ( ! $ arr1 [ $ v [ $ i ] ] ) echo $ v [ $ i ] . " ▁ " ; } $ n = 200 ; printPierpont ( $ n ) ; ? >


"Pizza cut problem ( Or Circle Division by Lines ) | Function for finding maximum pieces with n cuts . ; Driver code"
How can the above be solved in PHP?

< ? php function findMaximumPieces ( $ n ) { return 1 + $ n * ( $ n + 1 ) / 2 ; } echo findMaximumPieces ( 3 ) ; ? >


"Place k elements such that minimum distance is maximized | Returns true if it is possible to arrange k elements of arr [ 0. . n - 1 ] with minimum distance given as mid . ; Place first element at arr [ 0 ] position ; Initialize count of elements placed . ; Try placing k elements with minimum distance mid . ; Place next element if its distance from the previously placed element is greater than current mid ; Return if all elements are placed successfully ; Returns largest minimum distance for k elements in arr [ 0. . n - 1 ] . If elements can 't be placed, returns -1. ; Sort the positions ; Initialize result . ; Consider the maximum possible distance ; Do binary search for largest minimum distance ; If it is possible to place k elements with minimum distance mid , search for higher distance . ; Change value of variable max to mid iff all elements can be successfully placed ; If not possible to place k elements , search for lower distance ; Driver Code"
How can the above be solved in PHP?

< ? php function isFeasible ( $ mid , $ arr , $ n , $ k ) { $ pos = $ arr [ 0 ] ; $ elements = 1 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] - $ pos >= $ mid ) { $ pos = $ arr [ $ i ] ; $ elements ++ ; if ( $ elements == $ k ) return true ; } } return 0 ; } function largestMinDist ( $ arr , $ n , $ k ) { sort ( $ arr ) ; $ res = -1 ; $ left = 1 ; $ right = $ arr [ $ n - 1 ] ; while ( $ left < $ right ) { $ mid = ( $ left + $ right ) / 2 ; if ( isFeasible ( $ mid , $ arr , $ n , $ k ) ) { $ res = max ( $ res , $ mid ) ; $ left = $ mid + 1 ; } else $ right = $ mid ; } return $ res ; } $ arr = array ( 1 , 2 , 8 , 4 , 9 ) ; $ n = sizeof ( $ arr ) ; $ k = 3 ; echo largestMinDist ( $ arr , $ n , $ k ) ; ? >


"Polybius Square Cipher | function to display polybius cipher text ; convert each character to its encrypted code ; finding row of the table ; finding column of the table ; if character is ' k ' ; if character is greater than ' j ' ; Driver Code ; print the cipher of "geeksforgeeks"
How can the above be solved in PHP?

 < ? php function polybiusCipher ( $ s ) { $ row = 0 ; $ col = 0 ; for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) { $ row = floor ( ( ord ( $ s [ $ i ] ) - ord ( ' a ' ) ) / 5 ) + 1 ; $ col = ( ( ord ( $ s [ $ i ] ) - ord ( ' a ' ) ) % 5 ) + 1 ; if ( $ s [ $ i ] == ' k ' ) { $ row = $ row - 1 ; $ col = 5 - $ col + 1 ; } else if ( $ s [ $ i ] >= ' j ' ) { if ( $ col == 1 ) { $ col = 6 ; $ row = $ row - 1 ; } $ col = $ col - 1 ; } echo ( $ row . $ col ) ; } echo ( "
 " ) ; } $ s = " geeksforgeeks " ; polybiusCipher ( $ s ) ; ? >


"Position after taking N steps to the right and left in an alternate manner | Function to return the last destination ; Driver Code"
How can the above be solved in PHP?

< ? php function lastCoordinate ( $ n , $ a , $ b ) { return ( ( $ n + 1 ) / 2 ) * $ a - ( int ) ( $ n / 2 ) * $ b ; } $ n = 3 ; $ a = 5 ; $ b = 2 ; echo lastCoordinate ( $ n , $ a , $ b ) ; ? >


"Position of a person diametrically opposite on a circle | Function to return the required position ; Driver code"
How can the above be solved in PHP?

< ? php function getPosition ( $ n , $ m ) { if ( $ m > ( $ n / 2 ) ) return ( $ m - ( $ n / 2 ) ) ; return ( $ m + ( $ n / 2 ) ) ; } $ n = 8 ; $ m = 5 ; echo getPosition ( $ n , $ m ) ; ? >


"Position of an element after stable sort | Method returns the position of arr [ idx ] after performing stable - sort on array ; Count of elements smaller than current element plus the equal element occurring before given index ; If element is smaller then increase the smaller count ; If element is equal then increase count only if it occurs before ; Driver Code"
How can the above be solved in PHP?

< ? php function getIndexInSortedArray ( $ arr , $ n , $ idx ) { $ result = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] < $ arr [ $ idx ] ) $ result ++ ; if ( $ arr [ $ i ] == $ arr [ $ idx ] and $ i < $ idx ) $ result ++ ; } return $ result ; } $ arr = array ( 3 , 4 , 3 , 5 , 2 , 3 , 4 , 3 , 1 , 5 ) ; $ n = count ( $ arr ) ; $ idxOfEle = 5 ; echo getIndexInSortedArray ( $ arr , $ n , $ idxOfEle ) ; ? >


"Position of rightmost common bit in two numbers | Function to find the position of rightmost set bit in ' n ' ; Function to find the position of rightmost same bit in the binary representations of ' m ' and ' n ' ; position of rightmost same bit ; Driver Code"
How can the above be solved in PHP?

< ? php function getRightMostSetBit ( $ n ) { return log ( $ n & - $ n ) + 1 ; } function posOfRightMostSameBit ( $ m , $ n ) { return getRightMostSetBit ( ~ ( $ m ^ $ n ) ) ; } $ m = 16 ; $ n = 7 ; echo " Position ▁ = ▁ " , ceil ( posOfRightMostSameBit ( $ m , $ n ) ) ; ? >


"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"
How can the above be solved in PHP?

< ? 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 ) ; ? >


"Position of rightmost different bit | function to find rightmost different bit in two numbers . ; Driver code"
How can the above be solved in PHP?

< ? php function posOfRightMostDiffBit ( $ m , $ n ) { $ t = floor ( log ( $ m ^ $ n , 2 ) ) ; return $ t ; } $ m = 52 ; $ n = 4 ; echo " Position ▁ = ▁ " , posOfRightMostDiffBit ( $ m , $ n ) ; ? >


"Position of rightmost set bit | PHP Code for Position of rightmost set bit ; Driver Code"
How can the above be solved in PHP?

< ? php function getFirstSetBitPos ( $ n ) { return ceil ( log ( ( $ n & - $ n ) + 1 , 2 ) ) ; } $ n = 12 ; echo getFirstSetBitPos ( $ n ) ; ? >


"Position of rightmost set bit | PHP implementation of above approach ; counting the position of first set bit ; Driver code"
How can the above be solved in PHP?

 < ? php function Right_most_setbit ( $ num ) { $ pos = 1 ; $ INT_SIZE = 32 ; for ( $ i = 0 ; $ i < $ INT_SIZE ; $ i ++ ) { if ( ! ( $ num & ( 1 << $ i ) ) ) $ pos ++ ; else break ; } return $ pos ; } $ num = 18 ; $ pos = Right_most_setbit ( $ num ) ; echo $ pos ; echo ( "
 " ) ? >


"Position of rightmost set bit | function to find the rightmost set bit ; Position variable initialize with 1 m variable is used to check the set bit ; left shift ; Driver Code ; function call"
How can the above be solved in PHP?

< ? php function PositionRightmostSetbit ( $ n ) { $ position = 1 ; $ m = 1 ; while ( ! ( $ n & $ m ) ) { $ m = $ m << 1 ; $ position ++ ; } return $ position ; } $ n = 16 ; echo PositionRightmostSetbit ( $ n ) ; ? >


"Position of robot after given movements | function to find final position of robot after the complete movement ; traverse the instruction string ' move ' ; for each movement increment its respective counter ; required final position of robot ; Driver Code"
How can the above be solved in PHP?

< ? php function finalPosition ( $ move ) { $ l = strlen ( $ move ) ; $ countUp = 0 ; $ countDown = 0 ; $ countLeft = 0 ; $ countRight = 0 ; for ( $ i = 0 ; $ i < $ l ; $ i ++ ) { if ( $ move [ $ i ] == ' U ' ) $ countUp ++ ; else if ( $ move [ $ i ] == ' D ' ) $ countDown ++ ; else if ( $ move [ $ i ] == ' L ' ) $ countLeft ++ ; else if ( $ move [ $ i ] == ' R ' ) $ countRight ++ ; } echo " Final ▁ Position : ▁ ( " . ( $ countRight - $ countLeft ) . " , " ▁ , ▁ ( $ countUp ▁ - ▁ $ countDown ) ▁ . ▁ " ) " ▁ . " " } $ move = " UDDLLRUUUDUURUDDUULLDRRRR " ; finalPosition ( $ move ) ; ? >


"Position of the K | Function that returns the Kth set bit ; Traverse in the binary ; Check if the last bit is set or not ; Check if count is equal to k then return the index ; Increase the index as we move right ; Right shift the number by 1 ; Driver Code"
How can the above be solved in PHP?

< ? php function FindIndexKthBit ( $ n , $ k ) { $ cnt = 0 ; $ ind = 0 ; while ( $ n ) { if ( $ n & 1 ) $ cnt ++ ; if ( $ cnt == $ k ) return $ ind ; $ ind ++ ; $ n = $ n >> 1 ; } return -1 ; } $ n = 15 ; $ k = 3 ; $ ans = FindIndexKthBit ( $ n , $ k ) ; if ( $ ans != -1 ) echo $ ans ; else echo " No ▁ k - th ▁ set ▁ bit " ; ? >


"Positive elements at even and negative at odd positions ( Relative order not maintained ) | PHP program to rearrange positive and negative numbers ; Move forward the positive pointer till negative number number not encountered ; Move forward the negative pointer till positive number number not encountered ; Swap array elements to fix their position . ; Break from the while loop when any index exceeds the size of the array ; Driver code"
How can the above be solved in PHP?

< ? php function rearrange ( & $ a , $ size ) { $ positive = 0 ; $ negative = 1 ; while ( true ) { while ( $ positive < $ size && $ a [ $ positive ] >= 0 ) $ positive += 2 ; while ( $ negative < $ size && $ a [ $ negative ] <= 0 ) $ negative += 2 ; if ( $ positive < $ size && $ negative < $ size ) { $ temp = $ a [ $ positive ] ; $ a [ $ positive ] = $ a [ $ negative ] ; $ a [ $ negative ] = $ temp ; } else break ; } } $ arr = array ( 1 , -3 , 5 , 6 , -3 , 6 , 7 , -4 , 9 , 10 ) ; $ n = sizeof ( $ arr ) ; rearrange ( $ arr , $ n ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; ? >


"Possibility of a word from a given set of characters | PHP program to check if a query string is present is given set . ; Count occurrences of all characters in s . ; Check if number of occurrences of every character in q is less than or equal to that in s . ; Driver Code"
How can the above be solved in PHP?

< ? php function isPresent ( $ s , $ q ) { $ freq = array ( 256 ) ; for ( $ i = 0 ; $ i < 256 ; $ i ++ ) $ freq [ $ i ] = 0 ; for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) $ freq [ ord ( $ s [ $ i ] ) - ord ( ' a ' ) ] ++ ; for ( $ i = 0 ; $ i < strlen ( $ q ) ; $ i ++ ) { $ freq [ ord ( $ q [ $ i ] ) - ord ( ' a ' ) ] -- ; if ( $ freq [ ord ( $ q [ $ i ] ) - ord ( ' a ' ) ] < 0 ) return false ; } return true ; } $ s = " abctd " ; $ q = " cat " ; if ( isPresent ( $ s , $ q ) ) echo " Yes " ; else echo " No " ; ? >


"Possible cuts of a number such that maximum parts are divisible by 3 | Function to find the maximum number of numbers divisible by 3 in a large number ; store size of the string ; Stores last index of a remainder ; last visited place of remainder zero is at 0. ; To store result from 0 to i ; get the remainder ; Get maximum res [ i ] value ; Driver Code"
How can the above be solved in PHP?

 < ? php function MaximumNumbers ( $ s ) { $ n = strlen ( $ s ) ; $ remIndex = array_fill ( 0 , 3 , -1 ) ; $ remIndex [ 0 ] = 0 ; $ res = array ( ) ; $ r = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ r = ( $ r + $ s [ $ i - 1 ] - '0' ) % 3 ; $ res [ $ i ] = $ res [ $ i - 1 ] ; if ( $ remIndex [ $ r ] != -1 ) $ res [ $ i ] = max ( $ res [ $ i ] , $ res [ $ remIndex [ $ r ] ] + 1 ) ; $ remIndex [ $ r ] = $ i + 1 ; } return $ res [ $ n ] ; } $ s = "12345" ; print ( MaximumNumbers ( $ s ) ) # This  code is contributed by Ryuga
? >


"Possible cuts of a number such that maximum parts are divisible by 3 | PHP program to find the maximum number of numbers divisible by 3 in a large number ; This will contain the count of the splits ; This will keep sum of all successive integers , when they are indivisible by 3 ; This is the condition of finding a split ; Driver Code"
How can the above be solved in PHP?

< ? php function get_max_splits ( $ num_string ) { $ count = 0 ; $ running_sum = 0 ; for ( $ i = 0 ; $ i < strlen ( $ num_string ) ; $ i ++ ) { $ current_num = intval ( $ num_string [ $ i ] ) ; $ running_sum += $ current_num ; if ( $ current_num % 3 == 0 or ( $ running_sum != 0 and $ running_sum % 3 == 0 ) ) { $ count += 1 ; $ running_sum = 0 ; } } return $ count ; } print ( get_max_splits ( "12345" ) ) ; ? >


"Possible moves of knight | PHP program to find number of possible moves of knight ; To calculate possible moves ; All possible moves of a knight ; Check if each possible move is valid or not ; Position of knight after move ; count valid moves ; Return number of possible moves ; Driver Code"
How can the above be solved in PHP?


"Possible to form a triangle from array values | Method prints possible triangle when array values are taken as sides ; If number of elements are less than 3 , then no triangle is possible ; first sort the array ; loop for all 3 consecutive triplets ; If triplet satisfies triangle condition , break ; Driver Code"
How can the above be solved in PHP?

< ? php function isPossibleTriangle ( $ arr , $ N ) { if ( $ N < 3 ) return false ; sort ( $ arr ) ; for ( $ i = 0 ; $ i < $ N - 2 ; $ i ++ ) if ( $ arr [ $ i ] + $ arr [ $ i + 1 ] > $ arr [ $ i + 2 ] ) return true ; } $ arr = array ( 5 , 4 , 3 , 1 , 2 ) ; $ N = count ( $ arr ) ; if ( isPossibleTriangle ( $ arr , $ N ) ) echo " Yes " ; else echo " No " ; ? >


"Possible two sets from first N natural numbers difference of sums as D | Function returns true if it is possible to split into two sets otherwise returns false ; Driver code"
How can the above be solved in PHP?

< ? php function check ( $ N , $ D ) { $ temp = ( $ N * ( $ N + 1 ) ) / 2 + $ D ; return ( $ temp % 2 == 0 ) ; } $ N = 5 ; $ M = 7 ; if ( check ( $ N , $ M ) ) echo ( " yes " ) ; else echo ( " no " ) ;


"Powerful Number | function to check if the number is powerful ; First divide the number repeatedly by 2 ; If only 2 ^ 1 divides n ( not higher powers ) , then return false ; if n is not a power of 2 then this loop will execute repeat above process ; Find highest power of " factor " that divides n ; If only factor ^ 1 divides n ( not higher powers ) , then return false ; n must be 1 now if it is not a prime number . Since prime numbers are not powerful , we return false if n is not 1. ; Driver Code"
How can the above be solved in PHP?

 < ? php function isPowerful ( $ n ) { while ( $ n % 2 == 0 ) { $ power = 0 ; while ( $ n % 2 == 0 ) { $ n /= 2 ; $ power ++ ; } if ( $ power == 1 ) return false ; } for ( $ factor = 3 ; $ factor <= sqrt ( $ n ) ; $ factor += 2 ) { $ power = 0 ; while ( $ n % $ factor == 0 ) { $ n = $ n / $ factor ; $ power ++ ; } if ( $ power == 1 ) return false ; } return ( $ n == 1 ) ; } $ d = isPowerful ( 20 ) ? " YES
 " : " NO
 " ; echo $ d ; $ d = isPowerful ( 27 ) ? " YES
 " : " NO
 " ; echo $ d ; ? >


"Powers of 2 to required sum | PHP program to find the blocks for given number . ; Convert decimal number to its binary equivalent ; Displaying the output when the bit is '1' in binary equivalent of number . ; Driver Code"
How can the above be solved in PHP?

 < ? php function block ( $ x ) { $ v = array ( ) ; echo ' locks for ' . $ x . ' ▁ : ▁ ' ; while ( $ x > 0 ) { array_push ( $ v , intval ( $ x % 2 ) ) ; $ x = intval ( $ x / 2 ) ; } for ( $ i = 0 ; $ i < sizeof ( $ v ) ; $ i ++ ) { if ( $ v [ $ i ] == 1 ) { print $ i ; if ( $ i != sizeof ( $ v ) - 1 ) echo ' , ▁ ' ; } } echo "
 " ; } block ( 71307 ) ; block ( 1213 ) ; block ( 29 ) ; block ( 100 ) ; ? >


"Predict the winner of the game on the basis of absolute difference of sum by selecting numbers | Function to decide the winner ; Iterate for all numbers in the array ; If mod gives 0 ; If mod gives 1 ; If mod gives 2 ; If mod gives 3 ; Check the winning condition for X ; Driver code"
How can the above be solved in PHP?

< ? php function decideWinner ( $ a , $ n ) { $ count0 = 0 ; $ count1 = 0 ; $ count2 = 0 ; $ count3 = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] % 4 == 0 ) $ count0 ++ ; else if ( $ a [ $ i ] % 4 == 1 ) $ count1 ++ ; else if ( $ a [ $ i ] % 4 == 2 ) $ count2 ++ ; else if ( $ a [ $ i ] % 4 == 3 ) $ count3 ++ ; } if ( $ count0 % 2 == 0 && $ count1 % 2 == 0 && $ count2 % 2 == 0 && $ count3 == 0 ) return 1 ; else return 2 ; } $ a = array ( 4 , 8 , 5 , 9 ) ; $ n = count ( $ a ) ; if ( decideWinner ( $ a , $ n ) == 1 ) echo " X ▁ wins " ; else echo " Y ▁ wins " ; ? >


"Prefixes with more a than b | Function to count prefixes ; calculating for string S ; count == 0 or when N == 1 ; when all characters are a or a - b == 0 ; checking for saturation of string after repetitive addition ; Driver Code"
How can the above be solved in PHP?


"Previous greater element | php program previous greater element A naive solution to print previous greater element for every element in an array . ; Previous greater for first element never exists , so we print - 1. ; Let us process remaining elements . ; Find first element on left side that is greater than arr [ i ] . ; If all elements on left are smaller . ; Driver code"
How can the above be solved in PHP?


"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"
How can the above be solved in PHP?

< ? 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 ) ; ? >


"Primality Test | Set 1 ( Introduction and School Method ) | A school method based PHP program to check if a number is prime ; Corner case ; Check from 2 to n - 1 ; Driver Code"
How can the above be solved in PHP?

 < ? php function isPrime ( $ n ) { if ( $ n <= 1 ) return false ; for ( $ i = 2 ; $ i < $ n ; $ i ++ ) if ( $ n % $ i == 0 ) return false ; return true ; } $ tet = isPrime ( 11 ) ? " ▁ true
 " : " ▁ false
 " ; echo $ tet ; $ tet = isPrime ( 15 ) ? " ▁ true
 " : " ▁ false
 " ; echo $ tet ; ? >


"Primality test for the sum of digits at odd places of a number | Function that return sum of the digits at odd places ; Function that returns true if the number is prime else false ; Corner cases ; This condition is checked so that we can skip middle five numbers in the below loop ; Driver code ; Get the sum of the digits at odd places"
How can the above be solved in PHP?

< ? php function sum_odd ( $ n ) { $ sum = 0 ; $ pos = 1 ; while ( $ n ) { if ( $ pos % 2 == 1 ) $ sum += $ n % 10 ; $ n = ( int ) ( $ n / 10 ) ; $ pos ++ ; } return $ sum ; } function check_prime ( $ n ) { if ( $ n <= 1 ) return false ; if ( $ n <= 3 ) return true ; if ( $ n % 2 == 0 $ n % 3 == 0 ) return false ; for ( $ i = 5 ; $ i * $ i <= $ n ; $ i = ( $ i + 6 ) ) if ( $ n % $ i == 0 || $ n % ( $ i + 2 ) == 0 ) return false ; return true ; } $ n = 223 ; $ sum = sum_odd ( $ n ) ; if ( check_prime ( $ sum ) ) echo " YES " ; else echo " NO " ; ? >


"Prime Factor | function to print all prime factors of a given number n ; Print the number of 2 s that divide n ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; While i divides n , print i and divide n ; This condition is to handle the case when n is a prime number greater than 2 ; Driver Code"
How can the above be solved in PHP?


"Prime Factorization using Sieve O ( log n ) for multiple queries | PHP program to find prime factorization of a number n in O ( Log n ) time with precomputation allowed . ; stores smallest prime factor for every number ; Calculating SPF ( Smallest Prime Factor ) for every number till MAXN . Time Complexity : O ( nloglogn ) ; marking smallest prime factor for every number to be itself . ; separately marking spf for every even number as 2 ; checking if i is prime ; marking SPF for all numbers divisible by i ; marking spf [ j ] if it is not previously marked ; A O ( log n ) function returning primefactorization by dividing by smallest prime factor at every step ; precalculating Smallest Prime Factor ; calling getFactorization function"
How can the above be solved in PHP?

< ? php $ MAXN = 19999 ; $ spf = array_fill ( 0 , $ MAXN , 0 ) ; function sieve ( ) { global $ MAXN , $ spf ; $ spf [ 1 ] = 1 ; for ( $ i = 2 ; $ i < $ MAXN ; $ i ++ ) $ spf [ $ i ] = $ i ; for ( $ i = 4 ; $ i < $ MAXN ; $ i += 2 ) $ spf [ $ i ] = 2 ; for ( $ i = 3 ; $ i * $ i < $ MAXN ; $ i ++ ) { if ( $ spf [ $ i ] == $ i ) { for ( $ j = $ i * $ i ; $ j < $ MAXN ; $ j += $ i ) if ( $ spf [ $ j ] == $ j ) $ spf [ $ j ] = $ i ; } } } function getFactorization ( $ x ) { global $ spf ; $ ret = array ( ) ; while ( $ x != 1 ) { array_push ( $ ret , $ spf [ $ x ] ) ; if ( $ spf [ $ x ] ) $ x = ( int ) ( $ x / $ spf [ $ x ] ) ; } return $ ret ; } sieve ( ) ; $ x = 12246 ; echo " prime ▁ factorization ▁ for ▁ " . $ x . " ▁ : ▁ " ; $ p = getFactorization ( $ x ) ; for ( $ i = 0 ; $ i < count ( $ p ) ; $ i ++ ) echo $ p [ $ i ] . " ▁ " ; ? >


"Prime Number of Set Bits in Binary Representation | Set 2 | Function to create an array of prime numbers upto number ' n ' ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Append all the prime numbers to the list ; Utility function to count the number of set bits ; Driver program ; Here prime numbers are checked till the maximum number of bits possible because that the maximum bit sum possible is the number of bits itself ."
How can the above be solved in PHP?

< ? php function SieveOfEratosthenes ( $ n ) { $ prime = array_fill ( 0 , $ n + 1 , true ) ; for ( $ p = 2 ; $ p * $ p <= $ n ; $ p ++ ) { if ( $ prime [ $ p ] == true ) for ( $ i = $ p * 2 ; $ i < $ n + 1 ; $ i += $ p ) $ prime [ $ i ] = false ; } $ lis = array ( ) ; for ( $ p = 2 ; $ p <= $ n ; $ p ++ ) if ( $ prime [ $ p ] ) array_push ( $ lis , $ p ) ; return $ lis ; } function setBits ( $ n ) { $ cnt = 0 ; while ( $ n ) { if ( $ n & 1 ) $ cnt ++ ; $ n >>= 1 ; } ; return $ cnt ; } $ x = 4 ; $ y = 8 ; $ count = 0 ; $ primeArr = SieveOfEratosthenes ( ceil ( log ( $ y , 2 ) ) ) ; for ( $ i = $ x ; $ i < $ y + 1 ; $ i ++ ) { $ temp = setBits ( $ i ) ; if ( in_array ( $ temp , $ primeArr ) ) $ count += 1 ; } print ( $ count ) ; ? >


"Prime String | Function that checks if sum is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Driver code"
How can the above be solved in PHP?

 < ? php function isPrimeString ( $ str ) { $ len = strlen ( $ str ) ; $ n = 0 ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) $ n += ( int ) $ str [ $ i ] ; if ( $ n <= 1 ) return false ; if ( $ n <= 3 ) return true ; if ( $ n % 2 == 0 $ n % 3 == 0 ) return false ; for ( $ i = 5 ; $ i * $ i <= $ n ; $ i = $ i + 6 ) if ( $ n % $ i == 0 || $ n % ( $ i + 2 ) == 0 ) return false ; return true ; } $ str = " geekRam " ; if ( isPrimeString ( $ str ) ) echo " Yes " , "
 " ; else echo " No " , "
 " ; ? >


"Prime factors of a big number | function to calculate all the prime factors and count of every prime factor ; count the number of times 2 divides ; equivalent to n = n / 2 ; ; if 2 divides it ; check for all the possible numbers that can divide it ; if n at the end is a prime number . ; Driver Code"
How can the above be solved in PHP?


"Prime numbers after prime P with sum S | vector to store prime and N primes whose sum equals given S ; function to check prime number ; square root of x ; since 1 is not prime number ; if any factor is found return false ; no factor found ; function to display N primes whose sum equals S ; function to evaluate all possible N primes whose sum equals S ; if total equals S And total is reached using N primes ; display the N primes ; if total is greater than S or if index has reached last element ; add prime [ index ] to set vector ; include the ( index ) th prime to total ; remove element from set vector ; exclude ( index ) th prime ; function to generate all primes ; all primes less than S itself ; if i is prime add it to prime vector ; if primes are less than N ; Driver Code"
How can the above be solved in PHP?

 < ? php $ set = array ( ) ; $ prime = array ( ) ; function isPrime ( $ x ) { $ sqroot = sqrt ( $ x ) ; $ flag = true ; if ( $ x == 1 ) return false ; for ( $ i = 2 ; $ i <= $ sqroot ; $ i ++ ) if ( $ x % $ i == 0 ) return false ; return true ; } function display ( ) { global $ set , $ prime ; $ length = count ( $ set ) ; for ( $ i = 0 ; $ i < $ length ; $ i ++ ) echo ( $ set [ $ i ] . " ▁ " ) ; echo ( "
 " ) ; } function primeSum ( $ total , $ N , $ S , $ index ) { global $ set , $ prime ; if ( $ total == $ S && count ( $ set ) == $ N ) { display ( ) ; return ; } if ( $ total > $ S || $ index == count ( $ prime ) ) return ; array_push ( $ set , $ prime [ $ index ] ) ; primeSum ( $ total + $ prime [ $ index ] , $ N , $ S , $ index + 1 ) ; array_pop ( $ set ) ; primeSum ( $ total , $ N , $ S , $ index + 1 ) ; } function allPrime ( $ N , $ S , $ P ) { global $ set , $ prime ; for ( $ i = $ P + 1 ; $ i <= $ S ; $ i ++ ) { if ( isPrime ( $ i ) ) array_push ( $ prime , $ i ) ; } if ( count ( $ prime ) < $ N ) return ; primeSum ( 0 , $ N , $ S , 0 ) ; } $ S = 54 ; $ N = 2 ; $ P = 3 ; allPrime ( $ N , $ S , $ P ) ; ? >


"Prime points ( Points that split a number into two primes ) | Function to count number of digits ; Function to check whether a number is prime or not . Returns 0 if prime else - 1 ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to print prime points ; counting digits ; As single and double digit numbers do not have left and right number pairs ; Finding all left and right pairs . Printing the prime points accordingly . Discarding first and last index point ; Calculating left number ; Calculating right number ; Prime point condition ; No prime point found ; Driver Code"
How can the above be solved in PHP?

< ? php function countDigits ( $ n ) { $ count = 0 ; while ( $ n > 0 ) { $ count ++ ; $ n = ( int ) ( $ n / 10 ) ; } return $ count ; } function checkPrime ( $ n ) { if ( $ n <= 1 ) return -1 ; if ( $ n <= 3 ) return 0 ; if ( $ n % 2 == 0 $ n % 3 == 0 ) return -1 ; for ( $ i = 5 ; $ i * $ i <= $ n ; $ i = $ i + 6 ) if ( $ n % $ i == 0 || $ n % ( $ i + 2 ) == 0 ) return -1 ; return 0 ; } function printPrimePoints ( $ n ) { $ count = countDigits ( $ n ) ; if ( $ count == 1 $ count == 2 ) { echo " - 1" ; return ; } $ found = false ; for ( $ i = 1 ; $ i < ( $ count - 1 ) ; $ i ++ ) { $ left = ( int ) ( $ n / ( ( int ) pow ( 10 , $ count - $ i ) ) ) ; $ right = $ n % ( ( int ) pow ( 10 , $ count - $ i - 1 ) ) ; if ( checkPrime ( $ left ) == 0 && checkPrime ( $ right ) == 0 ) { echo $ i , " " ; $ found = true ; } } if ( $ found == false ) echo " - 1" ; } $ n = 2317 ; printPrimePoints ( $ n ) ; ? >


"Print ' K ' th least significant bit of a number | Function returns 1 if set , 0 if not ; Driver code ; Function call"
How can the above be solved in PHP?

< ? 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' ; ? >


"Print Bracket Number | function to print the bracket number ; used to print the bracket number for the left bracket ; used to obtain the bracket number for the right bracket ; traverse the given expression ' exp ' ; if current character is a left bracket ; print ' left _ bnum ' , ; push ' left _ bum ' on to the stack ' right _ bnum ' ; increment ' left _ bnum ' by 1 ; else if current character is a right bracket ; print the top element of stack ' right _ bnum ' it will be the right bracket number ; pop the top element from the stack ; Driver Code"
How can the above be solved in PHP?

< ? php function printBracketNumber ( $ exp , $ n ) { $ left_bnum = 1 ; $ right_bnum = array ( ) ; $ t = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ exp [ $ i ] == ' ( ' ) { echo $ left_bnum . " " ; $ right_bnum [ $ t ++ ] = $ left_bnum ; $ left_bnum ++ ; } else if ( $ exp [ $ i ] == ' ) ' ) { echo $ right_bnum [ $ t - 1 ] . " " ; $ right_bnum [ $ t - 1 ] = 1 ; $ t -- ; } } } $ exp = " ( a + ( b * c ) ) + ( d / e ) " ; $ n = strlen ( $ exp ) ; printBracketNumber ( $ exp , $ n ) ; ? >


"Print Fibonacci Series in reverse order | PHP Program to print Fibonacci series in reverse order ; assigning first and second elements ; storing sum in the preceding location ; printing array in reverse order ; Driver COde"
How can the above be solved in PHP?


"Print Fibonacci sequence using 2 variables | Simple php Program to print Fibonacci sequence ; Driver code"
How can the above be solved in PHP?


"Print N | function to generate n digit numbers ; if number generated ; Append 1 at the current number and reduce the remaining places by one ; If more ones than zeros , append 0 to the current number and reduce the remaining places by one ; Driver Code ; Function call"
How can the above be solved in PHP?

< ? php function printRec ( $ number , $ extraOnes , $ remainingPlaces ) { if ( 0 == $ remainingPlaces ) { echo ( $ number . " " ) ; return ; } printRec ( $ number . "1" , $ extraOnes + 1 , $ remainingPlaces - 1 ) ; if ( 0 < $ extraOnes ) printRec ( $ number . "0" , $ extraOnes - 1 , $ remainingPlaces - 1 ) ; } function printNums ( $ n ) { $ str = " " ; printRec ( $ str , 0 , $ n ) ; } $ n = 4 ; printNums ( $ n ) ;


"Print a case where the given sorting algorithm fails | Function to print a case where the given sorting algorithm fails ; only case where it fails ; Driver Code"
How can the above be solved in PHP?

< ? php function printCase ( $ n ) { if ( $ n <= 2 ) { echo ( -1 ) ; return ; } for ( $ i = $ n ; $ i >= 1 ; $ i -- ) { echo ( $ i ) ; echo ( " ▁ " ) ; } } $ n = 3 ; printCase ( $ n ) ; ? >


"Print a closest string that does not contain adjacent duplicates | Function to print simple string ; If any two adjacent characters are equal ; Initialize it to ' a ' ; Traverse the loop until it is different from the left and right letter . ; Driver Code"
How can the above be solved in PHP?


"Print a matrix in a spiral form starting from a point | PHP program to print a matrix in spiral form . ; Driver code ; Function calling"
How can the above be solved in PHP?

 < ? php $ MAX = 100 ; function printSpiral ( $ mat , $ r , $ c ) { global $ MAX ; $ i ; $ a = 0 ; $ b = 2 ; $ low_row = ( 0 > $ a ) ? 0 : $ a ; $ low_column = ( 0 > $ b ) ? 0 : $ b - 1 ; $ high_row = ( ( $ a + 1 ) >= $ r ) ? $ r - 1 : $ a + 1 ; $ high_column = ( ( $ b + 1 ) >= $ c ) ? $ c - 1 : $ b + 1 ; while ( ( $ low_row > 0 - $ r && $ low_column > 0 - $ c ) ) { for ( $ i = $ low_column + 1 ; $ i <= $ high_column && $ i < $ c && $ low_row >= 0 ; ++ $ i ) echo $ mat [ $ low_row ] [ $ i ] , " ▁ " ; $ low_row -= 1 ; for ( $ i = $ low_row + 2 ; $ i <= $ high_row && $ i < $ r && $ high_column < $ c ; ++ $ i ) echo $ mat [ $ i ] [ $ high_column ] , " ▁ " ; $ high_column += 1 ; for ( $ i = $ high_column - 2 ; $ i >= $ low_column && $ i >= 0 && $ high_row < $ r ; -- $ i ) echo $ mat [ $ high_row ] [ $ i ] , " ▁ " ; $ high_row += 1 ; for ( $ i = $ high_row - 2 ; $ i > $ low_row && $ i >= 0 && $ low_column >= 0 ; -- $ i ) echo $ mat [ $ i ] [ $ low_column ] , " ▁ " ; $ low_column -= 1 ; } echo "
 " ; } $ mat = array ( array ( 1 , 2 , 3 ) , array ( 4 , 5 , 6 ) , array ( 7 , 8 , 9 ) ) ; $ r = 3 ; $ c = 3 ; printSpiral ( $ mat , $ r , $ c ) ; ? >


"Print a number as string of ' A ' and ' B ' in lexicographic order | Function to calculate number of characters in corresponding string of ' A ' and ' B ' ; Since the minimum number of characters will be 1 ; Calculating number of characters ; Since k length string can represent at most pow ( 2 , k + 1 ) - 2 that is if k = 4 , it can represent at most pow ( 2 , 4 + 1 ) - 2 = 30 so we have to calculate the length of the corresponding string ; return the length of the corresponding string ; Function to print corresponding string of ' A ' and ' B ' ; Find length of string ; Since the first number that can be represented by k length string will be ( pow ( 2 , k ) - 2 ) + 1 and it will be AAA ... A , k times , therefore , N will store that how much we have to print ; At a particular time , we have to decide whether we have to print ' A ' or ' B ' , this can be check by calculating the value of pow ( 2 , k - 1 ) ; Print new line ; Driver code"
How can the above be solved in PHP?

 < ? php function no_of_characters ( $ M ) { $ k = 1 ; while ( true ) { if ( pow ( 2 , $ k + 1 ) - 2 < $ M ) $ k ++ ; else break ; } return $ k ; } function print_string ( $ M ) { $ k ; $ num ; $ N ; $ k = no_of_characters ( $ M ) ; $ N = $ M - ( pow ( 2 , $ k ) - 2 ) ; while ( $ k > 0 ) { $ num = pow ( 2 , $ k - 1 ) ; if ( $ num >= $ N ) echo " A " ; else { echo " B " ; $ N -= $ num ; } $ k -- ; } echo "
 " ; } $ M ; $ M = 30 ; print_string ( $ M ) ; $ M = 55 ; print_string ( $ M ) ; $ M = 100 ; print_string ( $ M ) ; ? >


"Print a number containing K digits with digital root D | Function to find a number ; If d is 0 k has to be 1 ; Print k - 1 zeroes ; Driver code"
How can the above be solved in PHP?

< ? php function printNumberWithDR ( $ k , $ d ) { if ( $ d == 0 && $ k != 1 ) echo " - 1" ; else { echo $ d ; $ k -- ; while ( $ k -- ) echo "0" ; } } $ k = 4 ; $ d = 4 ; printNumberWithDR ( $ k , $ d ) ; ? >


"Print a number strictly less than a given number such that all its digits are distinct . | Function to find a number less than n such that all its digits are distinct ; looping through numbers less than n ; initializing a hash array ; creating a copy of i ; initializing variables to compare lengths of digits ; counting frequency of the digits ; checking if each digit is present once ; Driver code"
How can the above be solved in PHP?


"Print all Good numbers in given range | To check whether n is a good number and doesn ' t ▁ contain ▁ digit ▁ ' d '. ; Get last digit and initialize sum from right side ; If last digit is d , return ; Traverse remaining digits ; Current digit ; If digit is d or digit is less than or equal to sum of digits on right side ; Update sum and n ; Print Good numbers in range [ L , R ] ; Traverse all numbers in given range ; If current numbers is good , print it . ; Driver Code ; Print good numbers in [ L , R ]"
How can the above be solved in PHP?

< ? php function isValid ( $ n , $ d ) { $ digit = $ n % 10 ; $ sum = $ digit ; if ( $ digit == $ d ) return false ; $ n = ( int ) ( $ n / 10 ) ; while ( $ n ) { $ digit = $ n % 10 ; if ( $ digit == $ d $ digit <= $ sum ) return false ; else { $ sum += $ digit ; $ n = ( int ) ( $ n / 10 ) ; } } return 1 ; } function printGoodNumbers ( $ L , $ R , $ d ) { for ( $ i = $ L ; $ i <= $ R ; $ i ++ ) { if ( isValid ( $ i , $ d ) ) echo $ i . " " ; } } $ L = 410 ; $ R = 520 ; $ d = 3 ; printGoodNumbers ( $ L , $ R , $ d ) ; ? >


"Print all increasing sequences of length k from first n natural numbers | A utility function to print contents of arr [ 0. . k - 1 ] ; A recursive function to print all increasing sequences of first n natural numbers . Every sequence should be length k . The array arr [ ] is used to store current sequence . ; If length of current increasing sequence becomes k , print it ; Decide the starting number to put at current position : If length is 0 , then there are no previous elements in arr [ ] . So start putting new numbers with 1. If length is not 0 , then start from value of previous element plus 1. ; Increase length ; Put all numbers ( which are greater than the previous element ) at new position . ; This is important . The variable ' len ' is shared among all function calls in recursion tree . Its value must be brought back before next iteration of while loop ; This function prints all increasing sequences of first n natural numbers . The length of every sequence must be k . This function mainly uses printSeqUtil ( ) ; An array to store individual sequences Initial length of ; current sequence ; Driver Code"
How can the above be solved in PHP?

 < ? php function printArr ( $ arr , $ k ) { for ( $ i = 0 ; $ i < $ k ; $ i ++ ) echo $ arr [ $ i ] , " ▁ " ; echo "
 " ; } function printSeqUtil ( $ n , $ k , $ len , $ arr ) { if ( $ len == $ k ) { printArr ( $ arr , $ k ) ; return ; } $ i = ( $ len == 0 ) ? 1 : $ arr [ $ len - 1 ] + 1 ; $ len ++ ; while ( $ i <= $ n ) { $ arr [ $ len - 1 ] = $ i ; printSeqUtil ( $ n , $ k , $ len , $ arr ) ; $ i ++ ; } $ len -- ; } function printSeq ( $ n , $ k ) { $ arr = array ( ) ; $ len = 0 ; printSeqUtil ( $ n , $ k , $ len , $ arr ) ; } $ k = 3 ; $ n = 7 ; printSeq ( $ n , $ k ) ; ? >


"Print all integers that are sum of powers of two given numbers | Function to print powerful integers ; Set is used to store distinct numbers in sorted order ; Store all the powers of y < bound in a vector to avoid calculating them again and again ; x ^ i ; If num is within limits insert it into the set ; Break out of the inner loop ; Adding any number to it will be out of bounds ; Increment i ; Print the contents of the set ; Driver code ; Print powerful integers"
How can the above be solved in PHP?

< ? php function powerfulIntegers ( $ x , $ y , $ bound ) { $ s = array ( ) ; $ powersOfY = array ( ) ; array_push ( $ powersOfY , 1 ) ; $ i = $ y ; while ( $ i < $ bound && $ y != 1 ) { array_push ( $ powersOfY , $ i ) ; $ i *= $ y ; } $ i = 0 ; while ( true ) { $ xPowI = pow ( $ x , $ i ) ; for ( $ j = 0 ; $ j < count ( $ powersOfY ) ; $ j ++ ) { $ num = $ xPowI + $ powersOfY [ $ j ] ; if ( $ num <= $ bound ) array_push ( $ s , $ num ) ; else break ; } if ( $ xPowI >= $ bound $ x == 1 ) break ; $ i += 1 ; } $ s = array_unique ( $ s ) ; sort ( $ s ) ; foreach ( $ s as & $ itr ) print ( $ itr . " " ) ; } $ x = 2 ; $ y = 3 ; $ bound = 10 ; powerfulIntegers ( $ x , $ y , $ bound ) ; ? >


"Print all multiplicative primes <= N | Function to return the digit product of n ; Function to print all multiplicative primes <= n ; Create a boolean array " prime [ 0 . . n + 1 ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; If i is prime and its digit sum is also prime i . e . i is a multiplicative prime ; Driver code"
How can the above be solved in PHP?

< ? php function digitProduct ( $ n ) { $ prod = 1 ; while ( $ n ) { $ prod = $ prod * ( $ n % 10 ) ; $ n = floor ( $ n / 10 ) ; } return $ prod ; } function printMultiplicativePrimes ( $ n ) { $ prime = array_fill ( 0 , $ n + 1 , true ) ; $ prime [ 0 ] = $ prime [ 1 ] = false ; for ( $ p = 2 ; $ p * $ p <= $ n ; $ p ++ ) { if ( $ prime [ $ p ] ) { for ( $ i = $ p * 2 ; $ i <= $ n ; $ i += $ p ) $ prime [ $ i ] = false ; } } for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { if ( $ prime [ $ i ] && $ prime [ digitProduct ( $ i ) ] ) echo $ i , " ▁ " ; } } $ n = 10 ; printMultiplicativePrimes ( $ n ) ; ? >


"Print all n | n , sum -- > value of inputs out -- > output array index -- > index of next digit to be filled in output array ; Base case ; If number becomes N - digit ; if sum of its digits is equal to given sum , print it ; Traverse through every digit . Note that here we ' re ▁ considering ▁ leading ▁ ▁ 0' s as digits ; append current digit to number ; recurse for next digit with reduced sum ; This is mainly a wrapper over findNDigitNumsUtil . It explicitly handles leading digit ; output array to store N - digit numbers ; fill 1 st position by every digit from 1 to 9 and calls findNDigitNumsUtil ( ) for remaining positions ; Driver Code"
How can the above be solved in PHP?

< ? php function findNDigitNumsUtil ( $ n , $ sum , $ out , $ index ) { if ( $ index > $ n $ sum < 0 ) return ; if ( $ index == $ n ) { if ( $ sum == 0 ) { $ out [ $ index ] = ' ' ; foreach ( $ out as & $ value ) print ( $ value ) ; print ( " ▁ " ) ; } return ; } for ( $ i = 0 ; $ i <= 9 ; $ i ++ ) { $ out [ $ index ] = chr ( $ i + ord ( '0' ) ) ; findNDigitNumsUtil ( $ n , $ sum - $ i , $ out , $ index + 1 ) ; } } function findNDigitNums ( $ n , $ sum ) { $ out = array_fill ( 0 , $ n + 1 , false ) ; for ( $ i = 1 ; $ i <= 9 ; $ i ++ ) { $ out [ 0 ] = chr ( $ i + ord ( '0' ) ) ; findNDigitNumsUtil ( $ n , $ sum - $ i , $ out , 1 ) ; } } $ n = 2 ; $ sum = 3 ; findNDigitNums ( $ n , $ sum ) ; ? >


"Print all n | n -- > value of input out -- > output array index -- > index of next digit to be filled in output array evenSum , oddSum -- > sum of even and odd digits so far ; Base case ; If number becomes n - digit ; if absolute difference between sum of even and odd digits is 1 , print the number ; If current index is odd , then add it to odd sum and recurse ; else else add to even sum and recurse ; This is mainly a wrapper over findNDigitNumsUtil . It explicitly handles leading digit and calls findNDigitNumsUtil ( ) for remaining indexes . ; output array to store n - digit numbers ; Initialize number index considered so far ; Initialize even and odd sums ; Explicitly handle first digit and call recursive function findNDigitNumsUtil for remaining indexes . Note that the first digit is considered to be present in even position . ; Driver program"
How can the above be solved in PHP?

< ? php function findNDigitNumsUtil ( $ n , $ out , $ index , $ evenSum , $ oddSum ) { if ( $ index > $ n ) return ; if ( $ index == $ n ) { if ( abs ( $ evenSum - $ oddSum ) == 1 ) { echo implode ( " " , $ out ) . " " } return ; } if ( $ index & 1 ) { for ( $ i = 0 ; $ i <= 9 ; $ i ++ ) { $ out [ $ index ] = $ i + '0' ; findNDigitNumsUtil ( $ n , $ out , $ index + 1 , $ evenSum , $ oddSum + $ i ) ; } } { for ( $ i = 0 ; $ i <= 9 ; $ i ++ ) { $ out [ $ index ] = $ i + '0' ; findNDigitNumsUtil ( $ n , $ out , $ index + 1 , $ evenSum + $ i , $ oddSum ) ; } } } function findNDigitNums ( $ n ) { $ out = array_fill ( 0 , $ n + 1 , " " ) ; $ index = 0 ; $ evenSum = 0 ; $ oddSum = 0 ; for ( $ i = 1 ; $ i <= 9 ; $ i ++ ) { $ out [ $ index ] = $ i + '0' ; findNDigitNumsUtil ( $ n , $ out , $ index + 1 , $ evenSum + $ i , $ oddSum ) ; } } $ n = 3 ; findNDigitNums ( $ n ) ; ? >


"Print all non | function to print array arr [ 0. . n - 1 ] ; Recursive Function to generate all non - increasing sequences with sum x arr [ ] -- > Elements of current sequence curr_sum -- > Current Sum curr_idx -- > Current index in arr [ ] ; If current sum is equal to x , then we found a sequence ; Try placing all numbers from 1 to x - curr_sum at current index ; The placed number must also be smaller than previously placed numbers and it may be equal to the previous stored value , i . e . , arr [ curr_idx - 1 ] if there exists a previous number ; Place number at curr_idx ; Recur ; Try next number ; A wrapper over generateUtil ( ) ; Array to store sequences on by one ; Driver Code"
How can the above be solved in PHP?

 < ? php function printArr ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] , " ▁ " ; echo " ▁
 " ; } function generateUtil ( $ x , $ arr , $ curr_sum , $ curr_idx ) { if ( $ curr_sum == $ x ) { printArr ( $ arr , $ curr_idx ) ; return ; } $ num = 1 ; while ( $ num <= $ x - $ curr_sum and ( $ curr_idx == 0 or $ num <= $ arr [ $ curr_idx - 1 ] ) ) { $ arr [ $ curr_idx ] = $ num ; generateUtil ( $ x , $ arr , $ curr_sum + $ num , $ curr_idx + 1 ) ; $ num ++ ; } } function generate ( $ x ) { $ arr = array ( ) ; generateUtil ( $ x , $ arr , 0 , 0 ) ; } $ x = 5 ; generate ( $ x ) ; ? >


"Print all numbers whose set of prime factors is a subset of the set of the prime factors of X | Function to print all the numbers ; Iterate for every element in the array ; Find the gcd ; Iterate till gcd is 1 of number and x ; Divide the number by gcd ; Find the new gcdg ; If the number is 1 at the end then print the number ; If no numbers have been there ; Driver code"
How can the above be solved in PHP?

< ? php function printNumbers ( $ a , $ n , $ x ) { $ flag = false ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ num = $ a [ $ i ] ; $ g = __gcd ( $ num , $ x ) ; while ( $ g != 1 ) { $ num /= $ g ; $ g = __gcd ( $ num , $ x ) ; } if ( $ num == 1 ) { $ flag = true ; echo $ a [ $ i ] , " " ; } } if ( ! $ flag ) echo ( " There ▁ are ▁ no ▁ such ▁ numbers " ) ; } function __gcd ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; return __gcd ( $ b , $ a % $ b ) ; } $ x = 60 ; $ a = array ( 2 , 5 , 10 , 7 , 17 ) ; $ n = count ( $ a ) ; printNumbers ( $ a , $ n , $ x ) ; ? >


"Print all pairs with given sum | Returns number of pairs in arr [ 0. . n - 1 ] with sum equal to ' sum ' ; Initialize result ; Consider all possible pairs and check their sums ; Driver Code"
How can the above be solved in PHP?


"Print all possible strings that can be made by placing spaces | Function recursively prints the strings having space pattern . i and j are indices in ' str [ ] ' and ' buff [ ] ' respectively ; Either put the character ; Or put a space followed by next character ; This function creates buf [ ] to store individual output string and uses printPatternUtil ( ) to print all permutations . ; Buffer to hold the string containing spaces 2 n - 1 characters and 1 string terminator ; Copy the first character as it is , since it will be always at first position ; Driver code"
How can the above be solved in PHP?

 < ? php function printPatternUtil ( $ str , $ buff , $ i , $ j , $ n ) { if ( $ i == $ n ) { $ buff [ $ j ] = ' ' ; echo str_replace ( ' , ▁ ' , ' ' , implode ( ' , ▁ ' , $ buff ) ) . "
 " ; return ; } $ buff [ $ j ] = $ str [ $ i ] ; printPatternUtil ( $ str , $ buff , $ i + 1 , $ j + 1 , $ n ) ; $ buff [ $ j ] = ' ▁ ' ; $ buff [ $ j + 1 ] = $ str [ $ i ] ; printPatternUtil ( $ str , $ buff , $ i +1 , $ j + 2 , $ n ) ; } function printPattern ( $ str ) { $ n = strlen ( $ str ) ; $ buf = array_fill ( 0 , 2 * $ n , null ) ; $ buf [ 0 ] = $ str [ 0 ] ; printPatternUtil ( $ str , $ buf , 1 , 1 , $ n ) ; } $ str = " ABCD " ; printPattern ( $ str ) ; ? >


"Print all possible strings that can be made by placing spaces | Function to print all subsequences ; Driver code"
How can the above be solved in PHP?

 < ? php function printSubsequences ( $ str ) { $ n = strlen ( $ str ) ; $ opsize = pow ( 2 , $ n - 1 ) ; for ( $ counter = 0 ; $ counter < $ opsize ; $ counter ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { echo $ str [ $ j ] ; if ( $ counter & ( 1 << $ j ) ) echo " ▁ " ; } echo "
 " ; } } $ str = " ABC " ; printSubsequences ( $ str ) ; ? >


"Print all safe primes below N | Function to print first n safe primes ; Initialize all entries of integer array as 1. A value in prime [ i ] will finally be 0 if i is Not a prime , else 1 ; 0 and 1 are not primes ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; If i is prime ; 2 p + 1 ; If 2 p + 1 is also a prime then set prime [ 2 p + 1 ] = 2 ; i is a safe prime ; Driver code"
How can the above be solved in PHP?


"Print all sequences of given length | A utility function that prints a given arr [ ] of length size ; The core function that recursively generates and prints all sequences of length k ; A function that uses printSequencesRecur ( ) to prints all sequences from 1 , 1 , . .1 to n , n , . . n ; Driver Code"
How can the above be solved in PHP?

 < ? php function printArray ( $ arr , $ size ) { for ( $ i = 0 ; $ i < $ size ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; echo "
 " ; return ; } function printSequencesRecur ( $ arr , $ n , $ k , $ index ) { if ( $ k == 0 ) { printArray ( $ arr , $ index ) ; } if ( $ k > 0 ) { for ( $ i = 1 ; $ i <= $ n ; ++ $ i ) { $ arr [ $ index ] = $ i ; printSequencesRecur ( $ arr , $ n , $ k - 1 , $ index + 1 ) ; } } } function printSequences ( $ n , $ k ) { $ arr = array ( ) ; printSequencesRecur ( $ arr , $ n , $ k , 0 ) ; return ; } $ n = 3 ; $ k = 2 ; printSequences ( $ n , $ k ) ; ? >


"Print all substring of a number without any conversion | Function to print the substrings of a number ; Calculate the total number of digits ; 0.5 has been added because of it will return double value like 99.556 ; Print all the numbers from starting position ; Update the no . ; Update the no . of digits ; Driver code"
How can the above be solved in PHP?


"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"
How can the above be solved in PHP?


"Print all triplets in sorted array that form AP | Function to print all triplets in given sorted array that forms AP ; Search other two elements of AP with arr [ i ] as middle . ; if a triplet is found ; Since elements are distinct , arr [ k ] and arr [ j ] cannot form any more triplets with arr [ i ] ; If middle element is more move to higher side , else move lower side . ; Driver code"
How can the above be solved in PHP?


"Print all triplets in sorted array that form AP | Function to print all triplets in given sorted array that forms AP ; Use hash to find if there is a previous element with difference equal to arr [ j ] - arr [ i ] ; Driver code"
How can the above be solved in PHP?

< ? php function printAllAPTriplets ( $ arr , $ n ) { $ s = array ( ) ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) { $ diff = $ arr [ $ j ] - $ arr [ $ i ] ; if ( in_array ( $ arr [ $ i ] - $ diff , $ arr ) ) echo ( ( $ arr [ $ i ] - $ diff ) . " " ▁ . ▁ $ arr [ $ i ] ▁ . ▁ " " ▁ . ▁ $ arr [ $ j ] ▁ . ▁ " " } array_push ( $ s , $ arr [ $ i ] ) ; } } $ arr = array ( 2 , 6 , 9 , 12 , 17 , 22 , 31 , 32 , 35 , 42 ) ; $ n = count ( $ arr ) ; printAllAPTriplets ( $ arr , $ n ) ; ? >


"Print all triplets with given sum | Prints all triplets in arr [ ] with given sum ; Driver code"
How can the above be solved in PHP?


"Print an N x M matrix such that each row and column has all the vowels in it | Function to print the required matrix ; Impossible to generate the required matrix ; Store all the vowels ; Print the matrix ; Print vowels for every index ; Shift the vowels by one ; Driver code"
How can the above be solved in PHP?


"Print array elements in alternatively increasing and decreasing order | Function to print array elements in alternative increasing and decreasing order ; First sort the array in increasing order ; start with 2 elements in increasing order ; till all the elements are not printed ; printing the elements in increasing order ; else printing the elements in decreasing order ; increasing the number of elements to printed in next iteration ; Driver Code"
How can the above be solved in PHP?


"Print digit 's position to be removed to make a number divisible by 6 | Function to print the number divisible by 6 after exactly removing a digit ; stores the sum of all elements ; traverses the string and converts string to number array and sums up ; if ( $a [ $n - 1 ] % 2 ) ODD CHECK ; if second last is odd or sum of n - 1 elements are not divisible by 3. ; second last is even and print n - 1 elements removing last digit ; last digit removed ; counter to check if any element after removing , its sum % 3 == 0 ; traverse till second last element ; to check if any element after removing , its sum % 3 == 0 ; the leftmost element ; break at the leftmost element ; stores the right most element ; if no element has been found as a [ i + 1 ] > a [ i ] ; if second last is even , then remove last if ( sum - last ) % 3 == 0 ; if no element which on removing gives sum % 3 == 0 ; Driver Code"
How can the above be solved in PHP?


"Print equal sum sets of array ( Partition problem ) | Set 1 | Function to print the equal sum sets of the array . ; Print set 1. ; Print set 2. ; Utility function to find the sets of the array which have equal sum . ; If entire array is traversed , compare both the sums . ; If sums are equal print both sets and return true to show sets are found . ; If sums are not equal then return sets are not found . ; Add current element to set 1. ; Recursive call after adding current element to set 1. ; If this inclusion results in equal sum sets partition then return true to show desired sets are found . ; If not then backtrack by removing current element from set1 and include it in set 2. ; Recursive call after including current element to set 2. ; Return true if array arr can be partitioned into two equal sum sets or not . ; Calculate sum of elements in array . ; If sum is odd then array cannot be partitioned . ; Declare vectors to store both the sets . ; Find both the sets . ; Driver code"
How can the above be solved in PHP?

 < ? php function printSets ( $ set1 , $ set2 ) { $ i = 0 ; for ( $ i = 0 ; $ i < count ( $ set1 ) ; $ i ++ ) { echo ( $ set1 [ $ i ] . " " ) ; } echo ( "
 " ) ; for ( $ i = 0 ; $ i < count ( $ set2 ) ; $ i ++ ) { echo ( $ set2 [ $ i ] . " " ) ; } } function findSets ( $ arr , $ n , & $ set1 , & $ set2 , $ sum1 , $ sum2 , $ pos ) { if ( $ pos == $ n ) { if ( $ sum1 == $ sum2 ) { printSets ( $ set1 , $ set2 ) ; return true ; } else return false ; } array_push ( $ set1 , $ arr [ $ pos ] ) ; $ res = findSets ( $ arr , $ n , $ set1 , $ set2 , $ sum1 + $ arr [ $ pos ] , $ sum2 , $ pos + 1 ) ; if ( $ res ) return $ res ; array_pop ( $ set1 ) ; array_push ( $ set2 , $ arr [ $ pos ] ) ; return findSets ( $ arr , $ n , $ set1 , $ set2 , $ sum1 , $ sum2 + $ arr [ $ pos ] , $ pos + 1 ) ; } function isPartitionPoss ( $ arr , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += $ arr [ $ i ] ; if ( $ sum % 2 != 0 ) return false ; $ set1 = array ( ) ; $ set2 = array ( ) ; return findSets ( $ arr , $ n , $ set1 , $ set2 , 0 , 0 , 0 ) ; } $ arr = array ( 5 , 5 , 1 , 11 ) ; $ n = count ( $ arr ) ; if ( isPartitionPoss ( $ arr , $ n ) == false ) echo ( " - 1" ) ; ? >


"Print factorials of a range in right aligned format | PHP Program to print format of factorial ; vector for store the result ; variable for store the each number factorial ; copy of first number ; found first number factorial ; push the first number in result vector ; incerement the first number ; found the all reaming number factorial loop is working until all required number factorial founded ; store the result of factorial ; incerement the first number ; return the result ; function for print the result ; setw ( ) is used for fill the blank right is used for right justification of data ; number which found the factorial of between range ; store the result of factorial ; function for found factorial ; function for print format"
How can the above be solved in PHP?

< ? php function find_factorial ( $ num1 , $ num2 ) { $ vec ; $ t = 0 ; $ fac = 1 ; $ temp = $ num1 ; while ( 1 ) { if ( $ temp == 1 ) break ; $ fac *= $ temp ; $ temp -- ; } $ vec [ $ t ++ ] = $ fac ; $ num1 ++ ; while ( $ num1 <= $ num2 ) { $ fac *= $ num1 ; $ vec [ $ t ++ ] = $ fac ; $ num1 ++ ; } return ( $ vec ) ; } function print_format ( $ result ) { $ x = count ( $ result ) ; $ digits = strlen ( ( string ) $ result [ $ x - 1 ] ) ; for ( $ i = 0 ; $ i < $ x ; $ i ++ ) { echo str_pad ( $ result [ $ i ] , ( $ digits + 1 ) , " " , ▁ STR _ PAD _ LEFT ) ▁ . ▁ " " } } $ m = 10 ; $ n = 20 ; $ result_fac ; $ result_fac = find_factorial ( $ m , $ n ) ; print_format ( $ result_fac ) ; ? >


"Print first k digits of 1 / n where n is a positive integer | Function to print first k digits after dot in value of 1 / n . n is assumed to be a positive integer . ; Initialize remainder ; Run a loop k times to print k digits ; The next digit can always be obtained as doing ( 10 * rem ) / 10 ; Update remainder ; Driver Code"
How can the above be solved in PHP?


"Print first n Fibonacci Numbers using direct formula | Function to calculate fibonacci using recurrence relation formula ; Using direct formula ; Driver code"
How can the above be solved in PHP?


"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"
How can the above be solved in PHP?

< ? 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 ) ; ? >


"Print given sentence into its equivalent ASCII form | Function to compute the ASCII value of each character one by one ; Driver code"
How can the above be solved in PHP?

 < ? php function ASCIISentence ( $ str ) { for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) echo ord ( $ str [ $ i ] ) ; } $ str = " GeeksforGeeks " ; echo " ASCII ▁ Sentence : " . "
 " ; ASCIISentence ( $ str ) ; ? >


"Print k numbers where all pairs are divisible by m | function to generate k numbers whose difference is divisible by m ; Using an adjacency list like representation to store numbers that lead to same remainder . ; stores the modulus when divided by m ; If we found k elements which have same remainder . ; If we could not find k elements ; Driver Code"
How can the above be solved in PHP?


"Print last character of each word in a string | Function to print the last character of each word in the given string ; Now , last word is also followed by a space ; If current character is a space ; Then previous character must be the last character of some word ; Driver code"
How can the above be solved in PHP?

< ? php function printLastChar ( $ str ) { $ str = $ str . " ▁ " ; for ( $ i = 1 ; $ i < strlen ( $ str ) ; $ i ++ ) { if ( ! strcmp ( $ str [ $ i ] , ' ▁ ' ) ) echo ( $ str [ $ i - 1 ] . " ▁ " ) ; } } $ str = " Geeks ▁ for ▁ Geeks " ; printLastChar ( $ str ) ; ? >


"Print last k digits of a ^ b ( a raised to power b ) | Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; $x = $x % $p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now $y = $y >> 1 ; y = y / 2 ; function to calculate number of digits in x ; function to print last k digits of a ^ b ; Generating 10 ^ k ; Calling modular exponentiation ; Printing leftmost zeros . Since ( a ^ b ) % k can have digits less then k . In that case we need to print zeros ; If temp is not zero then print temp If temp is zero then already printed ; Driver Code"
How can the above be solved in PHP?

< ? php function power ( $ x , $ y , $ p ) { $ res = 1 ; while ( $ y > 0 ) { if ( $ y & 1 ) $ res = ( $ res * $ x ) % $ p ; $ x = ( $ x * $ x ) % $ p ; } return $ res ; } function numberOfDigits ( $ x ) { $ i = 0 ; while ( $ x ) { $ x = ( int ) $ x / 10 ; $ i ++ ; } return $ i ; } function printLastKDigits ( $ a , $ b , $ k ) { echo " Last ▁ " , $ k ; echo " ▁ digits ▁ of ▁ " , $ a ; echo " ^ " , $ b , " ▁ = ▁ " ; $ temp = 1 ; for ( $ i = 1 ; $ i <= $ k ; $ i ++ ) $ temp *= 10 ; $ temp = power ( $ a , $ b , $ temp ) ; for ( $ i = 0 ; $ i < $ k - numberOfDigits ( $ temp ) ; $ i ++ ) echo 0 ; if ( $ temp ) echo $ temp ; } $ a = 11 ; $ b = 3 ; $ k = 2 ; printLastKDigits ( $ a , $ b , $ k ) ; ? >


"Print left rotation of array in O ( n ) time and O ( 1 ) space | Function to leftRotate array multiple times ; To get the starting point of rotated array ; Prints the rotated array from start position ; Driver Code ; Function Call ; Function Call ; Function Call"
How can the above be solved in PHP?

 < ? php function leftRotate ( $ arr , $ n , $ k ) { $ mod = $ k % $ n ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo ( $ arr [ ( $ mod + $ i ) % $ n ] ) , " ▁ " ; echo "
 " ; } $ arr = array ( 1 , 3 , 5 , 7 , 9 ) ; $ n = sizeof ( $ arr ) ; $ k = 2 ; leftRotate ( $ arr , $ n , $ k ) ; $ k = 3 ; leftRotate ( $ arr , $ n , $ k ) ; $ k = 4 ; leftRotate ( $ arr , $ n , $ k ) ; ? >


"Print matrix after applying increment operations in M ranges | Function to update and print the matrix after performing queries ; Add 1 to the first element of the sub - matrix ; If there is an element after the last element of the sub - matrix then decrement it by 1 ; Calculate the running sum ; Print the updated element ; Next line ; Size of the matrix ; Queries"
How can the above be solved in PHP?


"Print matrix in diagonal pattern | php program to print matrix in diagonal order ; Initialize indexes of element to be printed next ; Direction is initially from down to up ; Traverse the matrix till all elements get traversed ; If isUp = true then traverse from downward to upward ; Set i and j according to direction ; If isUp = 0 then traverse up to down ; Set i and j according to direction ; Revert the isUp to change the direction ; Driver code"
How can the above be solved in PHP?


"Print n 0 s and m 1 s such that no two 0 s and no three 1 s are together | Function to print the required pattern ; When condition fails ; When m = n - 1 ; Driver code"
How can the above be solved in PHP?


"Print n numbers such that their sum is a perfect square | Function to prn numbers such that their sum is a perfect square ; Print ith odd number ; Driver code"
How can the above be solved in PHP?

< ? php function findNumbers ( $ n ) { $ i = 1 ; while ( $ i <= $ n ) { echo ( ( 2 * $ i ) - 1 ) . " ▁ " ; $ i ++ ; } } $ n = 3 ; findNumbers ( $ n ) ; ? >


"Print n terms of Newman | Function to find the n - th element ; Declare array to store sequence ; Driver Code"
How can the above be solved in PHP?


"Print n x n spiral matrix using O ( 1 ) extra space | Prints spiral matrix of size n x n containing numbers from 1 to n x n ; x stores the layer in which ( i , j ) th element lies ; Finds minimum of four inputs ; For upper right half ; for lower left half ; Driver code ; print a n x n spiral matrix in O ( 1 ) space"
How can the above be solved in PHP?


"Print numbers in descending order along with their frequencies | Function to print the elements in descending along with their frequencies ; Sorts the element in decreasing order ; traverse the array elements ; Prints the number and count ; Prints the last step ; Driver Code"
How can the above be solved in PHP?

 < ? php function printElements ( & $ a , $ n ) { rsort ( $ a ) ; $ cnt = 1 ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { if ( $ a [ $ i ] != $ a [ $ i + 1 ] ) { echo ( $ a [ $ i ] ) ; echo ( " ▁ occurs ▁ " ) ; echo $ cnt ; echo ( " ▁ times
 " ) ; $ cnt = 1 ; } else $ cnt += 1 ; } echo ( $ a [ $ n - 1 ] ) ; echo ( " ▁ occurs ▁ " ) ; echo $ cnt ; echo ( " ▁ times
 " ) ; } $ a = array ( 1 , 1 , 1 , 2 , 3 , 4 , 9 , 9 , 10 ) ; $ n = sizeof ( $ a ) ; printElements ( $ a , $ 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"
How can the above be solved in PHP?

< ? 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 ) ; ? >


"Print pair with maximum AND value in an array | 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 ; Find the elements ; print the pair of elements ; inc count value after printing element ; return the result value ; Driver code"
How can the above be solved in PHP?

 < ? php function checkBit ( $ pattern , $ arr , $ n ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( ( $ pattern & $ arr [ $ i ] ) == $ pattern ) $ count ++ ; return $ count ; } function maxAND ( $ arr , $ n ) { $ res = 0 ; for ( $ bit = 31 ; $ bit >= 0 ; $ bit -- ) { $ count = checkBit ( $ res | ( 1 << $ bit ) , $ arr , $ n ) ; if ( $ count >= 2 ) $ res |= ( 1 << $ bit ) ; } if ( $ res == 0 ) echo " Not ▁ Possible
 " ; else { echo " Pair = " $ count = 0 ; for ( $ i = 0 ; $ i < $ n && $ count < 2 ; $ i ++ ) { if ( ( $ arr [ $ i ] & $ res ) == $ res ) { $ count ++ ; echo $ arr [ $ i ] . " " ; } } } return $ res ; } $ arr = array ( 4 , 8 , 6 , 2 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; echo " Maximum AND Value = " ? >


"Print prime numbers from 1 to N in reverse order | PHP program to print all primes between 1 to N in reverse order using Sieve of Eratosthenes ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Print all prime numbers in reverse order ; static input ; to display ; Reverseorder ( $N ) ; calling the function"
How can the above be solved in PHP?

 < ? php function Reverseorder ( $ n ) { $ prime = array_fill ( 0 , $ n + 1 , true ) ; for ( $ p = 2 ; $ p * $ p <= $ n ; $ p ++ ) { if ( $ prime [ $ p ] == true ) { for ( $ i = $ p * 2 ; $ i <= $ n ; $ i += $ p ) $ prime [ $ i ] = false ; } } for ( $ p = $ n ; $ p >= 2 ; $ p -- ) if ( $ prime [ $ p ] ) echo $ p . " " ; } $ N = 25 ; echo " Prime ▁ number ▁ in ▁ reverse ▁ order
 " ; if ( $ N == 1 ) echo " No ▁ prime ▁ no ▁ exist ▁ in ▁ this ▁ range " ; else ? >


"Print prime numbers with prime sum of digits in an array | Function to store the primes ; Function to return the sum of digits ; Function to print additive primes ; If the number is prime ; Check if it 's digit sum is prime ; Driver code"
How can the above be solved in PHP?

< ? php function sieve ( $ maxEle , & $ prime ) { $ prime [ 0 ] = $ prime [ 1 ] = 1 ; for ( $ i = 2 ; $ i * $ i <= $ maxEle ; $ i ++ ) { if ( ! $ prime [ $ i ] ) { for ( $ j = 2 * $ i ; $ j <= $ maxEle ; $ j += $ i ) $ prime [ $ j ] = 1 ; } } } function digitSum ( $ n ) { $ sum = 0 ; while ( $ n ) { $ sum += $ n % 10 ; $ n = $ n / 10 ; } return $ sum ; } function printAdditivePrime ( $ arr , $ n ) { $ maxEle = max ( $ arr ) ; $ prime = array_fill ( 0 , $ maxEle + 1 , 0 ) ; sieve ( $ maxEle , $ prime ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ prime [ $ arr [ $ i ] ] == 0 ) { $ sum = digitSum ( $ arr [ $ i ] ) ; if ( $ prime [ $ sum ] == 0 ) print ( $ arr [ $ i ] . " ▁ " ) ; } } } $ a = array ( 2 , 4 , 6 , 11 , 12 , 18 , 7 ) ; $ n = count ( $ a ) ; printAdditivePrime ( $ a , $ n ) ; ? >


"Print steps to make a number in form of 2 ^ X | Function to find the leftmost unset bit in a number . ; Function that perform the step ; Find the leftmost unset bit ; If the number has no bit unset , it means it is in form 2 ^ x - 1 ; Count the steps ; Iterate till number is of form 2 ^ x - 1 ; At even step increase by 1 ; Odd step xor with any 2 ^ m - 1 ; Find the leftmost unset bit ; 2 ^ m - 1 ; Perform the step ; Increase the steps ; Driver code"
How can the above be solved in PHP?

 < ? php function find_leftmost_unsetbit ( $ n ) { $ ind = -1 ; $ i = 1 ; while ( $ n ) { if ( ! ( $ n & 1 ) ) $ ind = $ i ; $ i ++ ; $ n >>= 1 ; } return $ ind ; } function perform_steps ( $ n ) { $ left = find_leftmost_unsetbit ( $ n ) ; if ( $ left == -1 ) { echo " No ▁ steps ▁ required " ; return ; } $ step = 1 ; while ( find_leftmost_unsetbit ( $ n ) != -1 ) { if ( $ step % 2 == 0 ) { $ n += 1 ; echo " Step " , $ step , " : ▁ Increase ▁ by ▁ 1
 " ; } else { $ m = find_leftmost_unsetbit ( $ n ) ; $ num = pow ( 2 , $ m ) - 1 ; $ n = $ n ^ $ num ; echo " Step " , $ step , " : ▁ Xor ▁ with ▁ " , $ num , "
 " ; } $ step += 1 ; } } $ n = 39 ; perform_steps ( $ n ) ; ? >


"Print string of odd length in ' X ' format | Function to print given string in cross pattern ; i and j are the indexes of characters to be displayed in the ith iteration i = 0 initially and go upto length of string j = length of string initially in each iteration of i , we increment i and decrement j , we print character only of k == i or k == j ; Driver code"
How can the above be solved in PHP?

 < ? php function pattern ( $ str , $ len ) { for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { $ j = $ len - 1 - $ i ; for ( $ k = 0 ; $ k < $ len ; $ k ++ ) { if ( $ k == $ i $ k == $ j ) echo $ str [ $ k ] ; else echo " ▁ " ; } echo "
 " ; } } $ str = " geeksforgeeks " ; $ len = strlen ( $ str ) ; pattern ( $ str , $ len ) ; ? >


"Print the balanced bracket expression using given brackets | Function to print balanced bracket expression if it is possible ; If the condition is met ; Print brackets of type - 1 ; Print brackets of type - 3 ; Print brackets of type - 4 ; Print brackets of type - 2 ; If the condition is not met ; Driver code"
How can the above be solved in PHP?


"Print the kth common factor of two numbers | Returns k 'th common factor of x and y. ; Find smaller of two numbers ; Count common factors until we either reach small or count becomes k . ; If we reached small ; Driver code"
How can the above be solved in PHP?

< ? php function findKCF ( $ x , $ y , $ k ) { $ small = min ( $ x , $ y ) ; $ count = 1 ; for ( $ i = 2 ; $ i <= $ small ; $ i ++ ) { if ( $ x % $ i == 0 && $ y % $ i == 0 ) $ count ++ ; if ( $ count == $ k ) return $ i ; } return -1 ; } $ x = 4 ; $ y = 24 ; $ k = 3 ; echo findKCF ( $ x , $ y , $ k ) ; ? >


"Print triplets with sum less than k | A Simple PHP program to count triplets with sum smaller than a given value ; Fix the first element as A [ i ] ; Fix the second element as A [ j ] ; Now look for the third number ; Driver Code"
How can the above be solved in PHP?


"Print triplets with sum less than k | PHP program to print triplets with sum smaller than a given value ; Sort input array ; Every iteration of loop counts triplet with first element as arr [ i ] . ; Initialize other two elements as corner elements of subarray arr [ j + 1. . k ] ; Use Meet in the Middle concept ; If sum of current triplet is more or equal , move right corner to look for smaller values ; Else move left corner ; This is important . For current i and j , there are total k - j third elements . ; Driver Code"
How can the above be solved in PHP?


"Print uncommon elements from two sorted arrays | PHP program to find uncommon elements of two sorted arrays ; If not common , prsmaller ; Skip common element ; printing remaining elements ; Driver code"
How can the above be solved in PHP?


"Print values of ' a ' in equation ( a + b ) <= n and a + b is divisible by x | function to Find values of a , in equation ( a + b ) <= n and a + b is divisible by x . ; least possible which is divisible by x ; run a loop to get required answer ; increase value by x ; answer is possible ; Driver code ; function call"
How can the above be solved in PHP?

< ? php function PossibleValues ( $ b , $ x , $ n ) { $ leastdivisible = ( intval ( $ b / $ x ) + 1 ) * $ x ; $ flag = 1 ; while ( $ leastdivisible <= $ n ) { if ( $ leastdivisible - $ b >= 1 ) { echo $ leastdivisible - $ b . " " ; $ leastdivisible += $ x ; $ flag = 0 ; } else break ; } if ( $ flag ) echo " - 1" ; } $ b = 10 ; $ x = 6 ; $ n = 40 ; PossibleValues ( $ b , $ x , $ n ) ; ? >


"Printing Items in 0 / 1 Knapsack | Prints the items which are kept in a knapsack of capacity W ; Build table K [ ] [ ] in bottom up manner ; stores the result of Knapsack ; either the result comes from the top ( K [ i - 1 ] [ w ] ) or from ( val [ i - 1 ] + K [ i - 1 ] [ w - wt [ i - 1 ] ] ) as in Knapsack table . If it comes from the latter one / it means the item is included . ; This item is included . ; Since this weight is included its value is deducted ; Driver code"
How can the above be solved in PHP?


"Printing string in plus ‘ + ’ pattern in the matrix | Function to make a cross in the matrix ; As , it is not possible to make the cross exactly in the middle of the matrix with an even length string . ; declaring a 2D array i . e a matrix ; Now , we will fill all the elements of the array with ' X ' ; Now , we will place the characters of the string in the matrix , such that a cross is formed in it . ; here the characters of the string will be added in the middle column of our array . ; here the characters of the string will be added in the middle row of our array . ; Now finally , we will print the array ; Driver Code"
How can the above be solved in PHP?

 < ? php function carveCross ( $ str ) { $ n = strlen ( $ str ) ; if ( $ n % 2 == 0 ) { echo ( " Not ▁ possible . ▁ Please ▁ enter ▁ " ) ; echo ( " odd ▁ length ▁ string .
 " ) ; } else { $ arr = array ( ) ; $ m = $ n / 2 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { $ arr [ $ i ] [ $ j ] = ' X ' ; } } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ arr [ $ i ] [ $ m ] = $ str [ $ i ] ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ arr [ $ m ] [ $ i ] = $ str [ $ i ] ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { echo ( $ arr [ $ i ] [ $ j ] . " " ) ; } echo ( "
 " ) ; } } } $ str = " PICTURE " ; carveCross ( $ str ) ; ? >


"Probability for three randomly chosen numbers to be in AP | function to calculate probability ; Driver code"
How can the above be solved in PHP?


"Probability of Knight to remain in the chessboard | size of the chessboard ; direction vector for the Knight ; returns true if the knight is inside the chessboard ; Bottom up approach for finding the probability to go out of chessboard . ; dp array ; for 0 number of steps , each position will have probability 1 ; for every number of steps s ; for every position ( x , y ) after s number of steps ; for every position reachable from ( x , y ) ; if this position lie inside the board ; store the result ; return the result ; number of steps ; Function Call"
How can the above be solved in PHP?


"Probability of a key K present in array | Function to find the probability ; find probability ; Driver Code"
How can the above be solved in PHP?

< ? php function kPresentProbability ( & $ a , $ n , $ k ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ a [ $ i ] == $ k ) $ count ++ ; return $ count / $ n ; } $ A = array ( 4 , 7 , 2 , 0 , 8 , 7 , 5 ) ; $ K = 2 ; $ N = sizeof ( $ A ) ; echo round ( kPresentProbability ( $ A , $ N , $ K ) , 2 ) ; ? >


"Probability of a random pair being the maximum weighted pair | Function to return probability ; Count occurrences of maximum element in A [ ] ; Count occurrences of maximum element in B [ ] ; Returning probability ; Driver code"
How can the above be solved in PHP?

< ? php function probability ( $ a , $ b , $ size1 , $ size2 ) { $ max1 = PHP_INT_MIN ; $ count1 = 0 ; for ( $ i = 0 ; $ i < $ size1 ; $ i ++ ) { if ( $ a [ $ i ] > $ max1 ) { $ max1 = $ a [ $ i ] ; $ count1 = 1 ; } else if ( $ a [ $ i ] == $ max1 ) { $ count1 ++ ; } } $ max2 = PHP_INT_MIN ; $ count2 = 0 ; for ( $ i = 0 ; $ i < $ size2 ; $ i ++ ) { if ( $ b [ $ i ] > $ max2 ) { $ max2 = $ b [ $ i ] ; $ count2 = 1 ; } else if ( $ b [ $ i ] == $ max2 ) { $ count2 ++ ; } } return ( double ) ( $ count1 * $ count2 ) / ( $ size1 * $ size2 ) ; } $ a = array ( 1 , 2 , 3 ) ; $ b = array ( 1 , 3 , 3 ) ; $ size1 = sizeof ( $ a ) ; $ size2 = sizeof ( $ b ) ; echo probability ( $ a , $ b , $ size1 , $ size2 ) ; ? >


"Probability of choosing a random pair with maximum sum in an array | Function to get max first and second ; If current element is smaller than first , then update both first and second ; If arr [ i ] is in between first and second then update second ; frequency of first maximum ; frequency of second maximum ; Returns probability of choosing a pair with maximum sum . ; Driver Code"
How can the above be solved in PHP?

< ? php function countMaxSumPairs ( $ a , $ n ) { $ first = PHP_INT_MIN ; $ second = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] > $ first ) { $ second = $ first ; $ first = $ a [ $ i ] ; } else if ( $ a [ $ i ] > $ second && $ a [ $ i ] != $ first ) $ second = $ a [ $ i ] ; } $ cnt1 = 0 ; $ cnt2 = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] == $ first ) $ cnt1 ++ ; if ( $ a [ $ i ] == $ second ) $ cnt2 ++ ; } if ( $ cnt1 == 1 ) return $ cnt2 ; if ( $ cnt1 > 1 ) return $ cnt1 * ( $ cnt1 - 1 ) / 2 ; } function findMaxSumProbability ( $ a , $ n ) { $ total = $ n * ( $ n - 1 ) / 2 ; $ max_sum_pairs = countMaxSumPairs ( $ a , $ n ) ; return ( float ) $ max_sum_pairs / ( float ) $ total ; } $ a = array ( 1 , 2 , 2 , 3 ) ; $ n = sizeof ( $ a ) ; echo findMaxSumProbability ( $ a , $ n ) ; ? >


"Probability of getting at least K heads in N tosses of Coins | Dynamic and Logarithm approach find probability of at least k heads ; dp [ i ] is going to store Log ( i ! ) in base 2 ; Initialize result ; Iterate from k heads to n heads ; Preprocess all the logarithm value on base 2 ; Driver code ; Probability of getting 2 head out of 3 coins ; Probability of getting 3 head out of 6 coins ; Probability of getting 500 head out of 10000 coins"
How can the above be solved in PHP?

 < ? php $ MAX = 100001 ; $ dp = array_fill ( 0 , $ MAX , 0 ) ; function probability ( $ k , $ n ) { global $ MAX , $ dp ; $ ans = 0 ; for ( $ i = $ k ; $ i <= $ n ; ++ $ i ) { $ res = $ dp [ $ n ] - $ dp [ $ i ] - $ dp [ $ n - $ i ] - $ n ; $ ans += pow ( 2.0 , $ res ) ; } return $ ans ; } function precompute ( ) { global $ MAX , $ dp ; for ( $ i = 2 ; $ i < $ MAX ; ++ $ i ) precompute ( ) ; echo probability ( 2 , 3 ) . "
 " ; echo probability ( 3 , 6 ) . "
 " ; echo probability ( 500 , 1000 ) ; ? >


"Probability of getting at least K heads in N tosses of Coins | Naive approach in PHP to find probability of at least k heads ; Returns probability of getting at least k heads in n tosses . ; Probability of getting exactly i heads out of n heads ; Note : 1 << n = pow ( 2 , n ) ; Preprocess all factorial only upto 19 , as after that it will overflow ; Driver code ; Probability of getting 2 head out of 3 coins ; Probability of getting 3 head out of 6 coins ; Probability of getting 12 head out of 18 coins"
How can the above be solved in PHP?

 < ? php $ MAX = 21 ; $ fact = array_fill ( 0 , $ MAX , 0 ) ; function probability ( $ k , $ n ) { global $ fact ; $ ans = 0 ; for ( $ i = $ k ; $ i <= $ n ; ++ $ i ) $ ans += $ fact [ $ n ] / ( $ fact [ $ i ] * $ fact [ $ n - $ i ] ) ; $ ans = $ ans / ( 1 << $ n ) ; return $ ans ; } function precompute ( ) { global $ fact ; $ fact [ 0 ] = $ fact [ 1 ] = 1 ; for ( $ i = 2 ; $ i < 20 ; ++ $ i ) $ fact [ $ i ] = $ fact [ $ i - 1 ] * $ i ; } precompute ( ) ; echo number_format ( probability ( 2 , 3 ) , 6 ) . "
 " ; echo number_format ( probability ( 3 , 6 ) , 6 ) . "
 " ; echo number_format ( probability ( 12 , 18 ) , 6 ) ; ? >


"Probability of getting two consecutive heads after choosing a random coin among two different types of coins | Function to return the probability of getting two consecutive heads ; Formula derived from Bayes 's theorem ; given the probability of getting a head for both the coins"
How can the above be solved in PHP?

< ? php function getProbability ( $ p , $ q ) { $ p /= 100 ; $ q /= 100 ; $ probability = ( $ p * $ p + $ q * $ q ) / ( $ p + $ q ) ; return $ probability ; } $ p = 80 ; $ q = 40 ; echo getProbability ( $ p , $ q ) ; ? >


"Probability of reaching a point with 2 or 3 steps at a time | Returns probability to reach N ; Driver code"
How can the above be solved in PHP?


"Problem of 8 Neighbours of an element in a 2 | Dimension of Array ; Count of 1 s ; Counting all neighbouring 1 s ; Comparing the number of neighbouring 1 s with given ranges ; Copying changes to the main matrix ; Driver code ; Function call to calculate the resultant matrix after ' K ' iterations . ; Printing Result"
How can the above be solved in PHP?


"Problems not solved at the end of Nth day | Function to find problems not solved at the end of Nth day ; Driver Code"
How can the above be solved in PHP?

< ? php function problemsLeft ( $ K , $ P , $ N ) { if ( $ K <= $ P ) return 0 ; else return ( $ K - $ P ) * $ N ; } $ K = 4 ; $ P = 1 ; $ N = 10 ; echo problemsLeft ( $ K , $ P , $ N ) ; ? >


"Product of N with its largest odd digit | Function to return the largest odd digit in n ; If all digits are even then - 1 will be returned ; Last digit from n ; If current digit is odd and > maxOdd ; Remove last digit ; Return the maximum odd digit ; Function to return the product of n with its largest odd digit ; If there are no odd digits in n ; Product of n with its largest odd digit ; Driver code"
How can the above be solved in PHP?

< ? php function largestOddDigit ( $ n ) { $ maxOdd = -1 ; while ( $ n > 0 ) { $ digit = $ n % 10 ; if ( $ digit % 2 == 1 && $ digit > $ maxOdd ) $ maxOdd = $ digit ; $ n = $ n / 10 ; } return $ maxOdd ; } function getProduct ( $ n ) { $ maxOdd = largestOddDigit ( $ n ) ; if ( $ maxOdd == -1 ) return -1 ; return ( $ n * $ maxOdd ) ; } $ n = 12345 ; echo getProduct ( $ n ) ; ? >


"Product of all the Composite Numbers in an array | Function that returns the the product of all composite numbers ; Find maximum value in the array ; Use sieve to find all prime numbers less than or equal to max_val Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Set 0 and 1 as primes as they don 't need to be  counted as composite numbers ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Find the product of all composite numbers in the arr [ ] ; Driver code"
How can the above be solved in PHP?

< ? php function compositeProduct ( $ arr , $ n ) { $ max_val = max ( $ arr ) ; $ prime = array_fill ( 0 , $ max_val + 1 , true ) ; $ prime [ 0 ] = true ; $ prime [ 1 ] = true ; for ( $ p = 2 ; $ p * $ p <= $ max_val ; $ p ++ ) { if ( $ prime [ $ p ] == true ) { for ( $ i = $ p * 2 ; $ i <= $ max_val ; $ i += $ p ) $ prime [ $ i ] = false ; } } $ product = 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( ! $ prime [ $ arr [ $ i ] ] ) { $ product *= $ arr [ $ i ] ; } return $ product ; } $ arr = array ( 2 , 3 , 4 , 5 , 6 , 7 ) ; $ n = count ( $ arr ) ; echo compositeProduct ( $ arr , $ n ) ; ? >


"Product of factors of number | PHP program to calculate product of factors of number ; Iterative Function to calculate ( x ^ y ) in O ( log y ) ; function to count the factors ; If factors are equal , count only once ; Otherwise count both ; Calculate product of factors ; If numFactor is odd return power ( n , numFactor / 2 ) * sqrt ( n ) ; Driver code"
How can the above be solved in PHP?

< ? php $ M = 1000000007 ; function power ( $ x , $ y ) { global $ M ; $ res = 1 ; while ( $ y > 0 ) { if ( $ y & 1 ) $ res = ( $ res * $ x ) % $ M ; $ y = ( $ y >> 1 ) % $ M ; $ x = ( $ x * $ x ) % $ M ; } return $ res ; } function countFactors ( $ n ) { $ count = 0 ; for ( $ i = 1 ; $ i * $ i <= $ n ; $ i ++ ) { if ( $ n % $ i == 0 ) { if ( $ n / $ i == $ i ) $ count ++ ; else $ count += 2 ; } } return $ count ; } function multiplyFactors ( $ n ) { $ numFactor = countFactors ( $ n ) ; $ product = power ( $ n , $ numFactor / 2 ) ; if ( $ numFactor & 1 ) $ product = ( $ product * sqrt ( $ n ) ) % $ M ; return $ product ; } $ n = 12 ; echo multiplyFactors ( $ n ) ; ? >


"Product of factors of number | PHP program to calculate product of factors of number ; function to product the factors ; If factors are equal , multiply only once ; Otherwise multiply both ; Driver Code"
How can the above be solved in PHP?

< ? php $ M = 1000000007 ; function multiplyFactors ( $ n ) { global $ M ; $ prod = 1 ; for ( $ i = 1 ; $ i * $ i <= $ n ; $ i ++ ) { if ( $ n % $ i == 0 ) { if ( $ n / $ i == $ i ) $ prod = ( $ prod * $ i ) % $ M ; else { $ prod = ( $ prod * $ i ) % $ M ; $ prod = ( $ prod * $ n / $ i ) % $ M ; } } } return $ prod ; } $ n = 12 ; echo multiplyFactors ( $ n ) ; ? >


"Product of first N factorials | To compute ( a * b ) % MOD ; $res = 0 ; Initialize result ; If b is odd , add ' a ' to result ; Multiply ' a ' with 2 ; Divide b by 2 ; Return result ; This function computes factorials and product by using above function i . e . modular multiplication ; Initialize product and fact with 1 ; ith factorial ; product of first i factorials ; If at any iteration , product becomes divisible by MOD , simply return 0 ; ; Driver Code"
How can the above be solved in PHP?

 < ? php function mulmod ( $ a , $ b , $ mod ) { $ a = $ a % $ mod ; while ( $ b > 0 ) { if ( $ b % 2 == 1 ) $ res = ( $ res + $ a ) % $ mod ; $ a = ( $ a * 2 ) % $ mod ; $ b /= 2 ; } return $ res % $ mod ; } function findProduct ( $ N ) { $ product = 1 ; $ fact = 1 ; $ MOD = 1000000000 ; for ( $ i = 1 ; $ i <= $ N ; $ i ++ ) { $ fact = mulmod ( $ fact , $ i , $ MOD ) ; $ product = mulmod ( $ product , $ fact , $ MOD ) ; if ( $ product == 0 ) return 0 ; } return $ product ; } $ N = 3 ; echo findProduct ( $ N ) , "
 " ; $ N = 5 ; echo findProduct ( $ N ) , "
 " ; ? >


"Product of given N fractions in reduced form | Function to return gcd of a and b ; Print the Product of N fraction in Reduced Form . ; finding the product of all N numerators and denominators . ; Finding GCD of new numerator and denominator ; Converting into reduced form . ; Driver Code"
How can the above be solved in PHP?

 < ? php function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; } function productReduce ( $ n , $ num , $ den ) { $ new_num = 1 ; $ new_den = 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ new_num *= $ num [ $ i ] ; $ new_den *= $ den [ $ i ] ; } $ GCD = gcd ( $ new_num , $ new_den ) ; $ new_num /= $ GCD ; $ new_den /= $ GCD ; echo $ new_num , " / " , $ new_den , "
 " ; } $ n = 3 ; $ num = array ( 1 , 2 , 5 ) ; $ den = array ( 2 , 1 , 6 ) ; productReduce ( $ n , $ num , $ den ) ; ? >


"Product of maximum in first array and minimum in second | Function to calculate the product ; Initialize max of first array ; initialize min of second array ; To find the maximum element in first array ; To find the minimum element in second array ; Process remaining elements ; Driven code"
How can the above be solved in PHP?


"Product of maximum in first array and minimum in second | Function to calculate the product ; Sort the arrays to find the maximum and minimum elements in given arrays ; Return product of maximum and minimum . ; Driver code"
How can the above be solved in PHP?

< ? php function minMaxProduct ( $ arr1 , $ arr2 , $ n1 , $ n2 ) { sort ( $ arr1 ) ; sort ( $ arr2 ) ; return $ arr1 [ $ n1 - 1 ] * $ arr2 [ 0 ] ; } $ arr1 = array ( 10 , 2 , 3 , 6 , 4 , 1 ) ; $ arr2 = array ( 5 , 1 , 4 , 2 , 6 , 9 ) ; $ n1 = count ( $ arr1 ) ; $ n2 = count ( $ arr2 ) ; echo minMaxProduct ( $ arr1 , $ arr2 , $ n1 , $ n2 ) ; ? >


"Product of unique prime factors of a number | A function to print all prime factors of a given number n ; Handle prime factor 2 explicitly so that can optimally handle other prime factors . ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; While i divides n , print i and divide n ; This condition is to handle the case when n is a prime number greater than 2 ; Driver Code"
How can the above be solved in PHP?

< ? php function productPrimeFactors ( $ n ) { $ product = 1 ; if ( $ n % 2 == 0 ) { $ product *= 2 ; while ( $ n % 2 == 0 ) $ n = $ n / 2 ; } for ( $ i = 3 ; $ i <= sqrt ( $ n ) ; $ i = $ i + 2 ) { if ( $ n % $ i == 0 ) { $ product = $ product * $ i ; while ( $ n % $ i == 0 ) $ n = $ n / $ i ; } } if ( $ n > 2 ) $ product = $ product * $ n ; return $ product ; } $ n = 44 ; echo productPrimeFactors ( $ n ) ; ? >


"Products of ranges in an array | Function to calculate Product in the given range . ; As our array is 0 based as and L and R are given as 1 based index . ; Driver code"
How can the above be solved in PHP?

< ? php function calculateProduct ( $ A , $ L , $ R , $ P ) { $ L = $ L - 1 ; $ R = $ R - 1 ; $ ans = 1 ; for ( $ i = $ L ; $ i <= $ R ; $ i ++ ) { $ ans = $ ans * $ A [ $ i ] ; $ ans = $ ans % $ P ; } return $ ans ; } $ A = array ( 1 , 2 , 3 , 4 , 5 , 6 ) ; $ P = 229 ; $ L = 2 ; $ R = 5 ; echo calculateProduct ( $ A , $ L , $ R , $ P ) , " " ; $ L = 1 ; $ R = 3 ; echo calculateProduct ( $ A , $ L , $ R , $ P ) , " " ; ? >


"Program for Area And Perimeter Of Rectangle | Utility function ; Driver program"
How can the above be solved in PHP?

 < ? php function areaRectangle ( $ a , $ b ) { $ area = $ a * $ b ; return $ area ; } function perimeterRectangle ( $ a , $ b ) { $ perimeter = 2 * ( $ a + $ b ) ; return $ perimeter ; } $ a = 5 ; $ b = 6 ; echo ( " Area ▁ = ▁ " ) ; echo ( areaRectangle ( $ a , $ b ) ) ; echo ( "
 " ) ; echo ( " Perimeter ▁ = ▁ " ) ; echo ( perimeterRectangle ( $ a , $ b ) ) ; ? >


"Program for Area Of Square after N | Function to calculate area of square after given number of folds ; Driver Code"
How can the above be solved in PHP?

< ? php function areaSquare ( $ side , $ fold ) { $ area = $ side * $ side ; return $ area * 1.0 / pow ( 2 , $ fold ) ; } $ side = 4 ; $ fold = 2 ; echo areaSquare ( $ side , $ fold ) ; ? >


"Program for Area Of Square | PHP program to find the aria of the square ; Driver Code"
How can the above be solved in PHP?

< ? php function areaSquare ( $ side ) { $ area = $ side * $ side ; return $ area ; } $ side = 4 ; echo ( areaSquare ( $ side ) ) ; ? >


"Program for Binary To Decimal Conversion | Function to convert binary to decimal ; Initializing base value to 1 , i . e 2 ^ 0 ; Driver Code"
How can the above be solved in PHP?

 < ? php function binaryToDecimal ( $ n ) { $ num = $ n ; $ dec_value = 0 ; $ base = 1 ; $ temp = $ num ; while ( $ temp ) { $ last_digit = $ temp % 10 ; $ temp = $ temp / 10 ; $ dec_value += $ last_digit * $ base ; $ base = $ base * 2 ; } return $ dec_value ; } $ num = 10101001 ; echo binaryToDecimal ( $ num ) , "
 " ; ? >


"Program for Binomial Coefficients table | Function to print binomial table ; B ( m , x ) is 1 if either m or x is 0. ; Otherwise using recursive formula B ( m , x ) = B ( m , x - 1 ) * ( m - x + 1 ) / x ; Driver Code"
How can the above be solved in PHP?

 < ? php function printbinomial ( $ max ) { for ( $ m = 0 ; $ m <= $ max ; $ m ++ ) { echo $ m ; $ binom = 1 ; for ( $ x = 0 ; $ x <= $ m ; $ x ++ ) { if ( $ m != 0 && $ x != 0 ) $ binom = $ binom * ( $ m - $ x + 1 ) / $ x ; echo " ▁ " , $ binom , " ▁ " ; } echo "
 " ; } } $ max = 10 ; printbinomial ( $ max ) ; ? >


"Program for Celsius To Fahrenheit conversion | function to convert Celsius scale to Fahrenheit scale ; Driver Code"
How can the above be solved in PHP?


"Program for Centered Icosahedral Number | Function to find Centered icosahedral number ; Formula to calculate nth Centered icosahedral number and return it into main function . ; Driver Code"
How can the above be solved in PHP?

 < ? php function centeredIcosahedralNum ( $ n ) { return ( 2 * $ n + 1 ) * ( 5 * $ n * $ n + 5 * $ n + 3 ) / 3 ; } $ n = 10 ; echo centeredIcosahedralNum ( $ n ) , "
 " ; $ n = 12 ; echo centeredIcosahedralNum ( $ n ) , "
 " ; ? >


"Program for Chocolate and Wrapper Puzzle | Returns maximum number of chocolates we can eat with given money , price of chocolate and number of wrapprices required to get a chocolate . ; Corner case ; First find number of chocolates that can be purchased with the given amount ; Now just add number of chocolates with the chocolates gained by wrapprices ; total money ; cost of each candy ; no of wrappers needs to be ; exchanged for one chocolate ."
How can the above be solved in PHP?

< ? php function countMaxChoco ( $ money , $ price , $ wrap ) { if ( $ money < $ price ) return 0 ; $ choc = $ money / $ price ; $ choc = $ choc + ( $ choc - 1 ) / ( $ wrap - 1 ) ; return $ choc ; } $ money = 15 ; $ price = 1 ; $ wrap = 3 ; echo countMaxChoco ( $ money , $ price , $ wrap ) ; ? >


"Program for Decimal to Binary Conversion | function to convert decimal to binary ; array to store binary number ; counter for binary array ; storing remainder in binary array ; printing binary array in reverse order ; Driver Code"
How can the above be solved in PHP?

< ? php function decToBinary ( $ n ) { $ binaryNum ; $ i = 0 ; while ( $ n > 0 ) { $ binaryNum [ $ i ] = $ n % 2 ; $ n = ( int ) ( $ n / 2 ) ; $ i ++ ; } for ( $ j = $ i - 1 ; $ j >= 0 ; $ j -- ) echo $ binaryNum [ $ j ] ; } $ n = 17 ; decToBinary ( $ n ) ; ? >


"Program for Identity Matrix | PHP program to check if a given matrix is identity $MAX = 100 ; ; Driver Code"
How can the above be solved in PHP?

< ? php function isIdentity ( $ mat , $ N ) { for ( $ row = 0 ; $ row < $ N ; $ row ++ ) { for ( $ col = 0 ; $ col < $ N ; $ col ++ ) { if ( $ row == $ col and $ mat [ $ row ] [ $ col ] != 1 ) return false ; else if ( $ row != $ col && $ mat [ $ row ] [ $ col ] != 0 ) return false ; } } return true ; } $ N = 4 ; $ mat = array ( array ( 1 , 0 , 0 , 0 ) , array ( 0 , 1 , 0 , 0 ) , array ( 0 , 0 , 1 , 0 ) , array ( 0 , 0 , 0 , 1 ) ) ; if ( isIdentity ( $ mat , $ N ) ) echo " Yes ▁ " ; else echo " No ▁ " ; ? >