{"text":"Function that will find loss","code":"< ? php function Loss ( $ SP , $ P ) { $ loss = 0 ; $ loss = ( ( 2 * $ P * $ P * $ SP ) \/ ( 100 * 100 - $ P * $ P ) ) ; print ( \" Loss \u2581 = \u2581 \" . round ( $ loss , 3 ) ) ; }"}
{"text":"Driver Code","code":"$ SP = 2400 ; $ P = 30 ;"}
{"text":"Calling Function","code":"Loss ( $ SP , $ P ) ; ? >"}
{"text":"PHP implementation of the approach","code":"< ? php $ MAXN = 10001 ;"}
{"text":"Stores smallest prime factor for every number","code":"$ spf = array_fill ( 0 , $ MAXN , 0 ) ;"}
{"text":"Hash to store prime factors count","code":"$ hash1 = array_fill ( 0 , $ MAXN , 0 ) ;"}
{"text":"Function to calculate SPF ( Smallest Prime Factor ) for every number till MAXN","code":"function sieve ( ) { global $ spf , $ MAXN , $ hash1 ; $ spf [ 1 ] = 1 ; for ( $ i = 2 ; $ i < $ MAXN ; $ i ++ )"}
{"text":"Marking smallest prime factor for every number to be itself","code":"$ spf [ $ i ] = $ i ;"}
{"text":"Separately marking spf for every even number as 2","code":"for ( $ i = 4 ; $ i < $ MAXN ; $ i += 2 ) $ spf [ $ i ] = 2 ;"}
{"text":"Checking if i is prime","code":"for ( $ i = 3 ; $ i * $ i < $ MAXN ; $ i ++ ) {"}
{"text":"Marking SPF for all numbers divisible by i","code":"if ( $ spf [ $ i ] == $ i ) { for ( $ j = $ i * $ i ; $ j < $ MAXN ; $ j += $ i )"}
{"text":"Marking spf [ j ] if it is not previously marked","code":"if ( $ spf [ $ j ] == $ j ) $ spf [ $ j ] = $ i ; } } }"}
{"text":"Function to store the prime factors after dividing by the smallest prime factor at every step","code":"function getFactorization ( $ x ) { global $ spf , $ MAXN , $ hash1 ; while ( $ x != 1 ) { $ temp = $ spf [ $ x ] ; if ( $ x % $ temp == 0 ) {"}
{"text":"Storing the count of prime factors in hash","code":"$ hash1 [ $ spf [ $ x ] ] ++ ; $ x = ( int ) ( $ x \/ $ spf [ $ x ] ) ; } while ( $ x % $ temp == 0 ) $ x = ( int ) ( $ x \/ $ temp ) ; } }"}
{"text":"Function that returns true if there are no common prime factors between x and other numbers of the array","code":"function check ( $ x ) { global $ spf , $ MAXN , $ hash1 ; while ( $ x != 1 ) { $ temp = $ spf [ $ x ] ;"}
{"text":"Checking whether it common prime factor with other numbers","code":"if ( $ x % $ temp == 0 && $ hash1 [ $ temp ] > 1 ) return false ; while ( $ x % $ temp == 0 ) $ x = ( int ) ( $ x \/ $ temp ) ; } return true ; }"}
{"text":"Function that returns true if there is an element in the array which is coprime with all the other elements of the array","code":"function hasValidNum ( $ arr , $ n ) { global $ spf , $ MAXN , $ hash1 ;"}
{"text":"Using sieve for generating prime factors","code":"sieve ( ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) getFactorization ( $ arr [ $ i ] ) ;"}
{"text":"Checking the common prime factors with other numbers","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( check ( $ arr [ $ i ] ) ) return true ; return false ; }"}
{"text":"Driver code","code":"$ arr = array ( 2 , 8 , 4 , 10 , 6 , 7 ) ; $ n = count ( $ arr ) ; if ( hasValidNum ( $ arr , $ n ) ) echo \" Yes \" ; else echo \" No \" ; ? >"}
{"text":"Function to return the number of ways to remove edges from the graph so that odd number of edges are left in the graph","code":"< ? php function countWays ( $ N ) {"}
{"text":"Total number of edges","code":"$ E = ( $ N * ( $ N - 1 ) ) \/ 2 ; if ( $ N == 1 ) return 0 ; return ( int ) pow ( 2 , $ E - 1 ) ; }"}
{"text":"Driver code","code":"$ N = 4 ; echo ( countWays ( $ N ) ) ; ? >"}
{"text":"Function to return the minimum required absolute difference","code":"< ? php function minAbsDiff ( $ n ) { $ mod = $ n % 4 ; if ( $ mod == 0 $ mod == 3 ) return 0 ; return 1 ; }"}
{"text":"Driver code","code":"$ n = 5 ; echo minAbsDiff ( $ n ) ; ? >"}
{"text":"PHP implementation of the above approach","code":"< ? php function check ( $ s ) {"}
{"text":"creating a frequency array","code":"$ freq = array_fill ( 0 , 10 , 0 ) ; while ( $ s != 0 ) {"}
{"text":"Finding the last digit of the number","code":"$ r = $ s % 10 ;"}
{"text":"Dividing the number by 10 to eliminate last digit","code":"$ s = ( int ) ( $ s \/ 10 ) ;"}
{"text":"counting frequency of each digit","code":"$ freq [ $ r ] += 1 ; } $ xor = 0 ;"}
{"text":"checking if the xor of all frequency is zero or not","code":"for ( $ i = 0 ; $ i < 10 ; $ i ++ ) $ xor = $ xor ^ $ freq [ $ i ] ; if ( $ xor == 0 ) return true ; else return false ; }"}
{"text":"Main Drive","code":"$ s = 122233 ; if ( check ( $ s ) ) print ( \" Yes \" ) ; else print ( \" No \" ) ; ? >"}
{"text":"Function to print N lines","code":"< ? php function printLines ( $ n , $ k ) {"}
{"text":"Iterate N times to print N lines","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { echo ( $ k * ( 6 * $ i + 1 ) ) ; echo ( \" \u2581 \" ) ; echo ( $ k * ( 6 * $ i + 2 ) ) ; echo ( \" \u2581 \" ) ; echo ( $ k * ( 6 * $ i + 3 ) ) ; echo ( \" \u2581 \" ) ; echo ( $ k * ( 6 * $ i + 5 ) ) ; echo ( \" STRNEWLINE \" ) ; } }"}
{"text":"Driver Code","code":"$ n = 2 ; $ k = 2 ; printLines ( $ n , $ k ) ; ? >"}
{"text":"PHP program to find sum of first n terms","code":"< ? php function calculateSum ( $ n ) {"}
{"text":"Sn = n * ( 4 * n * n + 6 * n - 1 ) \/ 3","code":"return ( pow ( 2 , $ n + 1 ) + $ n - 2 ) ; }"}
{"text":"number of terms to be included in sum","code":"$ n = 4 ;"}
{"text":"find the Sn","code":"echo \" Sum = \" ? >"}
{"text":"Function to find the number of partitions of N","code":"< ? php function partitions ( $ n ) { $ p = array_fill ( 0 , $ n + 1 , 0 ) ;"}
{"text":"Base case","code":"$ p [ 0 ] = 1 ; for ( $ i = 1 ; $ i < $ n + 1 ; $ i ++ ) { $ k = 1 ; while ( ( $ k * ( 3 * $ k - 1 ) ) \/ 2 <= $ i ) { $ p [ $ i ] += ( ( $ k % 2 ? 1 : -1 ) * $ p [ $ i - ( $ k * ( 3 * $ k - 1 ) ) \/ 2 ] ) ; if ( $ k > 0 ) $ k *= -1 ; else $ k = 1 - $ k ; } } return $ p [ $ n ] ; }"}
{"text":"Driver Code","code":"$ N = 20 ; print ( partitions ( $ N ) ) ; ? >"}
{"text":"Recursive function to count number of paths","code":"< ? php function countPaths ( $ n , $ m ) {"}
{"text":"If we reach bottom or top left , we are have only one way to reach ( 0 , 0 )","code":"if ( $ n == 0 $ m == 0 ) return 1 ;"}
{"text":"Else count sum of both ways","code":"return ( countPaths ( $ n - 1 , $ m ) + countPaths ( $ n , $ m - 1 ) ) ; }"}
{"text":"Driver Code","code":"$ n = 3 ; $ m = 2 ; echo \" \u2581 Number \u2581 of \u2581 Paths \u2581 \" , countPaths ( $ n , $ m ) ; ? >"}
{"text":"Returns maximum amount of gold that can be collected when journey started from first column and moves allowed are right , right - up and right - down","code":"< ? php function getMaxGold ( $ gold , $ m , $ n ) { $ MAX = 100 ;"}
{"text":"Create a table for storing intermediate results and initialize all cells to 0. The first row of goldMineTable gives the maximum gold that the miner can collect when starts that row","code":"$ goldTable = array ( array ( ) ) ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) for ( $ j = 0 ; $ j < $ n ; $ j ++ ) $ goldTable [ $ i ] [ $ j ] = 0 ; for ( $ col = $ n - 1 ; $ col >= 0 ; $ col -- ) { for ( $ row = 0 ; $ row < $ m ; $ row ++ ) {"}
{"text":"Gold collected on going to the cell on the rigth ( -> )","code":"if ( $ col == $ n - 1 ) $ right = 0 ; else $ right = $ goldTable [ $ row ] [ $ col + 1 ] ;"}
{"text":"Gold collected on going to the cell to right up ( \/ )","code":"if ( $ row == 0 or $ col == $ n - 1 ) $ right_up = 0 ; else $ right_up = $ goldTable [ $ row - 1 ] [ $ col + 1 ] ;"}
{"text":"Gold collected on going to the cell to right down ( \\ )","code":"if ( $ row == $ m - 1 or $ col == $ n - 1 ) $ right_down = 0 ; else $ right_down = $ goldTable [ $ row + 1 ] [ $ col + 1 ] ;"}
{"text":"Max gold collected from taking either of the above 3 paths","code":"$ goldTable [ $ row ] [ $ col ] = $ gold [ $ row ] [ $ col ] + max ( $ right , $ right_up , $ right_down ) ; } }"}
{"text":"The max amount of gold collected will be the max value in first column of all rows","code":"$ res = $ goldTable [ 0 ] [ 0 ] ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) $ res = max ( $ res , $ goldTable [ $ i ] [ 0 ] ) ; return $ res ; }"}
{"text":"Driver code","code":"$ gold = array ( array ( 1 , 3 , 1 , 5 ) , array ( 2 , 2 , 4 , 1 ) , array ( 5 , 0 , 2 , 3 ) , array ( 0 , 6 , 1 , 2 ) ) ; $ m = 4 ; $ n = 4 ; echo getMaxGold ( $ gold , $ m , $ n ) ; ? >"}
{"text":"PHP program to find minimum adjustment cost of an array","code":"< ? php $ M = 100 ;"}
{"text":"Function to find minimum adjustment cost of an array","code":"function minAdjustmentCost ( $ A , $ n , $ target ) {"}
{"text":"dp [ i ] [ j ] stores minimal adjustment cost on changing A [ i ] to j","code":"global $ M ; $ dp = array ( array ( ) ) ;"}
{"text":"handle first element of array separately","code":"for ( $ j = 0 ; $ j <= $ M ; $ j ++ ) $ dp [ 0 ] [ $ j ] = abs ( $ j - $ A [ 0 ] ) ;"}
{"text":"do for rest elements of the array","code":"for ( $ i = 1 ; $ i < $ n ; $ i ++ ) {"}
{"text":"replace A [ i ] to j and calculate minimal adjustment cost dp [ i ] [ j ]","code":"for ( $ j = 0 ; $ j <= $ M ; $ j ++ ) {"}
{"text":"initialize minimal adjustment cost to INT_MAX","code":"$ dp [ $ i ] [ $ j ] = PHP_INT_MAX ;"}
{"text":"consider all k such that k >= max ( j - target , 0 ) and k <= min ( M , j + target ) and take minimum","code":"for ( $ k = max ( $ j - $ target , 0 ) ; $ k <= min ( $ M , $ j + $ target ) ; $ k ++ ) $ dp [ $ i ] [ $ j ] = min ( $ dp [ $ i ] [ $ j ] , $ dp [ $ i - 1 ] [ $ k ] + abs ( $ A [ $ i ] - $ j ) ) ; } }"}
{"text":"return minimum value from last row of dp table","code":"$ res = PHP_INT_MAX ; for ( $ j = 0 ; $ j <= $ M ; $ j ++ ) $ res = min ( $ res , $ dp [ $ n - 1 ] [ $ j ] ) ; return $ res ; }"}
{"text":"Driver Code","code":"$ arr = array ( 55 , 77 , 52 , 61 , 39 , 6 , 25 , 60 , 49 , 47 ) ; $ n = count ( $ arr ) ; $ target = 10 ; echo \" Minimum \u2581 adjustment \u2581 cost \u2581 is \u2581 \" , minAdjustmentCost ( $ arr , $ n , $ target ) ; ? >"}
{"text":"Function to count the character ' a '","code":"< ? php function countChar ( $ str , $ x ) { $ count = 0 ; $ n = 10 ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) if ( $ str [ $ i ] == $ x ) $ count ++ ;"}
{"text":"atleast k repetition are required","code":"$ repetitions = ( int ) ( $ n \/ strlen ( $ str ) ) ; $ count = $ count * $ repetitions ;"}
{"text":"if n is not the multiple of the string size check for the remaining repeating character .","code":"for ( $ i = 0 ; $ i < $ n % strlen ( $ str ) ; $ i ++ ) { if ( $ str [ $ i ] == $ x ) $ count ++ ; } return $ count ; }"}
{"text":"Driver code","code":"$ str = \" abcac \" ; echo countChar ( $ str , ' a ' ) ; ? >"}
{"text":"Function that checks if the binary string contains m consecutive 1 ' s \u2581 or \u2581 0' s","code":"< ? php function check ( $ s , $ m ) {"}
{"text":"length of binary string","code":"$ l = count ( $ s ) ;"}
{"text":"counts zeros","code":"$ c1 = 0 ;"}
{"text":"counts 1 's","code":"$ c2 = 0 ; for ( $ i = 0 ; $ i <= $ l ; $ i ++ ) { if ( $ s [ $ i ] == '0' ) { $ c2 = 0 ;"}
{"text":"count consecutive 0 's","code":"$ c1 ++ ; } else { $ c1 = 0 ;"}
{"text":"count consecutive 1 's","code":"$ c2 ++ ; } if ( $ c1 == $ m or $ c2 == $ m ) return true ; } return false ; }"}
{"text":"Driver Code","code":"$ s = \"001001\" ; $ m = 2 ;"}
{"text":"function call","code":"if ( check ( $ s , $ m ) ) echo \" YES \" ; else echo \" NO \" ; ? >"}
{"text":"Function to find product of digits of elements at k - th level","code":"< ? php function productAtKthLevel ( $ tree , $ k ) { $ level = -1 ;"}
{"text":"$product = 1 ; Initialize result","code":"$ n = strlen ( $ tree ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) {"}
{"text":"increasing level number","code":"if ( $ tree [ $ i ] == ' ( ' ) $ level ++ ;"}
{"text":"decreasing level number","code":"else if ( $ tree [ $ i ] == ' ) ' $ level -- ; else {"}
{"text":"check if current level is the desired level or not","code":"if ( $ level == $ k ) $ product *= ( ord ( $ tree [ $ i ] ) - ord ( '0' ) ) ; } }"}
{"text":"required product","code":"return $ product ; }"}
{"text":"Driver Code","code":"$ tree = \" ( 0(5(6 ( ) ( ) ) ( 4 ( ) (9 ( ) ( ) ) ) ) ( 7(1 ( ) ( ) ) ( 3 ( ) ( ) ) ) ) \" ; $ k = 2 ; echo productAtKthLevel ( $ tree , $ k ) ; ? >"}
{"text":"PHP program to check if a given ISBN is valid or not .","code":"< ? php function isValidISBN ( $ isbn ) {"}
{"text":"length must be 10","code":"$ n = strlen ( $ isbn ) ; if ( $ n != 10 ) return -1 ;"}
{"text":"Computing weighted sum of first 9 digits","code":"$ sum = 0 ; for ( $ i = 0 ; $ i < 9 ; $ i ++ ) { $ digit = $ isbn [ $ i ] - '0' ; if ( 0 > $ digit 9 < $ digit ) return -1 ; $ sum += ( $ digit * ( 10 - $ i ) ) ; }"}
{"text":"Checking last digit .","code":"$ last = $ isbn [ 9 ] ; if ( $ last != ' X ' && ( $ last < '0' $ last > '9' ) ) return -1 ;"}
{"text":"If last digit is ' X ' , add 10 to sum , else add its value .","code":"$ sum += ( ( $ last == ' X ' ) ? 10 : ( $ last - '0' ) ) ;"}
{"text":"Return true if weighted sum of digits is divisible by 11.","code":"return ( $ sum % 11 == 0 ) ; }"}
{"text":"Driver code","code":"$ isbn = \"007462542X \" ; if ( isValidISBN ( $ isbn ) ) echo \" Valid \" ; else echo \" Invalid \" ; ? >"}
{"text":"Driver code","code":"< ? php $ d = 10 ;"}
{"text":"according to formula derived above","code":"$ a = ( 360 - ( 6 * $ d ) ) \/ 4 ;"}
{"text":"print all the angles","code":"echo $ a , \" , \u2581 \" , $ a + $ d , \" , \u2581 \" , $ a + ( 2 * $ d ) , \" , \u2581 \" , $ a + ( 3 * $ d ) ; ? >"}
{"text":"Function to find distance","code":"< ? php function distance ( $ a1 , $ b1 , $ c1 , $ d1 , $ a2 , $ b2 , $ c2 , $ d2 ) { if ( $ a1 \/ $ a2 == $ b1 \/ $ b2 && $ b1 \/ $ b2 == $ c1 \/ $ c2 ) { $ x1 = $ y1 = 0 ; $ z1 = - $ d1 \/ $ c1 ; $ d = abs ( ( $ c2 * $ z1 + $ d2 ) ) \/ ( sqrt ( $ a2 * $ a2 + $ b2 * $ b2 + $ c2 * $ c2 ) ) ; echo \" Perpendicular \u2581 distance \u2581 is \u2581 \" , $ d ; } else echo \" Planes \u2581 are \u2581 not \u2581 parallel \" ; }"}
{"text":"Driver Code","code":"$ a1 = 1 ; $ b1 = 2 ; $ c1 = -1 ; $ d1 = 1 ; $ a2 = 3 ; $ b2 = 6 ; $ c2 = -3 ; $ d2 = -4 ; distance ( $ a1 , $ b1 , $ c1 , $ d1 , $ a2 , $ b2 , $ c2 , $ d2 ) ; ? >"}
{"text":"Prints the minimum number that can be formed from input sequence of I ' s \u2581 and \u2581 D ' s","code":"< ? php function PrintMinNumberForPattern ( $ arr ) {"}
{"text":"Initialize current_max ( to make sure that we don 't use repeated  character","code":"$ curr_max = 0 ;"}
{"text":"Initialize last_entry ( Keeps track for last printed digit )","code":"$ last_entry = 0 ; $ j ;"}
{"text":"Iterate over input array","code":"for ( $ i = 0 ; $ i < strlen ( $ arr ) ; $ i ++ ) {"}
{"text":"Initialize ' noOfNextD ' to get count of next D 's available","code":"$ noOfNextD = 0 ; switch ( $ arr [ $ i ] ) { case ' I ' :"}
{"text":"If letter is ' I ' Calculate number of next consecutive D 's  available","code":"$ j = $ i + 1 ; while ( $ arr [ $ j ] == ' D ' && $ j < strlen ( $ arr ) ) { $ noOfNextD ++ ; $ j ++ ; } if ( $ i == 0 ) { $ curr_max = $ noOfNextD + 2 ;"}
{"text":"If ' I ' is first letter , print incremented sequence from 1","code":"echo \" \u2581 \" , ++ $ last_entry ; echo \" \u2581 \" , $ curr_max ;"}
{"text":"Set max digit reached","code":"$ last_entry = $ curr_max ; } else {"}
{"text":"If not first letter Get next digit to print","code":"$ curr_max = $ curr_max + $ noOfNextD + 1 ;"}
{"text":"Print digit for I","code":"$ last_entry = $ curr_max ; echo \" \u2581 \" , $ last_entry ; }"}
{"text":"For all next consecutive ' D ' print decremented sequence","code":"for ( $ k = 0 ; $ k < $ noOfNextD ; $ k ++ ) { echo \" \u2581 \" , -- $ last_entry ; $ i ++ ; } break ;"}
{"text":"If letter is ' D '","code":"case ' D ' : if ( $ i == 0 ) {"}
{"text":"If ' D ' is first letter in sequence . Find number of Next D 's available","code":"$ j = $ i + 1 ; while ( ( $ arr [ $ j ] == ' D ' ) && ( $ j < strlen ( $ arr ) ) ) { $ noOfNextD ++ ; $ j ++ ; }"}
{"text":"Calculate first digit to print based on number of consecutive D 's","code":"$ curr_max = $ noOfNextD + 2 ;"}
{"text":"Print twice for the first time","code":"echo \" \" \u2581 , \u2581 $ curr _ max \u2581 , STRNEWLINE \" \""}
{"text":"Store last entry","code":"$ last_entry = $ curr_max - 1 ; } else {"}
{"text":"If current ' D ' is not first letter Decrement last_entry","code":"echo \" \u2581 \" , $ last_entry - 1 ; $ last_entry -- ; } break ; } } echo \" \" }"}
{"text":"Driver Code","code":"PrintMinNumberForPattern ( \" IDID \" ) ; PrintMinNumberForPattern ( \" I \" ) ; PrintMinNumberForPattern ( \" DD \" ) ; PrintMinNumberForPattern ( \" II \" ) ; PrintMinNumberForPattern ( \" DIDI \" ) ; PrintMinNumberForPattern ( \" IIDDD \" ) ; PrintMinNumberForPattern ( \" DDIDDIID \" ) ; ? >"}
{"text":"function to check prime","code":"< ? php function isPrime ( $ n ) { $ c = 0 ; for ( $ i = 1 ; $ i < $ n \/ 2 ; $ i ++ ) { if ( $ n % $ i == 0 ) $ c ++ ; } if ( $ c == 1 ) return 1 ; else return 0 ; }"}
{"text":"Function to generate smallest possible number with given digits","code":"function findMinNum ( $ arr , $ n ) {"}
{"text":"Declare a hash array of size 10 and initialize all the elements to zero","code":"$ first = 0 ; $ last = 0 ; $ num ; $ rev ; $ i ; $ hash = array_fill ( 0 , 20 , 0 ) ;"}
{"text":"store the number of occurrences of the digits in the given array into the hash table","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ hash [ $ arr [ $ i ] ] ++ ; }"}
{"text":"Traverse the hash in ascending order to print the required number","code":"echo \" Minimum \u2581 number : \u2581 \" ; for ( $ i = 0 ; $ i <= 9 ; $ i ++ ) {"}
{"text":"Print the number of times a digits occurs","code":"for ( $ j = 0 ; $ j < $ hash [ $ i ] ; $ j ++ ) echo $ i ; }"}
{"text":"extracting the first digit","code":"for ( $ i = 0 ; $ i <= 9 ; $ i ++ ) { if ( $ hash [ $ i ] != 0 ) { $ first = $ i ; break ; } }"}
{"text":"extracting the last digit","code":"for ( $ i = 9 ; $ i >= 0 ; $ i -- ) { if ( $ hash [ $ i ] != 0 ) { $ last = $ i ; break ; } } $ num = $ first * 10 + $ last ; $ rev = $ last * 10 + $ first ;"}
{"text":"printing the prime combinations","code":"echo \" Prime combinations : \" if ( isPrime ( $ num ) && isPrime ( $ rev ) ) echo $ num . \" \u2581 \" . $ rev ; else if ( isPrime ( $ num ) ) echo $ num ; else if ( isPrime ( $ rev ) ) echo $ rev ; else echo \" No \u2581 combinations \u2581 exist \" ; }"}
{"text":"Driver Code","code":"$ arr = array ( 1 , 2 , 4 , 7 , 8 ) ; findMinNum ( $ arr , 5 ) ; ? >"}
{"text":"Function to return gcd of a and b","code":"< ? php function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; }"}
{"text":"function to check for gcd","code":"function coprime ( $ a , $ b ) {"}
{"text":"a and b are coprime if their gcd is 1.","code":"return ( gcd ( $ a , $ b ) == 1 ) ; }"}
{"text":"Checks if any possible triplet ( a , b , c ) satifying the condition that ( a , b ) is coprime , ( b , c ) is coprime but ( a , c ) isnt","code":"function possibleTripletInRange ( $ L , $ R ) { $ flag = false ; $ possibleA ; $ possibleB ; $ possibleC ;"}
{"text":"Generate and check for all possible triplets between L and R","code":"for ( $ a = $ L ; $ a <= $ R ; $ a ++ ) { for ( $ b = $ a + 1 ; $ b <= $ R ; $ b ++ ) { for ( $ c = $ b + 1 ; $ c <= $ R ; $ c ++ ) {"}
{"text":"if we find any such triplets set flag to true","code":"if ( coprime ( $ a , $ b ) && coprime ( $ b , $ c ) && ! coprime ( $ a , $ c ) ) { $ flag = true ; $ possibleA = $ a ; $ possibleB = $ b ; $ possibleC = $ c ; break ; } } } }"}
{"text":"flag = True indicates that a pair exists between L and R","code":"if ( $ flag == true ) { echo \" ( \" , $ possibleA , \" , \u2581 \" , $ possibleB , \" , \u2581 \" , $ possibleC , \" ) \" , \" \u2581 is \u2581 one \u2581 such \u2581 possible \u2581 triplet \u2581 between \u2581 \" , $ L , \" \u2581 and \u2581 \" , $ R , \" STRNEWLINE \" ; } else { echo \" No \u2581 Such \u2581 Triplet \u2581 exists \u2581 between \u2581 \" , $ L , \" \u2581 and \u2581 \" , $ R , \" STRNEWLINE \" ; } }"}
{"text":"Driver Code","code":"$ L ; $ R ;"}
{"text":"finding possible Triplet between 2 and 10","code":"$ L = 2 ; $ R = 10 ; possibleTripletInRange ( $ L , $ R ) ;"}
{"text":"finding possible Triplet between 23 and 46","code":"$ L = 23 ; $ R = 46 ; possibleTripletInRange ( $ L , $ R ) ; ? >"}
{"text":"function to check is it is possible to reach A and B starting from 1 and 1","code":"< ? php function possibleToReach ( $ a , $ b ) {"}
{"text":"find the cuberoot of the number","code":"$ c = ( $ a * $ b ) ;"}
{"text":"divide the number by cuberoot","code":"$ re1 = $ a \/ $ c ; $ re2 = $ b \/ $ c ;"}
{"text":"if it is a perfect cuberoot and divides a and b","code":"if ( ( $ re1 * $ re1 * $ re2 == $ a ) && ( $ re2 * $ re2 * $ re1 == $ b ) ) return 1 ; else return -1 ; }"}
{"text":"Driver Code","code":"$ A = 60 ; $ B = 450 ; if ( possibleToReach ( $ A , $ B ) ) echo \" yes \" ; else echo \" no \" ; ? >"}
{"text":"PHP program to check whether a number is undulating or not","code":"< ? php function isUndulating ( $ n ) {"}
{"text":"Considering the definition with restriction that there should be at least 3 digits","code":"if ( strlen ( $ n ) <= 2 ) return false ;"}
{"text":"Check if all alternate digits are same or not .","code":"for ( $ i = 2 ; $ i < strlen ( $ n ) ; $ i ++ ) if ( $ n [ $ i - 2 ] != $ n [ $ i ] ) false ; return true ; }"}
{"text":"Driver code","code":"$ n = \"1212121\" ; if ( isUndulating ( $ n ) ) echo ( \" Yes \" ) ; else echo ( \" No \" ) ; ? >"}
{"text":"Function to calculate the following series","code":"< ? php function Series ( $ n ) { $ i ; $ sums = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ sums += ( $ i * $ i ) ; return $ sums ; }"}
{"text":"Driver Code","code":"$ n = 3 ; $ res = Series ( $ n ) ; echo ( $ res ) ; ? >"}
{"text":"function to calculate the sum of all numbers divisible by 6 in range L - R . .","code":"< ? php function sum ( $ L , $ R ) {"}
{"text":"no of multiples of 6 upto r","code":"$ p = intval ( $ R \/ 6 ) ;"}
{"text":"no of multiples of 6 upto l - 1","code":"$ q = intval ( ( $ L - 1 ) \/ 6 ) ;"}
{"text":"summation of all multiples of 6 upto r","code":"$ sumR = intval ( 3 * ( $ p * ( $ p + 1 ) ) ) ;"}
{"text":"summation of all multiples of 6 upto l - 1","code":"$ sumL = intval ( ( $ q * ( $ q + 1 ) ) * 3 ) ;"}
{"text":"returns the answer","code":"return $ sumR - $ sumL ; }"}
{"text":"Driver Code","code":"$ L = 1 ; $ R = 20 ; echo sum ( $ L , $ R ) ; ? >"}
{"text":"PHP program to find the largest smaller number by swapping one digit . Returns largest possible number with one swap such that the number is smaller than str . It is assumed that there are leading 0 s .","code":"< ? php function prevNum ( $ str ) { $ len = strlen ( $ str ) ; $ index = -1 ;"}
{"text":"Traverse from right until we find a digit which is greater than its next digit . For example , in 34125 , our index is 4.","code":"for ( $ i = $ len - 2 ; $ i >= 0 ; $ i -- ) { if ( $ str [ $ i ] > $ str [ $ i + 1 ] ) { $ index = $ i ; break ; } }"}
{"text":"We can also use binary search here as digits after index are sorted in increasing order . Find the biggest digit in the right of arr [ index ] which is smaller than arr [ index ]","code":"$ smallGreatDgt = -1 ; for ( $ i = $ len - 1 ; $ i > $ index ; $ i -- ) { if ( $ str [ $ i ] < $ str [ $ index ] ) { if ( $ smallGreatDgt == -1 ) $ smallGreatDgt = $ i ; else if ( $ str [ $ i ] >= $ str [ $ smallGreatDgt ] ) $ smallGreatDgt = $ i ; } }"}
{"text":"If index is - 1 i . e . digits are in increasing order .","code":"if ( $ index == -1 ) return \" - 1\" ;"}
{"text":"Swap both values","code":"if ( $ smallGreatDgt != -1 ) { list ( $ str [ $ index ] , $ str [ $ smallGreatDgt ] ) = array ( $ str [ $ smallGreatDgt ] , $ str [ $ index ] ) ;"}
{"text":"swap ( str [ index ] , str [ smallGreatDgt ] ) ;","code":"return $ str ; } return \" - 1\" ; }"}
{"text":"Driver code","code":"$ str = \"34125\" ; echo prevNum ( $ str ) ; ? >"}
{"text":"returns value of poly [ 0 ] x ( n - 1 ) + poly [ 1 ] x ( n - 2 ) + . . + poly [ n - 1 ]","code":"< ? php function horner ( $ poly , $ n , $ x ) {"}
{"text":"Initialize result","code":"$ result = $ poly [ 0 ] ;"}
{"text":"Evaluate value of polynomial using Horner 's method","code":"for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ result = $ result * $ x + $ poly [ $ i ] ; return $ result ; }"}
{"text":"Returns sign value of polynomial","code":"function findSign ( $ poly , $ n , $ x ) { $ result = horner ( $ poly , $ n , $ x ) ; if ( $ result > 0 ) return 1 ; else if ( $ result < 0 ) return -1 ; return 0 ; }"}
{"text":"Let us evaluate value of 2 x3 - 6 x2 + 2 x - 1 for x = 3","code":"$ poly = array ( 2 , -6 , 2 , -1 ) ; $ x = 3 ; $ n = count ( $ poly ) ; echo \" Sign \u2581 of \u2581 polynomial \u2581 is \u2581 \" , findSign ( $ poly , $ n , $ x ) ; ? >"}
{"text":"PHP program to find minimum number to insert in array so their sum is prime","code":"< ? php $ MAX = 100005 ;"}
{"text":"function to calculate primes using sieve of eratosthenes","code":"function sieveOfEratostheneses ( ) { $ isPrime = array_fill ( true , $ MAX , NULL ) ; $ isPrime [ 1 ] = false ; for ( $ i = 2 ; $ i * $ i < $ MAX ; $ i ++ ) { if ( $ isPrime [ $ i ] ) { for ( $ j = 2 * $ i ; $ j < $ MAX ; $ j += $ i ) $ isPrime [ $ j ] = false ; } } }"}
{"text":"Find prime number greater than a number","code":"function findPrime ( $ n ) { $ num = $ n + 1 ;"}
{"text":"To return prime number greater than n","code":"while ( $ num ) {"}
{"text":"check if num is prime","code":"if ( $ isPrime [ $ num ] ) return $ num ;"}
{"text":"increment num","code":"$ num = $ num + 1 ; } return 0 ; }"}
{"text":"To find number to be added so sum of array is prime","code":"function minNumber ( & $ arr , $ n ) {"}
{"text":"call sieveOfEratostheneses to calculate primes","code":"sieveOfEratostheneses ( ) ; $ sum = 0 ;"}
{"text":"To find sum of array elements","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += $ arr [ $ i ] ; if ( $ isPrime [ $ sum ] ) return 0 ;"}
{"text":"To find prime number greater then sum","code":"$ num = findPrime ( $ sum ) ;"}
{"text":"Return difference of sum and num","code":"return $ num - $ sum ; }"}
{"text":"Driver Code","code":"$ arr = array ( 2 , 4 , 6 , 8 , 12 ) ; $ n = sizeof ( $ arr ) \/ sizeof ( $ arr [ 0 ] ) ; echo minNumber ( $ arr , $ n ) ; return 0 ; ? >"}
{"text":"Computes sum all sub - array","code":"< ? php function SubArraySum ( $ arr , $ n ) { $ result = 0 ; $ temp = 0 ;"}
{"text":"Pick starting point","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) {"}
{"text":"Pick ending point","code":"$ temp = 0 ; for ( $ j = $ i ; $ j < $ n ; $ j ++ ) {"}
{"text":"sum subarray between current starting and ending points","code":"$ temp += $ arr [ $ j ] $ result += $ temp ; } } return $ result ; }"}
{"text":"Driver Code","code":"$ arr = array ( 1 , 2 , 3 ) ; $ n = sizeof ( $ arr ) ; echo \" Sum \u2581 of \u2581 SubArray \u2581 : \u2581 \" , SubArraySum ( $ arr , $ n ) , \" STRNEWLINE \" ; ? >"}
{"text":"PHP program to find highest power of 2 smaller than or equal to n .","code":"< ? php function highestPowerof2 ( $ n ) { $ p = ( int ) log ( $ n , 2 ) ; return ( int ) pow ( 2 , $ p ) ; }"}
{"text":"Driver code","code":"$ n = 10 ; echo highestPowerof2 ( $ n ) ; ? >"}
{"text":"utility function to calculate a % m","code":"< ? php function aModM ( $ s , $ mod ) { $ number = 0 ; for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) {"}
{"text":"( s [ i ] - '0' ) gives the digit value and form the number","code":"$ number = ( $ number * 10 + ( $ s [ $ i ] - '0' ) ) ; $ number %= $ mod ; } return $ number ; }"}
{"text":"Returns find ( a ^ b ) % m","code":"function ApowBmodM ( $ a , $ b , $ m ) {"}
{"text":"Find a % m","code":"$ ans = aModM ( $ a , $ m ) ; $ mul = $ ans ;"}
{"text":"now multiply ans by b - 1 times and take mod with m","code":"for ( $ i = 1 ; $ i < $ b ; $ i ++ ) $ ans = ( $ ans * $ mul ) % $ m ; return $ ans ; }"}
{"text":"Driver code","code":"$ a = \"987584345091051645734583954832576\" ; $ b = 3 ; $ m = 11 ; echo ApowBmodM ( $ a , $ b , $ m ) ; return 0 ; ? >"}
{"text":"Prints all prime numbers smaller","code":"< ? php function SieveOfSundaram ( $ n ) {"}
{"text":"In general Sieve of Sundaram , produces primes smaller than ( 2 * x + 2 ) for a number given number x . Since we want primes smaller than n , we reduce n to half","code":"$ nNew = ( $ n - 1 ) \/ 2 ;"}
{"text":"Initialize all elements as not marked","code":"$ marked = array_fill ( 0 , ( $ nNew + 1 ) , false ) ;"}
{"text":"Main logic of Sundaram . Mark all numbers of the form i + j + 2 ij as true where 1 <= i <= j","code":"for ( $ i = 1 ; $ i <= $ nNew ; $ i ++ ) for ( $ j = $ i ; ( $ i + $ j + 2 * $ i * $ j ) <= $ nNew ; $ j ++ ) $ marked [ $ i + $ j + 2 * $ i * $ j ] = true ;"}
{"text":"Since 2 is a prime number","code":"if ( $ n > 2 ) echo \"2 \u2581 \" ;"}
{"text":"Print other primes . Remaining primes are of the form 2 * i + 1 such that marked [ i ] is false .","code":"for ( $ i = 1 ; $ i <= $ nNew ; $ i ++ ) if ( $ marked [ $ i ] == false ) echo ( 2 * $ i + 1 ) . \" \u2581 \" ; }"}
{"text":"Driver Code","code":"$ n = 20 ; SieveOfSundaram ( $ n ) ; ? >"}
{"text":"Function to calculate hamming distance","code":"< ? php function hammingDistance ( $ n1 , $ n2 ) { $ x = $ n1 ^ $ n2 ; $ setBits = 0 ; while ( $ x > 0 ) { $ setBits += $ x & 1 ; $ x >>= 1 ; } return $ setBits ; }"}
{"text":"Driver code","code":"$ n1 = 9 ; $ n2 = 14 ; echo ( hammingDistance ( 9 , 14 ) ) ; ? >"}
{"text":"function to find bitwise subsets Naive approach","code":"< ? php function printSubsets ( $ n ) { for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) if ( ( $ n & $ i ) == $ i ) echo $ i . \" \" ; }"}
{"text":"Driver Code","code":"$ n = 9 ; printSubsets ( $ n ) ; ? >"}
{"text":"PHP program to find MSB number for given n .","code":"< ? php function setBitNumber ( $ n ) {"}
{"text":"To find the position of the most significant set bit","code":"$ k = ( int ) ( log ( $ n , 2 ) ) ;"}
{"text":"To return the the value of the number with set bit at k - th position","code":"return 1 << $ k ; }"}
{"text":"Driver code","code":"$ n = 273 ; echo setBitNumber ( $ n ) ; ? >"}
{"text":"Function to count subsets such that all subsets have distinct elements .","code":"< ? php function subset ( $ ar , $ n ) {"}
{"text":"Take input and initialize res = 0","code":"$ res = 0 ;"}
{"text":"Sort the array","code":"sort ( $ ar ) ;"}
{"text":"Traverse the input array and find maximum frequency","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ count = 1 ;"}
{"text":"For each number find its repetition \/ frequency","code":"for ( ; $ i < $ n - 1 ; $ i ++ ) { if ( $ ar [ $ i ] == $ ar [ $ i + 1 ] ) $ count ++ ; else break ; }"}
{"text":"Update res","code":"$ res = max ( $ res , $ count ) ; } return $ res ; }"}
{"text":"Driver code","code":"$ arr = array ( 5 , 6 , 9 , 3 , 4 , 3 , 4 ) ; $ n = sizeof ( $ arr ) ; echo subset ( $ arr , $ n ) ; ? >"}
{"text":"function to check whether the array contains a set of contiguous integers","code":"< ? php function areElementsContiguous ( $ arr , $ n ) {"}
{"text":"Sort the array","code":"sort ( $ arr ) ;"}
{"text":"After sorting , check if current element is either same as previous or is one more .","code":"for ( $ i = 1 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] - $ arr [ $ i - 1 ] > 1 ) return false ; return true ; }"}
{"text":"Driver Code","code":"$ arr = array ( 5 , 2 , 3 , 6 , 4 , 4 , 6 , 6 ) ; $ n = sizeof ( $ arr ) ; if ( areElementsContiguous ( $ arr , $ n ) ) echo \" Yes \" ; else echo \" No \" ; ? >"}
{"text":"function to find largest d","code":"< ? php function findLargestd ( $ S , $ n ) { $ found = false ;"}
{"text":"sort the array in ascending order","code":"sort ( $ S ) ;"}
{"text":"iterating from backwards to find the required largest d","code":"for ( $ i = $ n - 1 ; $ i >= 0 ; $ i -- ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) {"}
{"text":"since all four a , b , c , d should be distinct","code":"if ( $ i == $ j ) continue ; for ( $ k = $ j + 1 ; $ k < $ n ; $ k ++ ) { if ( $ i == $ k ) continue ; for ( $ l = $ k + 1 ; $ l < $ n ; $ l ++ ) { if ( $ i == $ l ) continue ;"}
{"text":"if the current combination of j , k , l in the set is equal to S [ i ] return this value as this would be the largest d since we are iterating in descending order","code":"if ( $ S [ $ i ] == $ S [ $ j ] + $ S [ $ k ] + $ S [ $ l ] ) { $ found = true ; return $ S [ $ i ] ; } } } } } if ( $ found == false ) return PHP_INT_MIN ; }"}
{"text":"Driver Code","code":"$ S = array ( 2 , 3 , 5 , 7 , 12 ) ; $ n = count ( $ S ) ; $ ans = findLargestd ( $ S , $ n ) ; if ( $ ans == PHP_INT_MIN ) echo \" No \u2581 Solution \" ; else echo \" Largest \u2581 d \u2581 such \u2581 that \u2581 a \u2581 + \u2581 b \u2581 + \u2581 \" , \" c \u2581 = \u2581 d \u2581 is \u2581 \" , $ ans ; ? >"}
{"text":"Function to left Rotate arr [ ] of size n by 1","code":"< ? php function leftRotatebyOne ( & $ arr , $ n ) { $ temp = $ arr [ 0 ] ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) $ arr [ $ i ] = $ arr [ $ i + 1 ] ; $ arr [ $ n - 1 ] = $ temp ; }"}
{"text":"Function to left rotate arr [ ] of size n by d","code":"function leftRotate ( & $ arr , $ d , $ n ) { for ( $ i = 0 ; $ i < $ d ; $ i ++ ) leftRotatebyOne ( $ arr , $ n ) ; }"}
{"text":"utility function to print an array","code":"function printArray ( & $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . \" \u2581 \" ; }"}
{"text":"Driver Code","code":"$ arr = array ( 1 , 2 , 3 , 4 , 5 , 6 , 7 ) ; $ n = sizeof ( $ arr ) ; leftRotate ( $ arr , 2 , $ n ) ; printArray ( $ arr , $ n ) ; ? >"}
{"text":"Function to sort the elements of the array from index a to index b","code":"function partSort ( $ arr , $ N , $ a , $ b ) {"}
{"text":"Variables to store start and end of the index range","code":"$ l = min ( $ a , $ b ) ; $ r = max ( $ a , $ b ) ;"}
{"text":"Temporary array","code":"$ temp = array ( ) ; $ j = 0 ; for ( $ i = $ l ; $ i <= $ r ; $ i ++ ) { $ temp [ $ j ] = $ arr [ $ i ] ; $ j ++ ; }"}
{"text":"Sort the temporary array","code":"sort ( $ temp ) ;"}
{"text":"Modifying original array with temporary array elements","code":"$ j = 0 ; for ( $ i = $ l ; $ i <= $ r ; $ i ++ ) { $ arr [ $ i ] = $ temp [ $ j ] ; $ j ++ ; }"}
{"text":"Print the modified array","code":"for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { echo $ arr [ $ i ] . \" \" ; } }"}
{"text":"Driver code","code":"$ arr = array ( 7 , 8 , 4 , 5 , 2 ) ; $ a = 1 ; $ b = 4 ;"}
{"text":"length of the array","code":"$ N = count ( $ arr ) ; partSort ( $ arr , $ N , $ a , $ b ) ; ? >"}
{"text":"Function which pushes all zeros to end of an array .","code":"< ? php function pushZerosToEnd ( & $ arr , $ n ) {"}
{"text":"Count of non - zero elements","code":"$ count = 0 ;"}
{"text":"Traverse the array . If element encountered is non - zero , then replace the element at index ' count ' with this element","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] != 0 )"}
{"text":"here count is incremented","code":"$ arr [ $ count ++ ] = $ arr [ $ i ] ;"}
{"text":"Now all non - zero elements have been shifted to front and ' count ' is set as index of first 0. Make all elements 0 from count to end .","code":"while ( $ count < $ n ) $ arr [ $ count ++ ] = 0 ; }"}
{"text":"Driver Code","code":"$ arr = array ( 1 , 9 , 8 , 4 , 0 , 0 , 2 , 7 , 0 , 6 , 0 , 9 ) ; $ n = sizeof ( $ arr ) ; pushZerosToEnd ( $ arr , $ n ) ; echo \" Array \u2581 after \u2581 pushing \u2581 all \u2581 \" . \" zeros \u2581 to \u2581 end \u2581 of \u2581 array \u2581 : STRNEWLINE \" ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . \" \u2581 \" ; ? >"}
{"text":"A utility function to print an array of size n","code":"< ? php function printArray ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo ( $ arr [ $ i ] . \" \u2581 \" ) ; }"}
{"text":"Function to Rearrange positive and negative numbers in a array","code":"function RearrangePosNeg ( & $ arr , $ n ) { $ key ; $ j ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ key = $ arr [ $ i ] ;"}
{"text":"if current element is positive do nothing","code":"if ( $ key > 0 ) continue ;"}
{"text":"if current element is negative , shift positive elements of arr [ 0. . i - 1 ] , to one position to their right","code":"$ j = $ i - 1 ; while ( $ j >= 0 && $ arr [ $ j ] > 0 ) { $ arr [ $ j + 1 ] = $ arr [ $ j ] ; $ j = $ j - 1 ; }"}
{"text":"Put negative element at its right position","code":"$ arr [ $ j + 1 ] = $ key ; } }"}
{"text":"Driver program","code":"{ $ arr = array ( -12 , 11 , -13 , -5 , 6 , -7 , 5 , -3 , -6 ) ; $ n = sizeof ( $ arr ) ; RearrangePosNeg ( $ arr , $ n ) ; printArray ( $ arr , $ n ) ; }"}
{"text":"Simple PHP program to find all elements in array which have at - least two greater elements itself .","code":"< ? php function findElements ( $ arr , $ n ) {"}
{"text":"Pick elements one by one and count greater elements . If count is more than 2 , print that element .","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ count = 0 ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) if ( $ arr [ $ j ] > $ arr [ $ i ] ) $ count ++ ; if ( $ count >= 2 ) echo $ arr [ $ i ] . \" \u2581 \" ; } }"}
{"text":"Driver code","code":"$ arr = array ( 2 , -6 , 3 , 5 , 1 ) ; $ n = sizeof ( $ arr ) ; findElements ( $ arr , $ n ) ; ? >"}
{"text":"Sorting based PHP program to find all elements in array which have atleast two greater elements itself .","code":"< ? php function findElements ( $ arr , $ n ) { sort ( $ arr ) ; for ( $ i = 0 ; $ i < $ n - 2 ; $ i ++ ) echo $ arr [ $ i ] , \" \u2581 \" ; }"}
{"text":"Driver Code","code":"$ arr = array ( 2 , -6 , 3 , 5 , 1 ) ; $ n = count ( $ arr ) ; findElements ( $ arr , $ n ) ; ? > ;"}
{"text":"PHP program to find all elements in array which have atleast two greater elements itself .","code":"< ? php function findElements ( $ arr , $ n ) { $ first = PHP_INT_MIN ; $ second = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) {"}
{"text":"If current element is smaller than first then update both first and second","code":"if ( $ arr [ $ i ] > $ first ) { $ second = $ first ; $ first = $ arr [ $ i ] ; }"}
{"text":"If arr [ i ] is in between first and second then update second","code":"else if ( $ arr [ $ i ] > $ second ) $ second = $ arr [ $ i ] ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] < $ second ) echo $ arr [ $ i ] , \" \u2581 \" ; }"}
{"text":"Driver code","code":"$ arr = array ( 2 , -6 , 3 , 5 , 1 ) ; $ n = count ( $ arr ) ; findElements ( $ arr , $ n ) ; ? >"}
{"text":"function that returns smallest elements missing in a sorted array .","code":"< ? php function findFirstMissing ( $ array , $ start , $ end ) { if ( $ start > $ end ) return $ end + 1 ; if ( $ start != $ array [ $ start ] ) return $ start ; $ mid = ( $ start + $ end ) \/ 2 ;"}
{"text":"Left half has all elements from 0 to mid","code":"if ( $ array [ $ mid ] == $ mid ) return findFirstMissing ( $ array , $ mid + 1 , $ end ) ; return findFirstMissing ( $ array , $ start , $ mid ) ; }"}
{"text":"Driver Code","code":"$ arr = array ( 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 10 ) ; $ n = count ( $ arr ) ; echo \" Smallest \u2581 missing \u2581 element \u2581 is \u2581 \" , findFirstMissing ( $ arr , 2 , $ n - 1 ) ; ? >"}
{"text":"Function to return max sum such that no two elements are adjacent","code":"< ? php function FindMaxSum ( $ arr , $ n ) { $ incl = $ arr [ 0 ] ; $ excl = 0 ; $ excl_new ; $ i ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) {"}
{"text":"current max excluding i","code":"$ excl_new = ( $ incl > $ excl ) ? $ incl : $ excl ;"}
{"text":"current max including i","code":"$ incl = $ excl + $ arr [ $ i ] ; $ excl = $ excl_new ; }"}
{"text":"return max of incl and excl","code":"return ( ( $ incl > $ excl ) ? $ incl : $ excl ) ; }"}
{"text":"Driver Code","code":"$ arr = array ( 5 , 5 , 10 , 100 , 10 , 5 ) ; $ n = sizeof ( $ arr ) ; echo FindMaxSum ( $ arr , $ n ) ; ? >"}
{"text":"Returns beginning index of maximum average subarray of length ' k '","code":"< ? php function findMaxAverage ( $ arr , $ n , $ k ) {"}
{"text":"Check if ' k ' is valid","code":"if ( $ k > $ n ) return -1 ;"}
{"text":"Create and fill array to store cumulative sum . csum [ i ] stores sum of arr [ 0 ] to arr [ i ]","code":"$ csum = array ( ) ; $ csum [ 0 ] = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ csum [ $ i ] = $ csum [ $ i - 1 ] + $ arr [ $ i ] ;"}
{"text":"Initialize max_sm as sum of first subarray","code":"$ max_sum = $ csum [ $ k - 1 ] ; $ max_end = $ k - 1 ;"}
{"text":"Find sum of other subarrays and update max_sum if required .","code":"for ( $ i = $ k ; $ i < $ n ; $ i ++ ) { $ curr_sum = $ csum [ $ i ] - $ csum [ $ i - $ k ] ; if ( $ curr_sum > $ max_sum ) { $ max_sum = $ curr_sum ; $ max_end = $ i ; } }"}
{"text":"Return starting index","code":"return $ max_end - $ k + 1 ; }"}
{"text":"Driver Code","code":"$ arr = array ( 1 , 12 , -5 , -6 , 50 , 3 ) ; $ k = 4 ; $ n = count ( $ arr ) ; echo \" The \u2581 maximum \u2581 average \u2581 subarray \u2581 of \u2581 \" , \" length \u2581 \" , $ k , \" \u2581 begins \u2581 at \u2581 index \u2581 \" , findMaxAverage ( $ arr , $ n , $ k ) ; ? >"}
{"text":"Returns beginning index of maximum average subarray of length ' k '","code":"< ? php function findMaxAverage ( $ arr , $ n , $ k ) {"}
{"text":"Check if ' k ' is valid","code":"if ( $ k > $ n ) return -1 ;"}
{"text":"Compute sum of first ' k ' elements","code":"$ sum = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ k ; $ i ++ ) $ sum += $ arr [ $ i ] ; $ max_sum = $ sum ; $ max_end = $ k - 1 ;"}
{"text":"Compute sum of remaining subarrays","code":"for ( $ i = $ k ; $ i < $ n ; $ i ++ ) { $ sum = $ sum + $ arr [ $ i ] - $ arr [ $ i - $ k ] ; if ( $ sum > $ max_sum ) { $ max_sum = $ sum ; $ max_end = $ i ; } }"}
{"text":"Return starting index","code":"return $ max_end - $ k + 1 ; }"}
{"text":"Driver Code","code":"$ arr = array ( 1 , 12 , -5 , -6 , 50 , 3 ) ; $ k = 4 ; $ n = count ( $ arr ) ; echo \" The \u2581 maximum \u2581 average \u2581 subarray \u2581 of \u2581 \" , \" length \u2581 \" , $ k , \" \u2581 begins \u2581 at \u2581 index \u2581 \" , findMaxAverage ( $ arr , $ n , $ k ) ; ? >"}
{"text":"PHP Program to check for majority element in a sorted array function returns majority element in a sorted array","code":"< ? php function isMajority ( $ arr , $ n , $ x ) { $ i ;"}
{"text":"get last index according to n ( even or odd )","code":"$ last_index = $ n % 2 ? ( $ n \/ 2 + 1 ) : ( $ n \/ 2 ) ;"}
{"text":"search for first occurrence of x in arr [ ]","code":"for ( $ i = 0 ; $ i < $ last_index ; $ i ++ ) {"}
{"text":"check if x is present and is present more than n \/ 2 times","code":"if ( $ arr [ $ i ] == $ x && $ arr [ $ i + $ n \/ 2 ] == $ x ) return 1 ; } return 0 ; }"}
{"text":"Driver Code","code":"$ arr = array ( 1 , 2 , 3 , 4 , 4 , 4 , 4 ) ; $ n = sizeof ( $ arr ) ; $ x = 4 ; if ( isMajority ( $ arr , $ n , $ x ) ) echo $ x , \" \u2581 appears \u2581 more \u2581 than \u2581 \" , floor ( $ n \/ 2 ) , \" \u2581 times \u2581 in \u2581 arr [ ] \" ; else echo $ x , \" does \u2581 not \u2581 appear \u2581 more \u2581 than \u2581 \" , floor ( $ n \/ 2 ) , \" times \u2581 in \u2581 arr [ ] \" ; ? >"}
{"text":"Returns the best obtainable price for a rod of length n and price [ ] as prices of different pieces","code":"< ? php function cutRod ( $ price , $ n ) { $ val = array ( ) ; $ val [ 0 ] = 0 ; $ i ; $ j ;"}
{"text":"Build the table val [ ] in bottom up manner and return the last entry from the table","code":"for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ max_val = PHP_INT_MIN ; for ( $ j = 0 ; $ j < $ i ; $ j ++ ) $ max_val = max ( $ max_val , $ price [ $ j ] + $ val [ $ i - $ j - 1 ] ) ; $ val [ $ i ] = $ max_val ; } return $ val [ $ n ] ; }"}
{"text":"Driver program to test above functions","code":"$ arr = array ( 1 , 5 , 8 , 9 , 10 , 17 , 17 , 20 ) ; $ size = count ( $ arr ) ; echo \" Maximum \u2581 Obtainable \u2581 Value \u2581 is \u2581 \" , cutRod ( $ arr , $ size ) ; ? >"}
{"text":"Function to return the count of primes in the given array","code":"< ? php function primeCount ( $ arr , $ n ) {"}
{"text":"Find maximum value in the array","code":"$ max_val = max ( $ arr ) ;"}
{"text":"USE SIEVE TO FIND ALL PRIME NUMBERS LESS THAN OR EQUAL TO max_val Create a boolean array \" prime [ 0 . . n ] \" . A value in prime [ i ] will finally be false if i is Not a prime , else true .","code":"$ prime = array_fill ( 0 , $ max_val + 1 , true ) ;"}
{"text":"Remaining part of SIEVE","code":"$ prime [ 0 ] = false ; $ prime [ 1 ] = false ; for ( $ p = 2 ; $ p * $ p <= $ max_val ; $ p ++ ) {"}
{"text":"If prime [ p ] is not changed , then it is a prime","code":"if ( $ prime [ $ p ] == true ) {"}
{"text":"Update all multiples of p","code":"for ( $ i = $ p * 2 ; $ i <= $ max_val ; $ i += $ p ) $ prime [ $ i ] = false ; } }"}
{"text":"Find all primes in arr [ ]","code":"$ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ prime [ $ arr [ $ i ] ] ) $ count ++ ; return $ count ; }"}
{"text":"Function to generate the prefix array","code":"function getPrefixArray ( $ arr , $ n , $ pre ) {"}
{"text":"Fill the prefix array","code":"$ pre [ 0 ] = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ pre [ $ i ] = $ pre [ $ i - 1 ] + $ arr [ $ i ] ; } return $ pre ; }"}
{"text":"Driver code","code":"$ arr = array ( 1 , 4 , 8 , 4 ) ; $ n = count ( $ arr ) ;"}
{"text":"Prefix array of arr [ ]","code":"$ pre = array ( ) ; $ pre = getPrefixArray ( $ arr , $ n , $ pre ) ;"}
{"text":"Count of primes in the prefix array","code":"echo primeCount ( $ pre , $ n ) ; ? >"}
{"text":"Function to return the required value that must be added to x so that it is at least y percent of n","code":"< ? php function minValue ( $ n , $ x , $ y ) {"}
{"text":"Required value","code":"$ val = ( $ y * $ n ) \/ 100 ;"}
{"text":"If x is already >= y percent of n","code":"if ( $ x >= $ val ) return 0 ; else return ( ceil ( $ val ) - $ x ) ; }"}
{"text":"Driver code","code":"{ $ n = 10 ; $ x = 2 ; $ y = 40 ; echo ( minValue ( $ n , $ x , $ y ) ) ; }"}
{"text":"Utility function to check if a number is prime or not","code":"< ? php function isPrime ( $ n ) {"}
{"text":"Corner cases","code":"if ( $ n <= 1 ) return false ; if ( $ n <= 3 ) return true ;"}
{"text":"This is checked so that we can skip middle five numbers in below loop","code":"if ( $ n % 2 == 0 $ n % 3 == 0 ) return false ; for ( $ i = 5 ; $ i * $ i <= $ n ; $ i = $ i + 6 ) if ( $ n % $ i == 0 || $ n % ( $ i + 2 ) == 0 ) return false ; return true ; }"}
{"text":"Function that returns true if n is a factorial prime","code":"function isFactorialPrime ( $ n ) {"}
{"text":"If n is not prime then return false","code":"if ( ! isPrime ( $ n ) ) return false ; $ fact = 1 ; $ i = 1 ; while ( $ fact <= $ n + 1 ) {"}
{"text":"Calculate factorial","code":"$ fact = $ fact * $ i ;"}
{"text":"If n is a factorial prime","code":"if ( $ n + 1 == $ fact $ n - 1 == $ fact ) return true ; $ i ++ ; }"}
{"text":"n is not a factorial prime","code":"return false ; }"}
{"text":"Driver code","code":"$ n = 23 ; if ( isFactorialPrime ( $ n ) ) echo \" Yes \" ; else echo \" No \" ; ? >"}
{"text":"Get n","code":"< ? php $ n = 5 ;"}
{"text":"find fac1 = ( n - 1 ) !","code":"$ fac1 = 1 ; for ( $ i = 2 ; $ i <= $ n - 1 ; $ i ++ ) $ fac1 = $ fac1 * $ i ;"}
{"text":"Find fac2 = n !","code":"$ fac2 = $ fac1 * $ n ;"}
{"text":"Find total number of ways","code":"$ totalWays = $ fac1 * $ fac2 ;"}
{"text":"Print the total number of ways","code":"echo $ totalWays . \" STRNEWLINE \" ;"}
{"text":"Function to find the next perfect cube","code":"< ? php function nextPerfectCube ( $ N ) { $ nextN = ( int ) ( floor ( pow ( $ N , ( 1 \/ 3 ) ) ) + 1 ) ; return $ nextN * $ nextN * $ nextN ; }"}
{"text":"Driver code","code":"$ n = 35 ; print ( nextPerfectCube ( $ n ) ) ; ? >"}
{"text":"PHP Program position of n among the numbers made of 2 , 3 , 5 & 7","code":"< ? php function findpos ( $ n ) { $ pos = 0 ; for ( $ i = 0 ; isset ( $ n [ $ i ] ) != NULL ; $ i ++ ) { switch ( $ n [ $ i ] ) {"}
{"text":"If number is 2 then it is on the position pos * 2 + 1","code":"case '2' : $ pos = $ pos * 4 + 1 ; break ;"}
{"text":"If number is 3 then it is on the position pos * 2 + 2","code":"case '3' : $ pos = $ pos * 4 + 2 ; break ;"}
{"text":"If number is 5 then it is on the position pos * 2 + 3","code":"case '5' : $ pos = $ pos * 4 + 3 ; break ;"}
{"text":"If number is 7 then it is on the position pos * 2 + 4","code":"case '7' : $ pos = $ pos * 4 + 4 ; break ; } } return $ pos ; }"}
{"text":"Driver Code","code":"$ n = \"777\" ; echo findpos ( $ n ) ; ? >"}
{"text":"PHP Implementation of above method","code":"< ? php $ mod = 1000000007 ;"}
{"text":"Finding number of possible number with n digits excluding a particular digit","code":"function digitNumber ( $ n ) { global $ mod ;"}
{"text":"Checking if number of digits is zero","code":"if ( $ n == 0 ) return 1 ;"}
{"text":"Checking if number of digits is one","code":"if ( $ n == 1 ) return 9 ;"}
{"text":"Checking if number of digits is odd","code":"if ( $ n % 2 != 0 ) {"}
{"text":"Calling digitNumber function with ( digit - 1 ) \/ 2 digits ;","code":"$ temp = digitNumber ( ( $ n - 1 ) \/ 2 ) % $ mod ; return ( 9 * ( $ temp * $ temp ) % $ mod ) % $ mod ; } else {"}
{"text":"Calling digitNumber function with n \/ 2 digits","code":"$ temp = digitNumber ( $ n \/ 2 ) % $ mod ; return ( $ temp * $ temp ) % $ mod ; } } function countExcluding ( $ n , $ d ) { global $ mod ;"}
{"text":"Calling digitNumber function Checking if excluding digit is zero or non - zero","code":"if ( $ d == 0 ) return ( 9 * digitNumber ( $ n - 1 ) ) % $ mod ; else return ( 8 * digitNumber ( $ n - 1 ) ) % $ mod ; }"}
{"text":"Initializing variables","code":"$ d = 9 ; $ n = 3 ; print ( countExcluding ( $ n , $ d ) ) ; ? >"}
{"text":"Returns true if n is prime else false","code":"< ? php function isPrime ( $ n ) {"}
{"text":"Corner case","code":"if ( $ n <= 1 ) return -1 ;"}
{"text":"Check from 2 to n - 1","code":"for ( $ i = 2 ; $ i < $ n ; $ i ++ ) if ( $ n % $ i == 0 ) return -1 ; return 1 ; }"}
{"text":"Function will check whether number is Emirp or not","code":"function isEmirp ( $ n ) {"}
{"text":"Check if n is prime","code":"if ( isPrime ( $ n ) == -1 ) return -1 ;"}
{"text":"Find reverse of n","code":"$ rev = 0 ; while ( $ n != 0 ) { $ d = $ n % 10 ; $ rev = $ rev * 10 + $ d ; $ n \/= 10 ; }"}
{"text":"If both Original and Reverse are Prime , then it is an Emirp number","code":"return isPrime ( $ rev ) ; }"}
{"text":"Input number","code":"$ n = 13 ; if ( isEmirp ( $ n ) == -1 ) echo \" Yes \" ; else echo \" No \" ; ? >"}
{"text":"Function for convertion","code":"< ? php function Convert ( $ radian ) { $ pi = 3.14159 ; return ( $ radian * ( 180 \/ $ pi ) ) ; }"}
{"text":"Driver Code","code":"$ radian = 5.0 ; $ degree = Convert ( $ radian ) ; echo ( $ degree ) ; ? >"}
{"text":"Return sum of first n integers of an AP","code":"< ? php function sn ( $ n , $ an ) { return ( $ n * ( 1 + $ an ) ) \/ 2 ; }"}
{"text":"Return the trace of sum of row - major matrix and column - major matrix","code":"function trace ( $ n , $ m ) {"}
{"text":"Finding nth element in AP in case of Row major matrix .","code":"$ an = 1 + ( $ n - 1 ) * ( $ m + 1 ) ;"}
{"text":"Finding sum of first n integers of AP in case of Row major matrix","code":"$ rowmajorSum = sn ( $ n , $ an ) ;"}
{"text":"Finding nth element in AP in case of Row major matrix","code":"$ an = 1 + ( $ n - 1 ) * ( $ n + 1 ) ;"}
{"text":"Finding sum of first n integers of AP in case of Column major matrix","code":"$ colmajorSum = sn ( $ n , $ an ) ; return $ rowmajorSum + $ colmajorSum ; }"}
{"text":"Driver Code","code":"$ N = 3 ; $ M = 3 ; echo trace ( $ N , $ M ) , \" STRNEWLINE \" ; ? >"}
{"text":"Utility Function","code":"< ? php function max_area ( $ n , $ m , $ k ) { if ( $ k > ( $ n + $ m - 2 ) ) echo \" Not \u2581 possible \" , \" STRNEWLINE \" ; else { $ result ;"}
{"text":"for the 1 st case","code":"if ( $ k < max ( $ m , $ n ) - 1 ) { $ result = max ( $ m * ( $ n \/ ( $ k + 1 ) ) , $ n * ( $ m \/ ( $ k + 1 ) ) ) ; }"}
{"text":"for the second case","code":"else { $ result = max ( $ m \/ ( $ k - $ n + 2 ) , $ n \/ ( $ k - $ m + 2 ) ) ; }"}
{"text":"print final result","code":"echo $ result , \" STRNEWLINE \" ; } }"}
{"text":"Driver Code","code":"$ n = 3 ; $ m = 4 ; $ k = 1 ; max_area ( $ n , $ m , $ k ) ; ? >"}
{"text":"function to find the area","code":"< ? php function area_fun ( $ side ) { $ area = $ side * $ side ; return $ area ; }"}
{"text":"Driver program","code":"$ side = 4 ; $ area = area_fun ( $ side ) ; echo ( $ area ) ; ? >"}
{"text":"Utility method to compute number of ways in which N can be represented as sum of consecutive number","code":"< ? php function countConsecutive ( $ N ) {"}
{"text":"constraint on values of L gives us the time Complexity as O ( N ^ 0.5 )","code":"$ count = 0 ; for ( $ L = 1 ; $ L * ( $ L + 1 ) < 2 * $ N ; $ L ++ ) { $ a = ( int ) ( 1.0 * $ N - ( $ L * ( int ) ( $ L + 1 ) ) \/ 2 ) \/ ( $ L + 1 ) ; if ( $ a - ( int ) $ a == 0.0 ) $ count ++ ; } return $ count ; }"}
{"text":"Driver Code","code":"$ N = 15 ; echo countConsecutive ( $ N ) , \" STRNEWLINE \" ; $ N = 10 ; echo countConsecutive ( $ N ) , \" STRNEWLINE \" ; ? >"}
{"text":"Function to check Automorphic number","code":"< ? php function isAutomorphic ( $ N ) {"}
{"text":"Store the square","code":"$ sq = $ N * $ N ;"}
{"text":"Start Comparing digits","code":"while ( $ N > 0 ) {"}
{"text":"Return false , if any digit of N doesn ' t \u2581 \u2581 match \u2581 with \u2581 its \u2581 square ' s digits from last","code":"if ( $ N % 10 != $ sq % 10 ) return -1 ;"}
{"text":"Reduce N and square","code":"$ N \/= 10 ; $ sq \/= 10 ; } return 1 ; }"}
{"text":"Driver code","code":"$ N = 5 ; $ geeks = isAutomorphic ( $ N ) ? \" Automorphic \" : \" Not \u2581 Automorphic \" ; echo $ geeks ; ? >"}
{"text":"Return smallest number having maximum prime factors .","code":"< ? php function maxPrimefactorNum ( $ N ) {"}
{"text":"default value of boolean is false","code":"$ arr = array_fill ( 0 , $ N + 5 , true ) ;"}
{"text":"Sieve of eratosthenes","code":"for ( $ i = 3 ; $ i * $ i <= $ N ; $ i += 2 ) { if ( $ arr [ $ i ] ) for ( $ j = $ i * $ i ; $ j <= $ N ; $ j += $ i ) $ arr [ $ j ] = false ; }"}
{"text":"Storing prime numbers .","code":"$ prime = array ( ) ; array_push ( $ prime , 2 ) ; for ( $ i = 3 ; $ i <= $ N ; $ i += 2 ) if ( $ arr [ $ i ] ) array_push ( $ prime , $ i ) ;"}
{"text":"Generating number having maximum prime factors .","code":"$ i = 0 ; $ ans = 1 ; while ( $ ans * $ prime [ $ i ] <= $ N && $ i < count ( $ prime ) ) { $ ans *= $ prime [ $ i ] ; $ i ++ ; } return $ ans ; }"}
{"text":"Driver Code","code":"$ N = 40 ; print ( maxPrimefactorNum ( $ N ) ) ; ? >"}
{"text":"Function to calculate sum of all proper divisors num -- > given natural number","code":"< ? php function divSum ( $ num ) {"}
{"text":"Final result of summation of divisors","code":"$ result = 0 ;"}
{"text":"find all divisors which divides ' num '","code":"for ( $ i = 2 ; $ i <= sqrt ( $ num ) ; $ i ++ ) {"}
{"text":"if ' i ' is divisor of ' num '","code":"if ( $ num % $ i == 0 ) {"}
{"text":"if both divisors are same then add it only once else add both","code":"if ( $ i == ( $ num \/ $ i ) ) $ result += $ i ; else $ result += ( $ i + $ num \/ $ i ) ; } }"}
{"text":"Add 1 to the result as 1 is also a divisor","code":"return ( $ result + 1 ) ; }"}
{"text":"Driver Code","code":"$ num = 36 ; echo ( divSum ( $ num ) ) ; ? >"}
{"text":"Utility function to do modular exponentiation . It returns ( x ^ y ) % p .","code":"< ? php function power ( $ x , $ y , $ p ) {"}
{"text":"Initialize result","code":"$ res = 1 ;"}
{"text":"Update x if it is more than or equal to p","code":"$ x = $ x % $ p ; while ( $ y > 0 ) {"}
{"text":"If y is odd , multiply x with result","code":"if ( $ y & 1 ) $ res = ( $ res * $ x ) % $ p ;"}
{"text":"y must be even now y = y \/ 2","code":"$ y = $ y >> 1 ; $ x = ( $ x * $ x ) % $ p ; } return $ res ; }"}
{"text":"Returns true if square root of n under modulo p exists Assumption : p is of the form 3 * i + 4 where i >= 1","code":"function squareRoot ( $ n , $ p ) { if ( $ p % 4 != 3 ) { echo \" Invalid \u2581 Input \" ; return ; }"}
{"text":"Try \" + ( n ^ ( ( p \u2581 + \u2581 1 ) \/ 4 ) ) \"","code":"$ n = $ n % $ p ; $ x = power ( $ n , ( $ p + 1 ) \/ 4 , $ p ) ; if ( ( $ x * $ x ) % $ p == $ n ) { echo \" Square \u2581 root \u2581 is \u2581 \" , $ x ; return ; }"}
{"text":"Try \" - ( n \u2581 ^ \u2581 ( ( p \u2581 + \u2581 1 ) \/ 4 ) ) \"","code":"$ x = $ p - $ x ; if ( ( $ x * $ x ) % $ p == $ n ) { echo \" Square \u2581 root \u2581 is \u2581 \" , $ x ; return ; }"}
{"text":"If none of the above two work , then square root doesn 't exist","code":"echo \" Square \u2581 root \u2581 doesn ' t \u2581 exist \u2581 \" ; }"}
{"text":"Driver Code","code":"$ p = 7 ; $ n = 2 ; squareRoot ( $ n , $ p ) ; ? >"}
{"text":"Utility function to do modular exponentiation . It returns ( x ^ y ) % p","code":"< ? php function power ( $ x , $ y , $ p ) {"}
{"text":"Initialize result","code":"$ res = 1 ;"}
{"text":"Update x if it is more than or equal to p","code":"$ x = $ x % $ p ; while ( $ y > 0 ) {"}
{"text":"If y is odd , multiply x with result","code":"if ( $ y & 1 ) $ res = ( $ res * $ x ) % $ p ;"}
{"text":"y must be even now $y = $y >> 1 ; $y = $y \/ 2","code":"$ x = ( $ x * $ x ) % $ p ; } return $ res ; }"}
{"text":"This function is called for all k trials . It returns false if n is composite and returns false if n is probably prime . d is an odd number such that d * 2 < sup > r < \/ sup > = n - 1 for some r >= 1","code":"function miillerTest ( $ d , $ n ) {"}
{"text":"Pick a random number in [ 2. . n - 2 ] Corner cases make sure that n > 4","code":"$ a = 2 + rand ( ) % ( $ n - 4 ) ;"}
{"text":"Compute a ^ d % n","code":"$ x = power ( $ a , $ d , $ n ) ; if ( $ x == 1 $ x == $ n - 1 ) return true ;"}
{"text":"Keep squaring x while one of the following doesn 't  happen  (i) d does not reach n-1  (ii) (x^2) % n is not 1  (iii) (x^2) % n is not n-1","code":"while ( $ d != $ n - 1 ) { $ x = ( $ x * $ x ) % $ n ; $ d *= 2 ; if ( $ x == 1 ) return false ; if ( $ x == $ n - 1 ) return true ; }"}
{"text":"Return composite","code":"return false ; }"}
{"text":"It returns false if n is composite and returns true if n is probably prime . k is an input parameter that determines accuracy level . Higher value of k indicates more accuracy .","code":"function isPrime ( $ n , $ k ) {"}
{"text":"Corner cases","code":"if ( $ n <= 1 $ n == 4 ) return false ; if ( $ n <= 3 ) return true ;"}
{"text":"Find r such that n = 2 ^ d * r + 1 for some r >= 1","code":"$ d = $ n - 1 ; while ( $ d % 2 == 0 ) $ d \/= 2 ;"}
{"text":"Iterate given nber of ' k ' times","code":"for ( $ i = 0 ; $ i < $ k ; $ i ++ ) if ( ! miillerTest ( $ d , $ n ) ) return false ; return true ; }"}
{"text":"Driver Code Number of iterations","code":"$ k = 4 ; echo \" All \u2581 primes \u2581 smaller \u2581 than \u2581 100 : \u2581 STRNEWLINE \" ; for ( $ n = 1 ; $ n < 100 ; $ n ++ ) if ( isPrime ( $ n , $ k ) ) echo $ n , \" \u2581 \" ; ? >"}
{"text":"Function to find length of the longest consecutive 1 s in binary representation of a number","code":"< ? php function maxConsecutiveOnes ( $ x ) {"}
{"text":"Initialize result","code":"$ count = 0 ;"}
{"text":"Count the number of iterations to reach x = 0.","code":"while ( $ x != 0 ) {"}
{"text":"This operation reduces length of every sequence of 1 s by one .","code":"$ x = ( $ x & ( $ x << 1 ) ) ; $ count ++ ; } return $ count ; }"}
{"text":"Driver code","code":"echo maxConsecutiveOnes ( 14 ) , \" STRNEWLINE \" ; echo maxConsecutiveOnes ( 222 ) , \" STRNEWLINE \" ; ? >"}
{"text":"PHP Program to subtract two Number without using arithmetic operator","code":"< ? php function subtract ( $ x , $ y ) {"}
{"text":"Iterate till there is no carry","code":"while ( $ y != 0 ) {"}
{"text":"borrow contains common set bits of y and unset bits of x","code":"$ borrow = ( ~ $ x ) & $ y ;"}
{"text":"Subtraction of bits of x and y where at least one of the bits is not set","code":"$ x = $ x ^ $ y ;"}
{"text":"Borrow is shifted by one so that subtracting it from x gives the required sum","code":"$ y = $ borrow << 1 ; } return $ x ; }"}
{"text":"Driver Code","code":"$ x = 29 ; $ y = 13 ; echo \" x \u2581 - \u2581 y \u2581 is \u2581 \" , subtract ( $ x , $ y ) ; ? >"}
{"text":"PHP Program to subtract two Number without using arithmetic operator Recursive implementation .","code":"< ? php function subtract ( $ x , $ y ) { if ( $ y == 0 ) return $ x ; return subtract ( $ x ^ $ y , ( ~ $ x & $ y ) << 1 ) ; }"}
{"text":"Driver Code","code":"$ x = 29 ; $ y = 13 ; echo \" x \u2581 - \u2581 y \u2581 is \u2581 \" , subtract ( $ x , $ y ) ; # This  code is contributed by ajit NEW_LINE ? >"}
{"text":"Driver code","code":"< ? php $ N = 6 ; $ Even = $ N \/ 2 ; $ Odd = $ N - $ Even ; echo $ Even * $ Odd ; ? >"}
{"text":"function to print the steps","code":"< ? php function steps ( $ str , $ n ) {"}
{"text":"declare a flag","code":"$ x = 0 ;"}
{"text":"traverse through all the characters in the string","code":"for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) {"}
{"text":"if the x value is 0. . then we must increment till n ... set flag to true","code":"if ( $ x == 0 ) $ flag = true ;"}
{"text":"if the x value is n - 1 then we must decrement till 0 ... set flag as false","code":"if ( $ x == $ n - 1 ) $ flag = false ;"}
{"text":"print x * s","code":"for ( $ j = 0 ; $ j < $ x ; $ j ++ ) echo \" * \" ; echo $ str [ $ i ] , \" STRNEWLINE \" ;"}
{"text":"checking whether to increment or decrement x","code":"if ( $ flag == true ) $ x ++ ; else $ x -- ; } }"}
{"text":"Get the String and the number n","code":"$ n = 4 ; $ str = \" GeeksForGeeks \" ; echo \" String : \u2581 \" , $ str , \" STRNEWLINE \" ; echo \" Max \u2581 Length \u2581 of \u2581 Steps : \u2581 \" , $ n , \" STRNEWLINE \" ;"}
{"text":"calling the function","code":"steps ( $ str , $ n ) ; ? >"}
{"text":"function to check whether given binary number is evenly divisible by 2 ^ k or not","code":"< ? php function isDivisible ( $ str , $ k ) { $ n = strlen ( $ str ) ; $ c = 0 ;"}
{"text":"count of number of 0 from last","code":"for ( $ i = 0 ; $ i < $ k ; $ i ++ ) if ( $ str [ $ n - $ i - 1 ] == '0' ) $ c ++ ;"}
{"text":"if count = k , number is evenly divisible , so returns true else false","code":"return ( $ c == $ k ) ; }"}
{"text":"first example","code":"$ str1 = \"10101100\" ; $ k = 2 ; if ( isDivisible ( $ str1 , $ k ) ) echo \" Yes \" , \" STRNEWLINE \" ; else echo \" No \" , \" STRNEWLINE \" ;"}
{"text":"Second example","code":"$ str2 = \"111010100\" ; $ k = 2 ; if ( isDivisible ( $ str2 , $ k ) ) echo \" Yes \" , \" STRNEWLINE \" ; else echo \" No \" , \" STRNEWLINE \" ; ? >"}
{"text":"Returns true if s is a number else false","code":"< ? php function isNumber ( $ s ) { for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) if ( is_numeric ( $ s [ $ i ] ) == false ) return false ; return true ; }"}
{"text":"Saving the input in a string","code":"$ str = \"6790\" ;"}
{"text":"Function returns 1 if all elements are in range '0-9'","code":"if ( isNumber ( $ str ) ) echo \" Integer \" ;"}
{"text":"Function returns 0 if the input is not an integer","code":"else echo \" String \" ; ? >"}
{"text":"Function to print reverse of the passed string","code":"< ? php function reverse ( $ str ) { if ( ( $ str == null ) || ( strlen ( $ str ) <= 1 ) ) echo ( $ str ) ; else { echo ( $ str [ strlen ( $ str ) - 1 ] ) ; reverse ( substr ( $ str , 0 , ( strlen ( $ str ) - 1 ) ) ) ; } }"}
{"text":"Driver Code","code":"$ str = \" Geeks \u2581 for \u2581 Geeks \" ; reverse ( $ str ) ; ? >"}
{"text":"Function to find the area of a regular polygon","code":"< ? php function polyarea ( $ n , $ r ) {"}
{"text":"Side and radius cannot be negative","code":"if ( $ r < 0 && $ n < 0 ) return -1 ;"}
{"text":"Area degree converted to radians","code":"$ A = ( ( $ r * $ r * $ n ) * sin ( ( 360 \/ $ n ) * 3.14159 \/ 180 ) ) \/ 2 ; return $ A ; }"}
{"text":"Driver code","code":"$ r = 9 ; $ n = 6 ; echo polyarea ( $ n , $ r ) . \" STRNEWLINE \" ; ? >"}
{"text":"Function to find the Slope of other line","code":"< ? php function findPCSlope ( $ m ) { return -1.0 \/ $ m ; }"}
{"text":"Driver Code","code":"$ m = 2.0 ; echo findPCSlope ( $ m ) ; ? >"}
{"text":"Function to find area of segment","code":"< ? php function area_of_segment ( $ radius , $ angle ) { $ pi = 3.14159 ;"}
{"text":"Calculating area of sector","code":"$ area_of_sector = $ pi * ( $ radius * $ radius ) * ( $ angle \/ 360 ) ;"}
{"text":"Calculating area of triangle","code":"$ area_of_triangle = 1 \/ 2 * ( $ radius * $ radius ) * sin ( ( $ angle * $ pi ) \/ 180 ) ; return $ area_of_sector - $ area_of_triangle ; }"}
{"text":"Driver Code","code":"$ radius = 10.0 ; $ angle = 90.0 ; echo ( \" Area \u2581 of \u2581 minor \u2581 segment \u2581 = \u2581 \" ) ; echo ( area_of_segment ( $ radius , $ angle ) ) ; echo ( \" STRNEWLINE \" ) ; echo ( \" Area \u2581 of \u2581 major \u2581 segment \u2581 = \u2581 \" ) ; echo ( area_of_segment ( $ radius , ( 360 - $ angle ) ) ) ; ? >"}
{"text":"PHP program to find Area of a Sector","code":"< ? php function SectorArea ( $ radius , $ angle ) { if ( $ angle >= 360 ) echo ( \" Angle \u2581 not \u2581 possible \" ) ;"}
{"text":"Calculating area of the sector","code":"else { $ sector = ( ( 22 * $ radius * $ radius ) \/ 7 ) * ( $ angle \/ 360 ) ; echo ( $ sector ) ; } }"}
{"text":"Driver code","code":"$ radius = 9 ; $ angle = 60 ; SectorArea ( $ radius , $ angle ) ; ? >"}
{"text":"Recursive function to sort an array using insertion sort","code":"< ? php function insertionSortRecursive ( & $ arr , $ n ) {"}
{"text":"Base case","code":"if ( $ n <= 1 ) return ;"}
{"text":"Sort first n - 1 elements","code":"insertionSortRecursive ( $ arr , $ n - 1 ) ;"}
{"text":"Insert last element at its correct position in sorted array .","code":"$ last = $ arr [ $ n - 1 ] ; $ j = $ n - 2 ;"}
{"text":"Move elements of arr [ 0. . i - 1 ] , that are greater than key , to one position ahead of their current position","code":"while ( $ j >= 0 && $ arr [ $ j ] > $ last ) { $ arr [ $ j + 1 ] = $ arr [ $ j ] ; $ j -- ; } $ arr [ $ j + 1 ] = $ last ; }"}
{"text":"A utility function to print an array of size n","code":"function printArray ( & $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . \" \u2581 \" ; }"}
{"text":"Driver Code","code":"$ arr = array ( 12 , 11 , 13 , 5 , 6 ) ; $ n = sizeof ( $ arr ) ; insertionSortRecursive ( $ arr , $ n ) ; printArray ( $ arr , $ n ) ; ? >"}
{"text":"Function to check if array is wave array arr : input array n : size of array","code":"< ? php function isWaveArray ( $ arr , $ n ) { $ result = true ;"}
{"text":"Check the wave form * If arr [ 1 ] is greater than left and right * Same pattern will be followed by whole * elements , else reverse pattern * will be followed by array elements","code":"if ( $ arr [ 1 ] > $ arr [ 0 ] && $ arr [ 1 ] > $ arr [ 2 ] ) { for ( $ i = 1 ; $ i < ( $ n - 1 ) ; $ i += 2 ) { if ( $ arr [ $ i ] > $ arr [ $ i - 1 ] && $ arr [ $ i ] > $ arr [ $ i + 1 ] ) { $ result = true ; } else { $ result = false ; break ; } }"}
{"text":"Check for last element","code":"if ( $ result == true && $ n % 2 == 0 ) { if ( $ arr [ $ n - 1 ] <= $ arr [ $ n - 2 ] ) { $ result = false ; } } } else if ( $ arr [ 1 ] < $ arr [ 0 ] && $ arr [ 1 ] < $ arr [ 2 ] ) { for ( $ i = 1 ; $ i < $ n - 1 ; $ i += 2 ) { if ( $ arr [ $ i ] < $ arr [ $ i - 1 ] && $ arr [ $ i ] < $ arr [ $ i + 1 ] ) { $ result = true ; } else { $ result = false ; break ; } }"}
{"text":"Check for last element","code":"if ( $ result == true && $ n % 2 == 0 ) { if ( $ arr [ $ n - 1 ] >= $ arr [ $ n - 2 ] ) { $ result = false ; } } } return $ result ; }"}
{"text":"Array","code":"$ arr = array ( 1 , 3 , 2 , 4 ) ; $ n = sizeof ( $ arr ) ; if ( isWaveArray ( $ arr , $ n ) ) { echo \" YES \" ; } else { echo \" NO \" ; } ? >"}
{"text":"PHP program to Find the sum of first N odd Fibonacci numbers","code":"< ? php $ mod = 1000000007 ;"}
{"text":"Function to calculate sum of first N odd Fibonacci numbers","code":"function sumOddFibonacci ( $ n ) { global $ mod ; $ Sum [ $ n + 1 ] = array ( ) ;"}
{"text":"base values","code":"$ Sum [ 0 ] = 0 ; $ Sum [ 1 ] = 1 ; $ Sum [ 2 ] = 2 ; $ Sum [ 3 ] = 5 ; $ Sum [ 4 ] = 10 ; $ Sum [ 5 ] = 23 ; for ( $ i = 6 ; $ i <= $ n ; $ i ++ ) { $ Sum [ $ i ] = ( ( $ Sum [ $ i - 1 ] + ( 4 * $ Sum [ $ i - 2 ] ) % $ mod - ( 4 * $ Sum [ $ i - 3 ] ) % $ mod + $ mod ) % $ mod + ( $ Sum [ $ i - 4 ] - $ Sum [ $ i - 5 ] + $ mod ) % $ mod ) % $ mod ; } return $ Sum [ $ n ] ; }"}
{"text":"Driver code","code":"$ n = 6 ; echo sumOddFibonacci ( $ n ) ; ? >"}
{"text":"PHP program to reach N - th stair by taking a maximum of K leap","code":"< ? php function solve ( $ N , $ K ) {"}
{"text":"elements of combo [ ] stores the no of possible ways to reach it by all combinations of k leaps or less","code":"$ combo [ $ N + 1 ] = array ( ) ;"}
{"text":"assuming leap 0 exist and assigning its value to 1 for calculation","code":"$ combo [ 0 ] = 1 ;"}
{"text":"loop to iterate over all possible leaps upto k ;","code":"for ( $ i = 1 ; $ i <= $ K ; $ i ++ ) {"}
{"text":"in this loop we count all possible leaps to reach the jth stair with the help of ith leap or less","code":"for ( $ j = 0 ; $ j <= $ N ; $ j ++ ) {"}
{"text":"if the leap is not more than the i - j","code":"if ( $ j >= $ i ) {"}
{"text":"calculate the value and store in combo [ j ] to reuse it for next leap calculation for the jth stair","code":"$ combo [ $ j ] += $ combo [ $ j - $ i ] ; } } }"}
{"text":"returns the no of possible number of leaps to reach the top of building of n stairs","code":"return $ combo [ $ N ] ; }"}
{"text":"N i the no of total stairs K is the value of the greatest leap","code":"$ N = 29 ; $ K = 5 ; echo solve ( $ N , $ K ) ; solve ( $ N , $ K ) ; ? >"}
{"text":"Utility function to find LIS using Dynamic programming","code":"< ? php function computeLIS ( $ circBuff , $ start , $ end , $ n ) { $ LIS = Array ( ) ;"}
{"text":"Initialize LIS values for all indexes","code":"for ( $ i = $ start ; $ i < $ end ; $ i ++ ) $ LIS [ $ i ] = 1 ;"}
{"text":"Compute optimized LIS values in bottom up manner","code":"for ( $ i = $ start + 1 ; $ i < $ end ; $ i ++ )"}
{"text":"Set j on the basis of current window i . e . first element of the current window","code":"for ( $ j = $ start ; $ j < $ i ; $ j ++ ) if ( $ circBuff [ $ i ] > $ circBuff [ $ j ] && $ LIS [ $ i ] < $ LIS [ $ j ] + 1 ) $ LIS [ $ i ] = $ LIS [ $ j ] + 1 ;"}
{"text":"Pick maximum of all LIS values","code":"$ res = PHP_INT_MIN ; for ( $ i = $ start ; $ i < $ end ; $ i ++ ) $ res = max ( $ res , $ LIS [ $ i ] ) ; return $ res ; }"}
{"text":"Function to find LIS in Circular manner","code":"function LICS ( $ arr , $ n ) {"}
{"text":"Make a copy of given array by appending same array elements to itself","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ circBuff [ $ i ] = $ arr [ $ i ] ; for ( $ i = $ n ; $ i < 2 * $ n ; $ i ++ ) $ circBuff [ $ i ] = $ arr [ $ i - $ n ] ;"}
{"text":"Perform LIS for each window of size n","code":"$ res = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ res = max ( computeLIS ( $ circBuff , $ i , $ i + $ n , $ n ) , $ res ) ; return $ res ; }"}
{"text":"Driver Code","code":"$ arr = array ( 1 , 4 , 6 , 2 , 3 ) ; $ n = sizeof ( $ arr ) ; echo \" Length \u2581 of \u2581 LICS \u2581 is \u2581 \" , LICS ( $ arr , $ n ) ; ? >"}
{"text":"Returns the length and the LCIS of two arrays arr1 [ 0. . n - 1 ] and arr2 [ 0. . m - 1 ]","code":"< ? php function LCIS ( $ arr1 , $ n , $ arr2 , $ m ) {"}
{"text":"table [ j ] is going to store length of LCIS ending with arr2 [ j ] . We initialize it as 0 ,","code":"$ table = Array ( ) ; for ( $ j = 0 ; $ j < $ m ; $ j ++ ) $ table [ $ j ] = 0 ;"}
{"text":"Traverse all elements of arr1 [ ]","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) {"}
{"text":"Initialize current length of LCIS","code":"$ current = 0 ;"}
{"text":"For each element of arr1 [ ] , trvarse all elements of arr2 [ ] .","code":"for ( $ j = 0 ; $ j < $ m ; $ j ++ ) {"}
{"text":"If both the array have same elements . Note that we don 't break the loop here.","code":"if ( $ arr1 [ $ i ] == $ arr2 [ $ j ] ) if ( $ current + 1 > $ table [ $ j ] ) $ table [ $ j ] = $ current + 1 ;"}
{"text":"Now seek for previous smaller common element for current element of arr1","code":"if ( $ arr1 [ $ i ] > $ arr2 [ $ j ] ) if ( $ table [ $ j ] > $ current ) $ current = $ table [ $ j ] ; } }"}
{"text":"The maximum value in table [ ] is out result","code":"$ result = 0 ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) if ( $ table [ $ i ] > $ result ) $ result = $ table [ $ i ] ; return $ result ; }"}
{"text":"Driver Code","code":"$ arr1 = array ( 3 , 4 , 9 , 1 ) ; $ arr2 = array ( 5 , 3 , 8 , 9 , 10 , 2 , 1 ) ; $ n = sizeof ( $ arr1 ) ; $ m = sizeof ( $ arr2 ) ; echo \" Length \u2581 of \u2581 LCIS \u2581 is \u2581 \" , LCIS ( $ arr1 , $ n , $ arr2 , $ m ) ; ? >"}
{"text":"Function to return the maximized value of A","code":"< ? php function maxValue ( $ a , $ b ) {"}
{"text":"Sort digits in ascending order","code":"sort ( $ b ) ; $ n = sizeof ( $ a ) ; $ m = sizeof ( $ b ) ;"}
{"text":"j points to largest digit in B","code":"$ j = $ m - 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) {"}
{"text":"If all the digits of b have been used","code":"if ( $ j < 0 ) break ; if ( $ b [ $ j ] > $ a [ $ i ] ) { $ a [ $ i ] = $ b [ $ j ] ;"}
{"text":"Current digit has been used","code":"$ j -- ; } }"}
{"text":"Convert array into string","code":"$ a = implode ( \" \" , $ a ) ;"}
{"text":"Return the maximized value","code":"return $ a ; }"}
{"text":"Driver code","code":"# convert  string into array NEW_LINE $ a = str_split ( \"1234\" ) ; $ b = str_split ( \"4321\" ) ; echo maxValue ( $ a , $ b ) ; ? >"}
{"text":"Function to check if all of the digits in a number and it 's product with q are unequal or not","code":"< ? php function checkIfUnequal ( $ n , $ q ) {"}
{"text":"convert first number into string","code":"$ s1 = strval ( $ n ) ; $ a = array_fill ( 0 , 26 , NULL ) ;"}
{"text":"Insert elements from 1 st number to hash","code":"for ( $ i = 0 ; $ i < strlen ( $ s1 ) ; $ i ++ ) $ a [ ord ( $ s1 [ $ i ] ) - ord ( '0' ) ] ++ ;"}
{"text":"Calculate corresponding product","code":"$ prod = $ n * $ q ;"}
{"text":"Convert the product to string","code":"$ s2 = strval ( $ prod ) ;"}
{"text":"Using the hash check if any digit of product matches with the digits of input number","code":"for ( $ i = 0 ; $ i < strlen ( $ s2 ) ; $ i ++ ) {"}
{"text":"If yes , return false","code":"if ( $ a [ ord ( $ s2 [ $ i ] ) - ord ( '0' ) ] ) return false ; }"}
{"text":"Else , return true","code":"return true ; }"}
{"text":"Function to count numbers in the range [ l , r ] such that all of the digits of the number and it 's product with q are unequal","code":"function countInRange ( $ l , $ r , $ q ) { $ count = 0 ; for ( $ i = $ l ; $ i <= $ r ; $ i ++ ) {"}
{"text":"check for every number between l and r","code":"if ( checkIfUnequal ( $ i , $ q ) ) $ count ++ ; } return $ count ; }"}
{"text":"Driver Code","code":"$ l = 10 ; $ r = 12 ; $ q = 2 ;"}
{"text":"Function call","code":"echo countInRange ( $ l , $ r , $ q ) ; ? >"}
{"text":"function to check the binary string","code":"< ? php function is_possible ( $ s ) {"}
{"text":"length of string","code":"$ l = strlen ( $ s ) ; $ one = 0 ; $ zero = 0 ; for ( $ i = 0 ; $ i < $ l ; $ i ++ ) {"}
{"text":"count zero 's","code":"if ( $ s [ $ i ] == '0' ) $ zero ++ ;"}
{"text":"count one 's","code":"else $ one ++ ; }"}
{"text":"if length is even","code":"if ( $ l % 2 == 0 ) return ( $ one == $ zero ) ;"}
{"text":"if length is odd","code":"else return ( abs ( $ one - $ zero ) == 1 ) ; }"}
{"text":"Driver code","code":"$ s = \"100110\" ; if ( is_possible ( $ s ) ) echo ( \" Yes \" ) ; else echo ( \" No \" ) ; ? >"}
{"text":"Function to remove spaces and convert into camel case","code":"< ? php function convert ( $ s ) { $ n = strlen ( $ s ) ; $ s [ 0 ] = strtolower ( $ s [ 0 ] ) ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) {"}
{"text":"check for spaces in the sentence","code":"if ( $ s [ $ i ] == ' \u2581 ' && $ i < $ n ) {"}
{"text":"conversion into upper case","code":"$ s [ $ i + 1 ] = strtolower ( $ s [ $ i + 1 ] ) ; $ i ++ ; }"}
{"text":"If not space , copy character","code":"else $ s [ $ i ] = strtoupper ( $ s [ $ i ] ) ; }"}
{"text":"return string to main","code":"return $ s ; }"}
{"text":"Driver Code","code":"$ str = \" I \u2581 get \u2581 intern \u2581 at \u2581 geeksforgeeks \" ; echo ( convert ( $ str ) ) ; ? >"}
{"text":"Function to change character 's case","code":"< ? php function change_case ( $ a ) { $ l = strlen ( $ a ) ; for ( $ i = 0 ; $ i < $ l ; $ i ++ ) {"}
{"text":"If character is lowercase change to uppercase","code":"if ( $ a [ $ i ] >= ' a ' && $ a [ $ i ] <= ' z ' ) $ a [ $ i ] = chr ( 65 + ( ord ( $ a [ $ i ] ) - ord ( ' a ' ) ) ) ;"}
{"text":"If character is uppercase change to lowercase","code":"else if ( $ a [ $ i ] >= ' A ' && $ a [ $ i ] <= ' Z ' ) $ a [ $ i ] = chr ( 97 + ( ord ( $ a [ $ i ] ) - ord ( ' a ' ) ) ) ; } return $ a ; }"}
{"text":"Function to delete vowels","code":"function delete_vowels ( $ a ) { $ temp = \" \" ; $ l = strlen ( $ a ) ; for ( $ i = 0 ; $ i < $ l ; $ i ++ ) {"}
{"text":"If character is consonant","code":"if ( $ a [ $ i ] != ' a ' && $ a [ $ i ] != ' e ' && $ a [ $ i ] != ' i ' && $ a [ $ i ] != ' o ' && $ a [ $ i ] != ' u ' && $ a [ $ i ] != ' A ' && $ a [ $ i ] != ' E ' && $ a [ $ i ] != ' O ' && $ a [ $ i ] != ' U ' && $ a [ $ i ] != ' I ' ) $ temp = $ temp . $ a [ $ i ] ; } return $ temp ; }"}
{"text":"Function to insert \" # \"","code":"function insert_hash ( $ a ) { $ temp = \" \" ; $ l = strlen ( $ a ) ; for ( $ i = 0 ; $ i < $ l ; $ i ++ ) {"}
{"text":"If character is not special","code":"if ( ( $ a [ $ i ] >= ' a ' && $ a [ $ i ] <= ' z ' ) || ( $ a [ $ i ] >= ' A ' && $ a [ $ i ] <= ' Z ' ) ) $ temp = $ temp . ' # ' . $ a [ $ i ] ; else $ temp = $ temp . $ a [ $ i ] ; } return $ temp ; }"}
{"text":"Function to transform string","code":"function transformSting ( $ a ) { $ b = delete_vowels ( $ a ) ; $ c = change_case ( $ b ) ; $ d = insert_hash ( $ c ) ; echo ( $ d ) ; }"}
{"text":"Driver Code","code":"$ a = \" SunshinE ! ! \" ;"}
{"text":"Calling function","code":"transformSting ( $ a ) ; ? >"}
{"text":"PHP program to find n - th number containing only 3 and 5.","code":"< ? php function findNthNo ( $ n ) { $ res = \" \" ; while ( $ n >= 1 ) {"}
{"text":"If n is odd , append 3 and move to parent","code":"if ( $ n & 1 ) { $ res = $ res + \"3\" ; $ n = ( $ n - 1 ) \/ 2 ; }"}
{"text":"If n is even , append 5 and move to parent","code":"else { $ res = $ res . \"5\" ; $ n = ( $ n - 2 ) \/ 2 ; } }"}
{"text":"Reverse res and return .","code":"$ res = strrev ( $ res ) ; return $ res ; }"}
{"text":"Driver code","code":"$ n = 5 ; echo findNthNo ( $ n ) ; ? >"}
{"text":"function to find the nth Non - Square Number","code":"< ? php function findNthNonSquare ( $ n ) {"}
{"text":"conversion from int to long double is necessary in order to preserve decimal places after square root .","code":"$ x = $ n ;"}
{"text":"calculating the result","code":"$ ans = $ x + floor ( 0.5 + sqrt ( $ x ) ) ; return ( int ) $ ans ; }"}
{"text":"initializing the term number","code":"$ n = 16 ;"}
{"text":"Print the result","code":"echo \" The \u2581 \" . $ n . \" th \u2581 Non - Square \u2581 number \u2581 is \u2581 \" ; echo findNthNonSquare ( $ n ) ;"}
{"text":"function to calculate series sum","code":"< ? php function seiresSum ( $ n , $ a ) { return $ n * ( $ a [ 0 ] * $ a [ 0 ] - $ a [ 2 * $ n - 1 ] * $ a [ 2 * $ n - 1 ] ) \/ ( 2 * $ n - 1 ) ; }"}
{"text":"Driver code","code":"$ n = 2 ; $ a = array ( 1 , 2 , 3 , 4 ) ; echo seiresSum ( $ n , $ a ) ; ? >"}
{"text":"Function for checking if digit k is in n or not","code":"< ? php function checkdigit ( $ n , $ k ) { while ( $ n ) {"}
{"text":"finding remainder","code":"$ rem = $ n % 10 ;"}
{"text":"if digit found","code":"if ( $ rem == $ k ) return 1 ; $ n = $ n \/ 10 ; } return 0 ; }"}
{"text":"Function for finding nth number","code":"function findNthNumber ( $ n , $ k ) {"}
{"text":"since k is the first which satisfy the criteria , so consider it in count making count = 1 and starting from i = k + 1","code":"for ( $ i = $ k + 1 , $ count = 1 ; $ count < $ n ; $ i ++ ) {"}
{"text":"checking that the number contain k digit or divisible by k","code":"if ( checkdigit ( $ i , $ k ) || ( $ i % $ k == 0 ) ) $ count ++ ; if ( $ count == $ n ) return $ i ; } return -1 ; }"}
{"text":"Driver code","code":"$ n = 10 ; $ k = 2 ; echo findNthNumber ( $ n , $ k ) ; ? >"}
{"text":"PHP program to find middle of three distinct numbers","code":"< ? php function middleOfThree ( $ a , $ b , $ c ) {"}
{"text":"Function to find the middle of three number","code":"function middleOfThree ( $ a , $ b , $ c ) {"}
{"text":"Checking for b","code":"if ( ( $ a < $ b && $ b < $ c ) or ( $ c < $ b && $ b < $ a ) ) return $ b ;"}
{"text":"Checking for a","code":"else if ( ( $ b < $ a and $ a < $ c ) or ( $ c < $ a and $ a < $ b ) ) return $ a ; else return $ c ; }"}
{"text":"Driver Code","code":"$ a = 20 ; $ b = 30 ; $ c = 40 ; echo middleOfThree ( $ a , $ b , $ c ) ; ? >"}
{"text":"A Dynamic Programming based solution to find min cost to reach station N - 1 from station 0.","code":"< ? php $ INF = PHP_INT_MAX ; $ N = 4 ;"}
{"text":"This function returns the smallest possible cost to reach station N - 1 from station 0.","code":"function minCost ( $ cost ) { global $ INF ; global $ N ;"}
{"text":"dist [ i ] stores minimum cost to reach station i from station 0.","code":"$ dist [ $ N ] = array ( ) ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) $ dist [ $ i ] = $ INF ; $ dist [ 0 ] = 0 ;"}
{"text":"Go through every station and check if using it as an intermediate station gives better path","code":"for ( $ i = 0 ; $ i < $ N ; $ i ++ ) for ( $ j = $ i + 1 ; $ j < $ N ; $ j ++ ) if ( $ dist [ $ j ] > $ dist [ $ i ] + $ cost [ $ i ] [ $ j ] ) $ dist [ $ j ] = $ dist [ $ i ] + $ cost [ $ i ] [ $ j ] ; return $ dist [ $ N - 1 ] ; }"}
{"text":"Driver program to test above function","code":"$ cost = array ( array ( 0 , 15 , 80 , 90 ) , array ( INF , 0 , 40 , 50 ) , array ( INF , INF , 0 , 70 ) , array ( INF , INF , INF , 0 ) ) ; echo \" The \u2581 Minimum \u2581 cost \u2581 to \u2581 reach \u2581 station \u2581 \" , $ N , \" \u2581 is \u2581 \" , minCost ( $ cost ) ; ? >"}
{"text":"Return the Number of ways from a node to make a loop of size K in undirected complete connected graph of N nodes","code":"< ? php function numOfways ( $ n , $ k ) { $ p = 1 ; if ( $ k % 2 ) $ p = -1 ; return ( pow ( $ n - 1 , $ k ) + $ p * ( $ n - 1 ) ) \/ $ n ; }"}
{"text":"Driver Code","code":"$ n = 4 ; $ k = 2 ; echo numOfways ( $ n , $ k ) ; ? >"}
{"text":"Function to find the length of the chord","code":"< ? php function length_of_chord ( $ r , $ x ) { echo \" The \u2581 length \u2581 of \u2581 the \u2581 chord \" , \" \u2581 of \u2581 the \u2581 circle \u2581 is \u2581 \" , 2 * $ r * sin ( $ x * ( 3.14 \/ 180 ) ) ; }"}
{"text":"Driver code","code":"$ r = 4 ; $ x = 63 ; length_of_chord ( $ r , $ x ) ; ? >"}
{"text":"Function to find the area of the square","code":"< ? php function area ( $ a ) {"}
{"text":"a cannot be negative","code":"if ( $ a < 0 ) return -1 ;"}
{"text":"area of the square","code":"$ area = sqrt ( $ a ) \/ 6 ; return $ area ; }"}
{"text":"Driver code","code":"$ a = 10 ; echo area ( $ a ) ; ? >"}
{"text":"Function to find the length","code":"< ? php function longestRodInCuboid ( $ length , $ breadth , $ height ) { $ result ; $ temp ;"}
{"text":"temporary variable to hold the intermediate result","code":"$ temp = $ length * $ length + $ breadth * $ breadth + $ height * $ height ;"}
{"text":"length of longest rod is calculated using square root function","code":"$ result = sqrt ( $ temp ) ; return $ result ; }"}
{"text":"Driver code","code":"$ length = 12 ; $ breadth = 9 ; $ height = 8 ;"}
{"text":"calling longestRodInCuboid ( ) function to get the length of longest rod","code":"echo longestRodInCuboid ( $ length , $ breadth , $ height ) ; ? >"}
{"text":"function to Check whether a given point lies inside or on the rectangle or not","code":"< ? php function LiesInsieRectangle ( $ a , $ b , $ x , $ y ) { if ( $ x - $ y - $ b <= 0 && $ x - $ y + $ b >= 0 && $ x + $ y - 2 * $ a + $ b <= 0 && $ x + $ y - $ b >= 0 ) return true ; return false ; }"}
{"text":"Driver code","code":"$ a = 7 ; $ b = 2 ; $ x = 4 ; $ y = 5 ; if ( LiesInsieRectangle ( $ a , $ b , $ x , $ y ) ) echo \" Given \u2581 point \u2581 lies \u2581 \" . \" inside \u2581 the \u2581 rectangle \" ; else echo \" Given \u2581 point \u2581 does \u2581 not \" . \" \u2581 lie \u2581 on \u2581 the \u2581 rectangle \" ; ? >"}
{"text":"Return the maximum volume .","code":"< ? php function maxvolume ( $ s ) { $ maxvalue = 0 ;"}
{"text":"for length","code":"for ( $ i = 1 ; $ i <= $ s - 2 ; $ i ++ ) {"}
{"text":"for breadth","code":"for ( $ j = 1 ; $ j <= $ s - 1 ; $ j ++ ) {"}
{"text":"for height","code":"$ k = $ s - $ i - $ j ;"}
{"text":"calculating maximum volume .","code":"$ maxvalue = max ( $ maxvalue , $ i * $ j * $ k ) ; } } return $ maxvalue ; }"}
{"text":"Driver Code","code":"$ s = 8 ; echo ( maxvolume ( $ s ) ) ; ? >"}
{"text":"Return the maximum volume .","code":"< ? php function maxvolume ( $ s ) {"}
{"text":"finding length","code":"$ length = ( int ) ( $ s \/ 3 ) ; $ s -= $ length ;"}
{"text":"finding breadth","code":"$ breadth = ( int ) ( $ s \/ 2 ) ;"}
{"text":"finding height","code":"$ height = $ s - $ breadth ; return $ length * $ breadth * $ height ; }"}
{"text":"Driven Code","code":"$ s = 8 ; echo ( maxvolume ( $ s ) ) ; ? >"}
{"text":"function for calculating area of the hexagon .","code":"< ? php function hexagonArea ( $ s ) { return ( ( 3 * sqrt ( 3 ) * ( $ s * $ s ) ) \/ 2 ) ; }"}
{"text":"Length of a side","code":"$ s = 4 ; echo ( \" Area \u2581 : \u2581 \" ) ; echo ( hexagonArea ( $ s ) ) ; ? >"}
{"text":"function for finding max squares","code":"< ? php function maxSquare ( $ b , $ m ) {"}
{"text":"return in O ( 1 ) with derived formula","code":"return ( $ b \/ $ m - 1 ) * ( $ b \/ $ m ) \/ 2 ; }"}
{"text":"Driver Code","code":"$ b = 10 ; $ m = 2 ; echo maxSquare ( $ b , $ m ) ;"}
{"text":"Prints three sides of a right triangle from given area and hypotenuse if triangle is possible , else prints - 1.","code":"< ? php function findRightAngle ( $ A , $ H ) {"}
{"text":"Descriminant of the equation","code":"$ D = pow ( $ H , 4 ) - 16 * $ A * $ A ; if ( $ D >= 0 ) {"}
{"text":"applying the linear equation formula to find both the roots","code":"$ root1 = ( $ H * $ H + sqrt ( $ D ) ) \/ 2 ; $ root2 = ( $ H * $ H - sqrt ( $ D ) ) \/ 2 ; $ a = sqrt ( $ root1 ) ; $ b = sqrt ( $ root2 ) ; if ( $ b >= $ a ) echo $ a , \" \u2581 \" , $ b , \" \u2581 \" , $ H ; else echo $ b , \" \u2581 \" , $ a , \" \u2581 \" , $ H ; } else echo \" - 1\" ; }"}
{"text":"Driver code","code":"findRightAngle ( 6 , 5 ) ;"}
{"text":"PHP program to count number of 2 x 2 squares in a right isosceles triangle","code":"< ? php function numberOfSquares ( $ base ) {"}
{"text":"removing the extra part we would always need","code":"$ base = ( $ base - 2 ) ;"}
{"text":"Since each square has base of length of 2","code":"$ base = intdiv ( $ base , 2 ) ; return $ base * ( $ base + 1 ) \/ 2 ; }"}
{"text":"Driver code","code":"$ base = 8 ; echo numberOfSquares ( $ base ) ; ? >"}
{"text":"function to find fibonacci number","code":"< ? php function fib ( $ n ) { if ( $ n <= 1 ) return $ n ; return fib ( $ n - 1 ) + fib ( $ n - 2 ) ; }"}
{"text":"function for finding number of vertices in fibonacci cube graph","code":"function findVertices ( $ n ) {"}
{"text":"return fibonacci number for f ( n + 2 )","code":"return fib ( $ n + 2 ) ; }"}
{"text":"Driver Code","code":"$ n = 3 ; echo findVertices ( $ n ) ; ? >"}
{"text":"PHP implementation to sort the rows of matrix in ascending order followed by sorting the columns in descending order","code":"< ? php $ MAX_SIZE = 10 ;"}
{"text":"function to sort each row of the matrix according to the order specified by ascending .","code":"function sortByRow ( & $ mat , $ n , $ ascending ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ ascending ) sort ( $ mat [ $ i ] ) ; else rsort ( $ mat [ $ i ] ) ; } }"}
{"text":"function to find transpose of the matrix","code":"function transpose ( & $ mat , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) {"}
{"text":"swapping element at index ( i , j ) by element at index ( j , i )","code":"$ temp = $ mat [ $ i ] [ $ j ] ; $ mat [ $ i ] [ $ j ] = $ mat [ $ j ] [ $ i ] ; $ mat [ $ j ] [ $ i ] = $ temp ; } } }"}
{"text":"function to sort the matrix row - wise and column - wise","code":"function sortMatRowAndColWise ( & $ mat , $ n ) {"}
{"text":"sort rows of mat [ ] [ ]","code":"sortByRow ( $ mat , $ n , true ) ;"}
{"text":"get transpose of mat [ ] [ ]","code":"transpose ( $ mat , $ n ) ;"}
{"text":"again sort rows of mat [ ] [ ] in descending order .","code":"sortByRow ( $ mat , $ n , false ) ;"}
{"text":"again get transpose of mat [ ] [ ]","code":"transpose ( $ mat , $ n ) ; }"}
{"text":"function to print the matrix","code":"function printMat ( & $ mat , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) echo $ mat [ $ i ] [ $ j ] . \" \u2581 \" ; echo \" STRNEWLINE \" ; } }"}
{"text":"Driver Code","code":"$ n = 3 ; $ mat = array ( array ( 3 , 2 , 1 ) , array ( 9 , 8 , 7 ) , array ( 6 , 5 , 4 ) ) ; echo \" Original \u2581 Matrix : STRNEWLINE \" ; printMat ( $ mat , $ n ) ; sortMatRowAndColWise ( $ mat , $ n ) ; echo \" Matrix After Sorting : \" ; printMat ( $ mat , $ n ) ; ? >"}
{"text":"PHP implementation to sort the matrix row - wise and column - wise","code":"< ? php $ MAX_SIZE = 10 ;"}
{"text":"function to sort each row of the matrix","code":"function sortByRow ( & $ mat , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ )"}
{"text":"sorting row number ' i '","code":"sort ( $ mat [ $ i ] ) ; }"}
{"text":"function to find transpose of the matrix","code":"function transpose ( & $ mat , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) {"}
{"text":"swapping element at index ( i , j ) by element at index ( j , i )","code":"$ t = $ mat [ $ i ] [ $ j ] ; $ mat [ $ i ] [ $ j ] = $ mat [ $ j ] [ $ i ] ; $ mat [ $ j ] [ $ i ] = $ t ; } } }"}
{"text":"function to sort the matrix row - wise and column - wise","code":"function sortMatRowAndColWise ( & $ mat , $ n ) {"}
{"text":"sort rows of mat [ ] [ ]","code":"sortByRow ( $ mat , $ n ) ;"}
{"text":"get transpose of mat [ ] [ ]","code":"transpose ( $ mat , $ n ) ;"}
{"text":"again sort rows of mat [ ] [ ]","code":"sortByRow ( $ mat , $ n ) ;"}
{"text":"again get transpose of mat [ ] [ ]","code":"transpose ( $ mat , $ n ) ; }"}
{"text":"function to print the matrix","code":"function printMat ( & $ mat , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) echo $ mat [ $ i ] [ $ j ] . \" \u2581 \" ; echo \" STRNEWLINE \" ; } }"}
{"text":"Driver Code","code":"$ mat = array ( array ( 4 , 1 , 3 ) , array ( 9 , 6 , 8 ) , array ( 5 , 2 , 7 ) ) ; $ n = 3 ; echo \" Original \u2581 Matrix : STRNEWLINE \" ; printMat ( $ mat , $ n ) ; sortMatRowAndColWise ( $ mat , $ n ) ; echo \" Matrix After Sorting : \" ; printMat ( $ mat , $ n ) ; ? >"}
{"text":"Function for calculating Magic square","code":"< ? php function doublyEven ( $ n ) { $ arr = array_fill ( 0 , $ n , array_fill ( 0 , $ n , 0 ) ) ;"}
{"text":"filling matrix with its count value starting from 1 ;","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ n ; $ j ++ ) $ arr [ $ i ] [ $ j ] = ( $ n * $ i ) + $ j + 1 ;"}
{"text":"change value of Array elements at fix location as per rule ( n * n + 1 ) - arr [ i ] [ j ] Top Left corner of Matrix ( order ( n \/ 4 ) * ( n \/ 4 ) )","code":"for ( $ i = 0 ; $ i < $ n \/ 4 ; $ i ++ ) for ( $ j = 0 ; $ j < $ n \/ 4 ; $ j ++ ) $ arr [ $ i ] [ $ j ] = ( $ n * $ n + 1 ) - $ arr [ $ i ] [ $ j ] ;"}
{"text":"Top Right corner of Matrix ( order ( n \/ 4 ) * ( n \/ 4 ) )","code":"for ( $ i = 0 ; $ i < $ n \/ 4 ; $ i ++ ) for ( $ j = 3 * ( $ n \/ 4 ) ; $ j < $ n ; $ j ++ ) $ arr [ $ i ] [ $ j ] = ( $ n * $ n + 1 ) - $ arr [ $ i ] [ $ j ] ;"}
{"text":"Bottom Left corner of Matrix ( order ( n \/ 4 ) * ( n \/ 4 ) )","code":"for ( $ i = 3 * $ n \/ 4 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ n \/ 4 ; $ j ++ ) $ arr [ $ i ] [ $ j ] = ( $ n * $ n + 1 ) - $ arr [ $ i ] [ $ j ] ;"}
{"text":"Bottom Right corner of Matrix ( order ( n \/ 4 ) * ( n \/ 4 ) )","code":"for ( $ i = 3 * $ n \/ 4 ; $ i < $ n ; $ i ++ ) for ( $ j = 3 * $ n \/ 4 ; $ j < $ n ; $ j ++ ) $ arr [ $ i ] [ $ j ] = ( $ n * $ n + 1 ) - $ arr [ $ i ] [ $ j ] ;"}
{"text":"Centre of Matrix ( order ( n \/ 2 ) * ( n \/ 2 ) )","code":"for ( $ i = $ n \/ 4 ; $ i < 3 * $ n \/ 4 ; $ i ++ ) for ( $ j = $ n \/ 4 ; $ j < 3 * $ n \/ 4 ; $ j ++ ) $ arr [ $ i ] [ $ j ] = ( $ n * $ n + 1 ) - $ arr [ $ i ] [ $ j ] ;"}
{"text":"Printing the magic - square","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) echo $ arr [ $ i ] [ $ j ] . \" \u2581 \" ; echo \" STRNEWLINE \" ; } }"}
{"text":"Driver Code","code":"$ n = 8 ;"}
{"text":"Function call","code":"doublyEven ( $ n ) ; ? >"}
{"text":"rowa and cola are no of rows and columns of matrix A rowb and colb are no of rows and columns of matrix B","code":"< ? php $ cola = 2 ; $ rowa = 3 ; $ colb = 3 ; $ rowb = 2 ;"}
{"text":"Function to computes the Kronecker Product of two matrices","code":"function Kroneckerproduct ( $ A , $ B ) { global $ cola ; global $ rowa ; global $ colb ; global $ rowb ; $ C ;"}
{"text":"i loops till rowa","code":"for ( $ i = 0 ; $ i < $ rowa ; $ i ++ ) {"}
{"text":"k loops till rowb","code":"for ( $ k = 0 ; $ k < $ rowb ; $ k ++ ) {"}
{"text":"j loops till cola","code":"for ( $ j = 0 ; $ j < $ cola ; $ j ++ ) {"}
{"text":"l loops till colb","code":"for ( $ l = 0 ; $ l < $ colb ; $ l ++ ) {"}
{"text":"Each element of matrix A is multiplied by whole Matrix B resp and stored as Matrix C","code":"$ C [ $ i + $ l + 1 ] [ $ j + $ k + 1 ] = $ A [ $ i ] [ $ j ] * $ B [ $ k ] [ $ l ] ; echo ( $ C [ $ i + $ l + 1 ] [ $ j + $ k + 1 ] ) , \" TABSYMBOL \" ; } } echo \" \" } } }"}
{"text":"Driver Code","code":"$ A = array ( array ( 1 , 2 ) , array ( 3 , 4 ) , array ( 1 , 0 ) ) ; $ B = array ( array ( 0 , 5 , 2 ) , array ( 6 , 7 , 3 ) ) ; Kroneckerproduct ( $ A , $ B ) ; ? >"}
{"text":"PHP Program to check lower triangular matrix .","code":"< ? php $ N = 4 ;"}
{"text":"Function to check matrix is in lower triangular form or not .","code":"function isLowerTriangularMatrix ( $ mat ) { global $ N ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) for ( $ j = $ i + 1 ; $ j < $ N ; $ j ++ ) if ( $ mat [ $ i ] [ $ j ] != 0 ) return false ; return true ; }"}
{"text":"Driver Code","code":"$ mat = array ( array ( 1 , 0 , 0 , 0 ) , array ( 1 , 4 , 0 , 0 ) , array ( 4 , 6 , 2 , 0 ) , array ( 0 , 4 , 7 , 6 ) ) ;"}
{"text":"Function call","code":"if ( isLowerTriangularMatrix ( $ mat ) ) echo ( \" Yes \" ) ; else echo ( \" No \" ) ; ? >"}
{"text":"PHP Program to check upper triangular matrix .","code":"< ? php $ N = 4 ;"}
{"text":"Function to check matrix is in upper triangular form or not .","code":"function isUpperTriangularMatrix ( $ mat ) { global $ N ; for ( $ i = 1 ; $ i < $ N ; $ i ++ ) for ( $ j = 0 ; $ j < $ i ; $ j ++ ) if ( $ mat [ $ i ] [ $ j ] != 0 ) return false ; return true ; }"}
{"text":"Driver Code","code":"$ mat = array ( array ( 1 , 3 , 5 , 3 ) , array ( 0 , 4 , 6 , 2 ) , array ( 0 , 0 , 2 , 5 ) , array ( 0 , 0 , 0 , 6 ) ) ; if ( isUpperTriangularMatrix ( $ mat ) ) echo \" Yes \" ; else echo \" No \" ; ? >"}
{"text":"no of columns","code":"< ? php $ m = 3 ;"}
{"text":"no of rows","code":"$ n = 2 ;"}
{"text":"function to calculate the number of non empty sets of cell","code":"function countSets ( $ a ) { global $ m , $ n ;"}
{"text":"stores the final answer","code":"$ res = 0 ;"}
{"text":"traverses row - wise","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ u = 0 ; $ v = 0 ; for ( $ j = 0 ; $ j < $ m ; $ j ++ ) $ a [ $ i ] [ $ j ] ? $ u ++ : $ v ++ ; $ res += pow ( 2 , $ u ) - 1 + pow ( 2 , $ v ) - 1 ; }"}
{"text":"traverses column wise","code":"for ( $ i = 0 ; $ i < $ m ; $ i ++ ) { $ u = 0 ; $ v = 0 ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) $ a [ $ j ] [ $ i ] ? $ u ++ : $ v ++ ; $ res += pow ( 2 , $ u ) - 1 + pow ( 2 , $ v ) - 1 ; }"}
{"text":"at the end subtract n * m as no of single sets have been added twice .","code":"return $ res - ( $ n * $ m ) ; }"}
{"text":"Driver Code","code":"$ a = array ( array ( 1 , 0 , 1 ) , array ( 0 , 1 , 0 ) ) ; echo countSets ( $ a ) ; ? >"}
{"text":"Fills transpose of mat [ N ] [ N ] in tr [ N ] [ N ]","code":"< ? php for ( $ i = 0 ; $ i < $ N ; $ i ++ ) for ( $ j = 0 ; $ j < $ N ; $ j ++ ) if ( $ mat [ $ i ] [ $ j ] != $ tr [ $ i ] [ $ j ] ) return false ; return true ; }"}
{"text":"Returns true if mat [ N ] [ N ] is symmetric , else false","code":"function isSymmetric ( $ mat , $ N ) { $ tr = array ( array ( ) ) ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) for ( $ j = 0 ; $ j < $ N ; $ j ++ ) $ tr [ $ i ] [ $ j ] = $ mat [ $ j ] [ $ i ] ;"}
{"text":"Driver code","code":"$ mat = array ( array ( 1 , 3 , 5 ) , array ( 3 , 2 , 4 ) , array ( 5 , 4 , 1 ) ) ; if ( isSymmetric ( $ mat , 3 ) ) echo \" Yes \" ; else echo \" No \" ; ? >"}
{"text":"Efficient PHP code for check a matrix is symmetric or not .","code":"< ? php $ MAX = 100 ;"}
{"text":"Returns true if mat [ N ] [ N ] is symmetric , else false","code":"function isSymmetric ( $ mat , $ N ) { for ( $ i = 0 ; $ i < $ N ; $ i ++ ) for ( $ j = 0 ; $ j < $ N ; $ j ++ ) if ( $ mat [ $ i ] [ $ j ] != $ mat [ $ j ] [ $ i ] ) return false ; return true ; }"}
{"text":"Driver code","code":"$ mat = array ( array ( 1 , 3 , 5 ) , array ( 3 , 2 , 4 ) , array ( 5 , 4 , 1 ) ) ; if ( isSymmetric ( $ mat , 3 ) ) echo ( \" Yes \" ) ; else echo ( \" No \" ) ; ? >"}
{"text":"Size of given matrix","code":"< ? php $ MAX = 100 ;"}
{"text":"Returns Normal of a matrix of size n x n","code":"function findNormal ( $ mat , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ n ; $ j ++ ) $ sum += $ mat [ $ i ] [ $ j ] * $ mat [ $ i ] [ $ j ] ; return floor ( sqrt ( $ sum ) ) ; }"}
{"text":"Returns trace of a matrix of size n x n","code":"function findTrace ( $ mat , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += $ mat [ $ i ] [ $ i ] ; return $ sum ; }"}
{"text":"Driver Code","code":"$ mat = array ( array ( 1 , 1 , 1 , 1 , 1 ) , array ( 2 , 2 , 2 , 2 , 2 ) , array ( 3 , 3 , 3 , 3 , 3 ) , array ( 4 , 4 , 4 , 4 , 4 ) , array ( 5 , 5 , 5 , 5 , 5 ) ) ; echo \" Trace \u2581 of \u2581 Matrix \u2581 = \u2581 \" , findTrace ( $ mat , 5 ) , \" STRNEWLINE \" ; echo \" Normal \u2581 of \u2581 Matrix \u2581 = \u2581 \" , findNormal ( $ mat , 5 ) ; ? >"}
{"text":"Function for maximum determinant","code":"< ? php function maxDet ( $ n ) { return ( 2 * $ n * $ n * $ n ) ; }"}
{"text":"Function to print resulatant matrix","code":"function resMatrix ( $ n ) { for ( $ i = 0 ; $ i < 3 ; $ i ++ ) { for ( $ j = 0 ; $ j < 3 ; $ j ++ ) {"}
{"text":"three position where 0 appears","code":"if ( $ i == 0 && $ j == 2 ) echo \"0 \u2581 \" ; else if ( $ i == 1 && $ j == 0 ) echo \"0 \u2581 \" ; else if ( $ i == 2 && $ j == 1 ) echo \"0 \u2581 \" ;"}
{"text":"position where n appears","code":"else echo $ n , \" \" ; } echo \" \" } }"}
{"text":"Driver code","code":"$ n = 15 ; echo \" Maximum \u2581 Determinant \u2581 = \u2581 \" , maxDet ( $ n ) ; echo \" Resultant Matrix : \" resMatrix ( $ n ) ; ? >"}
{"text":"PHP implementation of Naive method to count of negative numbers in M [ n ] [ m ]","code":"< ? php function countNegative ( $ M , $ n , $ m ) { $ count = 0 ;"}
{"text":"Follow the path shown using arrows above","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ m ; $ j ++ ) { if ( $ M [ $ i ] [ $ j ] < 0 ) $ count += 1 ;"}
{"text":"no more negative numbers in this row","code":"else break ; } } return $ count ; }"}
{"text":"Driver Code","code":"$ M = array ( array ( -3 , -2 , -1 , 1 ) , array ( -2 , 2 , 3 , 4 ) , array ( 4 , 5 , 7 , 8 ) ) ; echo countNegative ( $ M , 3 , 4 ) ; ? >"}
{"text":"Function to count negative number","code":"< ? php function countNegative ( $ M , $ n , $ m ) {"}
{"text":"initialize result","code":"$ count = 0 ;"}
{"text":"Start with top right corner","code":"$ i = 0 ; $ j = $ m - 1 ;"}
{"text":"Follow the path shown using arrows above","code":"while ( $ j >= 0 and $ i < $ n ) { if ( $ M [ $ i ] [ $ j ] < 0 ) {"}
{"text":"j is the index of the last negative number in this row . So there must be ( j + 1 )","code":"$ count += $ j + 1 ;"}
{"text":"negative numbers in this row .","code":"$ i += 1 ; }"}
{"text":"move to the left and see if we can find a negative number there","code":"else $ j -= 1 ; } return $ count ; }"}
{"text":"Driver Code","code":"$ M = array ( array ( -3 , -2 , -1 , 1 ) , array ( -2 , 2 , 3 , 4 ) , array ( 4 , 5 , 7 , 8 ) ) ; echo countNegative ( $ M , 3 , 4 ) ; return 0 ; ? >"}
{"text":"A Naive method to find maximum value of $mat [ d ] [ e ] - ma [ a ] [ b ] such that $d > $a and $e > $b","code":"< ? php $ N = 5 ;"}
{"text":"The function returns maximum value A ( d , e ) - A ( a , b ) over all choices of indexes such that both $d > $a and $e > $b .","code":"function findMaxValue ( & $ mat ) { global $ N ;"}
{"text":"stores maximum value","code":"$ maxValue = PHP_INT_MIN ;"}
{"text":"Consider all possible pairs $mat [ $a ] [ $b ] and $mat [ $d ] [ $e ]","code":"for ( $ a = 0 ; $ a < $ N - 1 ; $ a ++ ) for ( $ b = 0 ; $ b < $ N - 1 ; $ b ++ ) for ( $ d = $ a + 1 ; $ d < $ N ; $ d ++ ) for ( $ e = $ b + 1 ; $ e < $ N ; $ e ++ ) if ( $ maxValue < ( $ mat [ $ d ] [ $ e ] - $ mat [ $ a ] [ $ b ] ) ) $ maxValue = $ mat [ $ d ] [ $ e ] - $ mat [ $ a ] [ $ b ] ; return $ maxValue ; }"}
{"text":"Driver Code","code":"$ mat = array ( array ( 1 , 2 , -1 , -4 , -20 ) , array ( -8 , -3 , 4 , 2 , 1 ) , array ( 3 , 8 , 6 , 1 , 3 ) , array ( -4 , -1 , 1 , 7 , -6 ) , array ( 0 , -4 , 10 , -5 , 1 ) ) ; echo \" Maximum \u2581 Value \u2581 is \u2581 \" . findMaxValue ( $ mat ) ; ? >"}
{"text":"An efficient method to find maximum value of mat [ d ] - ma [ a ] [ b ] such that c > a and d > b","code":"< ? php $ N = 5 ;"}
{"text":"The function returns maximum value A ( c , d ) - A ( a , b ) over all choices of indexes such that both c > a and d > b .","code":"function findMaxValue ( $ mat ) { global $ N ;"}
{"text":"stores maximum value","code":"$ maxValue = PHP_INT_MIN ;"}
{"text":"maxArr [ i ] [ j ] stores max of elements in matrix from ( i , j ) to ( N - 1 , N - 1 )","code":"$ maxArr [ $ N ] [ $ N ] = array ( ) ;"}
{"text":"last element of maxArr will be same 's as of  the input matrix","code":"$ maxArr [ $ N - 1 ] [ $ N - 1 ] = $ mat [ $ N - 1 ] [ $ N - 1 ] ;"}
{"text":"preprocess last row Initialize max","code":"$ maxv = $ mat [ $ N - 1 ] [ $ N - 1 ] ; for ( $ j = $ N - 2 ; $ j >= 0 ; $ j -- ) { if ( $ mat [ $ N - 1 ] [ $ j ] > $ maxv ) $ maxv = $ mat [ $ N - 1 ] [ $ j ] ; $ maxArr [ $ N - 1 ] [ $ j ] = $ maxv ; }"}
{"text":"preprocess last column Initialize max","code":"$ maxv = $ mat [ $ N - 1 ] [ $ N - 1 ] ; for ( $ i = $ N - 2 ; $ i >= 0 ; $ i -- ) { if ( $ mat [ $ i ] [ $ N - 1 ] > $ maxv ) $ maxv = $ mat [ $ i ] [ $ N - 1 ] ; $ maxArr [ $ i ] [ $ N - 1 ] = $ maxv ; }"}
{"text":"preprocess rest of the matrix from bottom","code":"for ( $ i = $ N - 2 ; $ i >= 0 ; $ i -- ) { for ( $ j = $ N - 2 ; $ j >= 0 ; $ j -- ) {"}
{"text":"Update maxValue","code":"if ( $ maxArr [ $ i + 1 ] [ $ j + 1 ] - $ mat [ $ i ] [ $ j ] > $ maxValue ) $ maxValue = $ maxArr [ $ i + 1 ] [ $ j + 1 ] - $ mat [ $ i ] [ $ j ] ;"}
{"text":"set maxArr ( i , j )","code":"$ maxArr [ $ i ] [ $ j ] = max ( $ mat [ $ i ] [ $ j ] , max ( $ maxArr [ $ i ] [ $ j + 1 ] , $ maxArr [ $ i + 1 ] [ $ j ] ) ) ; } } return $ maxValue ; }"}
{"text":"Driver Code","code":"$ mat = array ( array ( 1 , 2 , -1 , -4 , -20 ) , array ( -8 , -3 , 4 , 2 , 1 ) , array ( 3 , 8 , 6 , 1 , 3 ) , array ( -4 , -1 , 1 , 7 , -6 ) , array ( 0 , -4 , 10 , -5 , 1 ) ) ; echo \" Maximum \u2581 Value \u2581 is \u2581 \" . findMaxValue ( $ mat ) ; ? >"}
{"text":"size k x k Size of given matrix","code":"< ? php $ n = 5 ;"}
{"text":"function to find sum of all sub - squares of size k x k in a given square matrix of size n x n","code":"function printSumSimple ( $ mat , $ k ) { global $ n ;"}
{"text":"k must be smaller than or equal to n","code":"if ( $ k > $ n ) return ;"}
{"text":"row number of first cell in current sub - square of size k x k","code":"for ( $ i = 0 ; $ i < $ n - $ k + 1 ; $ i ++ ) {"}
{"text":"column of first cell in current sub - square of size k x k","code":"for ( $ j = 0 ; $ j < $ n - $ k + 1 ; $ j ++ ) {"}
{"text":"Calculate and print sum of current sub - square","code":"$ sum = 0 ; for ( $ p = $ i ; $ p < $ k + $ i ; $ p ++ ) for ( $ q = $ j ; $ q < $ k + $ j ; $ q ++ ) $ sum += $ mat [ $ p ] [ $ q ] ; echo $ sum , \" \" ; }"}
{"text":"Line separator for sub - squares starting with next row","code":"echo \" STRNEWLINE \" ; } }"}
{"text":"Driver Code","code":"$ mat = array ( array ( 1 , 1 , 1 , 1 , 1 ) , array ( 2 , 2 , 2 , 2 , 2 , ) , array ( 3 , 3 , 3 , 3 , 3 , ) , array ( 4 , 4 , 4 , 4 , 4 , ) , array ( 5 , 5 , 5 , 5 , 5 ) ) ; $ k = 3 ; printSumSimple ( $ mat , $ k ) ; ? >"}
{"text":"Size of given matrix","code":"< ? php $ n = 5 ;"}
{"text":"A O ( n ^ 2 ) function to find sum of all sub - squares of size k x k in a given square matrix of size n x n","code":"function printSumTricky ( $ mat , $ k ) { global $ n ;"}
{"text":"k must be smaller than or equal to n","code":"if ( $ k > $ n ) return ;"}
{"text":"1 : PREPROCESSING To store sums of all strips of size k x 1","code":"$ stripSum = array ( array ( ) ) ;"}
{"text":"Go column by column","code":"for ( $ j = 0 ; $ j < $ n ; $ j ++ ) {"}
{"text":"Calculate sum of first k x 1 rectangle in this column","code":"$ sum = 0 ; for ( $ i = 0 ; $ i < $ k ; $ i ++ ) $ sum += $ mat [ $ i ] [ $ j ] ; $ stripSum [ 0 ] [ $ j ] = $ sum ;"}
{"text":"Calculate sum of remaining rectangles","code":"for ( $ i = 1 ; $ i < $ n - $ k + 1 ; $ i ++ ) { $ sum += ( $ mat [ $ i + $ k - 1 ] [ $ j ] - $ mat [ $ i - 1 ] [ $ j ] ) ; $ stripSum [ $ i ] [ $ j ] = $ sum ; } }"}
{"text":"2 : CALCULATE SUM of Sub - Squares using stripSum [ ] [ ]","code":"for ( $ i = 0 ; $ i < $ n - $ k + 1 ; $ i ++ ) {"}
{"text":"Calculate and print sum of first subsquare in this row","code":"$ sum = 0 ; for ( $ j = 0 ; $ j < $ k ; $ j ++ ) $ sum += $ stripSum [ $ i ] [ $ j ] ; echo $ sum , \" \" ;"}
{"text":"Calculate sum of remaining squares in current row by removing the leftmost strip of previous sub - square and adding a new strip","code":"for ( $ j = 1 ; $ j < $ n - $ k + 1 ; $ j ++ ) { $ sum += ( $ stripSum [ $ i ] [ $ j + $ k - 1 ] - $ stripSum [ $ i ] [ $ j - 1 ] ) ; echo $ sum , \" \" ; } echo \" STRNEWLINE \" ; } }"}
{"text":"Driver Code","code":"$ mat = array ( array ( 1 , 1 , 1 , 1 , 1 ) , array ( 2 , 2 , 2 , 2 , 2 ) , array ( 3 , 3 , 3 , 3 , 3 ) , array ( 4 , 4 , 4 , 4 , 4 ) , array ( 5 , 5 , 5 , 5 , 5 ) ) ; $ k = 3 ; printSumTricky ( $ mat , $ k ) ; ? >"}
{"text":"PHP Program to find transpose of a matrix","code":"< ? php $ N = 4 ; $ M = 3 ;"}
{"text":"This function stores transpose of A [ ] [ ] in B [ ] [ ]","code":"function transpose ( & $ A , & $ B ) { for ( $ i = 0 ; $ i < $ N ; $ i ++ ) for ( $ j = 0 ; $ j < $ M ; $ j ++ ) $ B [ $ i ] [ $ j ] = $ A [ $ j ] [ $ i ] ; }"}
{"text":"Driver code","code":"$ A = array ( array ( 1 , 1 , 1 , 1 ) , array ( 2 , 2 , 2 , 2 ) , array ( 3 , 3 , 3 , 3 ) ) ; $ N = 4 ; $ M = 3 ; transpose ( $ A , $ B ) ; echo \" Result \u2581 matrix \u2581 is \u2581 STRNEWLINE \" ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { for ( $ j = 0 ; $ j < $ M ; $ j ++ ) { echo $ B [ $ i ] [ $ j ] ; echo \" \u2581 \" ; } echo \" STRNEWLINE \" ; } ? >"}
{"text":"PHP Program to find transpose of a matrix","code":"< ? php $ N = 4 ;"}
{"text":"Converts A [ ] [ ] to its transpose","code":"function transpose ( & $ A ) { for ( $ i = 0 ; $ i < $ N ; $ i ++ ) for ( $ j = $ i + 1 ; $ j < $ N ; $ j ++ ) { $ temp = $ A [ $ i ] [ $ j ] ; $ A [ $ i ] [ $ j ] = $ A [ $ j ] [ $ i ] ; $ A [ $ j ] [ $ i ] = $ temp ; } }"}
{"text":"Driver Code","code":"$ N = 4 ; $ A = array ( array ( 1 , 1 , 1 , 1 ) , array ( 2 , 2 , 2 , 2 ) , array ( 3 , 3 , 3 , 3 ) , array ( 4 , 4 , 4 , 4 ) ) ; transpose ( $ A ) ; echo \" Modified \u2581 matrix \u2581 is \u2581 \" . \" STRNEWLINE \" ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { for ( $ j = 0 ; $ j < $ N ; $ j ++ ) echo $ A [ $ i ] [ $ j ] . \" \u2581 \" ; echo \" STRNEWLINE \" ; } ? >"}
{"text":"A Naive Recursive PHP program to count paths with exactly ' k ' coins","code":"< ? php $ R = 3 ; $ C = 3 ;"}
{"text":"Recursive function to count paths with sum k from ( 0 , 0 ) to ( m , n )","code":"function pathCountRec ( $ mat , $ m , $ n , $ k ) {"}
{"text":"Base cases","code":"if ( $ m < 0 or $ n < 0 ) return 0 ; if ( $ m == 0 and $ n == 0 ) return ( $ k == $ mat [ $ m ] [ $ n ] ) ;"}
{"text":"( m , n ) can be reached either through ( m - 1 , n ) or through ( m , n - 1 )","code":"return pathCountRec ( $ mat , $ m - 1 , $ n , $ k - $ mat [ $ m ] [ $ n ] ) + pathCountRec ( $ mat , $ m , $ n - 1 , $ k - $ mat [ $ m ] [ $ n ] ) ; }"}
{"text":"A wrapper over pathCountRec ( )","code":"function pathCount ( $ mat , $ k ) { global $ R , $ C ; return pathCountRec ( $ mat , $ R - 1 , $ C - 1 , $ k ) ; }"}
{"text":"Driver program","code":"$ k = 12 ; $ mat = array ( array ( 1 , 2 , 3 ) , array ( 4 , 6 , 5 ) , array ( 3 , 2 , 1 ) ) ; echo pathCount ( $ mat , $ k ) ; ? >"}
{"text":"sort function","code":"< ? php function selection_sort ( & $ arr , $ n ) {"}
{"text":"One by one move boundary of unsorted subarray","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) {"}
{"text":"Find the minimum element in unsorted array","code":"$ low = $ i ; for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) { if ( $ arr [ $ j ] < $ arr [ $ low ] ) { $ low = $ j ; } }"}
{"text":"swap the minimum value to $ith node","code":"if ( $ arr [ $ i ] > $ arr [ $ low ] ) { $ tmp = $ arr [ $ i ] ; $ arr [ $ i ] = $ arr [ $ low ] ; $ arr [ $ low ] = $ tmp ; } } }"}
{"text":"Driver Code","code":"$ arr = array ( 64 , 25 , 12 , 22 , 11 ) ; $ len = count ( $ arr ) ; selection_sort ( $ arr , $ len ) ; echo \" Sorted \u2581 array \u2581 : \u2581 STRNEWLINE \" ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) echo $ arr [ $ i ] . \" \u2581 \" ; ? >"}
{"text":"An optimized version of Bubble Sort","code":"< ? php function bubbleSort ( & $ arr ) { $ n = sizeof ( $ arr ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ swapped = False ; for ( $ j = 0 ; $ j < $ n - $ i - 1 ; $ j ++ ) { if ( $ arr [ $ j ] > $ arr [ $ j + 1 ] ) {"}
{"text":"traverse the array from 0 to n - i - 1. Swap if the element found is greater than the next element","code":"$ t = $ arr [ $ j ] ; $ arr [ $ j ] = $ arr [ $ j + 1 ] ; $ arr [ $ j + 1 ] = $ t ; $ swapped = True ; } }"}
{"text":"IF no two elements were swapped by inner loop , then break","code":"if ( $ swapped == False ) break ; } }"}
{"text":"Driver code to test above","code":"$ arr = array ( 64 , 34 , 25 , 12 , 22 , 11 , 90 ) ; $ len = sizeof ( $ arr ) ; bubbleSort ( $ arr ) ; echo \" Sorted \u2581 array \u2581 : \u2581 STRNEWLINE \" ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) echo $ arr [ $ i ] . \" \u2581 \" ; ? >"}
{"text":"Function to find the cross over point ( the point before which elements are smaller than or equal to x and after which greater than x )","code":"< ? php function findCrossOver ( $ arr , $ low , $ high , $ x ) {"}
{"text":"Base cases x is greater than all","code":"if ( $ arr [ $ high ] <= $ x ) return $ high ;"}
{"text":"x is smaller than all","code":"if ( $ arr [ $ low ] > $ x ) return $ low ;"}
{"text":"Find the middle point","code":"$ mid = ( $ low + $ high ) \/ 2 ;"}
{"text":"If x is same as middle element , then return mid","code":"if ( $ arr [ $ mid ] <= $ x and $ arr [ $ mid + 1 ] > $ x ) return $ mid ;"}
{"text":"If x is greater than arr [ mid ] , then either arr [ mid + 1 ] is ceiling of x or ceiling lies in arr [ mid + 1. . . high ]","code":"if ( $ arr [ $ mid ] < $ x ) return findCrossOver ( $ arr , $ mid + 1 , $ high , $ x ) ; return findCrossOver ( $ arr , $ low , $ mid - 1 , $ x ) ; }"}
{"text":"This function prints k closest elements to x in arr [ ] . n is the number of elements in arr [ ]","code":"function printKclosest ( $ arr , $ x , $ k , $ n ) {"}
{"text":"Find the crossover point","code":"$ l = findCrossOver ( $ arr , 0 , $ n - 1 , $ x ) ;"}
{"text":"Right index to search","code":"$ r = $ l + 1 ;"}
{"text":"To keep track of count of elements already printed","code":"$ count = 0 ;"}
{"text":"If x is present in arr [ ] , then reduce left index Assumption : all elements in arr [ ] are distinct","code":"if ( $ arr [ $ l ] == $ x ) $ l -- ;"}
{"text":"Compare elements on left and right of crossover point to find the k closest elements","code":"while ( $ l >= 0 and $ r < $ n and $ count < $ k ) { if ( $ x - $ arr [ $ l ] < $ arr [ $ r ] - $ x ) echo $ arr [ $ l -- ] , \" \u2581 \" ; else echo $ arr [ $ r ++ ] , \" \u2581 \" ; $ count ++ ; }"}
{"text":"If there are no more elements on right side , then print left elements","code":"while ( $ count < $ k and $ l >= 0 ) echo $ arr [ $ l -- ] , \" \u2581 \" ; $ count ++ ;"}
{"text":"If there are no more elements on left side , then print right elements","code":"while ( $ count < $ k and $ r < $ n ) echo $ arr [ $ r ++ ] ; $ count ++ ; }"}
{"text":"Driver Code","code":"$ arr = array ( 12 , 16 , 22 , 30 , 35 , 39 , 42 , 45 , 48 , 50 , 53 , 55 , 56 ) ; $ n = count ( $ arr ) ; $ x = 35 ; $ k = 4 ; printKclosest ( $ arr , $ x , 4 , $ n ) ; ? >"}
{"text":"Returns the count of ways we can sum S [ 0. . . m - 1 ] coins to get sum n","code":"< ? php function coun ( $ S , $ m , $ n ) {"}
{"text":"If n is 0 then there is 1 solution ( do not include any coin )","code":"if ( $ n == 0 ) return 1 ;"}
{"text":"If n is less than 0 then no solution exists","code":"if ( $ n < 0 ) return 0 ;"}
{"text":"If there are no coins and n is greater than 0 , then no solution exist","code":"if ( $ m <= 0 && $ n >= 1 ) return 0 ;"}
{"text":"count is sum of solutions ( i ) including S [ m - 1 ] ( ii ) excluding S [ m - 1 ]","code":"return coun ( $ S , $ m - 1 , $ n ) + coun ( $ S , $ m , $ n - $ S [ $ m - 1 ] ) ; }"}
{"text":"Driver Code","code":"$ arr = array ( 1 , 2 , 3 ) ; $ m = count ( $ arr ) ; echo coun ( $ arr , $ m , 4 ) ; ? >"}
{"text":"Dynamic Programming PHP implementation of Coin Change problem","code":"< ? php function count_1 ( & $ S , $ m , $ n ) {"}
{"text":"table [ i ] will be storing the number of solutions for value i . We need n + 1 rows as the table is constructed in bottom up manner using the base case ( n = 0 )","code":"$ table = array_fill ( 0 , $ n + 1 , NULl ) ;"}
{"text":"Base case ( If given value is 0 )","code":"$ table [ 0 ] = 1 ;"}
{"text":"Pick all coins one by one and update the table [ ] values after the index greater than or equal to the value of the picked coin","code":"for ( $ i = 0 ; $ i < $ m ; $ i ++ ) for ( $ j = $ S [ $ i ] ; $ j <= $ n ; $ j ++ ) $ table [ $ j ] += $ table [ $ j - $ S [ $ i ] ] ; return $ table [ $ n ] ; }"}
{"text":"Driver Code","code":"$ arr = array ( 1 , 2 , 3 ) ; $ m = sizeof ( $ arr ) ; $ n = 4 ; $ x = count_1 ( $ arr , $ m , $ n ) ; echo $ x ; ? >"}
{"text":"Matrix Ai has dimension p [ i - 1 ] x p [ i ] for i = 1. . n","code":"< ? php function MatrixChainOrder ( $ p , $ n ) {"}
{"text":"For simplicity of the program , one extra row and one extra column are allocated in m [ ] [ ] . 0 th row and 0 th column of m [ ] [ ] are not used","code":"$ m [ ] [ ] = array ( $ n , $ n ) ;"}
{"text":"cost is zero when multiplying one matrix .","code":"for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ m [ $ i ] [ $ i ] = 0 ;"}
{"text":"L is chain length .","code":"for ( $ L = 2 ; $ L < $ n ; $ L ++ ) { for ( $ i = 1 ; $ i < $ n - $ L + 1 ; $ i ++ ) { $ j = $ i + $ L - 1 ; if ( $ j == $ n ) continue ; $ m [ $ i ] [ $ j ] = PHP_INT_MAX ; for ( $ k = $ i ; $ k <= $ j - 1 ; $ k ++ ) {"}
{"text":"q = cost \/ scalar multiplications","code":"$ q = $ m [ $ i ] [ $ k ] + $ m [ $ k + 1 ] [ $ j ] + $ p [ $ i - 1 ] * $ p [ $ k ] * $ p [ $ j ] ; if ( $ q < $ m [ $ i ] [ $ j ] ) $ m [ $ i ] [ $ j ] = $ q ; } } } return $ m [ 1 ] [ $ n - 1 ] ; }"}
{"text":"Driver Code","code":"$ arr = array ( 1 , 2 , 3 , 4 ) ; $ size = sizeof ( $ arr ) ; echo \" Minimum \u2581 number \u2581 of \u2581 multiplications \u2581 is \u2581 \" . MatrixChainOrder ( $ arr , $ size ) ; ? >"}
{"text":"Returns the best obtainable price for a rod of length n and price [ ] as prices of different pieces","code":"< ? php function cutRod ( $ price , $ n ) { if ( $ n <= 0 ) return 0 ; $ max_val = PHP_INT_MIN ;"}
{"text":"Recursively cut the rod in different pieces and compare different configurations","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ max_val = max ( $ max_val , $ price [ $ i ] + cutRod ( $ price , $ n - $ i - 1 ) ) ; return $ max_val ; }"}
{"text":"Driver Code","code":"$ arr = array ( 1 , 5 , 8 , 9 , 10 , 17 , 17 , 20 ) ; $ size = count ( $ arr ) ; echo \" Maximum \u2581 Obtainable \u2581 Value \u2581 is \u2581 \" , cutRod ( $ arr , $ size ) ; ? >"}
{"text":"Returns the best obtainable price for a rod of length n and price [ ] as prices of different pieces","code":"< ? php function cutRod ( $ price , $ n ) { $ val = array ( ) ; $ val [ 0 ] = 0 ; $ i ; $ j ;"}
{"text":"Build the table val [ ] in bottom up manner and return the last entry from the table","code":"for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ max_val = PHP_INT_MIN ; for ( $ j = 0 ; $ j < $ i ; $ j ++ ) $ max_val = max ( $ max_val , $ price [ $ j ] + $ val [ $ i - $ j - 1 ] ) ; $ val [ $ i ] = $ max_val ; } return $ val [ $ n ] ; }"}
{"text":"Driver program to test above functions","code":"$ arr = array ( 1 , 5 , 8 , 9 , 10 , 17 , 17 , 20 ) ; $ size = count ( $ arr ) ; echo \" Maximum \u2581 Obtainable \u2581 Value \u2581 is \u2581 \" , cutRod ( $ arr , $ size ) ; ? >"}
{"text":"function to multiply two numbers x and y","code":"< ? php function multiply ( $ x , $ y ) {"}
{"text":"0 multiplied with anything gives 0","code":"if ( $ y == 0 ) return 0 ;"}
{"text":"Add x one by one","code":"if ( $ y > 0 ) return ( $ x + multiply ( $ x , $ y - 1 ) ) ;"}
{"text":"the case where y is negative","code":"if ( $ y < 0 ) return - multiply ( $ x , - $ y ) ; }"}
{"text":"Driver Code","code":"echo multiply ( 5 , -11 ) ; ? >"}
{"text":"php program to print all primes smaller than or equal to n using Sieve of Eratosthenes","code":"< ? php function SieveOfEratosthenes ( $ n ) {"}
{"text":"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 .","code":"$ prime = array_fill ( 0 , $ n + 1 , true ) ; for ( $ p = 2 ; $ p * $ p <= $ n ; $ p ++ ) {"}
{"text":"If prime [ p ] is not changed , then it is a prime","code":"if ( $ prime [ $ p ] == true ) {"}
{"text":"Update all multiples of p","code":"for ( $ i = $ p * $ p ; $ i <= $ n ; $ i += $ p ) $ prime [ $ i ] = false ; } }"}
{"text":"Print all prime numbers","code":"for ( $ p = 2 ; $ p <= $ n ; $ p ++ ) if ( $ prime [ $ p ] ) echo $ p . \" \" ; }"}
{"text":"Driver Code","code":"$ n = 30 ; echo \" Following \u2581 are \u2581 the \u2581 prime \u2581 numbers \u2581 \" . \" smaller \u2581 than \u2581 or \u2581 equal \u2581 to \u2581 \" . $ n . \" STRNEWLINE \" ; SieveOfEratosthenes ( $ n ) ; ? >"}
{"text":"binomialCoeff","code":"< ? php function binomialCoeff ( $ n , $ k ) { $ res = 1 ; if ( $ k > $ n - $ k ) $ k = $ n - $ k ; for ( $ i = 0 ; $ i < $ k ; ++ $ i ) { $ res *= ( $ n - $ i ) ; $ res \/= ( $ i + 1 ) ; } return $ res ; }"}
{"text":"Function to print first n lines of Pascal 's Triangle","code":"function printPascal ( $ n ) {"}
{"text":"Iterate through every line and print entries in it","code":"for ( $ line = 0 ; $ line < $ n ; $ line ++ ) {"}
{"text":"Every line has number of integers equal to line number","code":"for ( $ i = 0 ; $ i <= $ line ; $ i ++ ) echo \" \" . binomialCoeff ( $ line , \u2581 $ i ) . \" \" echo \" STRNEWLINE \" ; } }"}
{"text":"Driver Code","code":"$ n = 7 ; printPascal ( $ n ) ; ? >"}
{"text":"A O ( n ^ 2 ) time and O ( n ^ 2 ) extra space method for Pascal 's Triangle","code":"< ? php function printPascal ( $ n ) {"}
{"text":"An auxiliary array to store generated pscal triangle values","code":"$ arr = array ( array ( ) ) ;"}
{"text":"Iterate through every line and print integer ( s ) in it","code":"for ( $ line = 0 ; $ line < $ n ; $ line ++ ) {"}
{"text":"Every line has number of integers equal to line number","code":"for ( $ i = 0 ; $ i <= $ line ; $ i ++ ) {"}
{"text":"First and last values in every row are 1","code":"if ( $ line == $ i $ i == 0 ) $ arr [ $ line ] [ $ i ] = 1 ;"}
{"text":"Other values are sum of values just above and left of above","code":"else $ arr [ $ line ] [ $ i ] = $ arr [ $ line - 1 ] [ $ i - 1 ] + $ arr [ $ line - 1 ] [ $ i ] ; echo $ arr [ $ line ] [ $ i ] . \" \" ; } echo \" \" } }"}
{"text":"Driver code","code":"$ n = 5 ; printPascal ( $ n ) ; ? >"}
{"text":"PHP program for Pascal ' s \u2581 Triangle \u2581 A \u2581 O ( n ^ 2 ) \u2581 time \u2581 and \u2581 O ( 1 ) \u2581 extra \u2581 space \u2581 method \u2581 for \u2581 Pascal ' s Triangle Pascal function","code":"< ? php function printPascal ( $ n ) { for ( $ line = 1 ; $ line <= $ n ; $ line ++ ) {"}
{"text":"used to represent C ( line , i )","code":"$ C = 1 ; for ( $ i = 1 ; $ i <= $ line ; $ i ++ ) {"}
{"text":"The first value in a line is always 1","code":"print ( $ C . \" \" ) ; $ C = $ C * ( $ line - $ i ) \/ $ i ; } print ( \" STRNEWLINE \" ) ; } }"}
{"text":"Driver code","code":"$ n = 5 ; printPascal ( $ n ) ; ? >"}
{"text":"PHP Program to add two numbers without using arithmetic operator","code":"< ? php function Add ( $ x , $ y ) {"}
{"text":"Iterate till there is no carry","code":"while ( $ y != 0 ) {"}
{"text":"carry now contains common set bits of x and y","code":"$ carry = $ x & $ y ;"}
{"text":"Sum of bits of x and y where at least one of the bits is not set","code":"$ x = $ x ^ $ y ;"}
{"text":"Carry is shifted by one so that adding it to x gives the required sum","code":"$ y = $ carry << 1 ; } return $ x ; }"}
{"text":"Driver Code","code":"echo Add ( 15 , 32 ) ; ? >"}
{"text":"This function will return n % d . d must be one of : 1 , 2 , 4 , 8 , 16 , 32 , ...","code":"< ? php function getModulo ( $ n , $ d ) { return ( $ n & ( $ d - 1 ) ) ; }"}
{"text":"Driver Code","code":"$ n = 6 ;"}
{"text":"d must be a power of 2","code":"$ d = 4 ; echo $ n , \" \u2581 moduo \" , \" \u2581 \" , $ d , \" \u2581 is \u2581 \" , \" \u2581 \" , getModulo ( $ n , $ d ) ; ? >"}
{"text":"Function to get no of set bits in binary representation of positive integer n","code":"< ? php function countSetBits ( $ n ) { $ count = 0 ; while ( $ n ) { $ count += $ n & 1 ; $ n >>= 1 ; } return $ count ; }"}
{"text":"Driver Code","code":"$ i = 9 ; echo countSetBits ( $ i ) ; ? >"}
{"text":"recursive function to count set bits","code":"< ? php function countSetBits ( $ n ) {"}
{"text":"base case","code":"if ( $ n == 0 ) return 0 ; else return 1 + countSetBits ( $ n & ( $ n - 1 ) ) ; }"}
{"text":"get value from user","code":"$ n = 9 ;"}
{"text":"function calling","code":"echo countSetBits ( $ n ) ; ? >"}
{"text":"Driver code","code":"< ? php $ t = log10 ( 4 ) ; $ x = log ( 15 , 2 ) ; $ tt = ceil ( $ t ) ; $ xx = ceil ( $ x ) ; echo ( $ tt ) , \" STRNEWLINE \" ; echo ( $ xx ) , \" STRNEWLINE \" ; ? >"}
{"text":"PHP program to count set bits by pre - storing count set bits in nibbles .","code":"< ? php $ num_to_bits = array ( 0 , 1 , 1 , 2 , 1 , 2 , 2 , 3 , 1 , 2 , 2 , 3 , 2 , 3 , 3 , 4 ) ;"}
{"text":"Recursively get nibble of a given number and map them in the array","code":"function countSetBitsRec ( $ num ) { global $ num_to_bits ; $ nibble = 0 ; if ( 0 == $ num ) return $ num_to_bits [ 0 ] ;"}
{"text":"Find last nibble","code":"$ nibble = $ num & 0xf ;"}
{"text":"Use pre - stored values to find count in last nibble plus recursively add remaining nibbles .","code":"return $ num_to_bits [ $ nibble ] + countSetBitsRec ( $ num >> 4 ) ; }"}
{"text":"Driver code","code":"$ num = 31 ; echo ( countSetBitsRec ( $ num ) ) ; ? >"}
{"text":"Function to get parity of number n . It returns 1 if n has odd parity , and returns 0 if n has even parity","code":"< ? php function getParity ( $ n ) { $ parity = 0 ; while ( $ n ) { $ parity = ! $ parity ; $ n = $ n & ( $ n - 1 ) ; } return $ parity ; }"}
{"text":"Driver Code","code":"$ n = 7 ; echo \" Parity \u2581 of \u2581 no \u2581 \" , $ n , \" \u2581 = \u2581 \" , getParity ( $ n ) ? \" odd \" : \" even \" ; ? >"}
{"text":"PHP Program to find whether a no is power of two Function to check Log base 2","code":"< ? php function Log2 ( $ x ) { return ( log10 ( $ x ) \/ log10 ( 2 ) ) ; }"}
{"text":"Function to check if x is power of 2","code":"function isPowerOfTwo ( $ n ) { return ( ceil ( Log2 ( $ n ) ) == floor ( Log2 ( $ n ) ) ) ; }"}
{"text":"Driver Code","code":"if ( isPowerOfTwo ( 31 ) ) echo \" Yes STRNEWLINE \" ; else echo \" No STRNEWLINE \" ; if ( isPowerOfTwo ( 64 ) ) echo \" Yes STRNEWLINE \" ; else echo \" No STRNEWLINE \" ; ? >"}
{"text":"Function to check if x is power of 2","code":"< ? php function isPowerOfTwo ( $ n ) { if ( $ n == 0 ) return 0 ; while ( $ n != 1 ) { if ( $ n % 2 != 0 ) return 0 ; $ n = $ n \/ 2 ; } return 1 ; }"}
{"text":"Driver Code","code":"if ( isPowerOfTwo ( 31 ) ) echo \" Yes STRNEWLINE \" ; else echo \" No STRNEWLINE \" ; if ( isPowerOfTwo ( 64 ) ) echo \" Yes STRNEWLINE \" ; else echo \" No STRNEWLINE \" ; ? >"}
{"text":"Function to check if x is power of 2","code":"< ? php function isPowerOfTwo ( $ x ) {"}
{"text":"First x in the below expression is for the case when x is 0","code":"return $ x && ( ! ( $ x & ( $ x - 1 ) ) ) ; }"}
{"text":"Driver Code","code":"if ( isPowerOfTwo ( 31 ) ) echo \" Yes STRNEWLINE \" ; else echo \" No STRNEWLINE \" ; if ( isPowerOfTwo ( 64 ) ) echo \" Yes STRNEWLINE \" ; else echo \" No STRNEWLINE \" ; ? >"}
{"text":"Returns maximum repeating element in arr [ 0. . n - 1 ] . The array elements are in range from 0 to k - 1","code":"< ? php function maxRepeating ( $ arr , $ n , $ k ) {"}
{"text":"Iterate though input array , for every element arr [ i ] , increment arr [ arr [ i ] % k ] by k","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ arr [ $ arr [ $ i ] % $ k ] += $ k ;"}
{"text":"Find index of the maximum repeating element","code":"$ max = $ arr [ 0 ] ; $ result = 0 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] > $ max ) { $ max = $ arr [ $ i ] ; $ result = $ i ; } }"}
{"text":"Return index of the maximum element","code":"return $ result ; }"}
{"text":"Driver Code","code":"$ arr = array ( 2 , 3 , 3 , 5 , 3 , 4 , 1 , 7 ) ; $ n = sizeof ( $ arr ) ; $ k = 8 ; echo \" The \u2581 maximum \u2581 repeating \u2581 number \u2581 is \u2581 \" , maxRepeating ( $ arr , $ n , $ k ) ; ? >"}
{"text":"function return derived formula value .","code":"< ? php function fun ( $ x ) { $ y = ( ( int ) ( $ x \/ 4 ) * 4 ) ;"}
{"text":"finding xor value of range [ y ... x ]","code":"$ ans = 0 ; for ( $ i = $ y ; $ i <= $ x ; $ i ++ ) $ ans ^= $ i ; return $ ans ; }"}
{"text":"function to solve query for l and r .","code":"function query ( $ x ) {"}
{"text":"if l or r is 0.","code":"if ( $ x == 0 ) return 0 ; $ k = ( int ) ( ( $ x + 1 ) \/ 2 ) ;"}
{"text":"finding x is divisible by 2 or not .","code":"return ( $ x %= 2 ) ? 2 * fun ( $ k ) : ( ( fun ( $ k - 1 ) * 2 ) ^ ( $ k & 1 ) ) ; } function allQueries ( $ q , $ l , $ r ) { for ( $ i = 0 ; $ i < $ q ; $ i ++ ) echo ( query ( $ r [ $ i ] ) ^ query ( $ l [ $ i ] - 1 ) ) , \" STRNEWLINE \" ; }"}
{"text":"Driver Code","code":"$ q = 3 ; $ l = array ( 2 , 2 , 5 ) ; $ r = array ( 4 , 8 , 9 ) ; allQueries ( $ q , $ l , $ r ) ; ? >"}
{"text":"Function to find minimum swaps to sort an array of 0 s and 1 s .","code":"< ? php function findMinSwaps ( $ arr , $ n ) {"}
{"text":"Array to store count of zeroes","code":"$ noOfZeroes [ $ n ] = array ( ) ; $ noOfZeroes = array_fill ( 0 , $ n , true ) ; $ count = 0 ;"}
{"text":"Count number of zeroes on right side of every one .","code":"$ noOfZeroes [ $ n - 1 ] = 1 - $ arr [ $ n - 1 ] ; for ( $ i = $ n - 2 ; $ i >= 0 ; $ i -- ) { $ noOfZeroes [ $ i ] = $ noOfZeroes [ $ i + 1 ] ; if ( $ arr [ $ i ] == 0 ) $ noOfZeroes [ $ i ] ++ ; }"}
{"text":"Count total number of swaps by adding number of zeroes on right side of every one .","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] == 1 ) $ count += $ noOfZeroes [ $ i ] ; } return $ count ; }"}
{"text":"Driver code","code":"$ arr = array ( 0 , 0 , 1 , 0 , 1 , 0 , 1 , 1 ) ; $ n = sizeof ( $ arr ) ; echo findMinSwaps ( $ arr , $ n ) ; ? >"}
{"text":"Prints two numbers that occur odd number of times . The function assumes that the array size is at least 2 and there are exactly two numbers occurring odd number of times .","code":"< ? php function printTwoOdd ( $ arr , $ size ) {"}
{"text":"Will hold XOR of two odd occurring elements","code":"$ xor2 = $ arr [ 0 ] ;"}
{"text":"Will have only single set bit of xor2","code":"$ set_bit_no ; $ i ; $ n = $ size - 2 ; $ x = 0 ; $ y = 0 ;"}
{"text":"Get the xor of all elements in arr [ ] . The xor will basically be xor of two odd occurring elements","code":"for ( $ i = 1 ; $ i < $ size ; $ i ++ ) $ xor2 = $ xor2 ^ $ arr [ $ i ] ;"}
{"text":"Get one set bit in the xor2 . We get rightmost set bit in the following line as it is easy to get","code":"$ set_bit_no = $ xor2 & ~ ( $ xor2 - 1 ) ;"}
{"text":"Now divide elements in two sets : 1 ) The elements having the corresponding bit as 1. 2 ) The elements having the corresponding bit as 0.","code":"for ( $ i = 0 ; $ i < $ size ; $ i ++ ) {"}
{"text":"XOR of first set is finally going to hold one odd occurring number x","code":"if ( $ arr [ $ i ] & $ set_bit_no ) $ x = $ x ^ $ arr [ $ i ] ;"}
{"text":"XOR of second set is finally going to hold the other odd occurring number y","code":"else $ y = $ y ^ $ arr [ $ i ] ; } echo \" The \u2581 two \u2581 ODD \u2581 elements \u2581 are \u2581 \" , $ x , \" \u2581 & \u2581 \" , $ y ; }"}
{"text":"Driver Code","code":"$ arr = array ( 4 , 2 , 4 , 5 , 2 , 3 , 3 , 1 ) ; $ arr_size = sizeof ( $ arr ) ; printTwoOdd ( $ arr , $ arr_size ) ; ? >"}
{"text":"The function assumes that the array is sorted","code":"< ? php function findPair ( & $ arr , $ size , $ n ) {"}
{"text":"Initialize positions of two elements","code":"$ i = 0 ; $ j = 1 ;"}
{"text":"Search for a pair","code":"while ( $ i < $ size && $ j < $ size ) { if ( $ i != $ j && $ arr [ $ j ] - $ arr [ $ i ] == $ n ) { echo \" Pair \u2581 Found : \u2581 \" . \" ( \" . $ arr [ $ i ] . \" , \u2581 \" . $ arr [ $ j ] . \" ) \" ; return true ; } else if ( $ arr [ $ j ] - $ arr [ $ i ] < $ n ) $ j ++ ; else $ i ++ ; } echo \" No \u2581 such \u2581 pair \" ; return false ; }"}
{"text":"Driver Code","code":"$ arr = array ( 1 , 8 , 30 , 40 , 100 ) ; $ size = sizeof ( $ arr ) ; $ n = 60 ; findPair ( $ arr , $ size , $ n ) ; ? >"}
{"text":"Returns true if a permutation of arr [ 0. . n - 1 ] can form arithmetic progression","code":"< ? php function checkIsAP ( $ arr , $ n ) { if ( $ n == 1 ) return true ;"}
{"text":"Sort array","code":"sort ( $ arr ) ;"}
{"text":"After sorting , difference between consecutive elements must be same .","code":"$ d = $ arr [ 1 ] - $ arr [ 0 ] ; for ( $ i = 2 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] - $ arr [ $ i - 1 ] != $ d ) return false ; return true ; }"}
{"text":"Driver Code","code":"$ arr = array ( 20 , 15 , 5 , 0 , 10 ) ; $ n = count ( $ arr ) ; if ( checkIsAP ( $ arr , $ n ) ) echo \" Yes \" ; else echo \" No \" ; ? >"}
{"text":"PHP Code to find no . of Ways of choosing a pair with maximum difference","code":"< ? php function countPairs ( $ a , $ n ) {"}
{"text":"To find minimum and maximum of the array","code":"$ mn = PHP_INT_MAX ; $ mx = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ mn = min ( $ mn , $ a [ $ i ] ) ; $ mx = max ( $ mx , $ a [ $ i ] ) ; }"}
{"text":"to find the count of minimum and maximum elements","code":"$ c1 = 0 ;"}
{"text":"Count variables","code":"$ c2 = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] == $ mn ) $ c1 ++ ; if ( $ a [ $ i ] == $ mx ) $ c2 ++ ; }"}
{"text":"condition for all elements equal","code":"if ( $ mn == $ mx ) return $ n * ( $ n - 1 ) \/ 2 ; else return $ c1 * $ c2 ; }"}
{"text":"Driver code","code":"$ a = array ( 3 , 2 , 1 , 1 , 3 ) ; $ n = count ( $ a ) ; echo countPairs ( $ a , $ n ) ; ? >"}
{"text":"Function to print the required numbers","code":"< ? php function findNumbers ( $ arr , $ n ) {"}
{"text":"Sum of first n natural numbers","code":"$ sumN = ( $ n * ( $ n + 1 ) ) \/ 2 ;"}
{"text":"Sum of squares of first n natural numbers","code":"$ sumSqN = ( $ n * ( $ n + 1 ) * ( 2 * $ n + 1 ) ) \/ 6 ;"}
{"text":"To store the sum and sum of squares of the array elements","code":"$ sum = 0 ; $ sumSq = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ sum += $ arr [ $ i ] ; $ sumSq += pow ( $ arr [ $ i ] , 2 ) ; } $ B = ( ( ( $ sumSq - $ sumSqN ) \/ ( $ sum - $ sumN ) ) + $ sumN - $ sum ) \/ 2 ; $ A = $ sum - $ sumN + $ B ; echo \" A = \" , \u2581 $ A , \u2581 \" B = \" }"}
{"text":"Driver code","code":"$ arr = array ( 1 , 2 , 2 , 3 , 4 ) ; $ n = sizeof ( $ arr ) ; findNumbers ( $ arr , $ n ) ; ? >"}
{"text":"Function to return the count of elements in arr [ ] which are less than the given key","code":"< ? php function countLessThan ( & $ arr , $ n , $ key ) { $ l = 0 ; $ r = $ n - 1 ; $ index = -1 ;"}
{"text":"Modified binary search","code":"while ( $ l <= $ r ) { $ m = intval ( ( $ l + $ r ) \/ 2 ) ; if ( $ arr [ $ m ] < $ key ) { $ l = $ m + 1 ; $ index = $ m ; } else { $ r = $ m - 1 ; } } return ( $ index + 1 ) ; }"}
{"text":"Function to return the count of elements in arr [ ] which are greater than the given key","code":"function countGreaterThan ( & $ arr , $ n , $ key ) { $ l = 0 ; $ r = $ n - 1 ; $ index = -1 ;"}
{"text":"Modified binary search","code":"while ( $ l <= $ r ) { $ m = intval ( ( $ l + $ r ) \/ 2 ) ; if ( $ arr [ $ m ] <= $ key ) { $ l = $ m + 1 ; } else { $ r = $ m - 1 ; $ index = $ m ; } } if ( $ index == -1 ) return 0 ; return ( $ n - $ index ) ; }"}
{"text":"Function to return the count of the required triplets","code":"function countTriplets ( $ n , & $ a , & $ b , & $ c ) {"}
{"text":"Sort all three arrays","code":"sort ( $ a ) ; sort ( $ b ) ; sort ( $ c ) ; $ count = 0 ;"}
{"text":"Iterate for all the elements of array B","code":"for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { $ current = $ b [ $ i ] ; $ a_index = -1 ; $ c_index = -1 ;"}
{"text":"Count of elements in A [ ] which are less than the chosen element from B [ ]","code":"$ low = countLessThan ( $ a , $ n , $ current ) ;"}
{"text":"Count of elements in C [ ] which are greater than the chosen element from B [ ]","code":"$ high = countGreaterThan ( $ c , $ n , $ current ) ;"}
{"text":"Update the count","code":"$ count += ( $ low * $ high ) ; } return $ count ; }"}
{"text":"Driver code","code":"$ a = array ( 1 , 5 ) ; $ b = array ( 2 , 4 ) ; $ c = array ( 3 , 6 ) ; $ size = sizeof ( $ a ) ; echo countTriplets ( $ size , $ a , $ b , $ c ) ; ? >"}
{"text":"Function to find the middle of three number","code":"< ? php function middleOfThree ( $ a , $ b , $ c ) {"}
{"text":"x is positive if a is greater than b . x is negative if b is greater than a .","code":"$ x = $ a - $ b ;"}
{"text":"Similar to x","code":"$ y = $ b - $ c ;"}
{"text":"Similar to x and y .","code":"$ z = $ a - $ c ;"}
{"text":"Checking if b is middle ( x and y both are positive )","code":"if ( $ x * $ y > 0 ) return $ b ;"}
{"text":"Checking if c is middle ( x and z both are positive )","code":"else if ( $ x * $ z > 0 ) return $ c ; else return $ a ; }"}
{"text":"Driver Code","code":"$ a = 20 ; $ b = 30 ; $ c = 40 ; echo middleOfThree ( $ a , $ b , $ c ) ; ? >"}
{"text":"Finds missing 4 numbers in O ( N ) time and O ( 1 ) auxiliary space .","code":"< ? php function missing4 ( $ arr , $ n ) {"}
{"text":"To keep track of 4 possible numbers greater than length of input array initialized as 0.","code":"$ helper = array ( 0 , 0 , 0 , 0 ) ;"}
{"text":"Traverse the input array and mark visited elements either by marking them as negative in arr [ ] or in helper [ ] .","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ temp = abs ( $ arr [ $ i ] ) ;"}
{"text":"If element is smaller than or equal to length , mark its presence in arr [ ]","code":"if ( $ temp <= $ n ) $ arr [ $ temp - 1 ] = $ arr [ $ temp - 1 ] * ( -1 ) ;"}
{"text":"Mark presence in helper [ ]","code":"else if ( $ temp > $ n ) { if ( $ temp % $ n != 0 ) $ helper [ $ temp % $ n - 1 ] = -1 ; else $ helper [ ( $ temp % $ n ) + $ n - 1 ] = -1 ; } }"}
{"text":"Print all those elements whose presence is not marked .","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] > 0 ) { $ a = $ i + 1 ; echo \" $ a \" , \" \u2581 \" ; } for ( $ i = 0 ; $ i < 4 ; $ i ++ ) if ( $ helper [ $ i ] >= 0 ) { $ b = $ n + $ i + 1 ; echo \" $ b \" , \" \u2581 \" ; } echo \" STRNEWLINE \" ; return ; }"}
{"text":"Driver code","code":"$ arr = array ( 1 , 7 , 3 , 12 , 5 , 10 , 8 , 4 , 9 ) ; $ n = sizeof ( $ arr ) ; missing4 ( $ arr , $ n ) ; ? >"}
{"text":"Function to find minimum range increments to sort an array","code":"< ? php function minMovesToSort ( $ arr , $ n ) { $ moves = 0 ; $ mn = $ arr [ $ n - 1 ] ; for ( $ i = $ n - 2 ; $ i >= 0 ; $ i -- ) {"}
{"text":"If current element is found greater than last element Increment all terms in range i + 1 to n - 1","code":"if ( $ arr [ $ i ] > $ mn ) $ moves += $ arr [ $ i ] - $ mn ;"}
{"text":"$mn = $arr [ $i ] ; Minimum in range i to n - 1","code":"} return $ moves ; }"}
{"text":"Driver Code","code":"$ arr = array ( 3 , 5 , 2 , 8 , 4 ) ; $ n = sizeof ( $ arr ) ; echo minMovesToSort ( $ arr , $ n ) ; ? >"}
{"text":"PHP Program to divide the array into N pairs such that maximum pair is minimized","code":"< ? php function findOptimalPairs ( $ arr , $ N ) { sort ( $ arr ) ;"}
{"text":"After Sorting Maintain two variables i and j pointing to start and end of array Such that smallest element of array pairs with largest element","code":"for ( $ i = 0 , $ j = $ N - 1 ; $ i <= $ j ; $ i ++ , $ j -- ) echo \" ( \" , $ arr [ $ i ] , \" , \u2581 \" , $ arr [ $ j ] , \" ) \" , \" \u2581 \" ; }"}
{"text":"Driver Code","code":"$ arr = array ( 9 , 6 , 5 , 1 ) ; $ N = sizeof ( $ arr ) ; findOptimalPairs ( $ arr , $ N ) ; ? >"}
{"text":"Function to return the minimum operations required","code":"< ? php function minOperations ( $ arr , $ n ) { $ result = 0 ; $ freq = array ( ) ;"}
{"text":"Count the frequency of each element","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ freq [ $ arr [ $ i ] ] = 0 ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ x = $ arr [ $ i ] ; $ freq [ $ x ] ++ ; }"}
{"text":"Maximum element from the array","code":"$ maxi = max ( $ arr ) ; for ( $ i = 1 ; $ i <= $ maxi ; $ i ++ ) { if ( $ freq [ $ i ] != 0 ) {"}
{"text":"Find all the multiples of i","code":"for ( $ j = $ i * 2 ; $ j <= $ maxi ; $ j = $ j + $ i ) {"}
{"text":"Delete the multiples","code":"$ freq [ $ j ] = 0 ; }"}
{"text":"Increment the operations","code":"$ result ++ ; } } return $ result ; }"}
{"text":"Driver code","code":"$ arr = array ( 2 , 4 , 2 , 4 , 4 , 4 ) ; $ n = count ( $ arr ) ; echo minOperations ( $ arr , $ n ) ; ? >"}
{"text":"Function to return minimum GCD among all subarrays","code":"< ? php function __gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return __gcd ( $ b % $ a , $ a ) ; } function minGCD ( $ arr , $ n ) { $ minGCD = 0 ;"}
{"text":"Minimum GCD among all sub - arrays will be the GCD of all the elements of the array","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ minGCD = __gcd ( $ minGCD , $ arr [ $ i ] ) ; return $ minGCD ; }"}
{"text":"Function to return minimum LCM among all subarrays","code":"function minLCM ( $ arr , $ n ) { $ minLCM = $ arr [ 0 ] ;"}
{"text":"Minimum LCM among all sub - arrays will be the minimum element from the array","code":"for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ minLCM = min ( $ minLCM , $ arr [ $ i ] ) ; return $ minLCM ; }"}
{"text":"Driver code","code":"$ arr = array ( 2 , 66 , 14 , 521 ) ; $ n = sizeof ( $ arr ) ; echo \" LCM \u2581 = \u2581 \" . minLCM ( $ arr , $ n ) . \" , \u2581 \" ; echo \" GCD \u2581 = \u2581 \" . minGCD ( $ arr , $ n ) ; ? >"}
{"text":"Function that returns the modified lexicographically smallest string after performing minimum number of given operations","code":"< ? php function formStringMinOperations ( $ s ) {"}
{"text":"Stores the initial frequencies of characters 0 s , 1 s and 2 s","code":"$ count = array_fill ( 0 , 3 , 0 ) ; for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) $ count [ $ s [ $ i ] - '0' ] ++ ;"}
{"text":"Stores number of processed characters upto that point of each type","code":"$ processed = array_fill ( 0 , 3 , 0 ) ;"}
{"text":"Required number of characters of each type","code":"$ reqd = floor ( strlen ( $ s ) \/ 3 ) ; for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) {"}
{"text":"If the current type has already reqd number of characters , no need to perform any operation","code":"if ( $ count [ $ s [ $ i ] - '0' ] == $ reqd ) continue ;"}
{"text":"Process all 3 cases","code":"if ( $ s [ $ i ] == '0' && $ count [ 0 ] > $ reqd && $ processed [ 0 ] >= $ reqd ) {"}
{"text":"Check for 1 first","code":"if ( $ count [ 1 ] < $ reqd ) { $ s [ $ i ] = '1' ; $ count [ 1 ] ++ ; $ count [ 0 ] -- ; }"}
{"text":"Else 2","code":"else if ( $ count [ 2 ] < $ reqd ) { $ s [ $ i ] = '2' ; $ count [ 2 ] ++ ; $ count [ 0 ] -- ; } }"}
{"text":"Here we need to check processed [ 1 ] only for 2 since 0 is less than 1 and we can replace it anytime","code":"if ( $ s [ $ i ] == '1' && $ count [ 1 ] > $ reqd ) { if ( $ count [ 0 ] < $ reqd ) { $ s [ $ i ] = '0' ; $ count [ 0 ] ++ ; $ count [ 1 ] -- ; } else if ( count [ 2 ] < $ reqd && $ processed [ 1 ] >= $ reqd ) { $ s [ $ i ] = '2' ; $ count [ 2 ] ++ ; $ count [ 1 ] -- ; } }"}
{"text":"Here we can replace 2 with 0 and 1 anytime","code":"if ( $ s [ $ i ] == '2' && $ count [ 2 ] > $ reqd ) { if ( $ count [ 0 ] < $ reqd ) { $ s [ $ i ] = '0' ; $ count [ 0 ] ++ ; $ count [ 2 ] -- ; } else if ( $ count [ 1 ] < $ reqd ) { $ s [ $ i ] = '1' ; $ count [ 1 ] ++ ; $ count [ 2 ] -- ; } }"}
{"text":"keep count of processed characters of each type","code":"$ processed [ $ s [ $ i ] - '0' ] ++ ; } return $ s ; }"}
{"text":"Driver Code","code":"$ s = \"011200\" ; echo formStringMinOperations ( $ s ) ; ? >"}
{"text":"PHP implementation of the approach","code":"< ? php $ N = 3 ;"}
{"text":"Function to return the maximum sum","code":"function FindMaximumSum ( $ ind , $ kon , $ a , $ b , $ c , $ n , $ dp ) { global $ N ;"}
{"text":"Base case","code":"if ( $ ind == $ n ) return 0 ;"}
{"text":"Already visited","code":"if ( $ dp [ $ ind ] [ $ kon ] != -1 ) return $ dp [ $ ind ] [ $ kon ] ; $ ans = -1e9 + 5 ;"}
{"text":"If the element has been taken from first array in previous step","code":"if ( $ kon == 0 ) { $ ans = max ( $ ans , $ b [ $ ind ] + FindMaximumSum ( $ ind + 1 , 1 , $ a , $ b , $ c , $ n , $ dp ) ) ; $ ans = max ( $ ans , $ c [ $ ind ] + FindMaximumSum ( $ ind + 1 , 2 , $ a , $ b , $ c , $ n , $ dp ) ) ; }"}
{"text":"If the element has been taken from second array in previous step","code":"else if ( $ kon == 1 ) { $ ans = max ( $ ans , $ a [ $ ind ] + FindMaximumSum ( $ ind + 1 , 0 , $ a , $ b , $ c , $ n , $ dp ) ) ; $ ans = max ( $ ans , $ c [ $ ind ] + FindMaximumSum ( $ ind + 1 , 2 , $ a , $ b , $ c , $ n , $ dp ) ) ; }"}
{"text":"If the element has been taken from third array in previous step","code":"else if ( $ kon == 2 ) { $ ans = max ( $ ans , $ a [ $ ind ] + FindMaximumSum ( $ ind + 1 , 1 , $ a , $ b , $ c , $ n , $ dp ) ) ; $ ans = max ( $ ans , $ b [ $ ind ] + FindMaximumSum ( $ ind + 1 , 0 , $ a , $ b , $ c , $ n , $ dp ) ) ; } return $ dp [ $ ind ] [ $ kon ] = $ ans ; }"}
{"text":"Driver code","code":"$ a = array ( 6 , 8 , 2 , 7 , 4 , 2 , 7 ) ; $ b = array ( 7 , 8 , 5 , 8 , 6 , 3 , 5 ) ; $ c = array ( 8 , 3 , 2 , 6 , 8 , 4 , 1 ) ; $ n = count ( $ a ) ; $ dp = array_fill ( 0 , $ n , array_fill ( 0 , $ N , -1 ) ) ;"}
{"text":"Pick element from first array","code":"$ x = FindMaximumSum ( 0 , 0 , $ a , $ b , $ c , $ n , $ dp ) ;"}
{"text":"Pick element from second array","code":"$ y = FindMaximumSum ( 0 , 1 , $ a , $ b , $ c , $ n , $ dp ) ;"}
{"text":"Pick element from third array","code":"$ z = FindMaximumSum ( 0 , 2 , $ a , $ b , $ c , $ n , $ dp ) ;"}
{"text":"Print the maximum of them","code":"print ( max ( $ x , max ( $ y , $ z ) ) ) ; ? >"}
{"text":"PHP implementation of the above approach","code":"< ? php $ mod = 1000000007 ;"}
{"text":"Function to return no of ways to build a binary string of length N such that 0 s always occur in groups of size K","code":"function noOfBinaryStrings ( $ N , $ k ) { global $ mod ; $ dp = array ( 0 , 100002 , NULL ) ; for ( $ i = 1 ; $ i <= $ k - 1 ; $ i ++ ) { $ dp [ $ i ] = 1 ; } $ dp [ $ k ] = 2 ; for ( $ i = $ k + 1 ; $ i <= $ N ; $ i ++ ) { $ dp [ $ i ] = ( $ dp [ $ i - 1 ] + $ dp [ $ i - $ k ] ) % $ mod ; } return $ dp [ $ N ] ; }"}
{"text":"Driver Code","code":"$ N = 4 ; $ K = 2 ; echo noOfBinaryStrings ( $ N , $ K ) ; ? >"}
{"text":"Function to find number of ways to pair people in party","code":"< ? php function findWaysToPair ( $ p ) {"}
{"text":"To store count of number of ways .","code":"$ dp = array ( ) ; $ dp [ 1 ] = 1 ; $ dp [ 2 ] = 2 ;"}
{"text":"Using the recurrence defined find count for different values of p .","code":"for ( $ i = 3 ; $ i <= $ p ; $ i ++ ) { $ dp [ $ i ] = $ dp [ $ i - 1 ] + ( $ i - 1 ) * $ dp [ $ i - 2 ] ; } return $ dp [ $ p ] ; }"}
{"text":"Driver code","code":"$ p = 3 ; echo findWaysToPair ( $ p ) ; ? >"}
{"text":"A simple recursive implementation for counting ways to reach a score using 1 and 2 with consecutive 2 allowed","code":"< ? php function CountWays ( $ n ) {"}
{"text":"base cases","code":"if ( $ n == 0 ) { return 1 ; } if ( $ n == 1 ) { return 1 ; } if ( $ n == 2 ) { return 1 + 1 ; }"}
{"text":"For cases n > 2","code":"return CountWays ( $ n - 1 ) + CountWays ( $ n - 3 ) ; }"}
{"text":"Driver Code","code":"$ n = 5 ; echo CountWays ( $ n ) ; ? >"}
{"text":"Returns sum of maximum sum subarray created after concatenating a [ 0. . n - 1 ] k times .","code":"< ? php function maxSubArraySumRepeated ( $ a , $ n , $ k ) { $ INT_MIN = 0 ; $ max_so_far = $ INT_MIN ; $ max_ending_here = 0 ; for ( $ i = 0 ; $ i < $ n * $ k ; $ i ++ ) {"}
{"text":"This is where it differs from Kadane 's algorithm.  We use modular arithmetic  to find next element.","code":"$ max_ending_here = $ max_ending_here + $ a [ $ i % $ n ] ; if ( $ max_so_far < $ max_ending_here ) $ max_so_far = $ max_ending_here ; if ( $ max_ending_here < 0 ) $ max_ending_here = 0 ; } return $ max_so_far ; }"}
{"text":"Driver Code","code":"$ a = array ( 10 , 20 , -30 , -1 ) ; $ n = sizeof ( $ a ) ; $ k = 3 ; echo \" Maximum \u2581 contiguous \u2581 sum \u2581 is \u2581 \" , maxSubArraySumRepeated ( $ a , $ n , $ k ) ; ? >"}
{"text":"function to find the longest increasing odd even subsequence","code":"< ? php function longOddEvenIncSeq ( & $ arr , $ n ) {"}
{"text":"lioes [ i ] stores longest increasing odd even subsequence ending at arr [ i ]","code":"$ lioes = array_fill ( 0 , $ n , NULL ) ;"}
{"text":"to store the length of longest increasing odd even subsequence","code":"$ maxLen = 0 ;"}
{"text":"Initialize LIOES values for all indexes","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ lioes [ $ i ] = 1 ;"}
{"text":"Compute optimized LIOES values in bottom up manner","code":"for ( $ i = 1 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ i ; $ j ++ ) if ( $ arr [ $ i ] > $ arr [ $ j ] && ( $ arr [ $ i ] + $ arr [ $ j ] ) % 2 != 0 && $ lioes [ $ i ] < $ lioes [ $ j ] + 1 ) $ lioes [ $ i ] = $ lioes [ $ j ] + 1 ;"}
{"text":"Pick maximum of all LIOES values","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ maxLen < $ lioes [ $ i ] ) $ maxLen = $ lioes [ $ i ] ;"}
{"text":"required maximum length","code":"return $ maxLen ; }"}
{"text":"Driver Code","code":"$ arr = array ( 1 , 12 , 2 , 22 , 5 , 30 , 31 , 14 , 17 , 11 ) ; $ n = sizeof ( $ arr ) ; echo \" Longest \u2581 Increasing \u2581 Odd \u2581 Even \u2581 \" . \" Subsequence : \u2581 \" . longOddEvenIncSeq ( $ arr , $ n ) ; ? >"}
{"text":"Matrix Ai has dimension p [ i - 1 ] x p [ i ] for i = 1. . n","code":"< ? php function MatrixChainOrder ( & $ p , $ i , $ j ) { if ( $ i == $ j ) return 0 ; $ min = PHP_INT_MAX ;"}
{"text":"place parenthesis at different places between first and last matrix , recursively calculate count of multiplications for each parenthesis placement and return the minimum count","code":"for ( $ k = $ i ; $ k < $ j ; $ k ++ ) { $ count = MatrixChainOrder ( $ p , $ i , $ k ) + MatrixChainOrder ( $ p , $ k + 1 , $ j ) + $ p [ $ i - 1 ] * $ p [ $ k ] * $ p [ $ j ] ; if ( $ count < $ min ) $ min = $ count ; }"}
{"text":"Return minimum count","code":"return $ min ; }"}
{"text":"Driver Code","code":"$ arr = array ( 1 , 2 , 3 , 4 , 3 ) ; $ n = sizeof ( $ arr ) ; echo \" Minimum \u2581 number \u2581 of \u2581 multiplications \u2581 is \u2581 \" . MatrixChainOrder ( $ arr , 1 , $ n - 1 ) ; ? >"}
{"text":"Function to return the count of repetitions of string a to generate string b","code":"< ? php function getCount ( $ a , $ b ) {"}
{"text":"If b cannot be generated by repeating a","code":"if ( strlen ( $ b ) % strlen ( $ a ) != 0 ) return -1 ; $ count = floor ( strlen ( $ b ) \/ strlen ( $ a ) ) ;"}
{"text":"Repeat a count number of times Repeat a count number of times","code":"$ str = \" \" ; for ( $ i = 0 ; $ i < $ count ; $ i ++ ) { $ str = $ str . $ a ; } if ( strcmp ( $ a , $ b ) ) return $ count ; return -1 ; }"}
{"text":"Driver code","code":"$ a = ' eeks ' $ b = ' eeksgeeks ' echo getCount ( $ a , $ b ) ; ? >"}
{"text":"Returns count of occurrences of \"1(0 + ) 1\"","code":"< ? php function countPattern ( $ str ) { $ len = strlen ( $ str ) ; $ oneSeen = 0 ;"}
{"text":"$count = 0 ; Initialize result","code":"for ( $ i = 0 ; $ i < $ len ; $ i ++ ) {"}
{"text":"Check if encountered '1' forms a valid pattern as specified","code":"if ( $ str [ $ i ] == '1' && $ oneSeen == 1 ) if ( $ str [ $ i - 1 ] == '0' ) $ count ++ ;"}
{"text":"if 1 encountered for first time set oneSeen to 1","code":"if ( $ str [ $ i ] == '1' && $ oneSeen == 0 ) $ oneSeen = 1 ;"}
{"text":"Check if there is any other character other than '0' or '1' . If so then set oneSeen to 0 to search again for new pattern","code":"if ( $ str [ $ i ] != '0' && $ str [ $ i ] != '1' ) $ oneSeen = 0 ; } return $ count ; }"}
{"text":"Driver Code","code":"$ str = \"100001abc101\" ; echo countPattern ( $ str ) ; ? >"}
{"text":"Function to return the minimum operations of the given type required to convert string s to string t","code":"< ? php function minOperations ( $ s , $ t , $ n ) { $ ct0 = 0 ; $ ct1 = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) {"}
{"text":"Characters are already equal","code":"if ( $ s [ $ i ] == $ t [ $ i ] ) continue ;"}
{"text":"Increment count of 0 s","code":"if ( $ s [ $ i ] == '0' ) $ ct0 ++ ;"}
{"text":"Increment count of 1 s","code":"else $ ct1 ++ ; } return max ( $ ct0 , $ ct1 ) ; }"}
{"text":"Driver code","code":"$ s = \"010\" ; $ t = \"101\" ; $ n = strlen ( $ s ) ; echo minOperations ( $ s , $ t , $ n ) ; ? >"}
{"text":"Function to return the decrypted string","code":"< ? php function decryptString ( $ str , $ n ) {"}
{"text":"Initial jump will be 1","code":"$ i = 0 ; $ jump = 1 ; $ decryptedStr = \" \" ; while ( $ i < $ n ) { $ decryptedStr . = $ str [ $ i ] ; $ i += $ jump ;"}
{"text":"Increment jump by 1 with every character","code":"$ jump ++ ; } return $ decryptedStr ; }"}
{"text":"Driver code","code":"$ str = \" geeeeekkkksssss \" ; $ n = strlen ( $ str ) ; echo decryptString ( $ str , $ n ) ; ? >"}
{"text":"Function to check which bit is to be flipped","code":"< ? php function bitToBeFlipped ( $ s ) {"}
{"text":"variable to store first and last character of string","code":"$ last = $ s [ strlen ( $ s ) - 1 ] ; $ first = $ s [ 0 ] ;"}
{"text":"Check if first and last characters are equal , if yes , then return the character which is not at last","code":"if ( $ last == $ first ) { if ( $ last == '0' ) { return '1' ; } else { return '0' ; } }"}
{"text":"else return last","code":"else if ( $ last != $ first ) { return $ last ; } }"}
{"text":"Driver Code","code":"$ s = \"1101011000\" ; echo bitToBeFlipped ( $ s ) ; ? >"}
{"text":"Returns numeric value of a subsequence of s . The subsequence to be picked is decided using bit pattern of num ( We pick all thosedigits for which there is a set bit in num )","code":"< ? php function findSubSequence ( $ s , $ num ) {"}
{"text":"Initialize the result","code":"$ res = 0 ;"}
{"text":"till n != 0","code":"$ i = 0 ; while ( $ num ) {"}
{"text":"if i - th bit is set then add this number","code":"if ( $ num & 1 ) $ res += $ s [ $ i ] - '0' ; $ i ++ ;"}
{"text":"right shintift i","code":"$ num = $ num >> 1 ; } return $ res ; }"}
{"text":"function to find combined sum of all individual subsequence sum","code":"function combinedSum ( string $ s ) {"}
{"text":"length of string","code":"$ n = strlen ( $ s ) ;"}
{"text":"stores the combined","code":"$ c_sum = 0 ;"}
{"text":"2 ^ n - 1 subsequences","code":"$ range = ( 1 << $ n ) - 1 ;"}
{"text":"loop for all subsequences","code":"for ( $ i = 0 ; $ i <= $ range ; $ i ++ ) $ c_sum += findSubSequence ( $ s , $ i ) ;"}
{"text":"returns the combined sum","code":"return $ c_sum ; }"}
{"text":"Driver Code","code":"$ s = \"123\" ; echo combinedSum ( $ s ) ; ? >"}
{"text":"Function to find the subsequence","code":"< ? php function findSubsequence ( $ str , $ k ) {"}
{"text":"Taking an extra array to keep record for character count in s","code":"$ a = array ( 1024 ) ; for ( $ i = 0 ; $ i < 26 ; $ i ++ ) $ a [ $ i ] = 0 ;"}
{"text":"Counting occurrences of all characters in str [ ]","code":"for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) { $ temp = ord ( $ str [ $ i ] ) - ord ( ' a ' ) ; $ a [ $ temp ] += 1 ; }"}
{"text":"Printing characters with count >= k in same order as they appear in str .","code":"for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) if ( $ a [ ord ( $ str [ $ i ] ) - ord ( ' a ' ) ] >= $ k ) echo $ str [ $ i ] ; }"}
{"text":"Driver code","code":"$ k = 2 ; findSubsequence ( \" geeksforgeeks \" , $ k ) ; ? >"}
{"text":"PHP program to convert a sentence to gOOGLE cASE .","code":"< ? php function convert ( $ str ) {"}
{"text":"empty strings","code":"$ w = \" \" ; $ z = \" \" ;"}
{"text":"convert input to upper case","code":"$ str = strtoupper ( $ str ) . \" \u2581 \" ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) {"}
{"text":"checki if character is not a space and adding it to $w","code":"$ ch = $ str [ $ i ] ; if ( $ ch != ' \u2581 ' ) $ w = $ w . $ ch ; else {"}
{"text":"converting first character to lower case and subsequent initial letter of another word to lower case","code":"$ z = $ z . strtolower ( $ w [ 0 ] ) . substr ( $ w , 1 ) . \" \u2581 \" ; $ w = \" \" ; } } return $ z ; }"}
{"text":"Driver code","code":"$ str = \" I \u2581 got \u2581 intern \u2581 at \u2581 geeksforgeeks \" ; echo ( convert ( $ str ) ) ; ? >"}
{"text":"PHP program to count the number of occurrence of a word in the given string","code":"< ? php function countOccurrences ( $ str , $ word ) {"}
{"text":"split the string by spaces","code":"$ a = explode ( \" \u2581 \" , $ str ) ;"}
{"text":"search for pattern in string","code":"$ count = 0 ; for ( $ i = 0 ; $ i < sizeof ( $ a ) ; $ i ++ ) {"}
{"text":"if match found increase count","code":"if ( $ word == $ a [ $ i ] ) $ count ++ ; } return $ count ; }"}
{"text":"Driver code","code":"$ str = \" GeeksforGeeks \u2581 A \u2581 computer \u2581 science \u2581 portal \u2581 for \u2581 geeks \u2581 \" ; $ word = \" portal \" ; echo ( countOccurrences ( $ str , $ word ) ) ; ? >"}
{"text":"Function to generate permutations","code":"< ? php function permute ( $ input ) { $ n = strlen ( $ input ) ;"}
{"text":"Number of permutations is 2 ^ n","code":"$ max = 1 << $ n ;"}
{"text":"Converting string to lower case","code":"$ input = strtolower ( $ input ) ;"}
{"text":"Using all subsequences and permuting them","code":"for ( $ i = 0 ; $ i < $ max ; $ i ++ ) { $ combination = $ input ;"}
{"text":"If j - th bit is set , we convert it to upper case","code":"for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( ( ( $ i >> $ j ) & 1 ) == 1 ) $ combination [ $ j ] = chr ( ord ( $ combination [ $ j ] ) - 32 ) ; }"}
{"text":"Printing current combination","code":"echo $ combination . \" \" ; } }"}
{"text":"Driver Code","code":"permute ( \" ABC \" ) ; ? >"}
{"text":"A function to check if a string str is palindrome","code":"< ? php function isPalindrome ( $ str ) {"}
{"text":"Start from leftmost and rightmost corners of str","code":"$ l = 0 ; $ h = strlen ( $ str ) - 1 ;"}
{"text":"Keep comparing characters while they are same","code":"while ( $ h > $ l ) if ( $ str [ $ l ++ ] != $ str [ $ h -- ] ) return false ; return true ; }"}
{"text":"Returns count of minimum palindromic subsequences to be removed to make string empty","code":"function minRemovals ( $ str ) {"}
{"text":"If string is empty","code":"if ( $ str [ 0 ] == ' ' ) return 0 ;"}
{"text":"If string is palindrome","code":"if ( isPalindrome ( $ str ) ) return 1 ;"}
{"text":"If string is not palindrome","code":"return 2 ; }"}
{"text":"Driver Code","code":"echo minRemovals ( \" 010010 \" ) , \u2581 \" \" echo minRemovals ( \"0100101\" ) , \" STRNEWLINE \" ; ? >"}
{"text":"Iterative function to calculate ( x ^ y ) % p in O ( log y )","code":"< ? php function power ( $ x , $ y , $ p ) {"}
{"text":"Initialize result","code":"$ res = 1 ;"}
{"text":"Update x if it is >= p","code":"$ x = $ x % $ p ; while ( $ y > 0 ) {"}
{"text":"If y is odd , multiply x with result","code":"if ( $ y & 1 ) $ res = ( $ res * $ x ) % $ p ;"}
{"text":"y must be even now y = y 2","code":"$ y = $ y >> 1 ; $ x = ( $ x * $ x ) % $ p ; } return $ res ; }"}
{"text":"Function to return XXX ... . . ( N times ) % M","code":"function findModuloByM ( $ X , $ N , $ M ) {"}
{"text":"Return the mod by M of smaller numbers","code":"if ( $ N < 6 ) {"}
{"text":"Creating a string of N X 's","code":"$ temp = chr ( 48 + $ X ) * $ N ;"}
{"text":"Converting the string to int and calculating the modulo","code":"$ res = intval ( $ temp ) % $ M ; return $ res ; }"}
{"text":"Checking the parity of N","code":"if ( $ N % 2 == 0 ) {"}
{"text":"Dividing the number into equal half","code":"$ half = findModuloByM ( $ X , ( int ) ( $ N \/ 2 ) , $ M ) % $ M ;"}
{"text":"Utilizing the formula for even N","code":"$ res = ( $ half * power ( 10 , ( int ) ( $ N \/ 2 ) , $ M ) + $ half ) % $ M ; return $ res ; } else {"}
{"text":"Dividing the number into equal half","code":"$ half = findModuloByM ( $ X , ( int ) ( $ N \/ 2 ) , $ M ) % $ M ;"}
{"text":"Utilizing the formula for odd N","code":"$ res = ( $ half * power ( 10 , ( int ) ( $ N \/ 2 ) + 1 , $ M ) + $ half * 10 + $ X ) % $ M ; return $ res ; } }"}
{"text":"Driver code","code":"$ X = 6 ; $ N = 14 ; $ M = 9 ;"}
{"text":"Print XXX ... ( N times ) % M","code":"print ( findModuloByM ( $ X , $ N , $ M ) ) ; ? >"}
{"text":"Function to find the length of the direct common tangent","code":"< ? php function lengtang ( $ r1 , $ r2 , $ d ) { echo \" The \u2581 length \u2581 of \u2581 the \u2581 direct \u2581 common \u2581 tangent \u2581 is \u2581 \" , sqrt ( pow ( $ d , 2 ) - pow ( ( $ r1 - $ r2 ) , 2 ) ) ; }"}
{"text":"Driver code","code":"$ r1 = 4 ; $ r2 = 6 ; $ d = 3 ; lengtang ( $ r1 , $ r2 , $ d ) ; ? >"}
{"text":"Function to find the radius","code":"< ? php function rad ( $ d , $ h ) { echo \" The \u2581 radius \u2581 of \u2581 the \u2581 circle \u2581 is \u2581 \" , ( ( $ d * $ d ) \/ ( 8 * $ h ) + $ h \/ 2 ) , \" STRNEWLINE \" ; }"}
{"text":"Driver code","code":"$ d = 4 ; $ h = 1 ; rad ( $ d , $ h ) ; ? >"}
{"text":"Function to find the shortest distance","code":"< ? php function shortdis ( $ r , $ d ) { echo \" The \u2581 shortest \u2581 distance \u2581 \" ; echo \" from \u2581 the \u2581 chord \u2581 to \u2581 centre \u2581 \" ; echo sqrt ( ( $ r * $ r ) - ( ( $ d * $ d ) \/ 4 ) ) ; }"}
{"text":"Driver code","code":"$ r = 4 ; $ d = 3 ; shortdis ( $ r , $ d ) ; ? >"}
{"text":"Function to find the length of the direct common tangent","code":"< ? php function lengtang ( $ r1 , $ r2 , $ d ) { echo \" The \u2581 length \u2581 of \u2581 the \u2581 direct \" , \" \u2581 common \u2581 tangent \u2581 is \u2581 \" , sqrt ( pow ( $ d , 2 ) - pow ( ( $ r1 - $ r2 ) , 2 ) ) , \" STRNEWLINE \" ; }"}
{"text":"Driver code","code":"$ r1 = 4 ; $ r2 = 6 ; $ d = 12 ; lengtang ( $ r1 , $ r2 , $ d ) ; ? >"}
{"text":"Function to find the side of the square","code":"< ? php function square ( $ a ) {"}
{"text":"the side cannot be negative","code":"if ( $ a < 0 ) return -1 ;"}
{"text":"side of the square","code":"$ x = 0.464 * $ a ; return $ x ; }"}
{"text":"Driver code","code":"$ a = 5 ; echo square ( $ a ) ; ? >"}
{"text":"Function to find the apothem of a regular polygon","code":"< ? php function polyapothem ( $ n , $ a ) {"}
{"text":"Side and side length cannot be negative","code":"if ( $ a < 0 && $ n < 0 ) return -1 ;"}
{"text":"Degree converted to radians","code":"return $ a \/ ( 2 * tan ( ( 180 \/ $ n ) * 3.14159 \/ 180 ) ) ; }"}
{"text":"Driver code","code":"$ a = 9 ; $ n = 6 ; echo polyapothem ( $ n , $ a ) . \" STRNEWLINE \" ; ? >"}
{"text":"Function to find the area of a regular polygon","code":"< ? php function polyarea ( $ n , $ a ) {"}
{"text":"Side and side length cannot be negative","code":"if ( $ a < 0 && $ n < 0 ) return -1 ;"}
{"text":"Area degree converted to radians","code":"$ A = ( $ a * $ a * $ n ) \/ ( 4 * tan ( ( 180 \/ $ n ) * 3.14159 \/ 180 ) ) ; return $ A ; }"}
{"text":"Driver code","code":"$ a = 9 ; $ n = 6 ; echo round ( polyarea ( $ n , $ a ) , 3 ) ; ? >"}
{"text":"Function to calculate the side of the polygon circumscribed in a circle","code":"< ? php function calculateSide ( $ n , $ r ) { $ theta ; $ theta_in_radians ; $ theta = 360 \/ $ n ; $ theta_in_radians = $ theta * 3.14 \/ 180 ; return 2 * $ r * sin ( $ theta_in_radians \/ 2 ) ; }"}
{"text":"Total sides of the polygon","code":"$ n = 3 ;"}
{"text":"Radius of the circumscribing circle","code":"$ r = 5 ; echo calculateSide ( $ n , $ r ) ; ? >"}
{"text":"Function to find the biggest right circular cylinder","code":"< ? php function cyl ( $ r , $ R , $ h ) {"}
{"text":"radii and height cannot be negative","code":"if ( $ h < 0 && $ r < 0 && $ R < 0 ) return -1 ;"}
{"text":"radius of right circular cylinder","code":"$ r1 = $ r ;"}
{"text":"height of right circular cylinder","code":"$ h1 = $ h ;"}
{"text":"volume of right circular cylinder","code":"$ V = ( 3.14 * pow ( $ r1 , 2 ) * $ h1 ) ; return $ V ; }"}
{"text":"Driver code","code":"$ r = 7 ; $ R = 11 ; $ h = 6 ; echo cyl ( $ r , $ R , $ h ) ;"}
{"text":"Function to calculate the perimeter","code":"< ? php function Perimeter ( $ s , $ n ) { $ perimeter = 1 ;"}
{"text":"Calculate Perimeter","code":"$ perimeter = $ n * $ s ; return $ perimeter ; }"}
{"text":"Get the number of sides","code":"$ n = 5 ;"}
{"text":"Get the length of side","code":"$ s = 2.5 ;"}
{"text":"find perimeter","code":"$ peri = Perimeter ( $ s , $ n ) ; echo \" Perimeter \u2581 of \u2581 Regular \u2581 Polygon \" , \" \u2581 with \u2581 \" , $ n , \" \u2581 sides \u2581 of \u2581 length \u2581 \" , $ s , \" \u2581 = \u2581 \" , $ peri ; ? >"}
{"text":"Function to find the area of the biggest rhombus","code":"< ? php function rhombusarea ( $ l , $ b ) {"}
{"text":"the length and breadth cannot be negative","code":"if ( $ l < 0 $ b < 0 ) return -1 ;"}
{"text":"area of the rhombus","code":"return ( $ l * $ b ) \/ 2 ; }"}
{"text":"Driver code","code":"$ l = 16 ; $ b = 6 ; echo rhombusarea ( $ l , $ b ) . \" STRNEWLINE \" ;"}
{"text":"function to find if given point lies inside a given rectangle or not .","code":"< ? php function FindPoint ( $ x1 , $ y1 , $ x2 , $ y2 , $ x , $ y ) { if ( $ x > $ x1 and $ x < $ x2 and $ y > $ y1 and $ y < $ y2 ) return true ; return false ; }"}
{"text":"bottom - left and top - right corners of rectangle","code":"$ x1 = 0 ; $ y1 = 0 ; $ x2 = 10 ; $ y2 = 8 ;"}
{"text":"given point","code":"$ x = 1 ; $ y = 5 ;"}
{"text":"function call","code":"if ( FindPoint ( $ x1 , $ y1 , $ x2 , $ y2 , $ x , $ y ) ) echo \" Yes \" ; else echo \" No \" ; ? >"}
{"text":"Function to find distance","code":"< ? php function shortest_distance ( $ x1 , $ y1 , $ z1 , $ a , $ b , $ c , $ d ) { $ d = abs ( ( $ a * $ x1 + $ b * $ y1 + $ c * $ z1 + $ d ) ) ; $ e = sqrt ( $ a * $ a + $ b * $ b + $ c * $ c ) ; echo \" Perpendicular \u2581 distance \u2581 is \u2581 \" . $ d \/ $ e ; }"}
{"text":"Driver Code","code":"$ x1 = 4 ; $ y1 = -4 ; $ z1 = 3 ; $ a = 2 ; $ b = -2 ; $ c = 5 ; $ d = 8 ;"}
{"text":"function call","code":"shortest_distance ( $ x1 , $ y1 , $ z1 , $ a , $ b , $ c , $ d ) ; ? >"}
{"text":"function to find the Volume of triangular prism","code":"< ? php function findVolume ( $ l , $ b , $ h ) {"}
{"text":"formula to find Volume","code":"$ volume = ( $ l * $ b * $ h ) \/ 2 ; return $ volume ; }"}
{"text":"Driver Code","code":"$ l = 18 ; $ b = 12 ; $ h = 9 ;"}
{"text":"function calling","code":"echo \" Volume \u2581 of \u2581 triangular \u2581 prism : \u2581 \" . findVolume ( $ l , $ b , $ h ) ; ? >"}
{"text":"function to find the midpoint of a line","code":"< ? php function midpoint ( $ x1 , $ x2 , $ y1 , $ y2 ) { echo ( ( float ) ( $ x1 + $ x2 ) \/ 2 . \" \u2581 , \u2581 \" . ( float ) ( $ y1 + $ y2 ) \/ 2 ) ; }"}
{"text":"Driver Code","code":"$ x1 = -1 ; $ y1 = 2 ; $ x2 = 3 ; $ y2 = -6 ; midpoint ( $ x1 , $ x2 , $ y1 , $ y2 ) ; ? >"}
{"text":"function to calculate arc length","code":"< ? php function arcLength ( $ diameter , $ angle ) { $ pi = 22.0 \/ 7.0 ; $ arc ; if ( $ angle >= 360 ) { echo \" Angle \u2581 cannot \" , \" \u2581 be \u2581 formed \" ; return 0 ; } else { $ arc = ( $ pi * $ diameter ) * ( $ angle \/ 360.0 ) ; return $ arc ; } }"}
{"text":"Driver Code","code":"$ diameter = 25.0 ; $ angle = 45.0 ; $ arc_len = arcLength ( $ diameter , $ angle ) ; echo ( $ arc_len ) ; ? >"}
{"text":"PHP program to check if a line touches or intersects or outside a circle .","code":"< ? php function checkCollision ( $ a , $ b , $ c , $ x , $ y , $ radius ) {"}
{"text":"Finding the distance of line from center .","code":"$ dist = ( abs ( $ a * $ x + $ b * $ y + $ c ) ) \/ sqrt ( $ a * $ a + $ b * $ b ) ;"}
{"text":"Checking if the distance is less than , greater than or equal to radius .","code":"if ( $ radius == $ dist ) echo \" Touch \" ; else if ( $ radius > $ dist ) echo \" Intersect \" ; else echo \" Outside \" ; }"}
{"text":"Driver Code","code":"$ radius = 5 ; $ x = 0 ; $ y = 0 ; $ a = 3 ; $ b = 4 ; $ c = 25 ; checkCollision ( $ a , $ b , $ c , $ x , $ y , $ radius ) ; ? >"}
{"text":"( X [ i ] , Y [ i ] ) are coordinates of i 'th point.","code":"< ? php function polygonArea ( $ X , $ Y , $ n ) {"}
{"text":"Initialize area","code":"$ area = 0.0 ;"}
{"text":"Calculate value of shoelace formula","code":"$ j = $ n - 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ area += ( $ X [ $ j ] + $ X [ $ i ] ) * ( $ Y [ $ j ] - $ Y [ $ i ] ) ;"}
{"text":"j is previous vertex to i","code":"$ j = $ i ; }"}
{"text":"Return absolute value","code":"return abs ( $ area \/ 2.0 ) ; }"}
{"text":"Driver Code","code":"$ X = array ( 0 , 2 , 4 ) ; $ Y = array ( 1 , 3 , 7 ) ; $ n = count ( $ X ) ; echo polygonArea ( $ X , $ Y , $ n ) ; ? >"}
{"text":"Function to return the average of x and y using bit operations","code":"< ? php function getAverage ( $ x , $ y ) {"}
{"text":"Calculate the average Floor value of ( x + y ) \/ 2","code":"$ avg = ( $ x & $ y ) + ( ( $ x ^ $ y ) >> 1 ) ; return $ avg ; }"}
{"text":"Driver code","code":"$ x = 10 ; $ y = 9 ; echo getAverage ( $ x , $ y ) ; ? >"}
{"text":"Function to find the smallest index such that there are no 0 or 1 to its right","code":"< ? php function smallestIndex ( $ a , $ n ) {"}
{"text":"Initially","code":"$ right1 = 0 ; $ right0 = 0 ;"}
{"text":"Traverse in the array","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) {"}
{"text":"Check if array element is 1","code":"if ( $ a [ $ i ] == 1 ) $ right1 = $ i ;"}
{"text":"a [ i ] = 0","code":"else $ right0 = $ i ; }"}
{"text":"Return minimum of both","code":"return min ( $ right1 , $ right0 ) ; }"}
{"text":"Driver code","code":"$ a = array ( 1 , 1 , 1 , 0 , 0 , 1 , 0 , 1 , 1 ) ; $ n = sizeof ( $ a ) ; echo smallestIndex ( $ a , $ n ) ; ? >"}
{"text":"Function to return the count of squares that can be visited by king in m moves","code":"< ? php function countSquares ( $ r , $ c , $ m ) {"}
{"text":"To store the count of squares","code":"$ squares = 0 ;"}
{"text":"Check all squares of the chessboard","code":"for ( $ i = 1 ; $ i <= 8 ; $ i ++ ) { for ( $ j = 1 ; $ j <= 8 ; $ j ++ ) {"}
{"text":"Check if square ( i , j ) is at a distance <= m units from king 's current position","code":"if ( max ( abs ( $ i - $ r ) , abs ( $ j - $ c ) ) <= $ m ) $ squares ++ ; } }"}
{"text":"Return count of squares","code":"return $ squares ; }"}
{"text":"Driver code","code":"$ r = 4 ; $ c = 4 ; $ m = 1 ; echo countSquares ( $ r , $ c , $ m ) ; ? >"}
{"text":"Function to return the count of required numbers","code":"< ? php function countNumbers ( $ L , $ R , $ K ) { if ( $ K == 9 ) $ K = 0 ;"}
{"text":"Count of numbers present in given range","code":"$ totalnumbers = $ R - $ L + 1 ;"}
{"text":"Number of groups of 9 elements starting from L","code":"$ factor9 = intval ( $ totalnumbers \/ 9 ) ;"}
{"text":"Left over elements not covered in factor 9","code":"$ rem = $ totalnumbers % 9 ;"}
{"text":"One Number in each group of 9","code":"$ ans = $ factor9 ;"}
{"text":"To check if any number in rem satisfy the property","code":"for ( $ i = $ R ; $ i > $ R - $ rem ; $ i -- ) { $ rem1 = $ i % 9 ; if ( $ rem1 == $ K ) $ ans ++ ; } return $ ans ; }"}
{"text":"Driver code","code":"$ L = 10 ; $ R = 22 ; $ K = 3 ; echo countNumbers ( $ L , $ R , $ K ) ; ? >"}
{"text":"Function to print the result for every query","code":"< ? php function BalanceArray ( $ A , & $ Q ) { $ ANS = array ( ) ; $ sum = 0 ; for ( $ i = 0 ; $ i < count ( $ A ) ; $ i ++ )"}
{"text":"If current element is even","code":"if ( $ A [ $ i ] % 2 == 0 ) $ sum = $ sum + $ A [ $ i ] ; for ( $ i = 0 ; $ i < count ( $ Q ) ; $ i ++ ) { $ index = $ Q [ $ i ] [ 0 ] ; $ value = $ Q [ $ i ] [ 1 ] ;"}
{"text":"If element is even then remove it from sum","code":"if ( $ A [ $ index ] % 2 == 0 ) $ sum = $ sum - $ A [ $ index ] ; $ A [ $ index ] = $ A [ $ index ] + $ value ;"}
{"text":"If the value becomes even after updating","code":"if ( $ A [ $ index ] % 2 == 0 ) $ sum = $ sum + $ A [ $ index ] ;"}
{"text":"Store sum for each query","code":"array_push ( $ ANS , $ sum ) ; }"}
{"text":"Print the result for every query","code":"for ( $ i = 0 ; $ i < count ( $ ANS ) ; $ i ++ ) echo $ ANS [ $ i ] . \" \u2581 \" ; }"}
{"text":"Driver code","code":"$ A = array ( 1 , 2 , 3 , 4 ) ; $ Q = array ( array ( 0 , 1 ) , array ( 1 , -3 ) , array ( 0 , -4 ) , array ( 3 , 2 ) ) ; BalanceArray ( $ A , $ Q ) ; ? >"}
{"text":"Function that calculates number of Hamiltonian cycle","code":"< ? php function Cycles ( $ N ) { $ fact = 1 ; $ result = 0 ; $ result = $ N - 1 ;"}
{"text":"Calculating factorial","code":"$ i = $ result ; while ( $ i > 0 ) { $ fact = $ fact * $ i ; $ i -- ; } return floor ( $ fact \/ 2 ) ; }"}
{"text":"Driver code","code":"$ N = 5 ; $ Number = Cycles ( $ N ) ; echo \" Hamiltonian \u2581 cycles \u2581 = \u2581 \" , $ Number ; ? >"}
{"text":"Function that returns true if n contains digit m exactly k times","code":"< ? php function digitWell ( $ n , $ m , $ k ) { $ cnt = 0 ; while ( $ n > 0 ) { if ( $ n % 10 == $ m ) ++ $ cnt ; $ n = floor ( $ n \/ 10 ) ; } return $ cnt == $ k ; }"}
{"text":"Function to return the smallest integer > n with digit m occurring exactly k times","code":"function findInt ( $ n , $ m , $ k ) { $ i = $ n + 1 ; while ( true ) { if ( digitWell ( $ i , $ m , $ k ) ) return $ i ; $ i ++ ; } }"}
{"text":"Driver code","code":"$ n = 111 ; $ m = 2 ; $ k = 2 ; echo findInt ( $ n , $ m , $ k ) ; ? >"}
{"text":"Function to return the count of odd numbers in the array","code":"< ? php function countOdd ( $ arr , $ n ) {"}
{"text":"Variable to count odd numbers","code":"$ odd = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) {"}
{"text":"Odd number","code":"if ( $ arr [ $ i ] % 2 == 1 ) $ odd ++ ; } return $ odd ; }"}
{"text":"Function to return the count of valid pairs","code":"function countValidPairs ( $ arr , $ n ) { $ odd = countOdd ( $ arr , $ n ) ; return ( $ odd * ( $ odd - 1 ) ) \/ 2 ; }"}
{"text":"Driver Code","code":"$ arr = array ( 1 , 2 , 3 , 4 , 5 ) ; $ n = sizeof ( $ arr ) ; echo countValidPairs ( $ arr , $ n ) ; ? >"}
{"text":"Function to return the gcd of two numbers","code":"< ? php function gcd ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; else return gcd ( $ b , $ a % $ b ) ; }"}
{"text":"Function to return the lcm of all the elements of the array","code":"function lcmOfArray ( & $ arr , $ n ) { if ( $ n < 1 ) return 0 ; $ lcm = $ arr [ 0 ] ;"}
{"text":"To calculate lcm of two numbers multiply them and divide the result by gcd of both the numbers","code":"for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ lcm = ( $ lcm * $ arr [ $ i ] ) \/ gcd ( $ lcm , $ arr [ $ i ] ) ;"}
{"text":"Return the LCM of the array elements","code":"return $ lcm ; }"}
{"text":"Function to return the smallest perfect cube divisible by all the elements of arr [ ]","code":"function minPerfectCube ( & $ arr , $ n ) {"}
{"text":"LCM of all the elements of arr [ ]","code":"$ lcm = lcmOfArray ( $ arr , $ n ) ; $ minPerfectCube = $ lcm ; $ cnt = 0 ; while ( $ lcm > 1 && $ lcm % 2 == 0 ) { $ cnt ++ ; $ lcm \/= 2 ; }"}
{"text":"If 2 divides lcm cnt number of times","code":"if ( $ cnt % 3 == 2 ) $ minPerfectCube *= 2 ; else if ( $ cnt % 3 == 1 ) $ minPerfectCube *= 4 ; $ i = 3 ;"}
{"text":"Check all the numbers that divide lcm","code":"while ( $ lcm > 1 ) { $ cnt = 0 ; while ( $ lcm % $ i == 0 ) { $ cnt ++ ; $ lcm \/= $ i ; } if ( $ cnt % 3 == 1 ) $ minPerfectCube *= $ i * $ i ; else if ( $ cnt % 3 == 2 ) $ minPerfectCube *= $ i ; $ i += 2 ; }"}
{"text":"Return the answer","code":"return $ minPerfectCube ; }"}
{"text":"Driver code","code":"$ arr = array ( 10 , 125 , 14 , 42 , 100 ) ; $ n = sizeof ( $ arr ) ; echo ( minPerfectCube ( $ arr , $ n ) ) ; ? >"}
{"text":"Utility function to check if a number is prime or not","code":"< ? php function isPrime ( $ n ) {"}
{"text":"Corner cases","code":"if ( $ n <= 1 ) return false ; if ( $ n <= 3 ) return true ;"}
{"text":"This is checked so that we can skip middle five numbers in below loop","code":"if ( $ n % 2 == 0 $ n % 3 == 0 ) return false ; for ( $ i = 5 ; $ i * $ i <= $ n ; $ i = $ i + 6 ) if ( $ n % $ i == 0 || $ n % ( $ i + 2 ) == 0 ) return false ; return true ; }"}
{"text":"Function that returns true if n is a strong prime","code":"function isStrongPrime ( $ n ) {"}
{"text":"If n is not a prime number or n is the first prime then return false","code":"if ( ! isPrime ( $ n ) $ n == 2 ) return false ;"}
{"text":"Initialize previous_prime to n - 1 and next_prime to n + 1","code":"$ previous_prime = $ n - 1 ; $ next_prime = $ n + 1 ;"}
{"text":"Find next prime number","code":"while ( ! isPrime ( $ next_prime ) ) $ next_prime ++ ;"}
{"text":"Find previous prime number","code":"while ( ! isPrime ( $ previous_prime ) ) $ previous_prime -- ;"}
{"text":"Arithmetic mean","code":"$ mean = ( $ previous_prime + $ next_prime ) \/ 2 ;"}
{"text":"If n is a strong prime","code":"if ( $ n > $ mean ) return true ; else return false ; }"}
{"text":"Driver code","code":"$ n = 11 ; if ( isStrongPrime ( $ n ) ) echo ( \" Yes \" ) ; else echo ( \" No \" ) ; ? >"}
{"text":"function to return the required number of digits to be removed","code":"< ? php function countDigitsToBeRemoved ( $ N , $ K ) {"}
{"text":"Converting the given number into string","code":"$ s = strval ( $ N ) ;"}
{"text":"variable to store number of digits to be removed","code":"$ res = 0 ;"}
{"text":"variable to denote if atleast one zero has been found","code":"$ f_zero = 0 ; for ( $ i = strlen ( $ s ) - 1 ; $ i >= 0 ; $ i -- ) { if ( $ K == 0 ) return $ res ; if ( $ s [ $ i ] == '0' ) {"}
{"text":"zero found","code":"$ f_zero = 1 ; $ K -- ; } else $ res ++ ; }"}
{"text":"return size - 1 if K is not zero and atleast one zero is present , otherwise result","code":"if ( ! $ K ) return $ res ; else if ( $ f_zero ) return strlen ( $ s ) - 1 ; return -1 ; }"}
{"text":"Driver Code to test above function","code":"$ N = 10904025 ; $ K = 2 ; echo countDigitsToBeRemoved ( $ N , $ K ) . \" \" ; $ N = 1000 ; $ K = 5 ; echo countDigitsToBeRemoved ( $ N , $ K ) . \" \" ; $ N = 23985 ; $ K = 2 ; echo countDigitsToBeRemoved ( $ N , $ K ) ; ? >"}
{"text":"Function to return the sum of the series","code":"< ? php function getSum ( $ a , $ n ) {"}
{"text":"variable to store the answer","code":"$ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; ++ $ i ) {"}
{"text":"Math . pow ( x , y ) returns x ^ y","code":"$ sum += ( $ i \/ pow ( $ a , $ i ) ) ; } return $ sum ; }"}
{"text":"Driver code","code":"$ a = 3 ; $ n = 3 ;"}
{"text":"Print the sum of the series","code":"echo ( getSum ( $ a , $ n ) ) ; ? >"}
{"text":"Utility function to find largest prime factor of a number","code":"< ? php function largestPrimeFactor ( $ n ) {"}
{"text":"Initialize the maximum prime factor variable with the lowest one","code":"$ max = -1 ;"}
{"text":"Print the number of 2 s that divide n","code":"while ( $ n % 2 == 0 ) { $ max = 2 ;"}
{"text":"$n >>= 1 ; equivalent to n \/= 2","code":"}"}
{"text":"n must be odd at this point , thus skip the even numbers and iterate only for odd integers","code":"for ( $ i = 3 ; $ i <= sqrt ( $ n ) ; $ i += 2 ) { while ( $ n % $ i == 0 ) { $ max = $ i ; $ n = $ n \/ $ i ; } }"}
{"text":"This condition is to handle the case when n is a prime number greater than 2","code":"if ( $ n > 2 ) $ max = $ n ; return $ max ; }"}
{"text":"Function to check Unusual number","code":"function checkUnusual ( $ n ) {"}
{"text":"Get the largest Prime Factor of the number","code":"$ factor = largestPrimeFactor ( $ n ) ;"}
{"text":"Check if largest prime factor is greater than sqrt ( n )","code":"if ( $ factor > sqrt ( $ n ) ) { return true ; } else { return false ; } }"}
{"text":"Driver Code","code":"$ n = 14 ; if ( checkUnusual ( $ n ) ) { echo \" YES \" . \" STRNEWLINE \" ; } else { echo \" NO \" . \" STRNEWLINE \" ; } ? >"}
{"text":"Function to print the desired result after computation","code":"< ? php function isHalfReducible ( $ arr , $ n , $ m ) { $ frequencyHash = array_fill ( 0 , $ m + 1 , 0 ) ; $ i = 0 ; for ( ; $ i < $ n ; $ i ++ ) { $ frequencyHash [ ( $ arr [ $ i ] % ( $ m + 1 ) ) ] ++ ; } for ( $ i = 0 ; $ i <= $ m ; $ i ++ ) { if ( $ frequencyHash [ $ i ] >= ( $ n \/ 2 ) ) break ; } if ( $ i <= $ m ) echo \" Yes STRNEWLINE \" ; else echo \" No STRNEWLINE \" ; }"}
{"text":"Driver Code","code":"$ arr = array ( 8 , 16 , 32 , 3 , 12 ) ; $ n = sizeof ( $ arr ) ; $ m = 7 ; isHalfReducible ( $ arr , $ n , $ m ) ; ? >"}
{"text":"Function to check if a number is prime or not","code":"< ? php function isPrime ( $ n ) {"}
{"text":"Corner cases","code":"if ( $ n <= 1 ) return false ; if ( $ n <= 3 ) return true ;"}
{"text":"This is checked so that we can skip middle five numbers in below loop","code":"if ( $ n % 2 == 0 or $ n % 3 == 0 ) return false ; for ( $ i = 5 ; $ i * $ i <= $ n ; $ i = $ i + 6 ) { if ( $ n % $ i == 0 or $ n % ( $ i + 2 ) == 0 ) { return false ; } } return true ; }"}
{"text":"Utility function to check power of two","code":"function isPowerOfTwo ( $ n ) { return ( $ n && ! ( $ n & ( $ n - 1 ) ) ) ; }"}
{"text":"Driver Code","code":"$ n = 43 ;"}
{"text":"Check if number is prime and of the form ( 2 ^ q + 1 ) \/ 3","code":"if ( isPrime ( $ n ) && ( isPowerOfTwo ( $ n * 3 - 1 ) ) ) { echo \" YES \" ; } else { echo \" NO \" ; } ? >"}
{"text":"Function to find the area of the square","code":"< ? php function area ( $ a ) {"}
{"text":"side of hexagon cannot be negative","code":"if ( $ a < 0 ) return -1 ;"}
{"text":"area of the square","code":"$ area = pow ( ( $ a * sqrt ( 3 ) ) \/ ( sqrt ( 2 ) ) , 2 ) ; return $ area ; }"}
{"text":"Driver code","code":"$ a = 5 ; echo area ( $ a ) . \" STRNEWLINE \" ; ? >"}
{"text":"calculate Nth term of series","code":"< ? php function nthTerm ( $ n ) { return 3 * pow ( $ n , 2 ) - 4 * $ n + 2 ; }"}
{"text":"Driver code","code":"$ N = 4 ; echo nthTerm ( $ N ) . \" STRNEWLINE \" ; ? >"}
{"text":"Function to calculate the sum","code":"< ? php function calculateSum ( $ n ) { return $ n * ( $ n + 1 ) \/ 2 + pow ( ( $ n * ( $ n + 1 ) \/ 2 ) , 2 ) ; }"}
{"text":"number of terms to be included in the sum","code":"$ n = 3 ;"}
{"text":"find the Sum","code":"echo \" Sum = \" ? >"}
{"text":"Function to check if arrays are permutations of each other .","code":"< ? php function arePermutations ( $ a , $ b , $ n , $ m ) { $ sum1 = 0 ; $ sum2 = 0 ; $ mul1 = 1 ; $ mul2 = 1 ;"}
{"text":"Calculating sum and multiply of first array","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ sum1 += $ a [ $ i ] ; $ mul1 *= $ a [ $ i ] ; }"}
{"text":"Calculating sum and multiply of second array","code":"for ( $ i = 0 ; $ i < $ m ; $ i ++ ) { $ sum2 += $ b [ $ i ] ; $ mul2 *= $ b [ $ i ] ; }"}
{"text":"If sum and mul of both arrays are equal , return true , else return false .","code":"return ( ( $ sum1 == $ sum2 ) && ( $ mul1 == $ mul2 ) ) ; }"}
{"text":"Driver code","code":"$ a = array ( 1 , 3 , 2 ) ; $ b = array ( 3 , 1 , 2 ) ; $ n = sizeof ( $ a ) ; $ m = sizeof ( $ b ) ; if ( arePermutations ( $ a , $ b , $ n , $ m ) ) echo \" Yes \" . \" STRNEWLINE \" ; else echo \" No \" . \" STRNEWLINE \" ;"}
{"text":"Function to find the B start to C","code":"< ? php function Race ( $ B , $ C ) { $ result = 0 ;"}
{"text":"When B completed it 's 100 meter  then Completed meters by C is","code":"$ result = ( ( $ C * 100 ) \/ $ B ) ; return 100 - $ result ; }"}
{"text":"Driver Code","code":"$ B = 10 ; $ C = 28 ;"}
{"text":"When A completed it 's 100 meter Then completed meters of B and C is","code":"$ B = 100 - $ B ; $ C = 100 - $ C ; echo Race ( $ B , $ C ) . \" \u2581 meters \" ; ? >"}
{"text":"Function to calculate the time","code":"< ? php function T_ime ( $ arr , $ n , $ Emptypipe ) { $ fill = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ fill += 1 \/ $ arr [ $ i ] ; $ fill = $ fill - ( 1 \/ $ Emptypipe ) ; return 1 \/ $ fill ; }"}
{"text":"Driver Code","code":"$ arr = array ( 12 , 14 ) ; $ Emptypipe = 30 ; $ n = count ( $ arr ) ; echo ( int ) T_ime ( $ arr , $ n , $ Emptypipe ) . \" \u2581 Hours \" ; ? >"}
{"text":"Function to check Divisibility","code":"< ? php function check ( $ n ) { $ sum = 0 ;"}
{"text":"Sum of all individual digits","code":"while ( $ n != 0 ) { $ sum += $ n % 10 ; $ n = ( int ) ( $ n \/ 10 ) ; }"}
{"text":"Condition","code":"if ( $ sum % 7 == 0 ) return 1 ; else return 0 ; }"}
{"text":"Octal number","code":"$ n = 25 ; ( check ( $ n ) == 1 ) ? print ( \" YES STRNEWLINE \" ) : print ( \" NO STRNEWLINE \" ) ; ? >"}
{"text":"PHP program to find sum of prime divisors of N","code":"< ? php $ N = 1000005 ;"}
{"text":"Function to check if the number is prime or not .","code":"function isPrime ( $ n ) { global $ N ;"}
{"text":"Corner cases","code":"if ( $ n <= 1 ) return false ; if ( $ n <= 3 ) return true ;"}
{"text":"This is checked so that we can skip middle five numbers in below loop","code":"if ( $ n % 2 == 0 $ n % 3 == 0 ) return false ; for ( $ i = 5 ; $ i * $ i <= $ n ; $ i = $ i + 6 ) if ( $ n % $ i == 0 || $ n % ( $ i + 2 ) == 0 ) return false ; return true ; }"}
{"text":"function to find sum of prime divisors of N","code":"function SumOfPrimeDivisors ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { if ( $ n % $ i == 0 ) { if ( isPrime ( $ i ) ) $ sum += $ i ; } } return $ sum ; }"}
{"text":"Driver code","code":"$ n = 60 ; echo \" Sum \u2581 of \u2581 prime \u2581 divisors \u2581 of \u2581 60 \u2581 is \u2581 \" . SumOfPrimeDivisors ( $ n ) ; ? >"}
{"text":"function to find prime divisors of all numbers from 1 to n","code":"< ? php function Sum ( $ N ) { for ( $ i = 0 ; $ i <= $ N ; $ i ++ ) $ SumOfPrimeDivisors [ $ i ] = 0 ; for ( $ i = 2 ; $ i <= $ N ; ++ $ i ) {"}
{"text":"if the number is prime","code":"if ( ! $ SumOfPrimeDivisors [ $ i ] ) {"}
{"text":"add this prime to all it 's multiples","code":"for ( $ j = $ i ; $ j <= $ N ; $ j += $ i ) { $ SumOfPrimeDivisors [ $ j ] += $ i ; } } } return $ SumOfPrimeDivisors [ $ N ] ; }"}
{"text":"Driver code","code":"$ N = 60 ; echo \" Sum \u2581 of \u2581 prime \u2581 divisors \u2581 of \u2581 60 \u2581 is \u2581 \" . Sum ( $ N ) ; ? >"}
{"text":"Function to find power","code":"< ? php function power ( $ x , $ y , $ p ) {"}
{"text":"Update x if it is more than or equal to p","code":"$ x = $ x % $ p ; while ( $ y > 0 ) {"}
{"text":"If y is odd , multiply x with the result","code":"if ( $ y & 1 ) $ res = ( $ res * $ x ) % $ p ;"}
{"text":"y must be even now $y = $y >> 1 ; y = y \/ 2","code":"$ x = ( $ x * $ x ) % $ p ; } return $ res ; }"}
{"text":"Driver Code","code":"$ a = 3 ;"}
{"text":"String input as b is very large","code":"$ b = \"100000000000000000000000000\" ; $ remainderB = 0 ; $ MOD = 1000000007 ;"}
{"text":"Reduce the number B to a small number using Fermat Little","code":"for ( $ i = 0 ; $ i < strlen ( $ b ) ; $ i ++ ) $ remainderB = ( $ remainderB * 10 + $ b [ $ i ] - '0' ) % ( $ MOD - 1 ) ; echo power ( $ a , $ remainderB , $ MOD ) ; ? >"}
{"text":"Function to find the square of 333. . .333 , 666. . .666 and 999. . .999","code":"< ? php function find_Square_369 ( $ num ) {"}
{"text":"if the number is 333. . .333","code":"if ( $ num [ 0 ] == '3' ) { $ a = '1' ; $ b = '0' ; $ c = '8' ; $ d = '9' ; }"}
{"text":"if the number is 666. . .666","code":"else if ( $ num [ 0 ] == ' 6 ' ) { $ a = '4' ; $ b = '3' ; $ c = '5' ; $ d = '6' ; }"}
{"text":"if the number is 999. . .999","code":"else { $ a = '9' ; $ b = '8' ; $ c = '0' ; $ d = '1' ; }"}
{"text":"variable for hold result","code":"$ result = \" \" ;"}
{"text":"find the no of digit","code":"$ size = strlen ( $ num ) ;"}
{"text":"add size - 1 time a in result","code":"for ( $ i = 1 ; $ i < $ size ; $ i ++ ) $ result = $ result . $ a ;"}
{"text":"add one time b in result","code":"$ result = $ result . $ b ;"}
{"text":"add size - 1 time c in result","code":"for ( $ i = 1 ; $ i < $ size ; $ i ++ ) $ result = $ result . $ c ;"}
{"text":"add one time d in result","code":"$ result = $ result . $ d ;"}
{"text":"return result","code":"return $ result ; }"}
{"text":"Drivers code","code":"$ num_3 = \"3333\" ; $ num_6 = \"6666\" ; $ num_9 = \"9999\" ; $ result = \" \" ;"}
{"text":"find square of 33. .33","code":"$ result = find_Square_369 ( $ num_3 ) ; echo \" Square \u2581 of \u2581 \" . $ num_3 . \" \u2581 is \u2581 : \u2581 \" . $ result . \" STRNEWLINE \" ;"}
{"text":"find square of 66. .66","code":"$ result = find_Square_369 ( $ num_6 ) ; echo \" Square \u2581 of \u2581 \" . $ num_6 . \" \u2581 is \u2581 : \u2581 \" . $ result . \" STRNEWLINE \" ;"}
{"text":"find square of 66. .66","code":"$ result = find_Square_369 ( $ num_9 ) ; echo \" Square \u2581 of \u2581 \" . $ num_9 . \" \u2581 is \u2581 : \u2581 \" . $ result . \" STRNEWLINE \" ; return 0 ; ? >"}
{"text":"Function to find the required factorial","code":"< ? php function fact ( $ n ) { if ( $ n == 0 $ n == 1 ) return 1 ; $ ans = 1 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ ans = $ ans * $ i ; return $ ans ; }"}
{"text":"Function to find nCr","code":"function nCr ( $ n , $ r ) { $ Nr = $ n ; $ Dr = 1 ; $ ans = 1 ; for ( $ i = 1 ; $ i <= $ r ; $ i ++ ) { $ ans = ( $ ans * $ Nr ) \/ ( $ Dr ) ; $ Nr -- ; $ Dr ++ ; } return $ ans ; }"}
{"text":"function to find the number of ways","code":"function solve ( $ n ) { $ N = 2 * $ n - 2 ; $ R = $ n - 1 ; return nCr ( $ N , $ R ) * fact ( $ n - 1 ) ; }"}
{"text":"Driver code","code":"$ n = 6 ; echo solve ( $ n ) ; ? >"}
{"text":"PHP program to find Pythagorean Triplet of given sum .","code":"< ? php function pythagoreanTriplet ( $ n ) {"}
{"text":"Considering triplets in sorted order . The value of first element in sorted triplet can be at - most n \/ 3.","code":"for ( $ i = 1 ; $ i <= $ n \/ 3 ; $ i ++ ) {"}
{"text":"The value of second element must be less than equal to n \/ 2","code":"for ( $ j = $ i + 1 ; $ j <= $ n \/ 2 ; $ j ++ ) { $ k = $ n - $ i - $ j ; if ( $ i * $ i + $ j * $ j == $ k * $ k ) { echo $ i , \" , \u2581 \" , $ j , \" , \u2581 \" , $ k ; return ; } } } echo \" No \u2581 Triplet \" ; }"}
{"text":"Driver Code","code":"$ n = 12 ; pythagoreanTriplet ( $ n ) ; ? >"}
{"text":"function to calculate factorial of a number","code":"< ? php function factorial ( $ n ) { $ f = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ f *= $ i ; return $ f ; }"}
{"text":"function to print the series","code":"function series ( $ A , $ X , $ n ) {"}
{"text":"calculating the value of n !","code":"$ nFact = factorial ( $ n ) ;"}
{"text":"loop to display the series","code":"for ( $ i = 0 ; $ i < $ n + 1 ; $ i ++ ) {"}
{"text":"For calculating the value of nCr","code":"$ niFact = factorial ( $ n - $ i ) ; $ iFact = factorial ( $ i ) ;"}
{"text":"calculating the value of A to the power k and X to the power k","code":"$ aPow = pow ( $ A , $ n - $ i ) ; $ xPow = pow ( $ X , $ i ) ;"}
{"text":"display the series","code":"echo ( $ nFact * $ aPow * $ xPow ) \/ ( $ niFact * $ iFact ) , \" \" ; } }"}
{"text":"Driver Code","code":"$ A = 3 ; $ X = 4 ; $ n = 5 ; series ( $ A , $ X , $ n ) ; ? >"}
{"text":"function to calculate series sum","code":"< ? php function seiresSum ( $ n , $ a ) { $ res = 0 ; for ( $ i = 0 ; $ i < 2 * $ n ; $ i ++ ) { if ( $ i % 2 == 0 ) $ res += $ a [ $ i ] * $ a [ $ i ] ; else $ res -= $ a [ $ i ] * $ a [ $ i ] ; } return $ res ; }"}
{"text":"Driver Code","code":"$ n = 2 ; $ a = array ( 1 , 2 , 3 , 4 ) ; echo seiresSum ( $ n , $ a ) ; ? >"}
{"text":"Function to return power of a no . ' r ' in factorial of n","code":"< ? php function power ( $ n , $ r ) {"}
{"text":"Keep dividing n by powers of ' r ' and update count","code":"$ count = 0 ; for ( $ i = $ r ; ( $ n \/ $ i ) >= 1 ; $ i = $ i * $ r ) $ count += $ n \/ $ i ; return $ count ; }"}
{"text":"Driver Code","code":"$ n = 6 ; $ r = 3 ; echo power ( $ n , $ r ) ; ? >"}
{"text":"Returns the Avg of first n odd numbers","code":"< ? php function avg_of_odd_num ( $ n ) {"}
{"text":"sum of first n odd number","code":"$ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += ( 2 * $ i + 1 ) ;"}
{"text":"Average of first n odd numbers","code":"return $ sum \/ $ n ; }"}
{"text":"Driver Code","code":"$ n = 20 ; echo ( avg_of_odd_num ( $ n ) ) ; ? >"}
{"text":"Return the average of sum of first n odd numbers","code":"< ? php function avg_of_odd_num ( $ n ) { return $ n ; }"}
{"text":"Driver Code","code":"$ n = 8 ; echo ( avg_of_odd_num ( $ n ) ) ; ? >"}
{"text":"function to fill Fibonacci Numbers in f [ ]","code":"< ? php function fib ( & $ f , $ N ) {"}
{"text":"1 st and 2 nd number of the series are 1 and 1","code":"$ f [ 1 ] = 1 ; $ f [ 2 ] = 1 ; for ( $ i = 3 ; $ i <= $ N ; $ i ++ )"}
{"text":"Add the previous 2 numbers in the series and store it","code":"$ f [ $ i ] = $ f [ $ i - 1 ] + $ f [ $ i - 2 ] ; } function fiboTriangle ( $ n ) {"}
{"text":"Fill Fibonacci numbers in f [ ] using fib ( ) . We need N = n * ( n + 1 ) \/ 2 Fibonacci numbers to make a triangle of height n","code":"$ N = $ n * ( $ n + 1 ) \/ 2 ; $ f = array ( ) ; fib ( $ f , $ N ) ;"}
{"text":"To store next Fibonacci Number to print","code":"$ fiboNum = 1 ;"}
{"text":"for loop to keep track of number of lines","code":"for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) {"}
{"text":"For loop to keep track of numbers in each line","code":"for ( $ j = 1 ; $ j <= $ i ; $ j ++ ) echo ( $ f [ $ fiboNum ++ ] . \" \u2581 \" ) ; echo ( \" STRNEWLINE \" ) ; } }"}
{"text":"Driver code","code":"$ n = 5 ; fiboTriangle ( $ n ) ; ? >"}
{"text":"Function to calculate the average of odd numbers","code":"< ? php function averageOdd ( $ n ) { if ( $ n % 2 == 0 ) { echo ( \" Invalid \u2581 Input \" ) ; return -1 ; } $ sum = 0 ; $ count = 0 ; while ( $ n >= 1 ) {"}
{"text":"count odd numbers","code":"$ count ++ ;"}
{"text":"store the sum of odd numbers","code":"$ sum += $ n ; $ n = $ n - 2 ; } return $ sum \/ $ count ; }"}
{"text":"Driver Code","code":"$ n = 15 ; echo ( averageOdd ( $ n ) ) ; ? >"}
{"text":"Function to calculate the average of odd numbers","code":"< ? php function averageOdd ( $ n ) { if ( $ n % 2 == 0 ) { echo ( \" Invalid \u2581 Input \" ) ; return -1 ; } return ( $ n + 1 ) \/ 2 ; }"}
{"text":"Driver Code","code":"$ n = 15 ; echo ( averageOdd ( $ n ) ) ; ? >"}
{"text":"Function to find the trinomial triangle value .","code":"< ? php function TrinomialValue ( $ n , $ k ) {"}
{"text":"base case","code":"if ( $ n == 0 && $ k == 0 ) return 1 ;"}
{"text":"base case","code":"if ( $ k < - $ n $ k > $ n ) return 0 ;"}
{"text":"recursive step .","code":"return TrinomialValue ( $ n - 1 , $ k - 1 ) + TrinomialValue ( $ n - 1 , $ k ) + TrinomialValue ( $ n - 1 , $ k + 1 ) ; }"}
{"text":"Function to print Trinomial Triangle of height n .","code":"function printTrinomial ( $ n ) {"}
{"text":"printing n rows .","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) {"}
{"text":"printing first half of triangle","code":"for ( $ j = - $ i ; $ j <= 0 ; $ j ++ ) echo TrinomialValue ( $ i , $ j ) , \" \u2581 \" ;"}
{"text":"printing second half of triangle .","code":"for ( $ j = 1 ; $ j <= $ i ; $ j ++ ) echo TrinomialValue ( $ i , $ j ) , \" \u2581 \" ; echo \" STRNEWLINE \" ; } }"}
{"text":"Driver Code","code":"$ n = 4 ; printTrinomial ( $ n ) ; ? >"}
{"text":"PHP Program to print trinomial triangle .","code":"< ? php $ MAX = 10 ;"}
{"text":"Function to find the trinomial triangle value .","code":"function TrinomialValue ( $ dp , $ n , $ k ) {"}
{"text":"Using property of trinomial triangle .","code":"if ( $ k < 0 ) $ k = - $ k ;"}
{"text":"If value already calculated , return that .","code":"if ( $ dp [ $ n ] [ $ k ] != 0 ) return $ dp [ $ n ] [ $ k ] ;"}
{"text":"base case","code":"if ( $ n == 0 && $ k == 0 ) return 1 ;"}
{"text":"base case","code":"if ( $ k < - $ n $ k > $ n ) return 0 ;"}
{"text":"recursive step and storing the value .","code":"return ( $ dp [ $ n ] [ $ k ] = TrinomialValue ( $ dp , $ n - 1 , $ k - 1 ) + TrinomialValue ( $ dp , $ n - 1 , $ k ) + TrinomialValue ( $ dp , $ n - 1 , $ k + 1 ) ) ; }"}
{"text":"Function to print Trinomial Triangle of height n .","code":"function printTrinomial ( $ n ) { global $ MAX ; $ dp ; for ( $ i = 0 ; $ i < $ MAX ; $ i ++ ) for ( $ j = 0 ; $ j < $ MAX ; $ j ++ ) $ dp [ $ i ] [ $ j ] = 0 ;"}
{"text":"printing n rows .","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) {"}
{"text":"printing first half of triangle","code":"for ( $ j = - $ i ; $ j <= 0 ; $ j ++ ) echo TrinomialValue ( $ dp , $ i , $ j ) . \" \u2581 \" ;"}
{"text":"printing second half of triangle .","code":"for ( $ j = 1 ; $ j <= $ i ; $ j ++ ) echo TrinomialValue ( $ dp , $ i , $ j ) . \" \u2581 \" ; echo \" STRNEWLINE \" ; } }"}
{"text":"Driven Code","code":"$ n = 4 ; printTrinomial ( $ n ) ; ? >"}
{"text":"function to find sum of largest prime factor of each number less than equal to n","code":"< ? php function sumOfLargePrimeFactor ( $ n ) {"}
{"text":"Create an integer array \" prime [ 0 . . n ] \" and initialize all entries of it as 0. A value in prime [ i ] will finally be 0 if ' i ' is a prime , else it will contain the largest prime factor of ' i ' .","code":"$ prime = array_fill ( 0 , $ n + 1 , 0 ) ; $ sum = 0 ; $ max = ( int ) ( $ n \/ 2 ) ; for ( $ p = 2 ; $ p <= $ max ; $ p ++ ) {"}
{"text":"If prime [ p ] is '0' , then it is a prime number","code":"if ( $ prime [ $ p ] == 0 ) {"}
{"text":"Update all multiples of p","code":"for ( $ i = $ p * 2 ; $ i <= $ n ; $ i += $ p ) $ prime [ $ i ] = $ p ; } }"}
{"text":"Sum up the largest prime factor of all the numbers","code":"for ( $ p = 2 ; $ p <= $ n ; $ p ++ ) {"}
{"text":"if ' p ' is a non - prime number then prime [ p ] gives its largesr prime factor","code":"if ( $ prime [ $ p ] ) $ sum += $ prime [ $ p ] ;"}
{"text":"' p ' is a prime number","code":"else $ sum += $ p ; }"}
{"text":"required sum","code":"return $ sum ; }"}
{"text":"Driver program to test above","code":"$ n = 12 ; echo \" Sum \u2581 = \u2581 \" . sumOfLargePrimeFactor ( $ n ) ; ? >"}
{"text":"Function for calculating sum of multiples of a upto N","code":"< ? php function calculate_sum ( $ a , $ N ) {"}
{"text":"Number of multiples","code":"$ m = $ N \/ $ a ;"}
{"text":"sum of first m natural numbers","code":"$ sum = $ m * ( $ m + 1 ) \/ 2 ;"}
{"text":"sum of multiples","code":"$ ans = $ a * $ sum ; return $ ans ; }"}
{"text":"Driver code","code":"$ a = 7 ; $ N = 49 ; echo \" Sum \u2581 of \u2581 multiples \u2581 of \u2581 \" . $ a , \" \u2581 up \u2581 to \u2581 \" . $ N . \" \u2581 = \u2581 \" . calculate_sum ( $ a , $ N ) ; ? >"}
{"text":"returns 1 when str is power of 2 return 0 when str is not a power of 2","code":"< ? php function isPowerOf2 ( $ str ) { $ len_str = strlen ( $ str ) ;"}
{"text":"sum stores the intermediate dividend while dividing .","code":"$ num = 0 ;"}
{"text":"if the input is \"1\" then return 0 because 2 ^ k = 1 where k >= 1 and here k = 0","code":"if ( $ len_str == 1 && $ str [ $ len_str - 1 ] == '1' ) return 0 ;"}
{"text":"Divide the number until it gets reduced to 1 if we are successfully able to reduce the number to 1 it means input string is power of two if in between an odd number appears at the end it means string is not divisible by two hence not a power of 2.","code":"while ( $ len_str != 1 $ str [ $ len_str - 1 ] != '1' ) {"}
{"text":"if the last digit is odd then string is not divisible by 2 hence not a power of two return 0.","code":"if ( ord ( $ str [ $ len_str - 1 ] - '0' ) % 2 == 1 ) return 0 ;"}
{"text":"divide the whole string by 2. i is used to track index in current number . j is used to track index for next iteration .","code":"$ j = 0 ; for ( $ i = 0 ; $ i < $ len_str ; $ i ++ ) { $ num = $ num * 10 + ( ord ( $ str [ $ i ] ) - ord ( '0' ) ) ;"}
{"text":"if num < 2 then we have to take another digit to the right of A [ i ] to make it bigger than A [ i ] . E . g . 214 \/ 2 -- > 107","code":"if ( $ num < 2 ) {"}
{"text":"if it 's not the first index. E.g 214  then we have to include 0.","code":"if ( $ i != 0 ) $ str [ $ j ++ ] = '0' ;"}
{"text":"for eg . \"124\" we will not write 064 so if it is the first index just ignore","code":"continue ; } $ str [ $ j ++ ] = chr ( ( int ) ( $ num \/ 2 ) + ord ( '0' ) ) ; $ num = ( $ num ) - ( int ) ( $ num \/ 2 ) * 2 ; }"}
{"text":"After every division by 2 the length of string is changed .","code":"$ len_str = $ j ; }"}
{"text":"if the string reaches to 1 then the str is a power of 2.","code":"return 1 ; }"}
{"text":"Driver code .","code":"$ str1 = \"124684622466842024680246842024662202000002\" ; $ str2 = \"1\" ; $ str3 = \"128\" ; print ( isPowerOf2 ( $ str1 ) . \" \" . isPowerOf2 ( $ str2 ) . \" \" ? > ? >"}
{"text":"Function to check whether a number is power of 2 or not","code":"< ? php function ispowerof2 ( $ num ) { if ( ( $ num & ( $ num - 1 ) ) == 0 ) return 1 ; return 0 ; }"}
{"text":"Driver Code","code":"$ num = 549755813888 ; echo ispowerof2 ( $ num ) ; ? >"}
{"text":"To count number of factors in a number","code":"< ? php function counDivisors ( $ X ) {"}
{"text":"Initialize count with 0","code":"$ count = 0 ;"}
{"text":"Increment count for every factor of the given number X .","code":"for ( $ i = 1 ; $ i <= $ X ; ++ $ i ) { if ( $ X % $ i == 0 ) { $ count ++ ; } }"}
{"text":"Return number of factors","code":"return $ count ; }"}
{"text":"Returns number of divisors in array multiplication","code":"function countDivisorsMult ( $ arr , $ n ) {"}
{"text":"Multipliying all elements of the given array .","code":"$ mul = 1 ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) $ mul *= $ arr [ $ i ] ;"}
{"text":"Calling function which count number of factors of the number","code":"return counDivisors ( $ mul ) ; }"}
{"text":"Driver code","code":"$ arr = array ( 2 , 4 , 6 ) ; $ n = sizeof ( $ arr ) ; echo countDivisorsMult ( $ arr , $ n ) ; ? >"}
{"text":"Function to find number of unordered pairs","code":"< ? php function freqPairs ( $ arr , $ n ) {"}
{"text":"Maximum element from the array","code":"$ max = max ( $ arr ) ;"}
{"text":"Array to store the frequency of each element","code":"$ freq = array_fill ( 0 , $ max + 1 , 0 ) ;"}
{"text":"Stores the number of unordered pairs","code":"$ count = 0 ;"}
{"text":"Store the frequency of each element","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ freq [ $ arr [ $ i ] ] ++ ;"}
{"text":"Find the number of unordered pairs","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 2 * $ arr [ $ i ] ; $ j <= $ max ; $ j += $ arr [ $ i ] ) {"}
{"text":"If the number j divisible by ith element is present in the array","code":"if ( $ freq [ $ j ] >= 1 ) $ count += $ freq [ $ j ] ; }"}
{"text":"If the ith element of the array has frequency more than one","code":"if ( $ freq [ $ arr [ $ i ] ] > 1 ) { $ count += $ freq [ $ arr [ $ i ] ] - 1 ; $ freq [ $ arr [ $ i ] ] -- ; } } return $ count ; }"}
{"text":"Driver code","code":"$ arr = array ( 3 , 2 , 4 , 2 , 6 ) ; $ n = count ( $ arr ) ; echo freqPairs ( $ arr , $ n ) ; ? >"}
{"text":"calculate Nth term of given series","code":"< ? php function Nth_Term ( $ n ) { return ( 2 * pow ( $ n , 3 ) - 3 * pow ( $ n , 2 ) + $ n + 6 ) \/ 6 ; }"}
{"text":"Driver code","code":"$ N = 8 ; echo Nth_Term ( $ N ) ; ? >"}
{"text":"Function to find n - th number in series made of 3 and 5","code":"< ? php function printNthElement ( $ n ) {"}
{"text":"create an array of size ( n + 1 )","code":"$ arr = array_fill ( 0 , ( $ n + 1 ) , NULL ) ; $ arr [ 1 ] = 3 ; $ arr [ 2 ] = 5 ; for ( $ i = 3 ; $ i <= $ n ; $ i ++ ) {"}
{"text":"If i is odd","code":"if ( $ i % 2 != 0 ) $ arr [ $ i ] = $ arr [ $ i \/ 2 ] * 10 + 3 ; else $ arr [ $ i ] = $ arr [ ( $ i \/ 2 ) - 1 ] * 10 + 5 ; } return $ arr [ $ n ] ; }"}
{"text":"Driver code","code":"$ n = 6 ; echo printNthElement ( $ n ) ; ? >"}
{"text":"Function to calculate Nth term of series","code":"< ? php function nthTerm ( $ N ) {"}
{"text":"By using abeove formula","code":"return ( $ N * ( ( int ) ( $ N \/ 2 ) + ( ( $ N % 2 ) * 2 ) + $ N ) ) ; }"}
{"text":"get the value of nthTerm","code":"$ N = 5 ;"}
{"text":"Calculate and print the Nth term","code":"echo \" Nth \u2581 term \u2581 for \u2581 N \u2581 = \u2581 \" , $ N , \" \u2581 : \u2581 \" , nthTerm ( $ N ) ; ? >"}
{"text":"function to print the series","code":"< ? php function series ( $ A , $ X , $ n ) {"}
{"text":"Calculating and printing first term","code":"$ term = pow ( $ A , $ n ) ; echo $ term , \" \" ;"}
{"text":"Computing and printing remaining terms","code":"for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) {"}
{"text":"Find current term using previous terms We increment power of X by 1 , decrement power of A by 1 and compute nCi using previous term by multiplying previous term with ( n - i + 1 ) \/ i","code":"$ term = $ term * $ X * ( $ n - $ i + 1 ) \/ ( $ i * $ A ) ; echo $ term , \" \" ; } }"}
{"text":"Driver Code","code":"$ A = 3 ; $ X = 4 ; $ n = 5 ; series ( $ A , $ X , $ n ) ; ? >"}
{"text":"function to check number is div by 8 or not using bitwise operator","code":"< ? php function Div_by_8 ( $ n ) { return ( ( ( $ n >> 3 ) << 3 ) == $ n ) ; }"}
{"text":"Driver program","code":"$ n = 16 ; if ( Div_by_8 ( $ n ) ) echo \" YES \" ; else echo \" NO \" ; ? >"}
{"text":"Function to calculate the average of even numbers","code":"< ? php function averageEven ( $ n ) { if ( $ n % 2 != 0 ) { echo ( \" Invalid \u2581 Input \" ) ; return -1 ; } $ sum = 0 ; $ count = 0 ; while ( $ n >= 2 ) {"}
{"text":"count even numbers","code":"$ count ++ ;"}
{"text":"store the sum of even numbers","code":"$ sum += $ n ; $ n = $ n - 2 ; } return $ sum \/ $ count ; }"}
{"text":"Driver Code","code":"$ n = 16 ; echo ( averageEven ( $ n ) ) ; ? >"}
{"text":"Function to calculate the average of even numbers","code":"< ? php function averageEven ( $ n ) { if ( $ n % 2 != 0 ) { echo ( \" Invalid \u2581 Input \" ) ; return -1 ; } return ( $ n + 2 ) \/ 2 ; }"}
{"text":"Driver Code","code":"$ n = 16 ; echo ( averageEven ( $ n ) ) ; return 0 ; ? >"}
{"text":"Recursive function to return gcd of a and b","code":"< ? php function gcd ( $ a , $ b ) {"}
{"text":"Everything divides 0","code":"if ( $ a == 0 $ b == 0 ) return 0 ;"}
{"text":"base case","code":"if ( $ a == $ b ) return $ a ;"}
{"text":"a is greater","code":"if ( $ a > $ b ) return gcd ( $ a - $ b , $ b ) ; return gcd ( $ a , $ b - $ a ) ; }"}
{"text":"function to find largest coprime divisor","code":"function cpFact ( $ x , $ y ) { while ( gcd ( $ x , $ y ) != 1 ) { $ x = $ x \/ gcd ( $ x , $ y ) ; } return $ x ; }"}
{"text":"Driver Code","code":"$ x = 15 ; $ y = 3 ; echo cpFact ( $ x , $ y ) , \" STRNEWLINE \" ; $ x = 14 ; $ y = 28 ; echo cpFact ( $ x , $ y ) , \" STRNEWLINE \" ; $ x = 7 ; $ y = 3 ; echo cpFact ( $ x , $ y ) ; ? >"}
{"text":"Returns count of numbers with k as last digit .","code":"< ? php function counLastDigitK ( $ low , $ high , $ k ) { $ count = 0 ; for ( $ i = $ low ; $ i <= $ high ; $ i ++ ) if ( $ i % 10 == $ k ) $ count ++ ; return $ count ; }"}
{"text":"Driver Code","code":"$ low = 3 ; $ high = 35 ; $ k = 3 ; echo counLastDigitK ( $ low , $ high , $ k ) ; ? >"}
{"text":"PHP implementation to print first N Taxicab ( 2 ) numbers :","code":"< ? php function printTaxicab2 ( $ N ) {"}
{"text":"Starting from 1 , check every number if it is Taxicab until count reaches N .","code":"$ i = 1 ; $ count = 0 ; while ( $ count < $ N ) { $ int_count = 0 ;"}
{"text":"Try all possible pairs ( j , k ) whose cube sums can be i .","code":"for ( $ j = 1 ; $ j <= pow ( $ i , 1.0 \/ 3 ) ; $ j ++ ) for ( $ k = $ j + 1 ; $ k <= pow ( $ i , 1.0 \/ 3 ) ; $ k ++ ) if ( $ j * $ j * $ j + $ k * $ k * $ k == $ i ) $ int_count ++ ;"}
{"text":"Taxicab ( 2 ) found","code":"if ( $ int_count == 2 ) { $ count ++ ; echo $ count , \" \" , \u2581 $ i , \u2581 \" \" } $ i ++ ; } }"}
{"text":"Driver code","code":"$ N = 5 ; printTaxicab2 ( $ N ) ; ? >"}
{"text":"A optimized school method based PHP program to check if a number is composite .","code":"< ? php function isComposite ( $ n ) {"}
{"text":"Corner cases","code":"if ( $ n <= 1 ) return false ; if ( $ n <= 3 ) return false ;"}
{"text":"This is checked so that we can skip middle five numbers in below loop","code":"if ( $ n % 2 == 0 $ n % 3 == 0 ) return true ; for ( $ i = 5 ; $ i * $ i <= $ n ; $ i = $ i + 6 ) if ( $ n % $ i == 0 || $ n % ( $ i + 2 ) == 0 ) return true ; return false ; }"}
{"text":"Driver Code","code":"if ( isComposite ( 11 ) ) echo \" true \" ; else echo \" false \" ; echo \" STRNEWLINE \" ; if ( isComposite ( 15 ) ) echo \" true \" ; else echo \" false \" ; echo \" STRNEWLINE \" ; ? >"}
{"text":"function to check if a number is prime or not","code":"< ? php function isPrime ( $ n ) {"}
{"text":"Corner case","code":"if ( $ n <= 1 ) return false ;"}
{"text":"Check from 2 to n - 1","code":"for ( $ i = 2 ; $ i < $ n ; $ i ++ ) if ( $ n % $ i == 0 ) return false ; return true ; }"}
{"text":"Find prime number greater than a number","code":"function findPrime ( $ n ) { $ num = $ n + 1 ;"}
{"text":"find prime greater than n","code":"while ( $ num ) {"}
{"text":"check if num is prime","code":"if ( isPrime ( $ num ) ) return $ num ;"}
{"text":"increment num","code":"$ num = $ num + 1 ; } return 0 ; }"}
{"text":"To find number to be added so sum of array is prime","code":"function minNumber ( $ arr , $ n ) { $ sum = 0 ;"}
{"text":"To find sum of array elements","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += $ arr [ $ i ] ;"}
{"text":"if sum is already prime return 0","code":"if ( isPrime ( $ sum ) ) return 0 ;"}
{"text":"To find prime number greater than sum","code":"$ num = findPrime ( $ sum ) ;"}
{"text":"Return difference of sum and num","code":"return $ num - $ sum ; }"}
{"text":"Driver Code","code":"$ arr = array ( 2 , 4 , 6 , 8 , 12 ) ; $ n = sizeof ( $ arr ) ; echo minNumber ( $ arr , $ n ) ; ? >"}
{"text":"function to calculate factorial","code":"< ? php function fact ( $ n ) { if ( $ n == 0 ) return 1 ; return $ n * fact ( $ n - 1 ) ; }"}
{"text":"function to calculate sum of divisor","code":"function div ( $ x ) { $ ans = 0 ; for ( $ i = 1 ; $ i <= $ x ; $ i ++ ) if ( $ x % $ i == 0 ) $ ans += $ i ; return $ ans ; }"}
{"text":"Returns sum of divisors of n !","code":"function sumFactDiv ( $ n ) { return div ( fact ( $ n ) ) ; }"}
{"text":"Driver Code","code":"$ n = 4 ; echo sumFactDiv ( $ n ) ; ? >"}
{"text":"allPrimes [ ] stores all prime numbers less than or equal to n .","code":"< ? php $ allPrimes = array ( ) ;"}
{"text":"Fills above vector allPrimes [ ] for a given n","code":"function sieve ( $ n ) { global $ allPrimes ;"}
{"text":"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 .","code":"$ prime = array_fill ( 0 , $ n + 1 , true ) ;"}
{"text":"Loop to update prime [ ]","code":"for ( $ p = 2 ; $ p * $ p <= $ n ; $ p ++ ) {"}
{"text":"If prime [ p ] is not changed , then it is a prime","code":"if ( $ prime [ $ p ] == true ) {"}
{"text":"Update all multiples of p","code":"for ( $ i = $ p * 2 ; $ i <= $ n ; $ i += $ p ) $ prime [ $ i ] = false ; } }"}
{"text":"Store primes in the vector allPrimes","code":"for ( $ p = 2 ; $ p <= $ n ; $ p ++ ) if ( $ prime [ $ p ] ) array_push ( $ allPrimes , $ p ) ; }"}
{"text":"Function to find all result of factorial number","code":"function factorialDivisors ( $ n ) { global $ allPrimes ;"}
{"text":"Initialize result","code":"$ result = 1 ;"}
{"text":"find exponents of all primes which divides n and less than n","code":"for ( $ i = 0 ; $ i < count ( $ allPrimes ) ; $ i ++ ) {"}
{"text":"Current divisor","code":"$ p = $ allPrimes [ $ i ] ;"}
{"text":"Find the highest power ( stored in exp ) ' \u2581 \u2581 of \u2581 allPrimes [ i ] \u2581 that \u2581 divides \u2581 n \u2581 using \u2581 \u2581 Legendre ' s formula .","code":"$ exp = 0 ; while ( $ p <= $ n ) { $ exp = $ exp + ( int ) ( $ n \/ $ p ) ; $ p = $ p * $ allPrimes [ $ i ] ; }"}
{"text":"Using the divisor function to calculate the sum","code":"$ result = $ result * ( pow ( $ allPrimes [ $ i ] , $ exp +1 ) - 1 ) \/ ( $ allPrimes [ $ i ] - 1 ) ; }"}
{"text":"return total divisors","code":"return $ result ; }"}
{"text":"Driver program to run the cases","code":"print ( factorialDivisors ( 4 ) ) ; ? >"}
{"text":"Return true if n is pandigit else return false .","code":"< ? php function checkPandigital ( $ b , $ n ) {"}
{"text":"Checking length is less than base","code":"if ( strlen ( $ n ) < $ b ) return 0 ; $ hash = array ( ) ; for ( $ i = 0 ; $ i < $ b ; $ i ++ ) $ hash [ $ i ] = 0 ;"}
{"text":"Traversing each digit of the number .","code":"for ( $ i = 0 ; $ i < strlen ( $ n ) ; $ i ++ ) {"}
{"text":"If digit is integer","code":"if ( $ n [ $ i ] >= '0' && $ n [ $ i ] <= '9' ) $ hash [ $ n [ $ i ] - '0' ] = 1 ;"}
{"text":"If digit is alphabet","code":"else if ( ord ( $ n [ $ i ] ) - ord ( ' A ' ) <= $ b - 11 ) $ hash [ ord ( $ n [ $ i ] ) - ord ( ' A ' ) + 10 ] = 1 ; }"}
{"text":"Checking hash array , if any index is unmarked .","code":"for ( $ i = 0 ; $ i < $ b ; $ i ++ ) if ( $ hash [ $ i ] == 0 ) return 0 ; return 1 ; }"}
{"text":"Driver Program","code":"$ b = 13 ; $ n = \"1298450376ABC \" ; if ( checkPandigital ( $ b , $ n ) ) echo \" Yes \" ; else echo \" No \" ; ? >"}
{"text":"Function to find minimum number of given operations to convert m to n","code":"< ? php function convert ( $ m , $ n ) { if ( $ m == $ n ) return 0 ;"}
{"text":"only way is to do - 1 ( m - n ) times","code":"if ( $ m > $ n ) return $ m - $ n ;"}
{"text":"not possible","code":"if ( $ m <= 0 && $ n > 0 ) return -1 ;"}
{"text":"n is greater and n is odd","code":"if ( $ n % 2 == 1 )"}
{"text":"perform ' - 1' on m ( or + 1 on n )","code":"return 1 + convert ( $ m , $ n + 1 ) ;"}
{"text":"n is even","code":"else"}
{"text":"perform ' * 2' on m ( or n \/ 2 on n )","code":"return 1 + convert ( $ m , $ n \/ 2 ) ; }"}
{"text":"Driver code","code":"{ $ m = 3 ; $ n = 11 ; echo \" Minimum \u2581 number \u2581 of \u2581 \" . \" operations \u2581 : \u2581 \" , convert ( $ m , $ n ) ; return 0 ; } ? >"}
{"text":"PHP program to find Seed of a number","code":"< ? php $ MAX = 10000 ; $ prodDig = array_fill ( 0 , $ MAX , 0 ) ;"}
{"text":"Stores product of digits of x in prodDig [ x ]","code":"function getDigitProduct ( $ x ) { global $ prodDig ;"}
{"text":"If x has single digit","code":"if ( $ x < 10 ) return $ x ;"}
{"text":"If digit product is already computed","code":"if ( $ prodDig [ $ x ] != 0 ) return $ prodDig [ $ x ] ;"}
{"text":"If digit product is not computed before .","code":"$ prod = ( int ) ( $ x % 10 ) * getDigitProduct ( ( int ) ( $ x \/ 10 ) ) ; $ prodDig [ $ x ] = $ prod ; return $ prod ; }"}
{"text":"Prints all seeds of n","code":"function findSeed ( $ n ) {"}
{"text":"Find all seeds using prodDig [ ]","code":"$ res = array ( ) ; for ( $ i = 1 ; $ i <= ( int ) ( $ n \/ 2 + 1 ) ; $ i ++ ) if ( $ i * getDigitProduct ( $ i ) == $ n ) array_push ( $ res , $ i ) ;"}
{"text":"If there was no seed","code":"if ( count ( $ res ) == 0 ) { echo \" NO \u2581 seed \u2581 exists STRNEWLINE \" ; return ; }"}
{"text":"Print seeds","code":"for ( $ i = 0 ; $ i < count ( $ res ) ; $ i ++ ) echo $ res [ $ i ] . \" \u2581 \" ; }"}
{"text":"Driver code","code":"$ n = 138 ; findSeed ( $ n ) ; ? >"}
{"text":"Return smallest number having maximum prime factors .","code":"< ? php function maxPrimefactorNum ( $ N ) { $ arr [ $ N + 5 ] = array ( ) ; $ arr = array_fill ( 0 , $ N + 1 , NULL ) ;"}
{"text":"Sieve of eratosthenes method to count number of prime factors .","code":"for ( $ i = 2 ; ( $ i * $ i ) <= $ N ; $ i ++ ) { if ( ! $ arr [ $ i ] ) for ( $ j = 2 * $ i ; $ j <= $ N ; $ j += $ i ) $ arr [ $ j ] ++ ; $ arr [ $ i ] = 1 ; } $ maxval = 0 ; $ maxint = 1 ;"}
{"text":"Finding number having maximum number of prime factor .","code":"for ( $ i = 1 ; $ i <= $ N ; $ i ++ ) { if ( $ arr [ $ i ] > $ maxval ) { $ maxval = $ arr [ $ i ] ; $ maxint = $ i ; } } return $ maxint ; }"}
{"text":"Driver Code","code":"$ N = 40 ; echo maxPrimefactorNum ( $ N ) , \" STRNEWLINE \" ; ? >"}
{"text":"function compute sum all sub - array","code":"< ? php function SubArraySum ( $ arr , $ n ) { $ result = 0 ;"}
{"text":"computing sum of subarray using formula","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ result += ( $ arr [ $ i ] * ( $ i + 1 ) * ( $ n - $ i ) ) ;"}
{"text":"return all subarray sum","code":"return $ result ; }"}
{"text":"Driver Code","code":"$ arr = array ( 1 , 2 , 3 ) ; $ n = sizeof ( $ arr ) ; echo \" Sum \u2581 of \u2581 SubArray \u2581 : \u2581 \" , SubArraySum ( $ arr , $ n ) , \" STRNEWLINE \" ; #This  code is contributed by aj_36 NEW_LINE ? >"}
{"text":"PHP program to find highest power of 2 smaller than or equal to n .","code":"< ? php function highestPowerof2 ( $ n ) { $ res = 0 ; for ( $ i = $ n ; $ i >= 1 ; $ i -- ) {"}
{"text":"If i is a power of 2","code":"if ( ( $ i & ( $ i - 1 ) ) == 0 ) { $ res = $ i ; break ; } } return $ res ; }"}
{"text":"Driver code","code":"$ n = 10 ; echo highestPowerof2 ( $ n ) ; ? >"}
{"text":"Function to find pairs that can represent the given number as sum of two cubes","code":"< ? php function findPairs ( $ n ) {"}
{"text":"find cube root of n","code":"$ cubeRoot = pow ( $ n , 1.0 \/ 3.0 ) ;"}
{"text":"create a array of size of size ' cubeRoot '","code":"$ cube = array ( ) ;"}
{"text":"for index i , cube [ i ] will contain i ^ 3","code":"for ( $ i = 1 ; $ i <= $ cubeRoot ; $ i ++ ) $ cube [ $ i ] = $ i * $ i * $ i ;"}
{"text":"Find all pairs in above sorted array cube [ ] whose sum is equal to n","code":"$ l = 1 ; $ r = $ cubeRoot ; while ( $ l < $ r ) { if ( $ cube [ $ l ] + $ cube [ $ r ] < $ n ) $ l ++ ; else if ( $ cube [ $ l ] + $ cube [ $ r ] > $ n ) $ r -- ; else { echo \" ( \" , $ l , \" , \u2581 \" , floor ( $ r ) , \" ) \" ; echo \" STRNEWLINE \" ; $ l ++ ; $ r -- ; } } }"}
{"text":"Driver code","code":"$ n = 20683 ; findPairs ( $ n ) ; ? >"}
{"text":"Utility function to find GCD of a and b","code":"< ? php function gcd ( $ a , $ b ) { while ( $ b != 0 ) { $ t = $ b ; $ b = $ a % $ b ; $ a = $ t ; } return $ a ; }"}
{"text":"Returns minimum difference between any two terms of shifted tables of ' a ' and ' b ' . ' x ' is shift in table of ' a ' and ' y ' is shift in table of ' b ' .","code":"function findMinDiff ( $ a , $ b , $ x , $ y ) {"}
{"text":"Calculate gcd of a nd b","code":"$ g = gcd ( $ a , $ b ) ;"}
{"text":"Calculate difference between x and y","code":"$ diff = abs ( $ x - $ y ) % $ g ; return min ( $ diff , $ g - $ diff ) ; }"}
{"text":"Driver Code","code":"$ a = 20 ; $ b = 52 ; $ x = 5 ; $ y = 7 ; echo findMinDiff ( $ a , $ b , $ x , $ y ) , \" STRNEWLINE \" ; ? >"}
{"text":"function to print the divisors","code":"< ? php function printDivisors ( $ n ) {"}
{"text":"Vector to store half of the divisors","code":"$ v ; $ t = 0 ; for ( $ i = 1 ; $ i <= ( int ) sqrt ( $ n ) ; $ i ++ ) { if ( $ n % $ i == 0 ) {"}
{"text":"check if divisors are equal","code":"if ( ( int ) $ n \/ $ i == $ i ) echo $ i . \" \" ; else { echo $ i . \" \" ;"}
{"text":"push the second divisor in the vector","code":"$ v [ $ t ++ ] = ( int ) $ n \/ $ i ; } } }"}
{"text":"The vector will be printed in reverse","code":"for ( $ i = count ( $ v ) - 1 ; $ i >= 0 ; $ i -- ) echo $ v [ $ i ] . \" \u2581 \" ; }"}
{"text":"Driver code","code":"echo \" The \u2581 divisors \u2581 of \u2581 100 \u2581 are : \u2581 STRNEWLINE \" ; printDivisors ( 100 ) ; ? >"}
{"text":"function to print the divisors","code":"< ? php function printDivisors ( $ n ) { for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) if ( $ n % $ i == 0 ) echo $ i , \" \u2581 \" ; }"}
{"text":"Driver Code","code":"echo \" The \u2581 divisors \u2581 of \u2581 100 \u2581 are : STRNEWLINE \" ; printDivisors ( 100 ) ; ? >"}
{"text":"Function to print the divisors","code":"< ? php function printDivisors ( $ n ) {"}
{"text":"Note that this loop runs till square root","code":"for ( $ i = 1 ; $ i <= sqrt ( $ n ) ; $ i ++ ) { if ( $ n % $ i == 0 ) {"}
{"text":"If divisors are equal , print only one","code":"if ( $ n \/ $ i == $ i ) echo $ i , \" \u2581 \" ;"}
{"text":"Otherwise print both","code":"else echo $ i , \" \" , \u2581 $ n \/ $ i , \" \" } } }"}
{"text":"Driver Code","code":"echo \" The \u2581 divisors \u2581 of \u2581 100 \u2581 are : \u2581 STRNEWLINE \" ; printDivisors ( 100 ) ; ? >"}
{"text":"PHP program for implementation of Sieve of Atkin","code":"< ? php function SieveOfAtkin ( $ limit ) {"}
{"text":"2 and 3 are known to be prime","code":"if ( $ limit > 2 ) echo 2 , \" \u2581 \" ; if ( $ limit > 3 ) echo 3 , \" \u2581 \" ;"}
{"text":"Initialise the sieve array with false values","code":"$ sieve [ $ limit ] = 0 ; for ( $ i = 0 ; $ i < $ limit ; $ i ++ ) $ sieve [ $ i ] = false ;"}
{"text":"Mark sieve [ n ] is true if one of the following is true : a ) n = ( 4 * x * x ) + ( y * y ) has odd number of solutions , i . e . , there exist odd number of distinct pairs ( x , y ) that satisfy the equation and n % 12 = 1 or n % 12 = 5. b ) n = ( 3 * x * x ) + ( y * y ) has odd number of solutions and n % 12 = 7 c ) n = ( 3 * x * x ) - ( y * y ) has odd number of solutions , x > y and n % 12 = 11","code":"for ( $ x = 1 ; $ x * $ x < $ limit ; $ x ++ ) { for ( $ y = 1 ; $ y * $ y < $ limit ; $ y ++ ) {"}
{"text":"Main part of Sieve of Atkin","code":"$ n = ( 4 * $ x * $ x ) + ( $ y * $ y ) ; if ( $ n <= $ limit && ( $ n % 12 == 1 $ n % 12 == 5 ) ) $ sieve [ $ n ] ^= true ; $ n = ( 3 * $ x * $ x ) + ( $ y * $ y ) ; if ( $ n <= $ limit && $ n % 12 == 7 ) $ sieve [ $ n ] = true ; $ n = ( 3 * $ x * $ x ) - ( $ y * $ y ) ; if ( $ x > $ y && $ n <= $ limit && $ n % 12 == 11 ) $ sieve [ $ n ] ^= true ; } }"}
{"text":"Mark all multiples of squares as non - prime","code":"for ( $ r = 5 ; $ r * $ r < $ limit ; $ r ++ ) { if ( $ sieve [ $ r ] ) { for ( $ i = $ r * $ r ; $ i < $ limit ; $ i += $ r * $ r ) $ sieve [ $ i ] = false ; } }"}
{"text":"Print primes using sieve [ ]","code":"for ( $ a = 5 ; $ a < $ limit ; $ a ++ ) if ( $ sieve [ $ a ] ) echo $ a , \" \u2581 \" ; }"}
{"text":"Driver Code","code":"$ limit = 20 ; SieveOfAtkin ( $ limit ) ; ? >"}
{"text":"PHP program to check if a point lies inside a circle or not","code":"< ? php function isInside ( $ circle_x , $ circle_y , $ rad , $ x , $ y ) {"}
{"text":"Compare radius of circle with distance of its center from given point","code":"if ( ( $ x - $ circle_x ) * ( $ x - $ circle_x ) + ( $ y - $ circle_y ) * ( $ y - $ circle_y ) <= $ rad * $ rad ) return true ; else return false ; }"}
{"text":"Driver Code","code":"$ x = 1 ; $ y = 1 ; $ circle_x = 0 ; $ circle_y = 1 ; $ rad = 2 ; if ( isInside ( $ circle_x , $ circle_y , $ rad , $ x , $ y ) ) echo \" Inside \" ; else echo \" Outside \" ; ? >"}
{"text":"Utility function to eval1uate a simple expression with one operator only .","code":"< ? php function eval1 ( $ a , $ op , $ b ) { if ( $ op == ' + ' ) return $ a + $ b ; if ( $ op == ' - ' ) return $ a - $ b ; if ( $ op == ' * ' ) return $ a * $ b ; }"}
{"text":"This function eval1uates all possible values and returns a list of eval1uated values .","code":"function eval1uateAll ( $ expr , $ low , $ high ) {"}
{"text":"To store result ( all possible evaluations of given expression ' expr ' )","code":"$ res = array ( ) ;"}
{"text":"If there is only one character , it must be a digit ( or operand ) , return it .","code":"if ( $ low == $ high ) { array_push ( $ res , ord ( $ expr [ $ low ] ) - ord ( ' 0 ' ) ) ; return $ res ; }"}
{"text":"If there are only three characters , middle one must be operator and corner ones must be operand","code":"if ( $ low == ( $ high - 2 ) ) { $ num = eval1 ( ord ( $ expr [ $ low ] ) - ord ( '0' ) , $ expr [ $ low + 1 ] , ord ( $ expr [ $ low + 2 ] ) - ord ( '0' ) ) ; array_push ( $ res , $ num ) ; return $ res ; }"}
{"text":"every i refers to an operator","code":"for ( $ i = $ low + 1 ; $ i <= $ high ; $ i += 2 ) {"}
{"text":"l refers to all the possible values in the left of operator ' expr [ i ] '","code":"$ l = eval1uateAll ( $ expr , $ low , $ i - 1 ) ;"}
{"text":"r refers to all the possible values in the right of operator ' expr [ i ] '","code":"$ r = eval1uateAll ( $ expr , $ i + 1 , $ high ) ;"}
{"text":"Take above eval1uated all possible values in left side of ' i '","code":"for ( $ s1 = 0 ; $ s1 < count ( $ l ) ; $ s1 ++ ) {"}
{"text":"Take above eval1uated all possible values in right side of ' i '","code":"for ( $ s2 = 0 ; $ s2 < count ( $ r ) ; $ s2 ++ ) {"}
{"text":"Calculate value for every pair and add the value to result .","code":"$ val = eval1 ( $ l [ $ s1 ] , $ expr [ $ i ] , $ r [ $ s2 ] ) ; array_push ( $ res , $ val ) ; } } } return $ res ; }"}
{"text":"Driver Code","code":"$ expr = \"1*2 + 3*4\" ; $ len = strlen ( $ expr ) ; $ ans = eval1uateAll ( $ expr , 0 , $ len - 1 ) ; for ( $ i = 0 ; $ i < count ( $ ans ) ; $ i ++ ) echo $ ans [ $ i ] . \" STRNEWLINE \" ; ? >"}
{"text":"This function returns true if n is lucky","code":"< ? php function isLucky ( $ n ) {"}
{"text":"Create an array of size 10 and initialize all elements as false . This array is used to check if a digit is already seen or not .","code":"$ arr = array ( ) ; for ( $ i = 0 ; $ i < 10 ; $ i ++ ) $ arr [ $ i ] = false ;"}
{"text":"Traverse through all digits of given number","code":"while ( $ n > 0 ) {"}
{"text":"Find the last digit","code":"$ digit = $ n % 10 ;"}
{"text":"If digit is already seen , return false","code":"if ( $ arr [ $ digit ] ) return false ;"}
{"text":"Mark this digit as seen","code":"$ arr [ $ digit ] = true ;"}
{"text":"Remove the last digit from number","code":"$ n = ( int ) ( $ n \/ 10 ) ; } return true ; }"}
{"text":"Driver Code","code":"$ arr = array ( 1291 , 897 , 4566 , 1232 , 80 , 700 ) ; $ n = sizeof ( $ arr ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( isLucky ( $ arr [ $ i ] ) ) echo $ arr [ $ i ] , \" \u2581 is \u2581 Lucky \u2581 STRNEWLINE \" ; else echo $ arr [ $ i ] , \" \u2581 is \u2581 not \u2581 Lucky \u2581 STRNEWLINE \" ; ? >"}
{"text":"PHP program to print squares of first ' n ' natural numbers wothout using * , \/ and -","code":"< ? php function printSquares ( $ n ) {"}
{"text":"Initialize ' square ' and first odd number","code":"$ square = 0 ; $ odd = 1 ;"}
{"text":"Calculate and print squares","code":"for ( $ x = 0 ; $ x < $ n ; $ x ++ ) {"}
{"text":"Print square","code":"echo $ square , \" \" ;"}
{"text":"Update ' square ' and ' odd '","code":"$ square = $ square + $ odd ; $ odd = $ odd + 2 ; } }"}
{"text":"Driver Code","code":"$ n = 5 ; printSquares ( $ n ) ; ? >"}
{"text":"PHP program to reverse digits of a number","code":"< ? php $ rev_num = 0 ; $ base_pos = 1 ;"}
{"text":"Recursive function to reverse digits of num","code":"function reversDigits ( $ num ) { global $ rev_num ; global $ base_pos ; if ( $ num > 0 ) { reversDigits ( ( int ) ( $ num \/ 10 ) ) ; $ rev_num += ( $ num % 10 ) * $ base_pos ; $ base_pos *= 10 ; } return $ rev_num ; }"}
{"text":"Driver Code","code":"$ num = 4562 ; echo \" Reverse \u2581 of \u2581 no . \u2581 is \u2581 \" , reversDigits ( $ num ) ; ? >"}
{"text":"function to find bitwise subsets Efficient approach","code":"< ? php function printSubsets ( $ n ) { for ( $ i = $ n ; $ i > 0 ; $ i = ( $ i - 1 ) & $ n ) echo $ i . \" \" ; echo \"0\" ; }"}
{"text":"Driver Code","code":"$ n = 9 ; printSubsets ( $ n ) ; ? >"}
{"text":"function to check recursively if the number is divisible by 17 or not","code":"< ? php function isDivisibleby17 ( $ n ) {"}
{"text":"if n = 0 or n = 17 then yes","code":"if ( $ n == 0 $ n == 17 ) return true ;"}
{"text":"if n is less then 17 , not divisible by 17","code":"if ( $ n < 17 ) return false ;"}
{"text":"reducing the number by floor ( n \/ 16 ) - n % 16","code":"return isDivisibleby17 ( ( int ) ( $ n >> 4 ) - ( int ) ( $ n & 15 ) ) ; }"}
{"text":"Driver Code","code":"$ n = 35 ; if ( isDivisibleby17 ( $ n ) ) echo $ n . \" \u2581 is \u2581 divisible \u2581 by \u2581 17\" ; else echo $ n . \" \u2581 is \u2581 not \u2581 divisible \u2581 by \u2581 17\" ; ? >"}
{"text":"Returns largest number with m set bits then m - 1 0 bits .","code":"< ? php function answer ( $ n ) {"}
{"text":"Start with 2 bits .","code":"$ m = 2 ;"}
{"text":"initial answer is 1 which meets the given condition","code":"$ ans = 1 ; $ r = 1 ;"}
{"text":"check for all numbers","code":"while ( $ r < $ n ) {"}
{"text":"compute the number","code":"$ r = ( pow ( 2 , $ m ) - 1 ) * ( pow ( 2 , $ m - 1 ) ) ;"}
{"text":"if less then N","code":"if ( $ r < $ n ) $ ans = $ r ;"}
{"text":"increment m to get the next number","code":"$ m ++ ; } return $ ans ; }"}
{"text":"Driver Code","code":"$ n = 7 ; echo answer ( $ n ) ; ? >"}
{"text":"Simple PhP program to find MSB number for given n .","code":"< ? php function setBitNumber ( $ n ) { if ( $ n == 0 ) return 0 ; $ msb = 0 ; $ n = $ n \/ 2 ; while ( $ n != 0 ) { $ n = $ n \/ 2 ; $ msb ++ ; } return ( 1 << $ msb ) ; }"}
{"text":"Driver code","code":"$ n = 0 ; echo setBitNumber ( $ n ) ; ? >"}
{"text":"PHP program to find MSB number for given n .","code":"< ? php function setBitNumber ( $ n ) {"}
{"text":"Suppose n is 273 ( binary is 100010001 ) . It does following 100010001 | 010001000 = 110011001","code":"$ n |= $ n >> 1 ;"}
{"text":"This makes sure 4 bits ( From MSB and including MSB ) are set . It does following 110011001 | 001100110 = 111111111","code":"$ n |= $ n >> 2 ; $ n |= $ n >> 4 ; $ n |= $ n >> 8 ; $ n |= $ n >> 16 ;"}
{"text":"Increment n by 1 so that there is only one set bit which is just before original MSB . n now becomes 1000000000","code":"$ n = $ n + 1 ;"}
{"text":"Return original MSB after shifting . n now becomes 100000000","code":"return ( $ n >> 1 ) ; }"}
{"text":"Driver code","code":"$ n = 273 ; echo setBitNumber ( $ n ) ; ? >"}
{"text":"Simple PHP code for counting trailing zeros in binary representation of a number","code":"< ? php function countTrailingZero ( $ x ) { $ count = 0 ; while ( ( $ x & 1 ) == 0 ) { $ x = $ x >> 1 ; $ count ++ ; } return $ count ; }"}
{"text":"Driver Code","code":"echo countTrailingZero ( 11 ) , \" STRNEWLINE \" ; ? >"}
{"text":"PHP code for counting trailing zeros in binary representation of a number","code":"< ? php function countTrailingZero ( $ x ) {"}
{"text":"Map a bit value mod 37 to its position","code":"$ lookup = array ( 32 , 0 , 1 , 26 , 2 , 23 , 27 , 0 , 3 , 16 , 24 , 30 , 28 , 11 , 0 , 13 , 4 , 7 , 17 , 0 , 25 , 22 , 31 , 15 , 29 , 10 , 12 , 6 , 0 , 21 , 14 , 9 , 5 , 20 , 8 , 19 , 18 ) ;"}
{"text":"Only difference between ( x and - x ) is the value of signed magnitude ( leftmostbit ) negative numbers signed bit is 1","code":"return $ lookup [ ( - $ x & $ x ) % 37 ] ; }"}
{"text":"Driver Code","code":"echo countTrailingZero ( 48 ) , \" STRNEWLINE \" ; ? >"}
{"text":"PHP program to evaluate ceil ( 7 n \/ 8 ) without using * and","code":"< ? php function multiplyBySevenByEight ( $ n ) {"}
{"text":"Note the inner bracket here . This is needed because precedence of ' - ' operator is higher than ' < < '","code":"return ( $ n - ( $ n >> 3 ) ) ; }"}
{"text":"Driver Code","code":"$ n = 9 ; echo multiplyBySevenByEight ( $ n ) ; ? >"}
{"text":"PHP program to evaluate 7 n \/ 8 without using * and \/","code":"< ? php function multiplyBySevenByEight ( $ n ) {"}
{"text":"Step 1 ) First multiply number by 7 i . e . 7 n = ( n << 3 ) - n Step 2 ) Divide result by 8","code":"return ( ( $ n << 3 ) - $ n ) >> 3 ; }"}
{"text":"Driver Code","code":"$ n = 15 ; echo multiplyBySevenByEight ( $ n ) ; ? >"}
{"text":"PHP implementation of the approach Function to return the maximized median","code":"< ? php function getMaxMedian ( $ arr , $ n , $ k ) { $ size = $ n + $ k ;"}
{"text":"Sort the array","code":"sort ( $ arr , $ n ) ;"}
{"text":"If size is even","code":"if ( $ size % 2 == 0 ) { $ median = ( float ) ( $ arr [ ( $ size \/ 2 ) - 1 ] + $ arr [ $ size \/ 2 ] ) \/ 2 ; return $ median ; }"}
{"text":"If size is odd","code":"$ median = $ arr [ $ size \/ 2 ] ; return $ median ; }"}
{"text":"Driver code","code":"$ arr = array ( 3 , 2 , 3 , 4 , 2 ) ; $ n = sizeof ( $ arr ) ; $ k = 2 ; echo ( getMaxMedian ( $ arr , $ n , $ k ) ) ;"}
{"text":"PHP program to print three numbers in sorted order using max function","code":"< ? php function printSorted ( $ a , $ b , $ c ) {"}
{"text":"Find maximum element","code":"$ get_max = max ( $ a , max ( $ b , $ c ) ) ;"}
{"text":"Find minimum element","code":"$ get_min = - max ( - $ a , max ( - $ b , - $ c ) ) ; $ get_mid = ( $ a + $ b + $ c ) - ( $ get_max + $ get_min ) ; echo $ get_min , \" \" \u2581 , \u2581 $ get _ mid , \u2581 \" \" }"}
{"text":"Driver Code","code":"$ a = 4 ; $ b = 1 ; $ c = 9 ; printSorted ( $ a , $ b , $ c ) ; ? >"}
{"text":"Function to sort an array using insertion sort","code":"< ? php function insertionSort ( & $ arr , $ n ) { for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ key = $ arr [ $ i ] ; $ j = $ i - 1 ;"}
{"text":"Move elements of arr [ 0. . i - 1 ] , that are greater than key , to one position ahead of their current position","code":"while ( $ j >= 0 && $ arr [ $ j ] > $ key ) { $ arr [ $ j + 1 ] = $ arr [ $ j ] ; $ j = $ j - 1 ; } $ arr [ $ j + 1 ] = $ key ; } }"}
{"text":"A utility function to print an array of size n","code":"function printArray ( & $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . \" \u2581 \" ; echo \" STRNEWLINE \" ; }"}
{"text":"Driver Code","code":"$ arr = array ( 12 , 11 , 13 , 5 , 6 ) ; $ n = sizeof ( $ arr ) ; insertionSort ( $ arr , $ n ) ; printArray ( $ arr , $ n ) ; ? >"}
{"text":"DP based function to count number of paths","code":"< ? php function countPaths ( $ n , $ m ) {"}
{"text":"$dp [ $n + 1 ] [ $m + 1 ] ; Fill entries in bottommost row and leftmost columns","code":"for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) $ dp [ $ i ] [ 0 ] = 1 ; for ( $ i = 0 ; $ i <= $ m ; $ i ++ ) $ dp [ 0 ] [ $ i ] = 1 ;"}
{"text":"Fill DP in bottom up manner","code":"for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) for ( $ j = 1 ; $ j <= $ m ; $ j ++ ) $ dp [ $ i ] [ $ j ] = $ dp [ $ i - 1 ] [ $ j ] + $ dp [ $ i ] [ $ j - 1 ] ; return $ dp [ $ n ] [ $ m ] ; }"}
{"text":"Driver Code","code":"$ n = 3 ; $ m = 2 ; echo \" \u2581 Number \u2581 of \u2581 Paths \u2581 \" , countPaths ( $ n , $ m ) ; ? >"}
{"text":"Returns the count of ways we can sum S [ 0. . . m - 1 ] coins to get sum n","code":"< ? php function coun ( $ S , $ m , $ n ) {"}
{"text":"If n is 0 then there is 1 solution ( do not include any coin )","code":"if ( $ n == 0 ) return 1 ;"}
{"text":"If n is less than 0 then no solution exists","code":"if ( $ n < 0 ) return 0 ;"}
{"text":"If there are no coins and n is greater than 0 , then no solution exist","code":"if ( $ m <= 0 && $ n >= 1 ) return 0 ;"}
{"text":"count is sum of solutions ( i ) including S [ m - 1 ] ( ii ) excluding S [ m - 1 ]","code":"return coun ( $ S , $ m - 1 , $ n ) + coun ( $ S , $ m , $ n - $ S [ $ m - 1 ] ) ; }"}
{"text":"Driver Code","code":"$ arr = array ( 1 , 2 , 3 ) ; $ m = count ( $ arr ) ; echo coun ( $ arr , $ m , 4 ) ; ? >"}
{"text":"isVowel ( ) is a function that returns true for a vowel and false otherwise .","code":"< ? php function isVowel ( $ c ) { return ( $ c == ' a ' $ c == ' e ' $ c == ' i ' $ c == ' o ' $ c == ' u ' ) ; }"}
{"text":"function to Encrypt the dtring","code":"function encryptString ( $ s , $ n , $ k ) { $ countVowels = 0 ; $ countConsonants = 0 ; $ ans = \" \" ;"}
{"text":"for each substring","code":"for ( $ l = 0 ; $ l <= $ n - $ k ; $ l ++ ) { $ countVowels = 0 ; $ countConsonants = 0 ;"}
{"text":"substring of size k","code":"for ( $ r = $ l ; $ r <= $ l + $ k - 1 ; $ r ++ ) {"}
{"text":"counting number of vowels and consonants","code":"if ( isVowel ( $ s [ $ r ] ) == true ) $ countVowels ++ ; else $ countConsonants ++ ; }"}
{"text":"append product to answer .","code":"$ ans = $ ans . ( string ) ( $ countVowels * $ countConsonants ) ; } return $ ans ; }"}
{"text":"Driver Code","code":"$ s = \" hello \" ; $ n = strlen ( $ s ) ; $ k = 2 ; echo encryptString ( $ s , $ n , $ k ) . \" STRNEWLINE \" ;"}
{"text":"Function to find the biggest right circular cylinder","code":"< ? php function findVolume ( $ a ) {"}
{"text":"side cannot be negative","code":"if ( $ a < 0 ) return -1 ;"}
{"text":"radius of right circular cylinder","code":"$ r = $ a \/ 2 ;"}
{"text":"height of right circular cylinder","code":"$ h = $ a ;"}
{"text":"volume of right circular cylinder","code":"$ V = 3.14 * pow ( $ r , 2 ) * $ h ; return $ V ; }"}
{"text":"Driver code","code":"$ a = 5 ; echo findVolume ( $ a ) . \" STRNEWLINE \" ;"}
{"text":"Function to find the volume of triangular pyramid","code":"< ? php function volumeTriangular ( $ a , $ b , $ h ) { $ vol = ( 0.1666 ) * $ a * $ b * $ h ; return $ vol ; }"}
{"text":"Function to find the volume of square pyramid","code":"function volumeSquare ( $ b , $ h ) { $ vol = ( 0.33 ) * $ b * $ b * $ h ; return $ vol ; }"}
{"text":"Function to find the volume of pentagonal pyramid","code":"function volumePentagonal ( $ a , $ b , $ h ) { $ vol = ( 0.83 ) * $ a * $ b * $ h ; return $ vol ; }"}
{"text":"Function to find the volume of hexagonal pyramid","code":"function volumeHexagonal ( $ a , $ b , $ h ) { $ vol = $ a * $ b * $ h ; return $ vol ; }"}
{"text":"Driver Code","code":"$ b = 4 ; $ h = 9 ; $ a = 4 ; echo ( \" Volume \u2581 of \u2581 triangular \u2581 base \u2581 pyramid \u2581 is \u2581 \" ) ; echo ( volumeTriangular ( $ a , $ b , $ h ) ) ; echo ( \" STRNEWLINE \" ) ; echo ( \" Volume \u2581 of \u2581 square \u2581 base \u2581 pyramid \u2581 is \u2581 \" ) ; echo ( volumeSquare ( $ b , $ h ) ) ; echo ( \" STRNEWLINE \" ) ; echo ( \" Volume \u2581 of \u2581 pentagonal \u2581 base \u2581 pyramid \u2581 is \u2581 \" ) ; echo ( volumePentagonal ( $ a , $ b , $ h ) ) ; echo ( \" STRNEWLINE \" ) ; echo ( \" Volume \u2581 of \u2581 Hexagonal \u2581 base \u2581 pyramid \u2581 is \u2581 \" ) ; echo ( volumeHexagonal ( $ a , $ b , $ h ) ) ; ? >"}
{"text":"Function for the area","code":"< ? php function Area ( $ b1 , $ b2 , $ h ) { return ( ( $ b1 + $ b2 ) \/ 2 ) * $ h ; }"}
{"text":"Driver Code","code":"$ base1 = 8 ; $ base2 = 10 ; $ height = 6 ; $ area = Area ( $ base1 , $ base2 , $ height ) ; echo ( \" Area \u2581 is : \u2581 \" ) ; echo ( $ area ) ; ? >"}
{"text":"PHP function to find number of diagonals in n sided convex polygon","code":"< ? php function numberOfDiagonals ( $ n ) { return $ n * ( $ n - 3 ) \/ 2 ; }"}
{"text":"Driver Code","code":"$ n = 5 ; echo $ n , \" \u2581 sided \u2581 convex \u2581 polygon \u2581 have \u2581 \" ; echo numberOfDiagonals ( $ n ) , \" \u2581 diagonals \" ;"}
{"text":"Function to prints kth sub - string","code":"< ? php function Printksubstring ( $ str , $ n , $ k ) {"}
{"text":"Total sub - strings possible","code":"$ total = floor ( ( $ n * ( $ n + 1 ) ) \/ 2 ) ;"}
{"text":"If k is greater than total number of sub - strings","code":"if ( $ k > $ total ) { printf ( \" - 1 STRNEWLINE \" ) ; return ; }"}
{"text":"To store number of sub - strings starting with ith character of the string","code":"$ substring = array ( ) ; $ substring [ 0 ] = 0 ;"}
{"text":"Compute the values","code":"$ temp = $ n ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) {"}
{"text":"substring [ i - 1 ] is added to store the cumulative sum","code":"$ substring [ $ i ] = $ substring [ $ i - 1 ] + $ temp ; $ temp -- ; }"}
{"text":"Binary search to find the starting index of the kth sub - string","code":"$ l = 1 ; $ h = $ n ; $ start = 0 ; while ( $ l <= $ h ) { $ m = floor ( ( $ l + $ h ) \/ 2 ) ; if ( $ substring [ $ m ] > $ k ) { $ start = $ m ; $ h = $ m - 1 ; } else if ( $ substring [ $ m ] < $ k ) $ l = $ m + 1 ; else { $ start = $ m ; break ; } }"}
{"text":"To store the ending index of the kth sub - string","code":"$ end = $ n - ( $ substring [ $ start ] - $ k ) ;"}
{"text":"Print the sub - string","code":"for ( $ i = $ start - 1 ; $ i < $ end ; $ i ++ ) print ( $ str [ $ i ] ) ; }"}
{"text":"Driver code","code":"$ str = \" abc \" ; $ k = 4 ; $ n = strlen ( $ str ) ; Printksubstring ( $ str , $ n , $ k ) ; ? >"}
{"text":"Function to return the lower insertion point of an element in a sorted array","code":"< ? php function LowerInsertionPoint ( $ arr , $ n , $ X ) {"}
{"text":"Base cases","code":"if ( $ X < $ arr [ 0 ] ) return 0 ; else if ( $ X > $ arr [ $ n - 1 ] ) return $ n ; $ lowerPnt = 0 ; $ i = 1 ; while ( $ i < $ n && $ arr [ $ i ] < $ X ) { $ lowerPnt = $ i ; $ i = $ i * 2 ; }"}
{"text":"Final check for the remaining elements which are < X","code":"while ( $ lowerPnt < $ n && $ arr [ $ lowerPnt ] < $ X ) $ lowerPnt ++ ; return $ lowerPnt ; }"}
{"text":"Driver code","code":"$ arr = array ( 2 , 3 , 4 , 4 , 5 , 6 , 7 , 9 ) ; $ n = sizeof ( $ arr ) ; $ X = 4 ; echo LowerInsertionPoint ( $ arr , $ n , $ X ) ; ? >"}
{"text":"Returns count of required positions","code":"< ? php function getCount ( $ M , $ N ) { $ count = 0 ;"}
{"text":"horizontal 1D array","code":"if ( $ M == 1 ) return $ N ;"}
{"text":"vertical 1D array","code":"if ( $ N == 1 ) return $ M ; if ( $ N > $ M ) {"}
{"text":"iterating for all possible i","code":"for ( $ i = 1 ; $ i <= $ M ; $ i ++ ) { $ numerator = $ N * $ i - $ N + $ M - $ i ; $ denominator = $ M - 1 ;"}
{"text":"checking if j is integer","code":"if ( $ numerator % $ denominator == 0 ) { $ j = $ numerator \/ $ denominator ;"}
{"text":"checking if j lies b \/ w 1 to N","code":"if ( $ j >= 1 and $ j <= $ N ) $ count ++ ; } } } else {"}
{"text":"iterating for all possible j","code":"for ( $ j = 1 ; $ j <= $ N ; $ j ++ ) { $ numerator = $ M * $ j - $ M + $ N - $ j ; $ denominator = $ N - 1 ;"}
{"text":"checking if i is integer","code":"if ( $ numerator % $ denominator == 0 ) { $ i = $ numerator \/ $ denominator ;"}
{"text":"checking if i lies b \/ w 1 to M","code":"if ( $ i >= 1 and $ i <= $ M ) $ count ++ ; } } } return $ count ; }"}
{"text":"Driver Code","code":"$ M = 3 ; $ N = 5 ; echo getCount ( $ M , $ N ) ; ? >"}
{"text":"Function to find the middle of three numbers","code":"< ? php function middleOfThree ( $ a , $ b , $ c ) {"}
{"text":"Compare each three number to find middle number . Enter only if a > b","code":"if ( $ a > $ b ) { if ( $ b > $ c ) return $ b ; else if ( $ a > $ c ) return $ c ; else return $ a ; } else {"}
{"text":"Decided a is not greater than b .","code":"if ( $ a > $ c ) return $ a ; else if ( $ b > $ c ) return $ c ; else return $ b ; } }"}
{"text":"Driver Code","code":"$ a = 20 ; $ b = 30 ; $ c = 40 ; echo middleOfThree ( $ a , $ b , $ c ) ; ? >"}
{"text":"Utility function to print the contents of an array","code":"< ? php function printArr ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] ; }"}
{"text":"A comparison function that return true if ' AB ' is smaller than ' BA ' when we concatenate two numbers ' A ' and ' B ' For example , it will return true if we pass 12 and 24 as arguments . This function will be used by sort ( ) function","code":"function compare ( $ num1 , $ num2 ) {"}
{"text":"Convert first number to string format","code":"$ A = ( string ) $ num1 ;"}
{"text":"Convert second number to string format","code":"$ B = ( string ) $ num2 ;"}
{"text":"Check if ' AB ' is smaller or ' BA ' and return bool value since comparison operator ' < = ' returns true or false","code":"if ( ( int ) ( $ A . $ B ) <= ( int ) ( $ B . $ A ) ) { return true ; } else return false ; }"}
{"text":"Function to print the arrangement with the smallest value","code":"function printSmallest ( $ N , $ arr ) {"}
{"text":"If we pass the name of the comparison function it will sort the array according to the compare function","code":"$ arr = sort_arr ( $ arr ) ;"}
{"text":"Print the sorted array","code":"printArr ( $ arr , $ N ) ; }"}
{"text":"Driver code","code":"$ arr = array ( 5 , 6 , 2 , 9 , 21 , 1 ) ; $ N = count ( $ arr ) ; printSmallest ( $ N , $ arr ) ; ? >"}
{"text":"Check whether any permutation exists which satisfy the condition .","code":"< ? php function isPossible ( $ a , $ b , $ n , $ k ) {"}
{"text":"Sort the array a [ ] in decreasing order .","code":"sort ( $ a ) ;"}
{"text":"Sort the array b [ ] in increasing order .","code":"rsort ( $ b ) ;"}
{"text":"Checking condition on each index .","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ a [ $ i ] + $ b [ $ i ] < $ k ) return false ; return true ; }"}
{"text":"Driven Program","code":"$ a = array ( 2 , 1 , 3 ) ; $ b = array ( 7 , 8 , 9 ) ; $ k = 10 ; $ n = count ( $ a ) ; if ( isPossible ( $ a , $ b , $ n , $ k ) ) echo \" Yes \" ; else echo \" No \" ; ? >"}
{"text":"Function to return the encrypted string","code":"< ? php function encryptString ( $ str , $ n ) { $ i = 0 ; $ cnt = 0 ; $ encryptedStr = \" \" ; while ( $ i < $ n ) {"}
{"text":"Number of times the current character will be repeated","code":"$ cnt = $ i + 1 ;"}
{"text":"Repeat the current character in the encrypted string","code":"while ( $ cnt -- ) $ encryptedStr . = $ str [ $ i ] ; $ i ++ ; } return $ encryptedStr ; }"}
{"text":"Driver code","code":"$ str = \" geeks \" ; $ n = strlen ( $ str ) ; echo encryptString ( $ str , $ n ) ; ? >"}
{"text":"Function to return required minimum difference","code":"< ? php function minDiff ( $ n , $ x , $ A ) { $ mn = $ A [ 0 ] ; $ mx = $ A [ 0 ] ;"}
{"text":"finding minimum and maximum values","code":"for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { $ mn = min ( $ mn , $ A [ $ i ] ) ; $ mx = max ( $ mx , $ A [ $ i ] ) ; }"}
{"text":"returning minimum possible difference","code":"return max ( 0 , $ mx - $ mn - 2 * $ x ) ; }"}
{"text":"Driver program","code":"$ n = 3 ; $ x = 3 ; $ A = array ( 1 , 3 , 6 ) ;"}
{"text":"function to return the answer","code":"echo minDiff ( $ n , $ x , $ A ) ; ? >"}
