"Program to find the value of sin ( nÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢ ÃƒÆ ’ Ã ¢ â ‚¬¦½ ÃƒÆ ’ Ã † â €™ Ãƒ ¢ Ã ¢ â € š ¬¹ ÃƒÆ ’ Ã ¢ â ‚¬¦ Ãƒ ¢ Ã ¢ â € š ¬ Ã … â € œ ) | PHP Program to find the value of sin ( n ? ) ; This function use to calculate the binomial coefficient upto 15 ; use simple DP to find coefficient ; Function to find the value of ; find cosTheta from sinTheta ; store required answer ; use to toggle sign in sequence . ; Driver code ."
How can the above be solved in PHP?

< ? php $ MAX = 16 ; $ nCr = array_fill ( 0 , $ MAX , array_fill ( 0 , $ MAX , 0 ) ) ; function binomial ( ) { global $ MAX , $ nCr ; for ( $ i = 0 ; $ i < $ MAX ; $ i ++ ) { for ( $ j = 0 ; $ j <= $ i ; $ j ++ ) { if ( $ j == 0 $ j == $ i ) $ nCr [ $ i ] [ $ j ] = 1 ; else $ nCr [ $ i ] [ $ j ] = $ nCr [ $ i - 1 ] [ $ j ] + $ nCr [ $ i - 1 ] [ $ j - 1 ] ; } } } function findCosNTheta ( $ sinTheta , $ n ) { global $ MAX , $ nCr ; $ cosTheta = sqrt ( 1 - $ sinTheta * $ sinTheta ) ; $ ans = 0 ; $ toggle = 1 ; for ( $ i = 1 ; $ i <= $ n ; $ i += 2 ) { $ ans = $ ans + $ nCr [ $ n ] [ $ i ] * pow ( $ cosTheta , $ n - $ i ) * pow ( $ sinTheta , $ i ) * $ toggle ; $ toggle = $ toggle * -1 ; } return $ ans ; } binomial ( ) ; $ sinTheta = 0.5 ; $ n = 10 ; echo findCosNTheta ( $ sinTheta , $ n ) ; ? >


"Program to find the value of tan ( nÎ ˜ ) | PHP program to find the value of cos ( n - theta ) ; This function use to calculate the binomial coefficient upto 15 ; use simple DP to find coefficient ; Function to find the value of ; store required answer ; use to toggle sign in sequence . ; calculate numerator ; calculate denominator ; Driver code ."
How can the above be solved in PHP?

< ? php $ MAX = 16 ; $ nCr = array_fill ( 0 , $ MAX , array_fill ( 0 , $ MAX , 0 ) ) ; function binomial ( ) { global $ MAX , $ nCr ; for ( $ i = 0 ; $ i < $ MAX ; $ i ++ ) { for ( $ j = 0 ; $ j <= $ i ; $ j ++ ) { if ( $ j == 0 $ j == $ i ) $ nCr [ $ i ] [ $ j ] = 1 ; else $ nCr [ $ i ] [ $ j ] = $ nCr [ $ i - 1 ] [ $ j ] + $ nCr [ $ i - 1 ] [ $ j - 1 ] ; } } } function findTanNTheta ( $ tanTheta , $ n ) { global $ MAX , $ nCr ; $ ans = 0 ; $ numerator = 0 ; $ denominator = 0 ; $ toggle = 1 ; for ( $ i = 1 ; $ i <= $ n ; $ i += 2 ) { $ numerator = $ numerator + $ nCr [ $ n ] [ $ i ] * pow ( $ tanTheta , $ i ) * $ toggle ; $ toggle = $ toggle * -1 ; } $ denominator = 1 ; $ toggle = -1 ; for ( $ i = 2 ; $ i <= $ n ; $ i += 2 ) { $ numerator = $ numerator + $ nCr [ $ n ] [ $ i ] * pow ( $ tanTheta , $ i ) * $ toggle ; $ toggle = $ toggle * -1 ; } $ ans = $ numerator / $ denominator ; return $ ans ; } binomial ( ) ; $ tanTheta = 0.3 ; $ n = 10 ; echo findTanNTheta ( $ tanTheta , $ n ) ; ? >


"Program to find third side of triangle using law of cosines | Function to calculate cos value of angle c ; Converting degrees to radian ; Maps the sum along the series ; Holds the actual value of sin ( n ) ; Function to find third side ; Driver Code ; function call"
How can the above be solved in PHP?

< ? php function cal_cos ( $ n ) { $ accuracy = 0.0001 ; $ x1 ; $ denominator ; $ cosx ; $ cosval ; $ n = $ n * ( 3.142 / 180.0 ) ; $ x1 = 1 ; $ cosx = $ x1 ; $ cosval = cos ( $ n ) ; $ i = 1 ; do { $ denominator = 2 * $ i * ( 2 * $ i - 1 ) ; $ x1 = - $ x1 * $ n * $ n / $ denominator ; $ cosx = $ cosx + $ x1 ; $ i = $ i + 1 ; } while ( $ accuracy <= ( $ cosval - $ cosx ) ) ; return $ cosx ; } function third_side ( $ a , $ b , $ c ) { $ angle = cal_cos ( $ c ) ; return sqrt ( ( $ a * $ a ) + ( $ b * $ b ) - 2 * $ a * $ b * $ angle ) ; } $ c = 49 ; $ a = 5 ; $ b = 8 ; echo third_side ( $ a , $ b , $ c ) ; ? >


"Program to find total number of edges in a Complete Graph | Function to find the total number of edges in a complete graph with N vertices ; Driver Code"
How can the above be solved in PHP?

< ? php function totEdge ( $ n ) { $ result = 0 ; $ result = ( $ n * ( $ n - 1 ) ) / 2 ; return $ result ; } $ n = 6 ; echo totEdge ( $ n ) ; ? >


"Program to find volume and surface area of pentagonal prism | function for surface area ; function for VOlume ; Driver Code"
How can the above be solved in PHP?

< ? php function surfaceArea ( $ a , $ b , $ h ) { return 5 * $ a * $ b + 5 * $ b * $ h ; } function volume ( $ b , $ h ) { return ( 5 * $ b * $ h ) / 2 ; } $ a = 5 ; $ b = 3 ; $ h = 7 ; echo " surface ▁ area ▁ = ▁ " , surfaceArea ( $ a , $ b , $ h ) , " , ▁ " ; echo " volume = " ? >


"Program to get the Sum of series : 1 | Function to get the series ; Computing sum of remaining n - 1 terms . ; Driver Code"
How can the above be solved in PHP?

< ? php function Series ( $ x , $ n ) { $ sum = 1 ; $ term = 1 ; $ fct = 1 ; $ p = 1 ; $ multi = 1 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ fct = $ fct * $ multi * ( $ multi + 1 ) ; $ p = $ p * $ x * $ x ; $ term = ( -1 ) * $ term ; $ multi += 2 ; $ sum = $ sum + ( $ term * $ p ) / $ fct ; } return $ sum ; } $ x = 9 ; $ n = 10 ; $ precision = 4 ; echo substr ( number_format ( Series ( $ x , $ n ) , $ precision + 1 , ' . ' , ' ' ) , 0 , -1 ) ; ? >


"Program to get the Sum of series : 1 | Function to get the series ; Sum of n - 1 terms starting from 2 nd term ; Driver Code"
How can the above be solved in PHP?

< ? php function Series ( $ x , $ n ) { $ sum = 1 ; $ term = 1 ; $ fct ; $ j ; $ y = 2 ; $ m ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ fct = 1 ; for ( $ j = 1 ; $ j <= $ y ; $ j ++ ) { $ fct = $ fct * $ j ; } $ term = $ term * ( -1 ) ; $ m = $ term * pow ( $ x , $ y ) / $ fct ; $ sum = $ sum + $ m ; $ y += 2 ; } return $ sum ; } $ x = 9 ; $ n = 10 ; $ precision = 4 ; echo substr ( number_format ( Series ( $ x , $ n ) , $ precision + 1 , ' . ' , ' ' ) , 0 , -1 ) ; ? >


"Program to implement Collatz Conjecture | Function to find if n reaches to 1 or not . ; Return true if n is positive ; Driver code"
How can the above be solved in PHP?

< ? php function isToOne ( $ n ) { if ( $ n > 0 ) return true ; return false ; } $ n = 5 ; isToOne ( $ n ) ? print ( " Yes " ) : print ( " No " ) ; ? >


"Program to implement Simpson 's 3/8 rule | Given function to be integrated ; Function to perform calculations ; Calculates value till integral limit ; Driver Code"
How can the above be solved in PHP?

< ? php function func ( $ x ) { return ( 1 / ( 1 + $ x * $ x ) ) ; } function calculate ( $ lower_limit , $ upper_limit , $ interval_limit ) { $ interval_size = ( $ upper_limit - $ lower_limit ) / $ interval_limit ; $ sum = func ( $ lower_limit ) + func ( $ upper_limit ) ; for ( $ i = 1 ; $ i < $ interval_limit ; $ i ++ ) { if ( $ i % 3 == 0 ) $ sum = $ sum + 2 * func ( $ lower_limit + $ i * $ interval_size ) ; else $ sum = $ sum + 3 * func ( $ lower_limit + $ i * $ interval_size ) ; } return ( 3 * $ interval_size / 8 ) * $ sum ; } $ interval_limit = 10 ; $ lower_limit = 1 ; $ upper_limit = 10 ; $ integral_res = calculate ( $ lower_limit , $ upper_limit , $ interval_limit ) ; echo $ integral_res ; ? >


"Program to implement standard deviation of grouped data | Function to find mean of grouped data . ; Function to find standard deviation of grouped data . ; Formula to find standard deviation of grouped data . ; Declare and initialize the upper limit of interval . ; Declare and initialize the upper limit of interval . ; Calculating the size of array ."
How can the above be solved in PHP?

< ? php function mean ( $ mid , $ freq , $ n ) { $ sum = 0 ; $ freqSum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ sum = $ sum + $ mid [ $ i ] * $ freq [ $ i ] ; $ freqSum = $ freqSum + $ freq [ $ i ] ; } return $ sum / $ freqSum ; } function groupedSD ( $ lower_limit , $ upper_limit , $ freq , $ n ) { $ mid = array ( ) ; $ sum = 0 ; $ freqSum = 0 ; $ sd ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ mid [ $ i ] = ( $ lower_limit [ $ i ] + $ upper_limit [ $ i ] ) / 2 ; $ sum = $ sum + $ freq [ $ i ] * $ mid [ $ i ] * $ mid [ $ i ] ; $ freqSum = $ freqSum + $ freq [ $ i ] ; } $ sd = sqrt ( ( $ sum - $ freqSum * mean ( $ mid , $ freq , $ n ) * mean ( $ mid , $ freq , $ n ) ) / ( $ freqSum - 1 ) ) ; return $ sd ; } $ lower_limit = array ( 50 , 61 , 71 , 86 , 96 ) ; $ upper_limit = array ( 60 , 70 , 85 , 95 , 100 ) ; $ freq = array ( 9 , 7 , 9 , 12 , 8 ) ; $ n = count ( $ lower_limit ) ; echo groupedSD ( $ lower_limit , $ upper_limit , $ freq , $ n ) ; ? >


"Program to implement standard error of mean | Function to find sample mean . ; loop to calculate sum of array elements . ; Function to calculate sample standard deviation . ; Function to calculate sample error . ; Formula to find sample error . ; Driver Code"
How can the above be solved in PHP?

< ? php function mean ( $ arr , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum = $ sum + $ arr [ $ i ] ; return $ sum / $ n ; } function SSD ( $ arr , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum = $ sum + ( $ arr [ $ i ] - mean ( $ arr , $ n ) ) * ( $ arr [ $ i ] - mean ( $ arr , $ n ) ) ; return sqrt ( $ sum / ( $ n - 1 ) ) ; } function sampleError ( $ arr , $ n ) { return SSD ( $ arr , $ n ) / sqrt ( $ n ) ; } { $ arr = array ( 78.53 , 79.62 , 80.25 , 81.05 , 83.21 , 83.46 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; echo sampleError ( $ arr , $ n ) ; return 0 ; } ? >


"Program to print Arithmetic Progression series | PHP Program to print an arithmetic progression series ; Printing AP by simply adding d to previous term . ; starting number ; Common difference ; N th term to be find"
How can the above be solved in PHP?

< ? php function printAP ( $ a , $ d , $ n ) { $ curr_term ; $ curr_term = a ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { echo ( $ curr_term . " " ) ; $ curr_term += $ d ; } } $ a = 2 ; $ d = 1 ; $ n = 5 ; printAP ( $ a , $ d , $ n ) ; ? >


"Program to print Collatz Sequence | PHP program to print Collatz sequence ; We simply follow steps while we do not reach 1 ; If $n is odd ; If even ; Print 1 at the end ; Driver code"
How can the above be solved in PHP?

< ? php function printCollatz ( $ n ) { while ( $ n != 1 ) { echo $ n . " " ; if ( $ n & 1 ) $ n = 3 * $ n + 1 ; else $ n = $ n / 2 ; } echo $ n ; } printCollatz ( 6 ) ; ? >


"Program to print GP ( Geometric Progression ) | function to print GP ; starting number ; Common ratio ; N th term to be find"
How can the above be solved in PHP?

< ? php function printGP ( $ a , $ r , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ curr_term = $ a * pow ( $ r , $ i ) ; echo $ curr_term , " " ; } } $ a = 2 ; $ r = 3 ; $ n = 5 ; printGP ( $ a , $ r , $ n ) ; ? >


"Program to print all substrings of a given string | Function to print all sub strings ; Pick starting point ; Pick ending point ; Print characters from current starting point to current ending point . ; Driver Code"
How can the above be solved in PHP?

 < ? php function subString ( $ str , $ n ) { for ( $ len = 1 ; $ len <= $ n ; $ len ++ ) { for ( $ i = 0 ; $ i <= $ n - $ len ; $ i ++ ) { $ j = $ i + $ len - 1 ; for ( $ k = $ i ; $ k <= $ j ; $ k ++ ) echo $ str [ $ k ] ; echo "
 " ; } } } $ str = " abc " ; subString ( $ str , strlen ( $ str ) ) ; ? >


"Program to print an array in Pendulum Arrangement with constant space | Function to print the Pendulum arrangement of the given array ; Sort the array ; pos stores the index of the last element of the array ; odd stores the last odd index in the array ; Move all odd index positioned elements to the right ; Shift the elements by one position from odd to pos ; Reverse the element from 0 to ( n - 1 ) / 2 ; Printing the pendulum arrangement ; Driver code"
How can the above be solved in PHP?

< ? php function pendulumArrangement ( $ arr , $ n ) { sort ( $ arr ) ; $ pos = $ n - 1 ; if ( $ n % 2 == 0 ) $ odd = $ n - 1 ; else $ odd = $ n - 2 ; while ( $ odd > 0 ) { $ temp = $ arr [ $ odd ] ; $ in = $ odd ; while ( $ in != $ pos ) { $ arr [ $ in ] = $ arr [ $ in + 1 ] ; $ in ++ ; } $ arr [ $ in ] = $ temp ; $ odd = $ odd - 2 ; $ pos = $ pos - 1 ; } $ start = 0 ; $ end = floor ( ( $ n - 1 ) / 2 ) ; for ( ; $ start < $ end ; $ start ++ , $ end -- ) { $ temp = $ arr [ $ start ] ; $ arr [ $ start ] = $ arr [ $ end ] ; $ arr [ $ end ] = $ temp ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] , " ▁ " ; } $ arr = array ( 11 , 2 , 4 , 55 , 6 , 8 ) ; $ n = count ( $ arr ) ; pendulumArrangement ( $ arr , $ n ) ; ? >


"Program to print an array in Pendulum Arrangement | Prints pendulam arrangement of arr [ ] ; sorting the elements ; Auxiliary array to store output ; calculating the middle index ; storing the minimum element in the middle i is index for output array and j is for input array . ; adjustment for when no . of elements is even ; Printing the pendulum arrangement ; input Array ; calculating the length of array A ; calling pendulum function"
How can the above be solved in PHP?

 < ? php function pendulumArrangement ( $ arr , $ n ) { sort ( $ arr , $ n ) ; sort ( $ arr ) ; $ op [ $ n ] = NULL ; $ mid = floor ( ( $ n - 1 ) / 2 ) ; $ j = 1 ; $ i = 1 ; $ op [ $ mid ] = $ arr [ 0 ] ; for ( $ i = 1 ; $ i <= $ mid ; $ i ++ ) { $ op [ $ mid + $ i ] = $ arr [ $ j ++ ] ; $ op [ $ mid - $ i ] = $ arr [ $ j ++ ] ; } if ( $ n % 2 == 0 ) $ op [ $ mid + $ i ] = $ arr [ $ j ] ; echo " Pendulum ▁ arrangement : " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ op [ $ i ] , " ▁ " ; echo "
 " ; } arr = array ( 14 , 6 , 19 , 21 , 12 ) ; $ n = sizeof ( $ arr ) ; pendulumArrangement ( $ arr , $ n ) ; ? >


"Program to print an array in Pendulum Arrangement | Prints pendulam arrangement of arr [ ] ; sorting the elements ; Auxiliary array to store output ; calculating the middle index ; storing the minimum element in the middle i is index for output array and j is for input array . ; adjustment for when no . of elements is even ; Printing the pendulum arrangement ; input Array ; calling pendulum function"
How can the above be solved in PHP?

 < ? php function pendulumArrangement ( $ arr , $ n ) { sort ( $ arr , $ n ) ; sort ( $ arr ) ; $ op [ $ n ] = NULL ; $ mid = floor ( ( $ n - 1 ) / 2 ) ; $ j = 1 ; $ i = 1 ; $ op [ $ mid ] = $ arr [ 0 ] ; for ( $ i = 1 ; $ i <= $ mid ; $ i ++ ) { $ op [ $ mid + $ i ] = $ arr [ $ j ++ ] ; $ op [ $ mid - $ i ] = $ arr [ $ j ++ ] ; } if ( $ n % 2 == 0 ) $ op [ $ mid + $ i ] = $ arr [ $ j ] ; echo " Pendulum ▁ arrangement : " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ op [ $ i ] , " ▁ " ; echo "
 " ; } $ arr = array ( 14 , 6 , 19 , 21 , 12 ) ; $ n = sizeof ( $ arr ) ; pendulumArrangement ( $ arr , $ n ) ; ? >


"Program to print characters present at prime indexes in a given string | PHP Program to print Characters at Prime index in a given String ; Corner case ; Check from 2 to n - 1 ; Function to print character at prime index ; Loop to check if index prime or not ; 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 ; } function prime_index ( $ input ) { $ n = strlen ( $ input ) ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) if ( isPrime ( $ i ) ) echo $ input [ $ i - 1 ] ; } $ input = " GeeksforGeeks " ; prime_index ( $ input ) ; ? >


"Program to print factors of a number in pairs | PHP program to print prime factors in pairs . ; Driver code"
How can the above be solved in PHP?

< ? php function printPFsInPairs ( $ n ) { for ( $ i = 1 ; $ i * $ i <= $ n ; $ i ++ ) if ( $ n % $ i == 0 ) echo $ i . " * " . ▁ $ n ▁ / ▁ $ i ▁ . " " } $ n = 24 ; printPFsInPairs ( $ n ) ; return 0 ; ? >


"Program to print first n Fibonacci Numbers | Set 1 | Function to print first n Fibonacci Numbers ; Driver Code"
How can the above be solved in PHP?

< ? php function printFibonacciNumbers ( $ n ) { $ f1 = 0 ; $ f2 = 1 ; $ i ; if ( $ n < 1 ) return ; echo ( $ f1 ) ; echo ( " ▁ " ) ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { echo ( $ f2 ) ; echo ( " ▁ " ) ; $ next = $ f1 + $ f2 ; $ f1 = $ f2 ; $ f2 = $ next ; } } printFibonacciNumbers ( 7 ) ; ? >


"Program to print multiplication table of a number | change input number Change here to ; change result ."
How can the above be solved in PHP?


"Program to print non square numbers | Function to check perfect square ; function to print all non square number ; variable which stores the count ; not perfect square ; Driver Code"
How can the above be solved in PHP?

< ? php function isPerfectSquare ( $ n ) { if ( $ n < 0 ) return false ; $ root = round ( sqrt ( $ n ) ) ; return $ n == $ root * $ root ; } function printnonsquare ( $ n ) { $ count = 0 ; for ( $ i = 1 ; $ count < $ n ; ++ $ i ) { if ( ! isPerfectSquare ( $ i ) ) { echo $ i . " " ; $ count ++ ; } } } $ n = 10 ; printnonsquare ( $ n ) ; ? >


"Program to print non square numbers | PHP program to print first n non - square numbers . ; Print curr_count numbers . curr_count is current gap between two square numbers . ; skip a square number . ; Count of next non - square numbers is next even number . ; Driver code"
How can the above be solved in PHP?

< ? php function printNonSquare ( $ n ) { $ curr_count = 2 ; $ num = 2 ; $ count = 0 ; while ( $ count < $ n ) { for ( $ i = 0 ; $ i < $ curr_count && $ count < $ n ; $ i ++ ) { echo ( $ num . " " ) ; $ count ++ ; $ num ++ ; } $ num ++ ; $ curr_count += 2 ; } } $ n = 10 ; printNonSquare ( $ n ) ; ? >


"Program to print non square numbers | Returns n - th non - square number . ; loop to print non squares below n ; Driver Code"
How can the above be solved in PHP?

< ? php function nonsquare ( $ n ) { return $ n + ( int ) ( 0.5 + sqrt ( $ n ) ) ; } function printNonSquare ( $ n ) { for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) printf ( nonsquare ( $ i ) . " ▁ " ) ; } $ n = 10 ; printNonSquare ( $ n ) ; ? >


"Program to print pentatope numbers upto Nth term | Function to generate nth tetrahedral number ; Function to print pentatope number series upto nth term . ; Initialize prev as 0. It store the sum of all previously generated pentatope numbers ; Loop to print pentatope series ; Find ith tetrahedral number ; Add ith tetrahedral number to sum of all previously generated tetrahedral number to get ith pentatope number ; Update sum of all previously generated tetrahedral number ; Driver code ; Function call to print pentatope number series"
How can the above be solved in PHP?

< ? php function findTetrahedralNumber ( $ n ) { return ( ( $ n * ( $ n + 1 ) * ( $ n + 2 ) ) / 6 ) ; } function printSeries ( $ n ) { $ prev = 0 ; $ curr ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ curr = findTetrahedralNumber ( $ i ) ; $ curr = $ curr + $ prev ; echo ( $ curr . " " ) ; $ prev = $ curr ; } } $ n = 10 ; printSeries ( $ n ) ; ? >


"Program to print pentatope numbers upto Nth term | Function to print pentatope series up to nth term ; Loop to print pentatope number series ; calculate and print ith pentatope number ; Driver code ; Function call to print pentatope number series"
How can the above be solved in PHP?


"Program to print tetrahedral numbers upto Nth term | function to generate nth triangular number ; function to print tetrahedral number series up to n ; Initialize prev as 0. It store the sum of all previously generated triangular number ; Loop to print series ; Find ithh triangular number ; Add ith triangular number to sum of all previously generated triangular number to get ith tetrahedral number ; Update sum of all previously generated triangular number ; Driver code ; function call to print series"
How can the above be solved in PHP?

< ? php function findTriangularNumber ( $ n ) { return ( $ n * ( $ n + 1 ) ) / 2 ; } function printSeries ( $ n ) { $ prev = 0 ; $ curr ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ curr = findTriangularNumber ( $ i ) ; $ curr = $ curr + $ prev ; echo ( $ curr . " " ) ; $ prev = $ curr ; } } $ n = 10 ; printSeries ( $ n ) ; ? >


"Program to print tetrahedral numbers upto Nth term | function to print tetrahedral number series up to n ; loop to print series ; Calculate and print ith tetrahedral number ; Driver code ; function call to print series"
How can the above be solved in PHP?


"Program to print the Sum of series | calculate Nth term of series ; Get the value of N ; Get the sum of the series"
How can the above be solved in PHP?


"Program to print the sum of the given nth term | function to calculate sum of series ; Sum of n terms is n ^ 2 ; Driver Code"
How can the above be solved in PHP?

< ? php function summingSeries ( $ n ) { return pow ( $ n , 2 ) ; } $ n = 100 ; echo " The ▁ sum ▁ of ▁ n ▁ term ▁ is : ▁ " , summingSeries ( $ n ) ; ? >


"Program to print triangular number series till n | Function to find triangular number ; Driver Code"
How can the above be solved in PHP?

< ? php function triangular_series ( $ n ) { for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) echo ( " ▁ " . $ i * ( $ i + 1 ) / 2 . " ▁ " ) ; } $ n = 5 ; triangular_series ( $ n ) ; ? >


"Program to print triangular number series till n | Function to find triangular number ; For each iteration increase j by 1 and add it into k ; Increasing j by 1 ; Add value of j into k and update k ; Driver Code"
How can the above be solved in PHP?

< ? php function triangular_series ( $ n ) { $ i ; $ j = 1 ; $ k = 1 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { echo ( " ▁ " . $ k . " ▁ " ) ; $ j = $ j + 1 ; $ k = $ k + $ j ; } } $ n = 5 ; triangular_series ( $ n ) ; ? >


"Program to replace a word with asterisks in a sentence | Function takes two parameter ; Break down sentence by ' ▁ ' spaces and store each individual word in a different list ; A new string to store the result ; Creating the censor which is an asterisks " * " text of the length of censor word ; count variable to access our word_list ; Iterating through our list of extracted words ; changing the censored word to created asterisks censor ; join the words ; Driver code"
How can the above be solved in PHP?

 < ? php function censor ( $ text , $ word ) { $ word_list = explode ( " ▁ " , $ text ) ; $ result = ' ' $ stars = " " ; for ( $ i = 0 ; $ i < strlen ( $ word ) ; $ i ++ ) $ stars . = " * " ; $ count = 0 ; $ index = 0 ; for ( $ i = 0 ; $ i < sizeof ( $ word_list ) ; $ i ++ ) { if ( $ word_list [ $ i ] == $ word ) $ word_list [ $ index ] = $ stars ; $ index += 1 ; } return implode ( ' ▁ ' , $ word_list ) ; } $ extract = " GeeksforGeeks ▁ is ▁ a ▁ computer ▁ science ▁ " . " portal for geeks . I am pursuing my " .
 	 	 	 	 	 " major in computer science . " $ cen = " computer " ; echo censor ( $ extract , $ cen ) ; ? >


"Program to replace every space in a string with hyphen | Get the String ; Traverse the string character by character . ; Changing the ith character to ' - ' if it 's a space. ; Print the modified string ."
How can the above be solved in PHP?

 < ? php $ str = " A ▁ computer ▁ science ▁ portal ▁ for ▁ geeks " ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; ++ $ i ) { if ( $ str [ $ i ] == ' ▁ ' ) { $ str [ $ i ] = ' - ' ; } } echo $ str . "
 " ;


"Program to sort string in descending order | PHP program to sort a string in descending order using library function ; Driver Code ; descOrder ( $s ) ; function call"
How can the above be solved in PHP?

< ? php function descOrder ( $ s ) { $ s = str_split ( $ s ) ; rsort ( $ s ) ; echo implode ( ' ' , $ s ) ; } $ s = " geeksforgeeks " ; ? >


"Puzzle | Minimum distance for Lizard | side of cube ; understand from diagram ; understand from diagram ; minimum distance"
How can the above be solved in PHP?

 < ? php $ a = 5 ; $ AC = $ a ; $ CE = 2 * $ a ; $ shortestDistance = ( double ) ( sqrt ( $ AC * $ AC + $ CE * $ CE ) ) ; echo $ shortestDistance . "
 " ; ? >


"Puzzle | Program to find number of squares in a chessboard | Function to return count of squares ; ; A better way to write n * ( n + 1 ) * ( 2 n + 1 ) / 6 ; Driver Code"
How can the above be solved in PHP?

< ? php function countSquares ( $ n ) { return ( $ n * ( $ n + 1 ) / 2 ) * ( 2 * $ n + 1 ) / 3 ; } $ n = 4 ; echo " Count ▁ of ▁ squares ▁ is ▁ " , countSquares ( $ n ) ; ? >


"Pythagorean Quadruple | function for checking ; Driver Code"
How can the above be solved in PHP?

< ? php function pythagorean_quadruple ( $ a , $ b , $ c , $ d ) { $ sum = $ a * $ a + $ b * $ b + $ c * $ c ; if ( $ d * $ d == $ sum ) return true ; else return false ; } $ a = 1 ; $ b = 2 ; $ c = 2 ; $ d = 3 ; if ( pythagorean_quadruple ( $ a , $ b , $ c , $ d ) ) echo " Yes " ; else echo " No " ; ? >


"Quadruplet pair with XOR zero in the given Array | PHP implementation of the approach ; Function that returns true if the array contains a valid quadruplet pair ; We can always find a valid quadruplet pair for array size greater than MAX ; For smaller size arrays , perform brute force ; Driver code"
How can the above be solved in PHP?

< ? php const MAX = 130 ; function validQuadruple ( $ arr , $ n ) { if ( $ n >= MAX ) return true ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) for ( $ k = $ j + 1 ; $ k < $ n ; $ k ++ ) for ( $ l = $ k + 1 ; $ l < $ n ; $ l ++ ) { if ( ( $ arr [ $ i ] ^ $ arr [ $ j ] ^ $ arr [ $ k ] ^ $ arr [ $ l ] ) == 0 ) { return true ; } } return false ; } $ arr = array ( 1 , 0 , 2 , 3 , 7 ) ; $ n = count ( $ arr ) ; if ( validQuadruple ( $ arr , $ n ) ) echo ( " Yes " ) ; else echo ( " No " ) ; ? >


"Queries for GCD of all numbers of an array except elements in a given range | Calculating GCD using euclid algorithm ; Filling the prefix and suffix array ; Filling the prefix array following relation prefix ( i ) = GCD ( prefix ( i - 1 ) , arr ( i ) ) ; Filling the suffix array following the relation suffix ( i ) = GCD ( suffix ( i + 1 ) , arr ( i ) ) ; To calculate gcd of the numbers outside range ; If l = 0 , we need to tell GCD of numbers from r + 1 to n ; If r = n - 1 we need to return the gcd of numbers from 1 to l ; Driver Code"
How can the above be solved in PHP?

< ? php function GCD ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; return GCD ( $ b , $ a % $ b ) ; } function FillPrefixSuffix ( & $ prefix , & $ arr , & $ suffix , $ n ) { $ prefix [ 0 ] = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ prefix [ $ i ] = GCD ( $ prefix [ $ i - 1 ] , $ arr [ $ i ] ) ; $ suffix [ $ n - 1 ] = $ arr [ $ n - 1 ] ; for ( $ i = $ n - 2 ; $ i >= 0 ; $ i -- ) $ suffix [ $ i ] = GCD ( $ suffix [ $ i + 1 ] , $ arr [ $ i ] ) ; } function GCDoutsideRange ( $ l , $ r , & $ prefix , & $ suffix , $ n ) { if ( $ l == 0 ) return $ suffix [ $ r + 1 ] ; if ( $ r == $ n - 1 ) return $ prefix [ $ l - 1 ] ; return GCD ( $ prefix [ $ l - 1 ] , $ suffix [ $ r + 1 ] ) ; } $ arr = array ( 2 , 6 , 9 ) ; $ n = sizeof ( $ arr ) ; $ prefix = array_fill ( 0 , $ n , NULL ) ; $ suffix = array_fill ( 0 , $ n , NULL ) ; FillPrefixSuffix ( $ prefix , $ arr , $ suffix , $ n ) ; $ l = 0 ; $ r = 0 ; echo GCDoutsideRange ( $ l , $ r , $ prefix , $ suffix , $ n ) . " " ; $ l = 1 ; $ r = 1 ; echo GCDoutsideRange ( $ l , $ r , $ prefix , $ suffix , $ n ) . " " ; $ l = 1 ; $ r = 2 ; echo GCDoutsideRange ( $ l , $ r , $ prefix , $ suffix , $ n ) . " " ; ? >


"Queries for bitwise OR in the index range [ L , R ] of the given array | PHP implementation of the approach ; Array to store bit - wise prefix count ; Function to find the prefix sum ; Loop for each bit ; Loop to find prefix count ; Function to answer query ; To store the answer ; Loop for each bit ; To store the number of variables with ith bit set ; Condition for ith bit of answer to be set ; Driver code"
How can the above be solved in PHP?

 < ? php $ MAX = 100000 ; $ bitscount = 32 ; $ prefix_count = array_fill ( 0 , $ bitscount , array_fill ( 0 , $ MAX , NULL ) ) ; function findPrefixCount ( & $ arr , $ n ) { global $ MAX , $ bitscount , $ prefix_count ; for ( $ i = 0 ; $ i < $ bitscount ; $ i ++ ) { $ prefix_count [ $ i ] [ 0 ] = ( ( $ arr [ 0 ] >> $ i ) & 1 ) ; for ( $ j = 1 ; $ j < $ n ; $ j ++ ) { $ prefix_count [ $ i ] [ $ j ] = ( ( $ arr [ $ j ] >> $ i ) & 1 ) ; $ prefix_count [ $ i ] [ $ j ] += $ prefix_count [ $ i ] [ $ j - 1 ] ; } } } function rangeOr ( $ l , $ r ) { global $ MAX , $ bitscount , $ prefix_count ; $ ans = 0 ; for ( $ i = 0 ; $ i < $ bitscount ; $ i ++ ) { if ( $ l == 0 ) $ x = $ prefix_count [ $ i ] [ $ r ] ; else $ x = $ prefix_count [ $ i ] [ $ r ] - $ prefix_count [ $ i ] [ l - 1 ] ; if ( $ x != 0 ) $ ans = ( $ ans | ( 1 << $ i ) ) ; } return $ ans ; } $ arr = array ( 7 , 5 , 3 , 5 , 2 , 3 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; findPrefixCount ( $ arr , $ n ) ; $ queries = array ( array ( 1 , 3 ) , array ( 4 , 5 ) ) ; $ q = sizeof ( $ queries ) / sizeof ( $ queries [ 0 ] ) ; for ( $ i = 0 ; $ i < $ q ; $ i ++ ) echo rangeOr ( $ queries [ $ i ] [ 0 ] , $ queries [ $ i ] [ 1 ] ) . "
 " ; return 0 ; ? >


"Queries for characters in a repeated string | Print whether index i and j have same element or not . ; Finding relative position of index i , j . ; Checking is element are same at index i , j . ; Driver Code"
How can the above be solved in PHP?

 < ? php function query ( $ s , $ i , $ j ) { $ n = strlen ( $ s ) ; $ i %= $ n ; $ j %= $ n ; if ( ( $ s [ $ i ] == $ s [ $ j ] ) ) echo " Yes
 " ; else echo " No " ; } $ X = " geeksforgeeks " ; query ( $ X , 0 , 8 ) ; query ( $ X , 8 , 13 ) ; query ( $ X , 6 , 15 ) ; ? >


"Queries for counts of array elements with values in given range | function to count elements within given range ; initialize result ; check if element is in range ; Driver Code ; Answer queries"
How can the above be solved in PHP?

< ? php function countInRange ( $ arr , $ n , $ x , $ y ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] >= $ x && $ arr [ $ i ] <= $ y ) $ count ++ ; } return $ count ; } $ arr = array ( 1 , 3 , 4 , 9 , 10 , 3 ) ; $ n = count ( $ arr ) ; $ i = 1 ; $ j = 4 ; echo countInRange ( $ arr , $ n , $ i , $ j ) . " " ; $ i = 9 ; $ j = 12 ; echo countInRange ( $ arr , $ n , $ i , $ j ) . " " ; ? >


"Queries for counts of array elements with values in given range | function to find first index >= x ; function to find last index <= y ; function to count elements within given range ; initialize result ; Driver Code ; Preprocess array ; Answer queries"
How can the above be solved in PHP?

< ? php function lowerIndex ( $ arr , $ n , $ x ) { $ l = 0 ; $ h = $ n - 1 ; while ( $ l <= $ h ) { $ mid = ( $ l + $ h ) / 2 ; if ( $ arr [ $ mid ] >= $ x ) $ h = $ mid - 1 ; else $ l = $ mid + 1 ; } return $ l ; } function upperIndex ( $ arr , $ n , $ y ) { $ l = 0 ; $ h = $ n - 1 ; while ( $ l <= $ h ) { $ mid = ( $ l + $ h ) / 2 ; if ( $ arr [ $ mid ] <= $ y ) $ l = $ mid + 1 ; else $ h = $ mid - 1 ; } return $ h ; } function countInRange ( $ arr , $ n , $ x , $ y ) { $ count = 0 ; $ count = ( upperIndex ( $ arr , $ n , $ y ) - lowerIndex ( $ arr , $ n , $ x ) + 1 ) ; $ t = floor ( $ count ) ; return $ t ; } $ arr = array ( 1 , 4 , 4 , 9 , 10 , 3 ) ; $ n = sizeof ( $ arr ) ; sort ( $ arr ) ; $ i = 1 ; $ j = 4 ; echo countInRange ( $ arr , $ n , $ i , $ j ) , " " ; $ i = 9 ; $ j = 12 ; echo countInRange ( $ arr , $ n , $ i , $ j ) , " " ; ? >


"Queries for decimal values of subarrays of a binary array | Fills pre [ ] ; returns the number represented by a binary subarray l to r ; if r is equal to n - 1 r + 1 does not exist ; Driver Code"
How can the above be solved in PHP?

< ? php function precompute ( & $ arr , $ n , & $ pre ) { $ pre [ $ n - 1 ] = $ arr [ $ n - 1 ] * pow ( 2 , 0 ) ; for ( $ i = $ n - 2 ; $ i >= 0 ; $ i -- ) $ pre [ $ i ] = $ pre [ $ i + 1 ] + $ arr [ $ i ] * ( 1 << ( $ n - 1 - $ i ) ) ; } function decimalOfSubarr ( & $ arr , $ l , $ r , $ n , & $ pre ) { if ( $ r != $ n - 1 ) return ( $ pre [ $ l ] - $ pre [ $ r + 1 ] ) / ( 1 << ( $ n - 1 - $ r ) ) ; return $ pre [ $ l ] / ( 1 << ( $ n - 1 - $ r ) ) ; } $ arr = array ( 1 , 0 , 1 , 0 , 1 , 1 ) ; $ n = sizeof ( $ arr ) ; $ pre = array_fill ( 0 , $ n , NULL ) ; precompute ( $ arr , $ n , $ pre ) ; echo decimalOfSubarr ( $ arr , 2 , 4 , $ n , $ pre ) . " " ; echo decimalOfSubarr ( $ arr , 4 , 5 , $ n , $ pre ) . " " ; ? >


"Queries for maximum difference between prime numbers in given ranges | PHP program to find maximum differences between two prime numbers in given ranges ; Precompute Sieve , Prefix array , Suffix array ; Sieve of Eratosthenes ; Precomputing Prefix array . ; Precompute Suffix array . ; Function to solve each query ; Driver Code"
How can the above be solved in PHP?

 < ? php $ MAX = 100005 ; function precompute ( & $ prefix , & $ suffix ) { global $ MAX ; $ prime = array_fill ( 0 , $ MAX , true ) ; for ( $ i = 2 ; $ i * $ i < $ MAX ; $ i ++ ) { if ( $ prime [ $ i ] ) { for ( $ j = $ i * $ i ; $ j < $ MAX ; $ j += $ i ) $ prime [ $ j ] = false ; } } $ prefix [ 1 ] = 1 ; $ suffix [ $ MAX - 1 ] = 1e9 + 7 ; for ( $ i = 2 ; $ i < $ MAX ; $ i ++ ) { if ( $ prime [ $ i ] ) $ prefix [ $ i ] = $ i ; else $ prefix [ $ i ] = $ prefix [ $ i - 1 ] ; } for ( $ i = $ MAX - 1 ; $ i > 1 ; $ i -- ) { if ( $ prime [ $ i ] ) $ suffix [ $ i ] = $ i ; else $ suffix [ $ i ] = $ suffix [ $ i + 1 ] ; } } function query ( $ prefix , $ suffix , $ L , $ R ) { if ( $ prefix [ $ R ] < $ L $ suffix [ $ L ] > $ R ) return 0 ; else return $ prefix [ $ R ] - $ suffix [ $ L ] ; } $ q = 3 ; $ L = array ( 2 , 2 , 24 ) ; $ R = array ( 5 , 2 , 28 ) ; $ prefix = array_fill ( 0 , $ MAX + 1 , 0 ) ; $ suffix = array_fill ( 0 , $ MAX + 1 , 0 ) ; precompute ( $ prefix , $ suffix ) ; for ( $ i = 0 ; $ i < $ q ; $ i ++ ) echo query ( $ prefix , $ suffix , $ L [ $ i ] , $ R [ $ i ] ) . "
 " ; ? >


"Queries on XOR of XORs of all subarrays | Output for each query ; If number of element is even . ; If number of element is odd . ; if l is even ; if l is odd ; Wrapper Function ; Evaluating prefixodd and prefixeven ; Driver code"
How can the above be solved in PHP?

 < ? php function ansQueries ( $ prefeven , $ prefodd , $ l , $ r ) { if ( ( $ r - $ l + 1 ) % 2 == 0 ) { echo "0" ; } else { if ( $ l % 2 == 0 ) echo ( $ prefeven [ $ r ] ^ $ prefeven [ $ l - 1 ] ) ; else echo ( $ prefodd [ $ r ] ^ $ prefodd [ $ l - 1 ] ) ; } echo "
 " ; } function wrapper ( array $ arr , $ n , array $ l , array $ r , $ q ) { $ prefodd = array_fill ( 0 , 100 , 0 ) ; $ prefeven = array_fill ( 0 , 100 , 0 ) ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { if ( ( $ i ) % 2 == 0 ) { $ prefeven [ $ i ] = $ arr [ $ i - 1 ] ^ $ prefeven [ $ i - 1 ] ; $ prefodd [ $ i ] = $ prefodd [ $ i - 1 ] ; } else { $ prefeven [ $ i ] = $ prefeven [ $ i - 1 ] ; $ prefodd [ $ i ] = $ prefodd [ $ i - 1 ] ^ $ arr [ $ i - 1 ] ; } } $ i = 0 ; while ( $ i != $ q ) { ansQueries ( $ prefeven , $ prefodd , $ l [ $ i ] , $ r [ $ i ] ) ; $ i ++ ; } } $ arr = array ( 1 , 2 , 3 , 4 , 5 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; $ l = array ( 1 , 1 , 2 ) ; $ r = array ( 2 , 3 , 4 ) ; $ q = sizeof ( $ l ) / sizeof ( $ l [ 0 ] ) ; wrapper ( $ arr , $ n , $ l , $ r , $ q ) ; ? >


"Queries on substring palindrome formation | Query type 1 : update string position i with character x . ; Print " Yes " if range [ L . . R ] can form palindrome , else print " No " . ; Find the frequency of each character in S [ L ... R ] . ; Checking if more than one character have frequency greater than 1. ; Driver code"
How can the above be solved in PHP?


"Queries on sum of odd number digit sums of all the factors of a number | PHP Program to answer queries on sum of sum of odd number digits of all the factors of a number ; finding sum of odd digit number in each integer . ; for each number ; using previous number sum , finding the current number num of odd digit also , adding last digit if it is odd . ; finding sum of sum of odd digit of all the factors of a number . ; for each possible factor ; adding the contribution . ; Wrapper function ; Driver Code"
How can the above be solved in PHP?

< ? php $ N = 1000005 ; function sumOddDigit ( & $ digitSum ) { global $ N ; for ( $ i = 1 ; $ i < $ N ; $ i ++ ) { $ digitSum [ $ i ] = $ digitSum [ intval ( $ i / 10 ) ] + intval ( $ i & 1 ) * ( $ i % 10 ) ; } } function sumFactor ( $ digitSum , & $ factorDigitSum ) { global $ N ; for ( $ i = 1 ; $ i < $ N ; $ i ++ ) { for ( $ j = $ i ; $ j < $ N ; $ j += $ i ) { $ factorDigitSum [ $ j ] += $ digitSum [ $ i ] ; } } } function wrapper ( $ q , $ n ) { global $ N ; $ digitSum = array ( ) ; $ factorDigitSum = array ( ) ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { $ digitSum [ $ i ] = 0 ; $ factorDigitSum [ $ i ] = 0 ; } sumOddDigit ( $ digitSum ) ; sumFactor ( $ digitSum , $ factorDigitSum ) ; for ( $ i = 0 ; $ i < $ q ; $ i ++ ) echo ( $ factorDigitSum [ $ n [ $ i ] ] . " ▁ " ) ; } $ q = 2 ; $ n = array ( 10 , 36 ) ; wrapper ( $ q , $ n ) ; ? >


"Queries on the sum of prime factor counts in a range | PHP program to find sum prime factors in given range . ; using sieve method to evaluating the prime factor of numbers ; if i is prime ; setting number of prime factor of a prime number . ; Returns sum of counts of prime factors in range from l to r . This function mainly uses count [ ] which is filled by Sieve ( ) ; finding the sum of number of prime factor of numbers in a range . ; Driver Code"
How can the above be solved in PHP?

< ? php $ MAX = 100006 ; function sieve ( & $ count ) { global $ MAX ; for ( $ i = 2 ; $ i * $ i <= $ MAX ; $ i ++ ) { if ( $ count [ $ i ] == 0 ) { for ( $ j = 2 * $ i ; $ j < $ MAX ; $ j += $ i ) $ count [ $ j ] ++ ; $ count [ $ i ] = 1 ; } } } function query ( $ count , $ l , $ r ) { $ sum = 0 ; for ( $ i = $ l ; $ i <= $ r ; $ i ++ ) $ sum += $ count [ $ i ] ; return $ sum ; } $ count = array_fill ( 0 , $ MAX , 0 ) ; sieve ( $ count ) ; echo query ( $ count , 6 , 10 ) . " " . query ( $ count , 1 , 5 ) ; ? >


"Queries to answer the number of ones and zero to the left of given index | Function to pre - calculate the left [ ] array ; Iterate in the binary array ; Initialize the number of 1 and 0 ; Increase the count ; Driver code ; Queries ; Solve queries"
How can the above be solved in PHP?

 < ? php function preCalculate ( $ binary , $ n ) { $ left = array ( ) ; $ count1 = 0 ; $ count0 = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ left [ $ i ] = array ( $ count1 , $ count0 ) ; if ( $ binary [ $ i ] ) $ count1 ++ ; else $ count0 ++ ; } return $ left ; } $ binary = array ( 1 , 1 , 1 , 0 , 0 , 1 , 0 , 1 , 1 ) ; $ n = count ( $ binary ) ; $ left = preCalculate ( $ binary , $ n ) ; $ queries = array ( 0 , 1 , 2 , 4 ) ; $ q = count ( $ queries ) ; for ( $ i = 0 ; $ i < $ q ; $ i ++ ) echo $ left [ $ queries [ $ i ] ] [ 0 ] , " ▁ ones ▁ " , $ left [ $ queries [ $ i ] ] [ 1 ] , " ▁ zeros
 " ; ? >


"Queries to check if it is possible to join boxes in a circle | PHP implementation of above approach ; Print the answer to each query ; setting the flag for exception ; replacing the greater element in i and j ; checking if that box is not used in previous query . ; checking if connecting to the same box ; case 1 : x < i and y lies between i and j ; case 2 : x lies between i and j and y > j ; if flag is not reset inbetween . ; Driver code"
How can the above be solved in PHP?


"Queries to check if substring [ L ... R ] is palindrome or not | PHP implementation of the approach ; Pre - processing function ; Get the size of the string ; Initially mark every position as false ; For the length ; Iterate for every index with length j ; If the length is less than 2 ; If characters are equal ; Check for equal ; Function to answer every query in O ( 1 ) ; Driver code"
How can the above be solved in PHP?


"Queries to count the number of unordered co | PHP program to find number of unordered coprime pairs of integers from 1 to N ; to store euler 's totient function ; to store required answer ; Computes and prints totient of all numbers smaller than or equal to N . ; Initialise the phi [ ] with 1 ; Compute other Phi values ; If phi [ p ] is not computed already , then number p is prime ; Phi of a prime number p is always equal to p - 1. ; Update phi values of all multiples of p ; Add contribution of p to its multiple i by multiplying with ( 1 - 1 / p ) ; function to compute number coprime pairs ; function call to compute euler totient function ; prefix sum of all euler totient function values ; function call"
How can the above be solved in PHP?

 < ? php $ N = 100005 ; $ phi = array_fill ( 0 , $ N , 0 ) ; $ S = array_fill ( 0 , $ N , 0 ) ; function computeTotient ( ) { global $ N , $ phi , $ S ; for ( $ i = 1 ; $ i < $ N ; $ i ++ ) $ phi [ $ i ] = $ i ; for ( $ p = 2 ; $ p < $ N ; $ p ++ ) { if ( $ phi [ $ p ] == $ p ) { $ phi [ $ p ] = $ p - 1 ; for ( $ i = 2 * $ p ; $ i < $ N ; $ i += $ p ) { $ phi [ $ i ] = ( int ) ( ( $ phi [ $ i ] / $ p ) * ( $ p - 1 ) ) ; } } } } function CoPrimes ( ) { global $ N , $ phi , $ S ; computeTotient ( ) ; for ( $ i = 1 ; $ i < $ N ; $ i ++ ) $ S [ $ i ] = $ S [ $ i - 1 ] + $ phi [ $ i ] ; } CoPrimes ( ) ; $ q = array ( 3 , 4 ) ; $ n = sizeof ( $ q ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo " Number ▁ of ▁ unordered ▁ coprime
 " . " pairs ▁ of ▁ integers ▁ from ▁ 1 ▁ to ▁ " . $ q [ $ i ] . " ▁ are ▁ " . $ S [ $ q [ $ i ] ] . "
 " ; ? >


"Queries to find the last non | Maximum distinct characters possible ; To store the frequency of the characters ; Function to pre - calculate the frequency array ; Only the first character has frequency 1 till index 0 ; Starting from the second character of the string ; For every possible character ; Current character under consideration ; If it is equal to the character at the current index ; Function to return the frequency of the given character in the sub - string $str [ $l ... $r ] ; Function to return the last non - repeating character ; Starting from the last character ; Current character ; If frequency of the current character is 1 then return the character ; All the characters of the sub - string are repeating ; Driver code ; Pre - calculate the frequency array"
How can the above be solved in PHP?

 < ? php $ MAX = 256 ; $ freq = array_fill ( 0 , 256 , array_fill ( 0 , 1000 , 0 ) ) ; function preCalculate ( $ str , $ n ) { global $ freq ; global $ MAX ; $ freq [ ord ( $ str [ 0 ] ) ] [ 0 ] = 1 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ ch = $ str [ $ i ] ; for ( $ j = 0 ; $ j < $ MAX ; $ j ++ ) { $ charToUpdate = chr ( $ j ) ; if ( $ charToUpdate == $ ch ) $ freq [ $ j ] [ $ i ] = $ freq [ $ j ] [ $ i - 1 ] + 1 ; else $ freq [ $ j ] [ $ i ] = $ freq [ $ j ] [ $ i - 1 ] ; } } } function getFrequency ( $ ch , $ l , $ r ) { global $ freq ; if ( $ l == 0 ) return $ freq [ ord ( $ ch ) ] [ $ r ] ; else return ( $ freq [ ord ( $ ch ) ] [ $ r ] - $ freq [ ord ( $ ch ) ] [ $ l - 1 ] ) ; } function lastNonRepeating ( $ str , $ n , $ l , $ r ) { for ( $ i = $ r ; $ i >= $ l ; $ i -- ) { $ ch = $ str [ $ i ] ; if ( getFrequency ( $ ch , $ l , $ r ) == 1 ) return $ ch ; } return " - 1" ; } $ str = " GeeksForGeeks " ; $ n = strlen ( $ str ) ; $ queries = array ( array ( 2 , 9 ) , array ( 2 , 3 ) , array ( 0 , 12 ) ) ; $ q = 3 ; preCalculate ( $ str , $ n ) ; for ( $ i = 0 ; $ i < $ q ; $ i ++ ) { echo ( lastNonRepeating ( $ str , $ n , $ queries [ $ i ] [ 0 ] , $ queries [ $ i ] [ 1 ] ) ) , "
 " ; } ? >


"Queries to find whether a number has exactly four distinct factors or not | Initialize global variable according to given condition so that it can be accessible to all function ; Function to calculate all number having four distinct factors ; 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 ; Initialize prime [ ] array which will contains all the primes from 1 - N ; Iterate over all the prime numbers ; Mark cube root of prime numbers ; Mark product of prime numbers ; Driver Code"
How can the above be solved in PHP?

 < ? php $ N = 1000001 ; $ fourDiv = array_fill ( 0 , $ N + 1 , false ) ; function fourDistinctFactors ( ) { global $ N ; global $ fourDiv ; $ primeAll = array_fill ( 0 , $ N + 1 , true ) ; for ( $ p = 2 ; $ p * $ p <= $ N ; $ p ++ ) { if ( $ primeAll [ $ p ] == true ) { for ( $ i = $ p * 2 ; $ i <= $ N ; $ i += $ p ) $ primeAll [ $ i ] = false ; } } $ prime ; $ x = 0 ; for ( $ p = 2 ; $ p <= $ N ; $ p ++ ) if ( $ primeAll [ $ p ] ) $ prime [ $ x ++ ] = $ p ; for ( $ i = 0 ; $ i < $ x ; ++ $ i ) { $ p = $ prime [ $ i ] ; if ( 1 * $ p * $ p * $ p <= $ N ) $ fourDiv [ $ p * $ p * $ p ] = true ; for ( $ j = $ i + 1 ; $ j < $ x ; ++ $ j ) { $ q = $ prime [ $ j ] ; if ( 1 * $ p * $ q > $ N ) break ; $ fourDiv [ $ p * $ q ] = true ; } } } fourDistinctFactors ( ) ; $ num = 10 ; if ( $ fourDiv [ $ num ] ) echo " Yes
 " ; else echo " No
 " ; $ num = 12 ; if ( $ fourDiv [ $ num ] ) echo " Yes
 " ; else echo " No
 " ; ? >


"Quickly find multiple left rotations of an array | Set 1 | Fills $temp with two copies of $arr ; Store $arr elements at i and i + n ; Function to left rotate an array k times ; Starting position of array after k rotations in temp [ ] will be k % n ; Print array after k rotations ; Driver Code"
How can the above be solved in PHP?

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


"Quickly find multiple left rotations of an array | Set 1 | Function to left rotate an array k times ; Print array after k rotations ; Driver program"
How can the above be solved in PHP?

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


"Quotient and remainder dividing by 2 ^ k ( a power of 2 ) | function to print remainder and quotient ; print Remainder by n AND ( m - 1 ) ; print quotient by right shifting n by ( log ( m , 2 ) ) times 2 is base ; Driver Code"
How can the above be solved in PHP?

 < ? php function divide ( $ n , $ m ) { echo " Remainder = " . ▁ ( ( $ n ) ▁ & ( $ m ▁ - ▁ 1 ) ) ;
 	 echo ▁ " Quotient = " } $ n = 43 ; $ m = 8 ; divide ( $ n , $ m ) ; ? >


"Rabin | d is the number of characters in the input alphabet ; pat -> pattern txt -> text q -> A prime number ; $p = 0 ; hash value for pattern $t = 0 ; hash value for txt ; The value of h would be " pow ( d , ▁ M - 1 ) % q " ; Calculate the hash value of pattern and first window of text ; Slide the pattern over text one by one ; Check the hash values of current window of text and pattern . If the hash values match then only check for characters on by one ; Check for characters one by one ; if p == t and pat [ 0. . . M - 1 ] = txt [ i , i + 1 , ... i + M - 1 ] ; Calculate hash value for next window of text : Remove leading digit , add trailing digit ; We might get negative value of t , converting it to positive ; Driver Code ; A prime number ; Function Call"
How can the above be solved in PHP?


"Rabin | d is the number of characters in the input alphabet ; pat -> pattern txt -> text q -> A prime number ; hash value ; for pattern hash value ; The value of h would be " pow ( d , ▁ M - 1 ) % q " ; Calculate the hash value of pattern and first window of text ; Slide the pattern over text one by one ; Check the hash values of current window of text and pattern . If the hash values match then only check for characters on by one ; Check for characters one by one ; if p == t and pat [ 0. . . M - 1 ] = txt [ i , i + 1 , ... i + M - 1 ] ; Calculate hash value for next window of text : Remove leading digit , add trailing digit ; We might get negative value of t , converting it to positive ; Driver Code ; A prime number ; Function Call"
How can the above be solved in PHP?


"Radix Sort | A function to do counting sort of arr [ ] according to the digit represented by exp . ; output array ; Store count of occurrences in count [ ] ; Change count [ i ] so that count [ i ] now contains actual position of this digit in output [ ] ; Build the output array ; Copy the output array to arr [ ] , so that arr [ ] now contains sorted numbers according to current digit ; The main function to that sorts arr [ ] of size n using Radix Sort ; Find the maximum number to know number of digits ; Do counting sort for every digit . Note that instead of passing digit number , exp is passed . exp is 10 ^ i where i is current digit number ; A utility function to print an array ; Driver Code ; Function Call"
How can the above be solved in PHP?

< ? php function countSort ( & $ arr , $ n , $ exp ) { $ output = array_fill ( 0 , $ n , 0 ) ; $ count = array_fill ( 0 , 10 , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ count [ ( $ arr [ $ i ] / $ exp ) % 10 ] ++ ; for ( $ i = 1 ; $ i < 10 ; $ i ++ ) $ count [ $ i ] += $ count [ $ i - 1 ] ; for ( $ i = $ n - 1 ; $ i >= 0 ; $ i -- ) { $ output [ $ count [ ( $ arr [ $ i ] / $ exp ) % 10 ] - 1 ] = $ arr [ $ i ] ; $ count [ ( $ arr [ $ i ] / $ exp ) % 10 ] -- ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ arr [ $ i ] = $ output [ $ i ] ; } function radixsort ( & $ arr , $ n ) { $ m = max ( $ arr ) ; for ( $ exp = 1 ; $ m / $ exp > 0 ; $ exp *= 10 ) countSort ( $ arr , $ n , $ exp ) ; } function PrintArray ( & $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; } $ arr = array ( 170 , 45 , 75 , 90 , 802 , 24 , 2 , 66 ) ; $ n = count ( $ arr ) ; radixsort ( $ arr , $ n ) ; PrintArray ( $ arr , $ n ) ; ? >


"Rank of all elements in an array | Function to find rank ; Rank Vector ; Sweep through all elements in A for each element count the number of less than and equal elements separately in r and s . ; Use formula to obtain rank ; Driver Code"
How can the above be solved in PHP?


"Rank of an element in a stream | Driver code"
How can the above be solved in PHP?

 < ? php $ a = array ( 5 , 1 , 14 , 4 , 15 , 9 , 7 , 20 , 11 ) ; $ key = 20 ; $ arraySize = sizeof ( $ a ) ; $ count = 0 ; for ( $ i = 0 ; $ i < $ arraySize ; $ i ++ ) { if ( $ a [ $ i ] <= $ key ) { $ count += 1 ; } } echo " Rank ▁ of ▁ " . $ key . " ▁ in ▁ stream ▁ is : ▁ " . ( $ count - 1 ) . "
 " ; ? >


"Ratio of mth and nth terms of an A . P . with given ratio of sums | function to calculate ratio of mth and nth term ; ratio will be tm / tn = ( 2 * m - 1 ) / ( 2 * n - 1 ) ; Driver code"
How can the above be solved in PHP?

< ? php function CalculateRatio ( $ m , $ n ) { return ( 2 * $ m - 1 ) / ( 2 * $ n - 1 ) ; } $ m = 6 ; $ n = 2 ; echo CalculateRatio ( $ m , $ n ) ; ? >


"Ratio of the distance between the centers of the circles and the point of intersection of two direct common tangents to the circles | Function to find the GCD ; Function to find the ratio ; Driver code"
How can the above be solved in PHP?

< ? php function GCD ( $ a , $ b ) { return ( $ b != 0 ? GCD ( $ b , $ a % $ b ) : $ a ) ; } function ratiotang ( $ r1 , $ r2 ) { echo " The ▁ ratio ▁ is ▁ " , $ r1 / GCD ( $ r1 , $ r2 ) , " ▁ : ▁ " , $ r2 / GCD ( $ r1 , $ r2 ) ; } $ r1 = 4 ; $ r2 = 6 ; ratiotang ( $ r1 , $ r2 ) ; ? >


"Ratio of the distance between the centers of the circles and the point of intersection of two transverse common tangents to the circles | PHP program to find the ratio of the distance between the centres of the circles and the point of intersection of two transverse common tangents to the circles which do not touch each other ; Function to find the ratio ; Driver code"
How can the above be solved in PHP?

< ? php function GCD ( $ a , $ b ) { return ( $ b != 0 ? GCD ( $ b , $ a % $ b ) : $ a ) ; } function ratiotang ( $ r1 , $ r2 ) { echo " The ▁ ratio ▁ is ▁ " , $ r1 / GCD ( $ r1 , $ r2 ) , " : " , $ r2 / GCD ( $ r1 , $ r2 ) ; } $ r1 = 4 ; $ r2 = 8 ; ratiotang ( $ r1 , $ r2 ) ; ? >


"Reallocation of elements based on Locality of Reference | A function to perform sequential search . ; Linearly search the element ; If not found ; Shift elements before one position ; Driver Code"
How can the above be solved in PHP?


"Rearrange Odd and Even values in Alternate Fashion in Ascending Order | PHP implementation of the above approach ; Sort the array ; $v1 = array ( ) ; to insert even values $v2 = array ( ) ; to insert odd values ; Set flag to true if first element is even ; Start rearranging array ; If first element is even ; Else , first element is Odd ; Print the rearranged array ; Driver code"
How can the above be solved in PHP?

< ? php function AlternateRearrange ( $ arr , $ n ) { sort ( $ arr ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] % 2 == 0 ) array_push ( $ v1 , $ arr [ $ i ] ) ; else array_push ( $ v2 , $ arr [ $ i ] ) ; $ index = 0 ; $ i = 0 ; $ j = 0 ; $ flag = false ; if ( $ arr [ 0 ] % 2 == 0 ) $ flag = true ; while ( $ index < $ n ) { if ( $ flag == true ) { $ arr [ $ index ++ ] = $ v1 [ $ i ++ ] ; $ flag = ! $ flag ; } else { $ arr [ $ index ++ ] = $ v2 [ $ j ++ ] ; $ flag = ! $ flag ; } } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] , " ▁ " ; } $ arr = array ( 9 , 8 , 13 , 2 , 19 , 14 ) ; $ n = sizeof ( $ arr ) ; AlternateRearrange ( $ arr , $ n ) ; ? >


"Rearrange a binary string as alternate x and y occurrences | Function which arrange the given string ; Counting number of 0 ' s ▁ and ▁ ▁ 1' s in the given string . ; Printing first all 0 ' s ▁ x - times ▁ ▁ and ▁ decrement ▁ count ▁ of ▁ 0' s x - times and do the similar task with '1' ; Driver Code"
How can the above be solved in PHP?


"Rearrange a string in sorted order followed by the integer sum | PHP program for above implementation ; Function to return string in lexicographic order followed by integers sum ; Traverse the string ; Count occurrence of uppercase alphabets ; Store sum of integers ; Traverse for all characters A to Z ; Append the current character in the string no . of times it occurs in the given string ; Append the sum of integers ; return resultant string ; Driver Code"
How can the above be solved in PHP?

< ? php $ MAX_CHAR = 26 ; function arrangeString ( $ str ) { global $ MAX_CHAR ; $ char_count = array_fill ( 0 , $ MAX_CHAR , NULL ) ; $ sum = 0 ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) { if ( $ str [ $ i ] >= ' A ' && $ str [ $ i ] <= ' Z ' ) $ char_count [ ord ( $ str [ $ i ] ) - ord ( ' A ' ) ] ++ ; else $ sum = $ sum + ( ord ( $ str [ $ i ] ) - ord ( '0' ) ) ; } $ res = " " ; for ( $ i = 0 ; $ i < $ MAX_CHAR ; $ i ++ ) { $ ch = chr ( ord ( ' A ' ) + $ i ) ; while ( $ char_count [ $ i ] -- ) $ res = $ res . $ ch ; } if ( $ sum > 0 ) $ res = $ res . strval ( $ sum ) ; return $ res ; } $ str = " ACCBA10D2EW30" ; echo arrangeString ( $ str ) ; ? >


"Rearrange an array in maximum minimum form | Set 1 | Prints max at first position , min at second position second max at third position , second min at fourth position and so on . ; Auxiliary array to hold modified array ; Indexes of smallest and largest elements from remaining array . ; To indicate whether we need to copy remaining largest or remaining smallest at next position ; Store result in temp [ ] ; Copy temp [ ] to arr [ ] ; Driver Code"
How can the above be solved in PHP?

 < ? php function rearrange ( & $ arr , $ n ) { $ temp = array ( ) ; $ small = 0 ; $ large = $ n - 1 ; $ flag = true ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ flag ) $ temp [ $ i ] = $ arr [ $ large -- ] ; else $ temp [ $ i ] = $ arr [ $ small ++ ] ; $ flag = ! $ flag ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ arr [ $ i ] = $ temp [ $ i ] ; } $ arr = array ( 1 , 2 , 3 , 4 , 5 , 6 ) ; $ n = count ( $ arr ) ; echo " Original ▁ Arrayn
 " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; rearrange ( $ arr , $ n ) ; echo " Modified Arrayn " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; ? >


"Rearrange an array in maximum minimum form | Set 2 ( O ( 1 ) extra space ) | Prints max at first position , min at second position second max at third position , second min at fourth position and so on . ; initialize index of first minimum and first maximum element ; store maximum element of array ; traverse array elements ; at even index : we have to put maximum element ; at odd index : we have to put minimum element ; array elements back to it 's original form ; Driver Code"
How can the above be solved in PHP?

 < ? php function rearrange ( & $ arr , $ n ) { $ max_idx = $ n - 1 ; $ min_idx = 0 ; $ max_elem = $ arr [ $ n - 1 ] + 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ i % 2 == 0 ) { $ arr [ $ i ] += ( $ arr [ $ max_idx ] % $ max_elem ) * $ max_elem ; $ max_idx -- ; } else { $ arr [ $ i ] += ( $ arr [ $ min_idx ] % $ max_elem ) * $ max_elem ; $ min_idx ++ ; } } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ arr [ $ i ] = ( int ) ( $ arr [ $ i ] / $ max_elem ) ; } $ arr = array ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ) ; $ n = sizeof ( $ arr ) ; echo " Original ▁ Array " . "
 " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; rearrange ( $ arr , $ n ) ; echo " Modified Array " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ;


"Rearrange an array so that arr [ i ] becomes arr [ arr [ i ] ] with O ( 1 ) extra space | The function to rearrange an array in - place so that arr [ i ] becomes arr [ arr [ i ] ] . ; First step : Increase all values by ( arr [ arr [ i ] ] % n ) * n ; Second Step : Divide all values by n ; A utility function to print an array of size n ; Driver Code"
How can the above be solved in PHP?

 < ? php function rearrange ( & $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ arr [ $ i ] += ( $ arr [ $ arr [ $ i ] ] % $ n ) * $ n ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ arr [ $ i ] = intval ( $ arr [ $ i ] / $ n ) ; } function printArr ( & $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; echo "
 " ; } $ arr = array ( 3 , 2 , 0 , 1 ) ; $ n = sizeof ( $ arr ) ; echo " Given ▁ array ▁ is ▁
 " ; printArr ( $ arr , $ n ) ; rearrange ( $ arr , $ n ) ; echo " Modified ▁ array ▁ is ▁
 " ; printArr ( $ arr , $ n ) ; ? >


"Rearrange an array to maximize i * arr [ i ] | Function to calculate the maximum points earned by making an optimal selection on the given array ; Sorting the array ; Variable to store the total points earned ; Driver code"
How can the above be solved in PHP?

< ? php function findOptimalSolution ( $ a , $ N ) { sort ( $ a ) ; $ points = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { $ points += $ a [ $ i ] * $ i ; } return $ points ; } $ a = array ( 1 , 4 , 2 , 3 , 9 ) ; $ N = sizeof ( $ a ) ; echo ( findOptimalSolution ( $ a , $ N ) ) ; ? >


"Rearrange array such that arr [ i ] >= arr [ j ] if i is even and arr [ i ] <= arr [ j ] if i is odd and j < i | function to rearrange the array ; total even positions ; total odd positions ; copy original array in an auxiliary array ; sort the auxiliary array ; fill up odd position in original array ; fill up even positions in original array ; display array ; Driver code"
How can the above be solved in PHP?

< ? php function rearrangeArr ( & $ arr , $ n ) { $ evenPos = intval ( $ n / 2 ) ; $ oddPos = $ n - $ evenPos ; $ tempArr = array_fill ( 0 , $ n , NULL ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ tempArr [ $ i ] = $ arr [ $ i ] ; sort ( $ tempArr ) ; $ j = $ oddPos - 1 ; for ( $ i = 0 ; $ i < $ n ; $ i += 2 ) { $ arr [ $ i ] = $ tempArr [ $ j ] ; $ j -- ; } $ j = $ oddPos ; for ( $ i = 1 ; $ i < $ n ; $ i += 2 ) { $ arr [ $ i ] = $ tempArr [ $ j ] ; $ j ++ ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; } $ arr = array ( 1 , 2 , 3 , 4 , 5 , 6 , 7 ) ; $ size = sizeof ( $ arr ) ; rearrangeArr ( $ arr , $ size ) ; ? >


"Rearrange array such that even index elements are smaller and odd index elements are greater | Swap ; Rearrange ; Utility that prints out an array in a line ; Driver Code"
How can the above be solved in PHP?

 < ? php function swap ( & $ a , & $ b ) { $ temp = $ a ; $ a = $ b ; $ b = $ temp ; } function rearrange ( & $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { if ( $ i % 2 == 0 && $ arr [ $ i ] > $ arr [ $ i + 1 ] ) swap ( $ arr [ $ i ] , $ arr [ $ i + 1 ] ) ; if ( $ i % 2 != 0 && $ arr [ $ i ] < $ arr [ $ i + 1 ] ) swap ( $ arr [ $ i ] , $ arr [ $ i + 1 ] ) ; } } function printArray ( & $ arr , $ size ) { for ( $ i = 0 ; $ i < $ size ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; echo "
 " ; } $ arr = array ( 6 , 4 , 2 , 1 , 8 , 3 ) ; $ n = sizeof ( $ arr ) ; echo " Before ▁ rearranging : ▁
 " ; printArray ( $ arr , $ n ) ; rearrange ( $ arr , $ n ) ; echo " After ▁ rearranging : ▁
 " ; printArray ( $ arr , $ n ) ; ? >


"Rearrange the array to maximize the number of primes in prefix sum of the array | Function to print the re - arranged array ; Count the number of ones and twos in a [ ] ; If the array element is 1 ; Array element is 2 ; If it has at least one 2 Fill up first 2 ; Decrease the cnt of ones if even ; Fill up with odd count of ones ; Fill up with remaining twos ; If even ones , then fill last position ; Print the rearranged array ; Driver code"
How can the above be solved in PHP?


"Recaman 's sequence | Prints first n terms of Recaman sequence ; First term of the sequence is always 0 ; Fill remaining terms using recursive formula . ; If arr [ i - 1 ] - i is negative or already exists . ; Driver Code"
How can the above be solved in PHP?


"Recaman 's sequence | Prints first n terms of Recaman sequence ; Print first term and store it in a hash ; Print remaining terms using recursive formula . ; If arr [ i - 1 ] - i is negative or already exists . ; Driver code"
How can the above be solved in PHP?

< ? php function recaman ( $ n ) { if ( $ n <= 0 ) return ; print ( "0 , ▁ " ) ; $ s = array ( ) ; array_push ( $ s , 0 ) ; $ prev = 0 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ curr = $ prev - $ i ; if ( $ curr < 0 or in_array ( $ curr , $ s ) ) $ curr = $ prev + $ i ; array_push ( $ s , $ curr ) ; print ( $ curr . " , " ) ; $ prev = $ curr ; } } $ n = 17 ; recaman ( $ n ) ; ? >


"Rectangle with minimum possible difference between the length and the width | Function to print the length ( l ) and breadth ( b ) of the rectangle having area = N and | l - b | as minimum as possible ; i is a factor ; l >= sqrt ( area ) >= i ; so here l is + ve always ; Here l and b are length and breadth of the rectangle ; Driver code"
How can the above be solved in PHP?

< ? php function find_rectangle ( $ area ) { $ M = floor ( sqrt ( $ area ) ) ; for ( $ i = $ M ; $ i >= 1 ; $ i -- ) { if ( $ area % $ i == 0 ) { $ l = floor ( $ area / $ i ) ; $ b = $ i ; break ; } } echo " l = " , ▁ $ l , ▁ " , b = " , ▁ $ b , ▁ " " } $ area = 99 ; find_rectangle ( $ area ) ; ? >


"Rectangular ( or Pronic ) Numbers | Returns n - th rectangular number ; Driver Code"
How can the above be solved in PHP?

< ? php function findRectNum ( $ n ) { return $ n * ( $ n + 1 ) ; } $ n = 6 ; echo findRectNum ( $ n ) ; ? >


"Recursive Programs to find Minimum and Maximum elements of array | function to print Minimum element using recursion ; if size = 0 means whole array has been traversed ; Driver Code"
How can the above be solved in PHP?

< ? php function findMinRec ( $ A , $ n ) { if ( $ n == 1 ) return $ A [ 0 ] ; return min ( $ A [ $ n - 1 ] , findMinRec ( $ A , $ n - 1 ) ) ; } $ A = array ( 1 , 4 , 45 , 6 , -50 , 10 , 2 ) ; $ n = sizeof ( $ A ) ; echo findMinRec ( $ A , $ n ) ; ? >


"Recursive Programs to find Minimum and Maximum elements of array | function to return maximum element using recursion ; if n = 0 means whole array has been traversed ; Driver Code ; Function calling"
How can the above be solved in PHP?

< ? php function findMaxRec ( $ A , $ n ) { if ( $ n == 1 ) return $ A [ 0 ] ; return max ( $ A [ $ n - 1 ] , findMaxRec ( $ A , $ n - 1 ) ) ; } $ A = array ( 1 , 4 , 45 , 6 , -50 , 10 , 2 ) ; $ n = sizeof ( $ A ) ; echo findMaxRec ( $ A , $ n ) ; ? >


"Recursive function to check if a string is palindrome | A recursive function that check a str [ s . . e ] is palindrome or not . ; If there is only one character ; If first and last characters do not match ; If there are more than two characters , check if middle substring is also palindrome or not . ; An empty string is considered as palindrome ; Driver Code"
How can the above be solved in PHP?

< ? php function isPalRec ( $ str , $ s , $ e ) { if ( $ s == $ e ) return true ; if ( $ str [ $ s ] != $ str [ $ e ] ) return false ; if ( $ s < $ e + 1 ) return isPalRec ( $ str , $ s + 1 , $ e - 1 ) ; return true ; } function isPalindrome ( $ str ) { $ n = strlen ( $ str ) ; if ( $ n == 0 ) return true ; return isPalRec ( $ str , 0 , $ n - 1 ) ; } { $ str = " geeg " ; if ( isPalindrome ( $ str ) ) echo ( " Yes " ) ; else echo ( " No " ) ; return 0 ; } ? >


"Recursive program for prime number | Returns true if n is prime , else return false . i is current divisor to check . ; Base cases ; Check for next divisor ; Driver Code"
How can the above be solved in PHP?

< ? php function isPrime ( $ n , $ i = 2 ) { if ( $ n <= 2 ) return ( $ n == 2 ) ? true : false ; if ( $ n % $ i == 0 ) return false ; if ( $ i * $ i > $ n ) return true ; return isPrime ( $ n , $ i + 1 ) ; } $ n = 15 ; if ( isPrime ( $ n ) ) echo ( " Yes " ) ; else echo ( " No " ) ; ? >


"Recursive program to check if number is palindrome or not | Recursive function that returns the reverse of digits ; base case ; stores the reverse of a number ; Driver Code"
How can the above be solved in PHP?

< ? php function rev ( $ n , $ temp ) { if ( $ n == 0 ) return $ temp ; $ temp = ( $ temp * 10 ) + ( $ n % 10 ) ; return rev ( $ n / 10 , $ temp ) ; } $ n = 121 ; $ temp = rev ( $ n , 0 ) ; if ( $ temp != $ n ) echo " yes " ; else echo " no " ; ? >


"Recursive program to insert a star between pair of identical characters | Function to insert * at desired position ; Append current character ; If we reached last character ; If next character is same , append ' * ' ; Driver code"
How can the above be solved in PHP?

< ? php function pairStar ( & $ input , & $ output , $ i = 0 ) { $ output = $ output . $ input [ $ i ] ; if ( $ i == strlen ( $ input ) - 1 ) return ; if ( $ input [ $ i ] == $ input [ $ i + 1 ] ) $ output = $ output . ' * ' ; pairStar ( $ input , $ output , $ i +1 ) ; } $ input = " geeks " ; $ output = " " ; pairStar ( $ input , $ output ) ; echo $ output ; return 0 ; ? >


"Recursive solution to count substrings with same first and last characters | Function to count substrings with same first and last characters ; base cases ; Driver Code"
How can the above be solved in PHP?

< ? php function countSubstrs ( $ str , $ i , $ j , $ n ) { if ( $ n == 1 ) return 1 ; if ( $ n <= 0 ) return 0 ; $ res = countSubstrs ( $ str , $ i + 1 , $ j , $ n - 1 ) + countSubstrs ( $ str , $ i , $ j - 1 , $ n - 1 ) - countSubstrs ( $ str , $ i + 1 , $ j - 1 , $ n - 2 ) ; if ( $ str [ $ i ] == $ str [ $ j ] ) $ res ++ ; return $ res ; } $ str = " abcab " ; $ n = strlen ( $ str ) ; echo ( countSubstrs ( $ str , 0 , $ n - 1 , $ n ) ) ; ? >


"Recursive sum of digit in n ^ x , where n and x are very large | function to get sum of digits of a number ; function to return sum ; Find sum of digits in n ; Find remainder of exponent ; Driver code"
How can the above be solved in PHP?

< ? php function digSum ( $ n ) { if ( $ n == 0 ) return 0 ; return ( $ n % 9 == 0 ) ? 9 : ( $ n % 9 ) ; } function PowDigSum ( $ n , $ x ) { $ sum = digSum ( $ n ) ; $ rem = $ x % 6 ; if ( ( $ sum == 3 $ sum == 6 ) && $ x > 1 ) return 9 ; else if ( $ x == 1 ) return $ sum ; else if ( $ x == 0 ) return 1 ; else if ( $ rem == 0 ) return digSum ( pow ( $ sum , 6 ) ) ; else return digSum ( pow ( $ sum , $ rem ) ) ; } $ n = 33333 ; $ x = 332654 ; echo PowDigSum ( $ n , $ x ) ; ? >


"Recursive sum of digits of a number formed by repeated appends | return single digit sum of a number . ; Returns recursive sum of digits of a number formed by repeating a number X number of times until sum become single digit . ; Driver Code"
How can the above be solved in PHP?

< ? php function digSum ( $ n ) { if ( $ n == 0 ) return 0 ; return ( $ n % 9 == 0 ) ? 9 : ( $ n % 9 ) ; } function repeatedNumberSum ( $ n , $ x ) { $ sum = $ x * digSum ( $ n ) ; return digSum ( $ sum ) ; } $ n = 24 ; $ x = 3 ; echo repeatedNumberSum ( $ n , $ x ) ; ? >


"Recursive sum of digits of a number is prime or not | Function for recursive digit sum ; function to check if prime or not the single digit ; calls function which returns sum till single digit ; checking prime ; Driver code"
How can the above be solved in PHP?

< ? php function recDigSum ( $ n ) { if ( $ n == 0 ) return 0 ; else { if ( $ n % 9 == 0 ) return 9 ; else return $ n % 9 ; } } function check ( $ n ) { $ n = recDigSum ( $ n ) ; if ( $ n == 2 or $ n == 3 or $ n == 5 or $ n == 7 ) echo " Yes " ; else echo " No " ; } $ n = 5602 ; check ( $ n ) ; ? >


"Recursively break a number in 3 parts to get maximum sum | Function to find the maximum sum ; base conditions ; Fill in bottom - up manner using recursive formula . ; Driver Code"
How can the above be solved in PHP?


"Recursively break a number in 3 parts to get maximum sum | Function to find the maximum sum ; base conditions ; recursively break the number and return what maximum you can get ; Driver program to run the case"
How can the above be solved in PHP?

< ? php function breakSum ( $ n ) { if ( $ n == 0 $ n == 1 ) return $ n ; return max ( ( breakSum ( intval ( $ n / 2 ) ) + breakSum ( intval ( $ n / 3 ) ) + breakSum ( intval ( $ n / 4 ) ) ) , $ n ) ; } $ n = 12 ; echo breakSum ( $ n ) ; ? >


"Reduce the fraction to its lowest form | Function to reduce a fraction to its lowest form ; Driver Code"
How can the above be solved in PHP?

< ? php function reduceFraction ( $ x , $ y ) { $ d ; $ d = __gcd ( $ x , $ y ) ; $ x = $ x / $ d ; $ y = $ y / $ d ; echo ( " x ▁ = ▁ " . $ x . " , y = " } function __gcd ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; return __gcd ( $ b , $ a % $ b ) ; } $ x = 16 ; $ y = 10 ; reduceFraction ( $ x , $ y ) ; ? >


"Refactorable number | Function to count all divisors ; Initialize result ; If divisors are equal , count only one . ; Otherwise count both ; Driver Code"
How can the above be solved in PHP?

 < ? php function isRefactorableNumber ( $ n ) { $ divCount = 0 ; for ( $ i = 1 ; $ i <= sqrt ( $ n ) ; ++ $ i ) { if ( $ n % $ i == 0 ) { if ( $ n / $ i == $ i ) ++ $ divCount ; else $ divCount += 2 ; } } return $ n % $ divCount == 0 ; } $ n = 8 ; if ( isRefactorableNumber ( $ n ) ) echo " yes " ; else echo " no " ; echo "
 " ; $ n = 14 ; if ( isRefactorableNumber ( $ n ) ) echo " yes " ; else echo " no " ; ? >


"Reflection of a point at 180 degree rotation of another point | PHP Program for find the 180 degree reflection of one point around another point . ; Driver Code"
How can the above be solved in PHP?


"Regular polygon using only 1 s in a binary numbered circle | method returns true if polygon is possible with ' midpoints ' number of midpoints ; loop for getting first vertex of polygon ; loop over array values at ' midpoints ' distance ; and ( & ) all those values , if even one of them is 0 , val will be 0 ; if val is still 1 and ( N / midpoints ) or ( number of vertices ) are more than two ( for a polygon minimum ) print result and return true ; method prints sides in the polygon or print not possible in case of no possible polygon ; limit for iterating over divisors ; If i divides N then i and ( N / i ) will be divisors ; check polygon for both divisors ; Driver Code"
How can the above be solved in PHP?

 < ? php function checkPolygonWithMidpoints ( $ arr , $ N , $ midpoints ) { for ( $ j = 0 ; $ j < $ midpoints ; $ j ++ ) { $ val = 1 ; for ( $ k = $ j ; $ k < $ N ; $ k += $ midpoints ) { $ val &= $ arr [ $ k ] ; } if ( $ val && $ N / $ midpoints > 2 ) { echo " Polygon ▁ possible ▁ with ▁ side ▁ length ▁ " , ( $ N / $ midpoints ) , "
 " ; return true ; } } return false ; } function isPolygonPossible ( $ arr , $ N ) { $ limit = sqrt ( $ N ) ; for ( $ i = 1 ; $ i <= $ limit ; $ i ++ ) { if ( $ N % $ i == 0 ) { if ( checkPolygonWithMidpoints ( $ arr , $ N , $ i ) || checkPolygonWithMidpoints ( $ arr , $ N , ( $ N / $ i ) ) ) return ; } } echo " Not ▁ possiblen " ; } $ arr = array ( 1 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 1 , 1 ) ; $ N = sizeof ( $ arr ) ; isPolygonPossible ( $ arr , $ N ) ; ? >


"Remainder with 7 for large numbers | Function which return Remainder after dividing the number by 7 ; This series is used to find remainder with 7 ; Index of next element in series ; Initialize result ; Traverse num from end ; Find current digit of num ; Add next term to result ; Move to next term in series ; Make sure that result never goes beyond 7. ; Make sure that remainder is positive ; Driver Code"
How can the above be solved in PHP?

< ? php function remainderWith7 ( $ num ) { $ series = array ( 1 , 3 , 2 , -1 , -3 , -2 ) ; $ series_index = 0 ; $ result = 0 ; for ( $ i = strlen ( $ num ) - 1 ; $ i >= 0 ; $ i -- ) { $ digit = $ num [ $ i ] - '0' ; $ result += $ digit * $ series [ $ series_index ] ; $ series_index = ( $ series_index + 1 ) % 6 ; $ result %= 7 ; } if ( $ result < 0 ) $ result = ( $ result + 7 ) % 7 ; return $ result ; } { $ str = "12345" ; echo " Remainder ▁ with ▁ 7 ▁ is ▁ " , ( remainderWith7 ( $ str ) ) ; return 0 ; } ? >


"Remove array end element to maximize the sum of product | PHP program to find maximum score we can get by removing elements from either end . ; If only one element left . ; If already calculated , return the value . ; Computing Maximum value when element at index i and index j is to be choosed . ; Driver Code"
How can the above be solved in PHP?

< ? php $ MAX = 50 ; function solve ( $ dp , $ a , $ low , $ high , $ turn ) { if ( $ low == $ high ) return $ a [ $ low ] * $ turn ; if ( $ dp [ $ low ] [ $ high ] != 0 ) return $ dp [ $ low ] [ $ high ] ; $ dp [ $ low ] [ $ high ] = max ( $ a [ $ low ] * $ turn + solve ( $ dp , $ a , $ low + 1 , $ high , $ turn + 1 ) , $ a [ $ high ] * $ turn + solve ( $ dp , $ a , $ low , $ high - 1 , $ turn + 1 ) ) ; return $ dp [ $ low ] [ $ high ] ; } $ arr = array ( 1 , 3 , 1 , 5 , 2 ) ; $ n = count ( $ arr ) ; $ dp = array ( ) ; for ( $ i = 0 ; $ i < $ MAX ; $ i ++ ) { $ dp [ $ i ] = array_fill ( $ i , $ MAX , 0 ) ; } echo solve ( $ dp , $ arr , 0 , $ n - 1 , 1 ) ; ? >


"Remove characters from a numeric string such that string becomes divisible by 8 | Function that return true if sub is a sub - sequence in s ; Function to return a multiple of 8 formed after removing 0 or more characters from the given string ; Iterate over all multiples of 8 ; If current multiple exists as a subsequence in the given string ; Driver Code"
How can the above be solved in PHP?

< ? php function checkSub ( $ sub , $ s ) { $ j = 0 ; for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) if ( $ sub [ $ j ] == $ s [ $ i ] ) $ j ++ ; return $ j == strlen ( $ sub ) ; } function getMultiple ( $ s ) { for ( $ i = 0 ; $ i < 1e3 ; $ i += 8 ) { if ( checkSub ( ( string ) ( $ i ) , $ s ) ) return $ i ; } return -1 ; } $ s = "3454" ; echo getMultiple ( $ s ) ;


"Remove consecutive alphabets which are in same case | Function to return the modified string ; Traverse through the remaining characters in the string ; If the current and the previous characters are not in the same case then take the character ; Driver code"
How can the above be solved in PHP?

< ? php function removeChars ( $ s ) { $ modifiedStr = " " ; $ modifiedStr = $ modifiedStr . $ s [ 0 ] ; for ( $ i = 1 ; $ i < strlen ( $ s ) ; $ i ++ ) { if ( ctype_upper ( $ s [ $ i ] ) && ctype_lower ( $ s [ $ i - 1 ] ) || ctype_lower ( $ s [ $ i ] ) && ctype_upper ( $ s [ $ i - 1 ] ) ) $ modifiedStr = $ modifiedStr . $ s [ $ i ] ; } return $ modifiedStr ; } $ s = " GeeksForGeeks " ; echo removeChars ( $ s ) ; ? >


"Remove consecutive vowels from string | function which returns True or False for occurrence of a vowel ; this compares vowel with character ' c ' ; function to print resultant string ; print 1 st character ; loop to check for each character ; comparison of consecutive characters ; Driver code"
How can the above be solved in PHP?

< ? php function is_vow ( $ c ) { return ( $ c == ' a ' ) || ( $ c == ' e ' ) || ( $ c == ' i ' ) || ( $ c == ' o ' ) || ( $ c == ' u ' ) ; } function removeVowels ( $ str ) { printf ( $ str [ 0 ] ) ; for ( $ i = 1 ; $ i < strlen ( $ str ) ; $ i ++ ) if ( ( ! is_vow ( $ str [ $ i - 1 ] ) ) || ( ! is_vow ( $ str [ $ i ] ) ) ) printf ( $ str [ $ i ] ) ; } $ str = " ▁ geeks ▁ for ▁ geeks " ; removeVowels ( $ str ) ; ? >


"Remove duplicates from a string in O ( 1 ) extra space | Function to remove duplicates ; keeps track of visited characters ; gets character value ; keeps track of length of resultant string ; check if Xth bit of counter is unset ; mark current character as visited ; Driver code"
How can the above be solved in PHP?

< ? php function removeDuplicatesFromString ( $ str ) { $ counter = 0 ; $ i = 0 ; $ size = strlen ( $ str ) ; $ x = 0 ; $ length = 0 ; while ( $ i < $ size ) { $ x = ord ( $ str [ $ i ] ) - 97 ; if ( ( $ counter & ( 1 << $ x ) ) == 0 ) { $ str [ $ length ] = chr ( 97 + $ x ) ; $ counter = $ counter | ( 1 << $ x ) ; $ length ++ ; } $ i ++ ; } return substr ( $ str , 0 , $ length ) ; } $ str = " geeksforgeeks " ; echo removeDuplicatesFromString ( $ str ) ; ? >


"Remove exactly one element from the array such that max | function to calculate max - min ; Driver code"
How can the above be solved in PHP?


"Remove exactly one element from the array such that max | function to calculate max - min ; There should be at - least two elements ; To store first and second minimums ; To store first and second maximums ; Driver code"
How can the above be solved in PHP?

< ? php function max_min ( $ a , $ n ) { if ( $ n <= 1 ) return PHP_INT_MAX ; $ f_min = $ a [ 0 ] ; $ s_min = PHP_INT_MAX ; $ f_max = $ a [ 0 ] ; $ s_max = ~ PHP_INT_MAX ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] <= $ f_min ) { $ s_min = $ f_min ; $ f_min = $ a [ $ i ] ; } else if ( $ a [ $ i ] < $ s_min ) { $ s_min = $ a [ $ i ] ; } if ( $ a [ $ i ] >= $ f_max ) { $ s_max = $ f_max ; $ f_max = $ a [ $ i ] ; } else if ( $ a [ $ i ] > $ s_max ) { $ s_max = $ a [ $ i ] ; } } return min ( ( $ f_max - $ s_min ) , ( $ s_max - $ f_min ) ) ; } $ a = array ( 1 , 3 , 3 , 7 ) ; $ n = sizeof ( $ a ) ; echo ( max_min ( $ a , $ n ) ) ; ? >


"Remove minimum elements from either side such that 2 * min becomes more than max | A utility function to find minimum in arr [ l . . h ] ; A utility function to find maximum in arr [ l . . h ] ; Returns the minimum number of removals from either end in arr [ l . . h ] so that 2 * min becomes greater than max . ; Create a table to store solutions of subproblems ; Fill table using above recursive formula . Note that the table is filled in diagonal fashion ( similar to http : goo . gl / PQqoS ) , from diagonal elements to table [ 0 ] [ n - 1 ] which is the result . ; Driver Code"
How can the above be solved in PHP?

< ? php function min1 ( $ arr , $ l , $ h ) { $ mn = $ arr [ $ l ] ; for ( $ i = $ l + 1 ; $ i <= $ h ; $ i ++ ) if ( $ mn > $ arr [ $ i ] ) $ mn = $ arr [ $ i ] ; return $ mn ; } function max1 ( $ arr , $ l , $ h ) { $ mx = $ arr [ $ l ] ; for ( $ i = $ l + 1 ; $ i <= $ h ; $ i ++ ) if ( $ mx < $ arr [ $ i ] ) $ mx = $ arr [ $ i ] ; return $ mx ; } function minRemovalsDP ( $ arr , $ n ) { $ table = array_fill ( 0 , $ n , array_fill ( 0 , $ n , 0 ) ) ; for ( $ gap = 0 ; $ gap < $ n ; ++ $ gap ) { for ( $ i = 0 , $ j = $ gap ; $ j < $ n ; ++ $ i , ++ $ j ) { $ mn = min1 ( $ arr , $ i , $ j ) ; $ mx = max1 ( $ arr , $ i , $ j ) ; $ table [ $ i ] [ $ j ] = ( 2 * $ mn > $ mx ) ? 0 : min ( $ table [ $ i ] [ $ j - 1 ] + 1 , $ table [ $ i + 1 ] [ $ j ] + 1 ) ; } } return $ table [ 0 ] [ $ n - 1 ] ; } $ arr = array ( 20 , 4 , 1 , 3 ) ; $ n = count ( $ arr ) ; echo minRemovalsDP ( $ arr , $ n ) ; ? >


"Remove minimum elements from either side such that 2 * min becomes more than max | A utility function to find minimum in arr [ l . . h ] ; A utility function to find maximum in arr [ l . . h ] ; Returns the minimum number of removals from either end in arr [ l . . h ] so that 2 * min becomes greater than max . ; If there is 1 or less elements , return 0. For a single element , 2 * min > max . ( Assumption : All elements are positive in arr [ ] ) ; 1 ) Find minimum and maximum in arr [ l . . h ] ; If the property is followed , no removals needed ; Otherwise remove a character from left end and recur , then remove a character from right end and recur , take the minimum of two is returned ; Driver Code"
How can the above be solved in PHP?

< ? php function min_1 ( & $ arr , $ l , $ h ) { $ mn = $ arr [ $ l ] ; for ( $ i = $ l + 1 ; $ i <= $ h ; $ i ++ ) if ( $ mn > $ arr [ $ i ] ) $ mn = $ arr [ $ i ] ; return $ mn ; } function max_1 ( & $ arr , $ l , $ h ) { $ mx = $ arr [ $ l ] ; for ( $ i = $ l + 1 ; $ i <= $ h ; $ i ++ ) if ( $ mx < $ arr [ $ i ] ) $ mx = $ arr [ $ i ] ; return $ mx ; } function minRemovals ( & $ arr , $ l , $ h ) { if ( $ l >= $ h ) return 0 ; $ mn = min_1 ( $ arr , $ l , $ h ) ; $ mx = max_1 ( $ arr , $ l , $ h ) ; if ( 2 * $ mn > $ mx ) return 0 ; return min ( minRemovals ( $ arr , $ l + 1 , $ h ) , minRemovals ( $ arr , $ l , $ h - 1 ) ) + 1 ; } $ arr = array ( 4 , 5 , 100 , 9 , 10 , 11 , 12 , 15 , 200 ) ; $ n = sizeof ( $ arr ) ; echo minRemovals ( $ arr , 0 , $ n - 1 ) ; ? >


"Remove minimum elements from either side such that 2 * min becomes more than max | Returns the minimum number of removals from either end in arr [ l . . h ] so that 2 * min becomes greater than max . ; Initialize starting and ending indexes of the maximum sized subarray with property 2 * min > max ; Choose different elements as starting point ; Initialize min and max for the current start ; Choose different ending points for current start ; Update min and max if necessary ; If the property is violated , then no point to continue for a bigger array ; Update longest_start and longest_end if needed ; If not even a single element follow the property , then return n ; Return the number of elements to be removed ; Driver Code"
How can the above be solved in PHP?

< ? php function minRemovalsDP ( $ arr , $ n ) { $ longest_start = -1 ; $ longest_end = 0 ; for ( $ start = 0 ; $ start < $ n ; $ start ++ ) { $ min = PHP_INT_MAX ; $ max = PHP_INT_MIN ; for ( $ end = $ start ; $ end < $ n ; $ end ++ ) { $ val = $ arr [ $ end ] ; if ( $ val < $ min ) $ min = $ val ; if ( $ val > $ max ) $ max = $ val ; if ( 2 * $ min <= $ max ) break ; if ( $ end - $ start > $ longest_end - $ longest_start $ longest_start == -1 ) { $ longest_start = $ start ; $ longest_end = $ end ; } } } if ( $ longest_start == -1 ) return $ n ; return ( $ n - ( $ longest_end - $ longest_start + 1 ) ) ; } $ arr = array ( 4 , 5 , 100 , 9 , 10 , 11 , 12 , 15 , 200 ) ; $ n = sizeof ( $ arr ) ; echo minRemovalsDP ( $ arr , $ n ) ; ? >


"Remove one bit from a binary number to get maximum value | Function to find the maximum binary number ; Traverse the binary number ; Try finding a 0 and skip it ; Get the binary number ; Find the maximum binary number"
How can the above be solved in PHP?

< ? php function printMaxAfterRemoval ( $ s ) { $ flag = false ; $ n = strlen ( $ s ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ s [ $ i ] == '0' && $ flag == false ) { $ flag = true ; continue ; } else echo $ s [ $ i ] ; } } $ s = "1001" ; printMaxAfterRemoval ( $ s ) ; ? >


"Remove repeated digits in a given number | PHP program to remove repeated digits ; Store first digits as previous digit ; Initialize power ; Iterate through all digits of n , note that the digits are processed from least significant digit to most significant digit . ; Store current digit ; Add the current digit to the beginning of result ; Update previous result and power ; Remove last digit from n ; Driver Code"
How can the above be solved in PHP?

< ? php function removeRecur ( $ n ) { $ prev_digit = $ n % 10 ; $ pow = 10 ; $ res = $ prev_digit ; while ( $ n ) { $ curr_digit = $ n % 10 ; if ( $ curr_digit != $ prev_digit ) { $ res += $ curr_digit * $ pow ; $ prev_digit = $ curr_digit ; $ pow *= 10 ; } $ n = $ n / 10 ; } return $ res ; } $ n = 12224 ; echo removeRecur ( $ n ) ; ? >


"Reorder an array according to given indexes | Function to reorder elements of arr [ ] according to index [ ] ; $temp [ $n ] ; arr [ i ] should be present at index [ i ] index ; Copy temp [ ] to arr [ ] ; Driver Code"
How can the above be solved in PHP?

 < ? php function reorder ( $ arr , $ index , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ temp [ $ index [ $ i ] ] = $ arr [ $ i ] ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ arr [ $ i ] = $ temp [ $ i ] ; $ index [ $ i ] = $ i ; } echo " Reordered ▁ array ▁ is : ▁
 " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { echo $ arr [ $ i ] . " " ; } echo " Modified Index array is : " for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { echo $ index [ $ i ] . " " ; } } $ arr = array ( 50 , 40 , 70 , 60 , 90 ) ; $ index = array ( 3 , 0 , 4 , 1 , 2 ) ; $ n = sizeof ( $ arr ) ; reorder ( $ arr , $ index , $ n ) ; ? >


"Reorder the position of the words in alphabetical order | Function to print the ordering of words ; Creating list of words and assigning them index numbers ; Sort the list of words lexicographically ; Print the ordering ; Driver Code"
How can the above be solved in PHP?

< ? php function reArrange ( $ words , $ n ) { $ freq = array ( ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ freq [ $ words [ $ i ] ] = ( $ i + 1 ) ; } sort ( $ words ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ freq [ $ words [ $ i ] ] , " ▁ " ; } $ words = array ( " live " , " place " , " travel " , " word " , " sky " ) ; $ n = count ( $ words ) ; reArrange ( $ words , $ n ) ; ? >


"Repeated Unit Divisibility | To find least value of k ; To check n is coprime or not ; to store R ( k ) mod n and 10 ^ k mod n value ; Driver Code"
How can the above be solved in PHP?

< ? php function repUnitValue ( $ n ) { if ( $ n % 2 == 0 $ n % 5 == 0 ) return 0 ; $ rem = 1 ; $ power = 1 ; $ k = 1 ; while ( $ rem % $ n != 0 ) { $ k ++ ; $ power = $ power * 10 % $ n ; $ rem = ( $ rem + $ power ) % $ n ; } return $ k ; } $ n = 13 ; echo repUnitValue ( $ n ) ; ? >


"Repeated subtraction among two numbers | Returns count of steps before one of the numbers become 0 after repeated subtractions . ; If y divides x , then simply return x / y . ; Else recur . Note that this function works even if x is smaller than y because in that case first recursive call exchanges roles of x and y . ; Driver code"
How can the above be solved in PHP?

< ? php function countSteps ( $ x , $ y ) { if ( $ x % $ y == 0 ) return floor ( ( ( int ) $ x / $ y ) ) ; return floor ( ( ( int ) $ x / $ y ) + countSteps ( $ y , $ x % $ y ) ) ; } $ x = 100 ; $ y = 19 ; echo countSteps ( $ x , $ y ) ; ? >


"Repeatedly search an element by doubling it after every successful search | Php program to repeatedly search an element by doubling it after every successful search ; Sort the given array so that binary search can be applied on it ; Maximum array element ; search for the element b present or not in array ; Driver code"
How can the above be solved in PHP?

< ? php function binary_search ( $ a , $ x , $ lo = 0 , $ hi = NULL ) { if ( $ hi == NULL ) $ hi = count ( $ a ) ; while ( $ lo < $ hi ) { $ mid = ( $ lo + $ hi ) / 2 ; $ midval = $ a [ $ mid ] ; if ( $ midval < $ x ) $ lo = $ mid + 1 ; else if ( $ midval > $ x ) $ hi = $ mid ; else return $ mid ; } return -1 ; } function findElement ( $ a , $ n , $ b ) { sort ( $ a ) ; $ mx = $ a [ $ n - 1 ] ; while ( $ b < max ( $ a ) ) { if ( binary_search ( $ a , $ b , 0 , $ n ) != -1 ) $ b *= 2 ; else return $ b ; } return $ b ; } $ a = array ( 1 , 2 , 3 ) ; $ n = count ( $ a ) ; $ b = 1 ; echo findElement ( $ a , $ n , $ b ) ; ? >


"Replace a character c1 with c2 and c2 with c1 in a string S | PHP program to replace c1 with c2 and c2 with c1 ; loop to traverse in the string ; check for c1 and replace ; check for c2 and replace ; Driver Code"
How can the above be solved in PHP?


"Replace all occurrences of a string with space | Function to extract the secret message ; Replacing all occurrences of Sub in Str by empty spaces ; Removing unwanted spaces in the start and end of the string ; Driver code"
How can the above be solved in PHP?

< ? php function extractSecretMessage ( $ Str , $ Sub ) { $ Str = str_replace ( $ Sub , " ▁ " , $ Str ) ; return trim ( $ Str ) ; } $ Str = " LIELIEILIEAMLIECOOL " ; $ Sub = " LIE " ; echo extractSecretMessage ( $ Str , $ Sub ) ; ? >


"Replace all occurrences of string AB with C without using extra space | PHP program to replace all occurrences of " AB " with " C " ; Start traversing from second character ; If previous character is ' A ' and current character is 'B" ; Replace previous character with ' C ' and move all subsequent characters one position back ; Driver code"
How can the above be solved in PHP?

 < ? php function translate ( & $ str ) { if ( $ str [ 0 ] == ' ' ) return ; for ( $ i = 1 ; $ i < strlen ( $ str ) ; $ i ++ ) { if ( $ str [ $ i - 1 ] == ' A ' && $ str [ $ i ] == ' B ' ) { $ str [ $ i - 1 ] = ' C ' ; for ( $ j = $ i ; $ j < strlen ( $ str ) ; $ j ++ ) $ str [ $ j ] = $ str [ $ j + 1 ] ; } } return ; } $ str = " helloABworldABGfG " ; translate ( $ str ) ; echo " The ▁ modified ▁ string ▁ is ▁ :
 " ; echo $ str ; ? >


"Replace consonants with next immediate consonants alphabetically in a String | Function to check if a character is vowel or not ; Function that replaces consonant with next immediate consonant alphabatically ; Start traversing the string ; if character is z , than replace it with character b ; if the alphabet is not z ; replace the element with next immediate alphabet ; if next immediate alphabet is vowel , than take next 2 nd immediate alphabet ( since no two vowels occurs consecutively in alphabets ) hence no further checking is required ; Driver code"
How can the above be solved in PHP?

< ? php function isVowel ( $ ch ) { if ( $ ch != ' a ' && $ ch != ' e ' && $ ch != ' i ' && $ ch != ' o ' && $ ch != ' u ' ) return false ; return true ; } function replaceConsonants ( $ s ) { for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) { if ( ! isVowel ( $ s [ $ i ] ) ) { if ( $ s [ $ i ] == ' z ' ) $ s [ $ i ] = ' b ' ; else { $ s [ $ i ] = chr ( ord ( $ s [ $ i ] ) + 1 ) ; if ( isVowel ( $ s [ $ i ] ) ) $ s [ $ i ] = chr ( ord ( $ s [ $ i ] ) + 1 ) ; } } } return $ s ; } $ s = " geeksforgeeks " ; echo replaceConsonants ( $ s ) ; ? >


"Replace every array element by Bitwise Xor of previous and next element | PHP program to update every array element with sum of previous and next numbers in array ; Nothing to do when array size is 1 ; store current value of arr [ 0 ] and update it ; Update rest of the array elements ; Store current value of next interaction ; Update current value using previous value ; Update previous value ; Update last array element separately ; Driver Code ; Print the modified array"
How can the above be solved in PHP?

< ? php function ReplaceElements ( & $ arr , $ n ) { if ( $ n <= 1 ) return ; $ prev = $ arr [ 0 ] ; $ arr [ 0 ] = $ arr [ 0 ] ^ $ arr [ 1 ] ; for ( $ i = 1 ; $ i < $ n - 1 ; $ i ++ ) { $ curr = $ arr [ $ i ] ; $ arr [ $ i ] = $ prev ^ $ arr [ $ i + 1 ] ; $ prev = $ curr ; } $ arr [ $ n - 1 ] = $ prev ^ $ arr [ $ n - 1 ] ; } $ arr = array ( 2 , 3 , 4 , 5 , 6 ) ; $ n = sizeof ( $ arr ) ; ReplaceElements ( $ arr , $ n ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; ? >


"Replace every element of the array by product of all other elements | PHP program to Replace every element by the product of all other elements ; Calculate the product of all the elements ; Replace every element product of all other elements ; Driver Code ; Print the modified array ."
How can the above be solved in PHP?

< ? php function ReplaceElements ( $ arr , $ n ) { $ prod = 1 ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { $ prod *= $ arr [ $ i ] ; } for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { $ arr [ $ i ] = ( int ) ( $ prod / $ arr [ $ i ] ) ; } return $ arr ; } $ arr = array ( 2 , 3 , 3 , 5 , 7 ) ; $ n = sizeof ( $ arr ) ; $ arr1 = ReplaceElements ( $ arr , $ n ) ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { echo $ arr1 [ $ i ] . " " ; } ? >


"Replace every element of the array with BitWise XOR of all other | Function to replace the elements ; Calculate the xor of all the elements ; Replace every element by the xor of all other elements ; Driver code ; Print the modified array ."
How can the above be solved in PHP?

< ? php function ReplaceElements ( $ arr , $ n ) { $ X = 0 ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { $ X ^= $ arr [ $ i ] ; } for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { $ arr [ $ i ] = $ X ^ $ arr [ $ i ] ; } return $ arr ; } $ arr = array ( 2 , 3 , 3 , 5 , 5 ) ; $ n = sizeof ( $ arr ) ; $ arr1 = ReplaceElements ( $ arr , $ n ) ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { echo ( $ arr1 [ $ i ] . " " ) ; } ? >


"Replace every element with the greatest element on right side | Function to replace every element with the next greatest element ; Initialize the next greatest element ; The next greatest element for the rightmost element is always - 1 ; Replace all other elements with the next greatest ; Store the current element ( needed later for updating the next greatest element ) ; Replace current element with the next greatest ; Update the greatest element , if needed ; A utility Function that prints an array ; Driver Code"
How can the above be solved in PHP?

 < ? php function nextGreatest ( & $ arr , $ size ) { $ max_from_right = $ arr [ $ size - 1 ] ; $ arr [ $ size - 1 ] = -1 ; for ( $ i = $ size - 2 ; $ i >= 0 ; $ i -- ) { $ temp = $ arr [ $ i ] ; $ arr [ $ i ] = $ max_from_right ; if ( $ max_from_right < $ temp ) $ max_from_right = $ temp ; } } function printArray ( $ arr , $ size ) { for ( $ i = 0 ; $ i < $ size ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; echo "
 " ; } $ arr = array ( 16 , 17 , 4 , 3 , 5 , 2 ) ; $ size = count ( $ arr ) ; nextGreatest ( $ arr , $ size ) ; echo " The ▁ modified ▁ array ▁ is : ▁
 " ; printArray ( $ arr , $ size ) ; ? >


"Replace the maximum element in the array by coefficient of range | Utility function to print the contents of the array ; Function to replace the maximum element from the array with the coefficient of range of the array ; Maximum element from the array ; Minimum element from the array ; Calculate the coefficient of range for the array ; Assuming all the array elements are distinct . Replace the maximum element with the coefficient of range of the array ; Print the updated array ; Driver code"
How can the above be solved in PHP?

< ? php function printArr ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; } function replaceMax ( $ arr , $ n ) { $ max = max ( $ arr ) ; $ min = min ( $ arr ) ; $ range = $ max - $ min ; $ coeffOfRange = round ( $ range / ( $ max + $ min ) , 6 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] == $ max ) { $ arr [ $ i ] = $ coeffOfRange ; break ; } } printArr ( $ arr , $ n ) ; } $ arr = array ( 15 , 16 , 10 , 9 , 6 , 7 , 17 ) ; $ n = count ( $ arr ) ; replaceMax ( $ arr , $ n ) ; ? >


"Replace two consecutive equal values with one greater | Function to replace consecutive equal elements ; Index in result ; to print new array ; Driver Code"
How can the above be solved in PHP?

< ? php function replace_elements ( $ arr , $ n ) { $ pos = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ arr [ $ pos ++ ] = $ arr [ $ i ] ; while ( $ pos > 1 && $ arr [ $ pos - 2 ] == $ arr [ $ pos - 1 ] ) { $ pos -- ; $ arr [ $ pos - 1 ] ++ ; } } for ( $ i = 0 ; $ i < $ pos ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; } $ arr = array ( 6 , 4 , 3 , 4 , 3 , 3 , 5 ) ; $ n = count ( $ arr ) ; replace_elements ( $ arr , $ n ) ; ? >


"Replace two substrings ( of a string ) with each other | Function to return the resultant string ; Iterate through all positions i ; Current sub - string of length = len ( A ) = len ( B ) ; If current sub - string gets equal to A or B ; Update S after replacing A ; Update S after replacing B ; Return the updated string ; Driver code"
How can the above be solved in PHP?

< ? php function updateString ( $ S , $ A , $ B ) { $ l = strlen ( $ A ) ; for ( $ i = 0 ; $ i + $ l <= strlen ( $ S ) ; $ i ++ ) { $ curr = substr ( $ S , $ i , $ i + $ l ) ; if ( strcmp ( $ curr , $ A ) == 0 ) { $ new_string = substr ( $ S , 0 , $ i ) . $ B . substr ( $ S , $ i + $ l , strlen ( $ S ) ) ; $ S = $ new_string ; $ i += $ l - 1 ; } else { $ new_string = substr ( $ S , 0 , $ i ) . $ A . substr ( $ S , $ i + $ l , strlen ( $ S ) ) ; $ S = $ new_string ; $ i += $ l - 1 ; } } return $ S ; } $ S = " aab " ; $ A = " aa " ; $ B = " bb " ; echo ( updateString ( $ S , $ A , $ B ) ) ;


"Represent a given set of points by the best possible straight line | function to calculate m and c that best fit points represented by x [ ] and y [ ] ; Driver Code"
How can the above be solved in PHP?


"Represent n as the sum of exactly k powers of two | Set 2 | Function to print k numbers which are powers of two and whose sum is equal to n ; Initialising the sum with k ; Initialising an array A with k elements and filling all elements with 1 ; Iterating A [ ] from k - 1 to 0 ; Update sum and A [ i ] till sum + A [ i ] is less than equal to n ; Impossible to find the combination ; Possible solution is stored in A [ ] ; Driver code"
How can the above be solved in PHP?

< ? php function FindAllElements ( $ n , $ k ) { $ sum = $ k ; $ A = array_fill ( 0 , $ k , 1 ) ; for ( $ i = $ k - 1 ; $ i >= 0 ; -- $ i ) { while ( $ sum + $ A [ $ i ] <= $ n ) { $ sum += $ A [ $ i ] ; $ A [ $ i ] *= 2 ; } } if ( $ sum != $ n ) { echo " Impossible " ; } else { for ( $ i = 0 ; $ i < $ k ; ++ $ i ) echo $ A [ $ i ] , ' ▁ ' ; } } $ n = 12 ; $ k = 6 ; FindAllElements ( $ n , $ k ) ; ? >


"Representation of a number in powers of other | PHP program to check if m can be represented as powers of w . ; break ; None of 3 worked . ; If m is not zero means , it can 't be  represented in terms of powers of w. ; Driver code"
How can the above be solved in PHP?

 < ? php function asPowerSum ( $ w , $ m ) { while ( $ m ) { if ( ( $ m - 1 ) % $ w == 0 ) $ m = ( $ m - 1 ) / $ w ; else if ( ( $ m + 1 ) % $ w == 0 ) $ m = ( $ m + 1 ) / $ w ; else if ( $ m % $ w == 0 ) $ m = $ m / $ w ; else } return ( $ m == 0 ) ; } $ w = 3 ; $ m = 7 ; if ( asPowerSum ( $ w , $ m ) ) echo " Yes
 " ; else echo " No
 " ; ? >


"Return maximum occurring character in an input string | PHP program to output the maximum occurring character in a string ; Create array to keep the count of individual characters and initialize the array as 0 ; Construct character count array from the input string . ; Initialize max count ; Traversing through the string and maintaining the count of each character ; Driver Code"
How can the above be solved in PHP?

< ? php $ ASCII_SIZE = 256 ; function getMaxOccuringChar ( $ str ) { global $ ASCII_SIZE ; $ count = array_fill ( 0 , $ ASCII_SIZE , NULL ) ; $ len = strlen ( $ str ) ; $ max = 0 ; for ( $ i = 0 ; $ i < ( $ len ) ; $ i ++ ) { $ count [ ord ( $ str [ $ i ] ) ] ++ ; if ( $ max < $ count [ ord ( $ str [ $ i ] ) ] ) { $ max = $ count [ ord ( $ str [ $ i ] ) ] ; $ result = $ str [ $ i ] ; } } return $ result ; } $ str = " sample ▁ string " ; echo " Max ▁ occurring ▁ character ▁ is ▁ " . getMaxOccuringChar ( $ str ) ; ? >