"Count valid pairs in the array satisfying given conditions | Function to return total valid pairs ; Initialize count of all the elements ; frequency count of all the elements ; Add total valid pairs ; Exclude pairs made with a single element i . e . ( x , x ) ; Driver Code ; Function call to print required answer"
Solution in php:

< ? php function ValidPairs ( $ arr , $ n ) { $ count = array_fill ( 0 , 121 , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ count [ $ arr [ $ i ] ] += 1 ; $ ans = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ arr [ $ i ] < $ arr [ $ j ] ) continue ; if ( abs ( $ arr [ $ i ] - $ arr [ $ j ] ) % 2 == 1 ) continue ; $ ans += $ count [ $ arr [ $ i ] ] * $ count [ $ arr [ $ j ] ] ; if ( $ arr [ $ i ] == $ arr [ $ j ] ) $ ans -= $ count [ $ arr [ $ i ] ] ; } return $ ans ; } $ arr = array ( 16 , 17 , 18 ) ; $ n = count ( $ arr ) ; echo ( ValidPairs ( $ arr , $ n ) ) ; ? >


"Count ways to build street under given constraints | function to count ways of building a street of n rows ; base case ; ways of building houses in both the spots of ith row ; ways of building an office in one of the two spots of ith row ; total ways for n rows ; Driver Code"
Solution in php:


"Count ways to distribute m items among n people | function used to generate binomial coefficient time complexity O ( m ) ; Helper function for generating no of ways to distribute m . mangoes amongst n people ; not enough mangoes to be distributed ; ways -> ( n + m - 1 ) C ( n - 1 ) ; m represents number of mangoes n represents number of people"
Solution in php:

< ? php function binomial_coefficient ( $ n , $ m ) { $ res = 1 ; if ( $ m > $ n - $ m ) $ m = $ n - $ m ; for ( $ i = 0 ; $ i < $ m ; ++ $ i ) { $ res *= ( $ n - $ i ) ; $ res /= ( $ i + 1 ) ; } return $ res ; } function calculate_ways ( $ m , $ n ) { if ( $ m < $ n ) return 0 ; $ ways = binomial_coefficient ( $ n + $ m - 1 , $ n - 1 ) ; return $ ways ; } $ m = 7 ; $ n = 5 ; $ result = calculate_ways ( $ m , $ n ) ; echo $ result ; ? >


"Count ways to divide circle using N non | PHP code to count ways to divide circle using N non - intersecting chords . ; n = no of points required ; dp array containing the sum ; returning the required number ; Driver Code"
Solution in php:

 < ? php function chordCnt ( $ A ) { $ n = 2 * $ A ; $ dpArray = array_fill ( 0 , $ n + 1 , 0 ) ; $ dpArray [ 0 ] = 1 ; $ dpArray [ 2 ] = 1 ; for ( $ i = 4 ; $ i <= $ n ; $ i += 2 ) { for ( $ j = 0 ; $ j < $ i - 1 ; $ j += 2 ) { $ dpArray [ $ i ] += ( $ dpArray [ $ j ] * $ dpArray [ $ i - 2 - $ j ] ) ; } } return $ dpArray [ $ n ] ; } $ N = 2 ; echo chordCnt ( $ N ) , "
 " ; $ N = 1 ; echo chordCnt ( $ N ) , "
 " ; $ N = 4 ; echo chordCnt ( $ N ) , "
 " ; ? >


"Count ways to express even number â €˜ nâ €™ as sum of even integers | Initialize mod variable as constant ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now $y = $y >> 1 ; y = y / 2 ; Return number of ways to write ' n ' as sum of even integers ; Driver code"
Solution in php:

 < ? php $ MOD = 1000000000.0 ; function power ( $ x , $ y , $ p ) { $ res = 1 ; $ x = $ x % $ p ; while ( $ y > 0 ) { if ( $ y & 1 ) $ res = ( 1 * $ res * $ x ) % $ p ; $ x = ( 1 * $ x * $ x ) % $ p ; } return $ res ; } function countEvenWays ( $ n ) { global $ MOD ; return power ( 2 , $ n / 2 - 1 , $ MOD ) ; } $ n = 6 ; echo countEvenWays ( $ n ) , "
 " ; $ n = 8 ; echo countEvenWays ( $ n ) ; ? >


"Count ways to form minimum product triplets | function to calculate number of triples ; Sort the array ; Count occurrences of third element ; If all three elements are same ( minimum element appears at least 3 times ) . Answer is nC3 . ; If minimum element appears once . Answer is nC2 . ; Minimum two elements are distinct . Answer is nC1 . ; Driver code"
Solution in php:

< ? php function noOfTriples ( $ arr , $ n ) { sort ( $ arr ) ; $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] == $ arr [ 2 ] ) $ count ++ ; if ( $ arr [ 0 ] == $ arr [ 2 ] ) return ( $ count - 2 ) * ( $ count - 1 ) * ( $ count ) / 6 ; else if ( $ arr [ 1 ] == $ arr [ 2 ] ) return ( $ count - 1 ) * ( $ count ) / 2 ; return $ count ; } $ arr = array ( 1 , 3 , 3 , 4 ) ; $ n = count ( $ arr ) ; echo noOfTriples ( $ arr , $ n ) ; ? >


"Count ways to spell a number with repeated digits | Function to calculate all possible spells of a number with repeated digits num -- > string which is favourite number ; final count of total possible spells ; iterate through complete number ; count contiguous frequency of particular digit num [ i ] ; Compute 2 ^ ( count - 1 ) and multiply with result ; Driver Code"
Solution in php:

< ? php function spellsCount ( $ num ) { $ n = strlen ( $ num ) ; $ result = 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ count = 1 ; while ( $ i < $ n - 1 && $ num [ $ i + 1 ] == $ num [ $ i ] ) { $ count ++ ; $ i ++ ; } $ result = $ result * pow ( 2 , $ count - 1 ) ; } return $ result ; } $ num = "11112" ; echo spellsCount ( $ num ) ; ? >


"Count zeros in a row wise and column wise sorted matrix | PHP program to count number of 0 s in the given row - wise and column - wise sorted binary matrix . ; Function to count number of 0 s in the given row - wise and column - wise sorted binary matrix . ; start from bottom - left corner of the matrix ; stores number of zeroes in the matrix ; move up until you find a 0 ; if zero is not found in current column , we are done ; add 0 s present in current column to result ; move right to next column ; Driver Code"
Solution in php:

< ? php $ N = 5 ; function countZeroes ( $ mat ) { $ row = $ N - 1 ; $ col = 0 ; $ count = 0 ; while ( $ col < $ N ) { while ( $ mat [ $ row ] [ $ col ] ) if ( -- $ row < 0 ) return $ count ; $ count += ( $ row + 1 ) ; $ col ++ ; } return $ count ; } $ mat = array ( array ( 0 , 0 , 0 , 0 , 1 ) , array ( 0 , 0 , 0 , 1 , 1 ) , array ( 0 , 1 , 1 , 1 , 1 ) , array ( 1 , 1 , 1 , 1 , 1 ) , array ( 1 , 1 , 1 , 1 , 1 ) ) ; echo countZeroes ( $ mat ) ; ? >


"Counting cross lines in an array | Function return count of cross line in an array ; Move elements of arr [ 0. . i - 1 ] , that are greater than key , to one position ahead of their current position ; increment cross line by one ; Driver Code"
Solution in php:

< ? php function countCrossLine ( $ arr , $ n ) { $ count_crossline = 0 ; $ i ; $ key ; $ j ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ key = $ arr [ $ i ] ; $ j = $ i - 1 ; while ( $ j >= 0 and $ arr [ $ j ] > $ key ) { $ arr [ $ j + 1 ] = $ arr [ $ j ] ; $ j = $ j - 1 ; $ count_crossline ++ ; } $ arr [ $ j + 1 ] = $ key ; } return $ count_crossline ; } $ arr = array ( 4 , 3 , 1 , 2 ) ; $ n = count ( $ arr ) ; echo countCrossLine ( $ arr , $ n ) ; ? >


"Counting even decimal value substrings in a binary string | function return count of even decimal value substring ; store the count of even decimal value substring ; substring started with '0' ; increment result by ( n - i ) because all substring which are generated by this character produce even decimal value . ; Driver Code"
Solution in php:

< ? php function evenDecimalValue ( $ str , $ n ) { $ result = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ str [ $ i ] == '0' ) { $ result += ( $ n - $ i ) ; } } return $ result ; } $ str = "10010" ; $ n = 5 ; echo evenDecimalValue ( $ str , $ n ) ; return 0 ; ? >


"Counting even decimal value substrings in a binary string | generate all substring in arr [ 0. . n - 1 ] ; store the count ; Pick starting point ; Pick ending point ; substring between current starting and ending points ; increment power of 2 by one ; Driver Code"
Solution in php:

< ? php function evenDecimalValue ( $ str , $ n ) { $ result = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = $ i ; $ j < $ n ; $ j ++ ) { $ decimalValue = 0 ; $ powerOf2 = 1 ; for ( $ k = $ i ; $ k <= $ j ; $ k ++ ) { $ decimalValue += ( ( $ str [ $ k ] - '0' ) * $ powerOf2 ) ; $ powerOf2 *= 2 ; } if ( $ decimalValue % 2 == 0 ) $ result ++ ; } } return $ result ; } $ str = "10010" ; $ n = 5 ; echo evenDecimalValue ( $ str , $ n ) ; ? >


"Counting numbers of n digits that are monotone | PHP program to count numbers of n digits that are monotone . ; Considering all possible digits as { 1 , 2 , 3 , . .9 } ; DP [ i ] [ j ] is going to store monotone numbers of length i + 1 considering j + 1 digits . ; Unit length numbers ; Single digit numbers ; Filling rest of the entries in bottom up manner . ; Driver code"
Solution in php:


"Counting numbers whose difference from reverse is a product of k | PHP program to Count the numbers within a given range in which when you subtract a number from its reverse , the difference is a product of k ; function to check if the number and its reverse have their absolute difference divisible by k ; reverse the number ; Driver code"
Solution in php:

< ? php function isRevDiffDivisible ( $ x , $ k ) { $ n = $ x ; $ m = 0 ; $ flag ; while ( $ x > 0 ) { $ m = $ m * 10 + $ x % 10 ; $ x = ( int ) $ x / 10 ; } return ( abs ( $ n - $ m ) % $ k == 0 ) ; } function countNumbers ( $ l , $ r , $ k ) { $ count = 0 ; for ( $ i = $ l ; $ i <= $ r ; $ i ++ ) if ( isRevDiffDivisible ( $ i , $ k ) ) ++ $ count ; return $ count ; } $ l = 20 ; $ r = 23 ; $ k = 6 ; echo countNumbers ( $ l , $ r , $ k ) ; ? >


"Counting pairs when a person can form pair with at most one | Number of ways in which participant can take part . ; Base condition ; A participant can choose to consider ( 1 ) Remains single . Number of people reduce to ( x - 1 ) ( 2 ) Pairs with one of the ( x - 1 ) others . For every pairing , number of peopl reduce to ( x - 2 ) . ; Driver code"
Solution in php:

< ? php function numberOfWays ( $ x ) { if ( $ x == 0 $ x == 1 ) return 1 ; else return numberOfWays ( $ x - 1 ) + ( $ x - 1 ) * numberOfWays ( $ x - 2 ) ; } $ x = 3 ; echo numberOfWays ( $ x ) ; ? >


"Counting pairs when a person can form pair with at most one | PHP program for Number of ways in which participant can take part . ; Driver code"
Solution in php:


"Covering maximum array elements with given value | Function to find value for covering maximum array elements ; sort the students in ascending based on the candies ; To store the number of happy students ; To store the running sum ; If the current student can 't be made happy ; increment the count if we can make the ith student happy ; If the sum = x then answer is n ; If the count is equal to n then the answer is n - 1 ; driver code"
Solution in php:

< ? php function maxArrayCover ( $ a , $ n , $ x ) { sort ( $ a ) ; $ cc = 0 ; $ s = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ s += $ a [ $ i ] ; if ( $ s > $ x ) { break ; } $ cc += 1 ; } if ( array_sum ( $ a ) == $ x ) { return $ n ; } else { if ( $ cc == $ n ) { return $ n - 1 ; } else { return $ cc ; } } } $ n = 3 ; $ x = 70 ; $ a = array ( 10 , 20 , 30 ) ; echo maxArrayCover ( $ a , $ n , $ x ) ; ? >


"Create a matrix with alternating rectangles of O and X | Function to print alternating rectangles of 0 and X ; k - starting row index m - ending row index l - starting column index n - ending column index i - iterator ; Store given number of rows and columns for later use ; Iniitialize the character to be stoed in a [ ] [ ] ; Fill characters in a [ ] [ ] in spiral form . Every iteration fills one rectangle of either Xs or Os ; Fill the first row from the remaining rows ; Fill the last column from the remaining columns ; Fill the last row from the remaining rows ; Print the first column from the remaining columns ; Flip character for next iteration ; Print the filled matrix ; Driver Code"
Solution in php:


"Create a new string by alternately combining the characters of two halves of the string in reverse | Function performing calculations ; Calculating the two halves of string s as first and second . The final string p ; It joins the characters to final string in reverse order ; It joins the characters to final string in reverse order ; Driver code ; Calling function"
Solution in php:


"Cube Free Numbers smaller than n | Efficient PHP Program to print all cube free numbers smaller than or equal to n . ; Initialize all numbers as not cube free ; Traverse through all possible cube roots ; If i itself is cube free ; Mark all multiples of i as not cube free ; Print all cube free numbers ; Driver Code"
Solution in php:

< ? php function printCubeFree ( $ n ) { $ cubFree = array_fill ( 0 , ( $ n + 1 ) , 1 ) ; $ i = 2 ; while ( $ i * $ i * $ i <= $ n ) { if ( $ cubFree [ $ i ] == 1 ) { $ multiple = 1 ; while ( $ i * $ i * $ i * $ multiple <= $ n ) { $ cubFree [ $ i * $ i * $ i * $ multiple ] = 0 ; $ multiple += 1 ; } } $ i += 1 ; } for ( $ i = 2 ; $ i < $ n + 1 ; $ i ++ ) if ( $ cubFree [ $ i ] == 1 ) echo $ i . " " ; } printCubeFree ( 20 ) ; ? >


"Cube Free Numbers smaller than n | Returns true if n is a cube free number , else returns false . ; check for all possible divisible cubes ; Print all cube free numbers smaller than n . ; Driver code"
Solution in php:

< ? php function isCubeFree ( $ n ) { if ( $ n == 1 ) return false ; for ( $ i = 2 ; $ i * $ i * $ i <= $ n ; $ i ++ ) if ( $ n % ( $ i * $ i * $ i ) == 0 ) return false ; return true ; } function printCubeFree ( $ n ) { for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) if ( isCubeFree ( $ i ) ) echo $ i . " " ; } $ n = 20 ; printCubeFree ( $ n ) ; ? >


"Cunningham chain | Function to print Cunningham chain of the first kind ; Iterate till all elements are printed ; check prime or not ; Driver Code"
Solution in php:


"Cunningham chain | Function to print Cunningham chain of the second kind ; Iterate till all elements are printed ; check prime or not ; Driver Code"
Solution in php:


"Decimal Equivalent of Gray Code and its Inverse | Function to convert given decimal number of gray code into its inverse in decimal form ; Taking xor until n becomes zero ; Driver Code"
Solution in php:

< ? php function inversegrayCode ( $ n ) { $ inv = 0 ; for ( ; $ n ; $ n = $ n >> 1 ) $ inv ^= $ n ; return $ inv ; } $ n = 15 ; echo inversegrayCode ( $ n ) ; ? >


"Decimal representation of given binary string is divisible by 10 or not | function to check whether decimal representation of given binary number is divisible by 10 or not ; if last digit is '1' , then number is not divisible by 10 ; to accumulate the sum of last digits in perfect powers of 2 ; traverse from the 2 nd last up to 1 st digit in ' bin ' ; if digit in '1' ; calculate digit 's  position from the right ; according to the digit 's  position, obtain the last  digit of the applicable  perfect power of 2 ; if last digit is 0 , then divisible by 10 ; not divisible by 10 ; Driver Code"
Solution in php:

< ? php function isDivisibleBy10 ( $ bin ) { $ n = strlen ( $ bin ) ; if ( $ bin [ $ n - 1 ] == '1' ) return false ; $ sum = 0 ; for ( $ i = $ n - 2 ; $ i >= 0 ; $ i -- ) { if ( $ bin [ $ i ] == '1' ) { $ posFromRight = $ n - $ i - 1 ; if ( $ posFromRight % 4 == 1 ) $ sum = $ sum + 2 ; else if ( $ posFromRight % 4 == 2 ) $ sum = $ sum + 4 ; else if ( $ posFromRight % 4 == 3 ) $ sum = $ sum + 8 ; else if ( $ posFromRight % 4 == 0 ) $ sum = $ sum + 6 ; } } if ( $ sum % 10 == 0 ) return true ; return false ; } $ bin = "11000111001110" ; if ( isDivisibleBy10 ( $ bin ) ) echo " Yes " ; else echo " No " ; ? >


"Decimal representation of given binary string is divisible by 20 or not | function to check whether decimal representation of given binary number is divisible by 10 or not ; if last digit is '1' , then number is not divisible by 10 ; to accumulate the sum of last digits in perfect powers of 2 ; traverse from the 2 nd last up to 1 st digit in ' bin ' ; if digit in '1' ; calculate digit 's position  from the right ; according to the digit 's position,  obtain the last digit of the  applicable perfect power of 2 ; if last digit is 0 , then divisible by 10 ; not divisible by 10 ; function to check whether decimal representation of given binary number is divisible by 20 or not ; if ' bin ' is an odd number ; check if bin ( 0. . n - 2 ) is divisible by 10 or not ; Driver code"
Solution in php:

< ? php function isDivisibleBy10 ( $ bin , $ n ) { if ( $ bin [ $ n - 1 ] == '1' ) return false ; $ sum = 0 ; for ( $ i = $ n - 2 ; $ i >= 0 ; $ i -- ) { if ( $ bin [ $ i ] == '1' ) { $ posFromRight = $ n - $ i - 1 ; if ( $ posFromRight % 4 == 1 ) $ sum = $ sum + 2 ; else if ( $ posFromRight % 4 == 2 ) $ sum = $ sum + 4 ; else if ( $ posFromRight % 4 == 3 ) $ sum = $ sum + 8 ; else if ( $ posFromRight % 4 == 0 ) $ sum = $ sum + 6 ; } } if ( $ sum % 10 == 0 ) return true ; return false ; } function isDivisibleBy20 ( $ bin , $ n ) { if ( $ bin [ $ n - 1 ] == '1' ) return false ; return isDivisibleBy10 ( $ bin , $ n - 1 ) ; } $ bin = "101000" ; $ n = strlen ( $ bin ) ; if ( isDivisibleBy20 ( $ bin , $ n - 1 ) ) echo " Yes " ; else echo " No " ; ? >


"Decimal to binary conversion without using arithmetic operators | function for decimal to binary conversion without using arithmetic operators ; to store the binary equivalent of decimal ; to get the last binary digit of the number ' n ' and accumulate it at the beginning of ' bin ' ; right shift ' n ' by 1 ; required binary number ; Driver Code"
Solution in php:


"Deficient Number | Function to calculate sum of divisors ; Initialize sum of prime factors ; Note that this loop runs till square root of n ; If divisors are equal , take only one of them ; Otherwise take both ; Function to check Deficient Number ; Check if sum ( n ) < 2 * n ; Driver Code"
Solution in php:

 < ? php function divisorsSum ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= sqrt ( $ n ) ; $ i ++ ) { if ( $ n % $ i == 0 ) { if ( $ n / $ i == $ i ) { $ sum = $ sum + $ i ; } else { $ sum = $ sum + $ i ; $ sum = $ sum + ( $ n / $ i ) ; } } } return $ sum ; } function isDeficient ( $ n ) { return ( divisorsSum ( $ n ) < ( 2 * $ n ) ) ; } $ ds = isDeficient ( 12 ) ? " YES
 " : " NO
 " ; echo ( $ ds ) ; $ ds = isDeficient ( 15 ) ? " YES
 " : " NO
 " ; echo ( $ ds ) ; ? >


"Delannoy Number | Return the nth Delannoy Number . ; Base case ; Recursive step . ; Driver Code"
Solution in php:

< ? php function dealnnoy ( $ n , $ m ) { if ( $ m == 0 or $ n == 0 ) return 1 ; return dealnnoy ( $ m - 1 , $ n ) + dealnnoy ( $ m - 1 , $ n - 1 ) + dealnnoy ( $ m , $ n - 1 ) ; } $ n = 3 ; $ m = 4 ; echo dealnnoy ( $ n , $ m ) ; ? >


"Delannoy Number | Return the nth Delannoy Number . ; Base cases ; Driven Code"
Solution in php:


"Delete array element in given index range [ L | Delete L to R elements ; Return size of Array after delete element ; Driver Code"
Solution in php:


"Demlo number ( Square of 11. . .1 ) | To return demlo number . This function assumes that the length of str is smaller than 10. ; Add numbers to res upto size of str and then add number reverse to it ; Driver Code"
Solution in php:

< ? php function printDemlo ( $ str ) { $ len = strlen ( $ str ) ; $ res = " " ; for ( $ i = 1 ; $ i <= $ len ; $ i ++ ) $ res . = chr ( $ i + 48 ) ; for ( $ i = $ len - 1 ; $ i >= 1 ; $ i -- ) $ res . = chr ( $ i + 48 ) ; return $ res ; } $ str = "111111" ; echo printDemlo ( $ str ) ; ? >


"Deserium Number | Returns count of digits in n . ; Returns true if x is Diserium ; Compute powers of digits from right to left . ; If sum of powers is same as given number . ; Driver Code"
Solution in php:

< ? php function countDigits ( $ n ) { $ c = 0 ; do { $ c ++ ; $ n = $ n / 10 ; } while ( $ n != 0 ) ; return $ c ; } function isDeserium ( $ x ) { $ temp = $ x ; $ p = countDigits ( $ x ) ; $ sum = 0 ; while ( $ x != 0 ) { $ digit = $ x % 10 ; $ sum += pow ( $ digit , $ p ) ; $ p -- ; $ x = $ x / 10 ; } return ( $ sum == $ temp ) ; } $ x = 135 ; if ( isDeserium ( $ x ) ) echo " No " ; else echo " Yes " ; ? >


"Detect if two integers have opposite signs | Function to detect signs ; Driver Code"
Solution in php:

< ? php function oppositeSigns ( $ x , $ y ) { return ( ( $ x ^ $ y ) < 0 ) ; } $ x = 100 ; $ y = -100 ; if ( oppositeSigns ( $ x , $ y ) == true ) echo ( " Signs ▁ are ▁ opposite " ) ; else echo ( " Signs ▁ are ▁ not ▁ opposite " ) ; ? >


"Determine if a string has all Unique Characters | PHP program to illustrate string with unique characters using brute force technique ; Assuming string can have characters a - z , this has 32 bits set to 0 ; if that bit is already set in checker , return false ; otherwise update and continue by setting that bit in the checker ; no duplicates encountered , return true ; Driver Code"
Solution in php:

 < ? php function uniqueCharacters ( $ str ) { $ checker = 0 ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) { $ bitAtIndex = $ str [ $ i ] - ' a ' ; if ( ( $ checker & ( 1 << $ bitAtIndex ) ) > 0 ) { return false ; } $ checker = $ checker | ( 1 << $ bitAtIndex ) ; } return true ; } $ str = " geeksforgeeks " ; if ( uniqueCharacters ( $ str ) ) { echo " The ▁ String ▁ " , $ str , " ▁ has ▁ all ▁ unique ▁ characters
 " ; } else { echo " The ▁ String ▁ " , $ str , " ▁ has ▁ duplicate ▁ characters
 " ; } ? >


"Determine if a string has all Unique Characters | PHP program to illustrate string with unique characters using brute force technique ; If at any time we encounter 2 same characters , return false ; If no duplicate characters encountered , return true ; Driver Code"
Solution in php:

 < ? php function uniqueCharacters ( $ str ) { for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) { for ( $ j = $ i + 1 ; $ j < strlen ( $ str ) ; $ j ++ ) { if ( $ str [ $ i ] == $ str [ $ j ] ) { return false ; } } } return true ; } $ str = " GeeksforGeeks " ; if ( uniqueCharacters ( $ str ) ) { echo " The ▁ String ▁ " , $ str , " ▁ has ▁ all ▁ unique ▁ characters
 " ; } else { echo " The ▁ String ▁ " , $ str , " ▁ has ▁ duplicate ▁ characters
 " ; } ? >


"Determine the count of Leaf nodes in an N | Function to calculate leaf nodes in n - ary tree ; Driver code"
Solution in php:

< ? php function calcNodes ( $ N , $ I ) { $ result = 0 ; $ result = $ I * ( $ N - 1 ) + 1 ; return $ result ; } $ N = 5 ; $ I = 2 ; echo " Leaf ▁ nodes ▁ = ▁ " . calcNodes ( $ N , $ I ) ; ? >


"Determine the number of squares of unit area that a given line will pass through . | Function to return the required position ;  ; Driver Code"
Solution in php:

< ? php function noOfSquares ( $ x1 , $ y1 , $ x2 , $ y2 ) { $ dx = abs ( $ x2 - $ x1 ) ; $ dy = abs ( $ y2 - $ y1 ) ; $ ans = $ dx + $ dy - gcd ( $ dx , $ dy ) ; echo ( $ ans ) ; } function gcd ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; return gcd ( $ b , $ a % $ b ) ; } $ x1 = 1 ; $ y1 = 1 ; $ x2 = 4 ; $ y2 = 3 ; noOfSquares ( $ x1 , $ y1 , $ x2 , $ y2 ) ; ? >


"Determine the position of the third person on regular N sided polygon | Function to find out the number of that vertices ; Another person can 't stand on  vertex on which 2 children stand. ; calculating minimum jumps from each vertex . ; Calculate sum of jumps . ; Driver code ; Calling function"
Solution in php:

< ? php function vertices ( $ N , $ A , $ B ) { $ position = 0 ; $ minisum = PHP_INT_MAX ; $ sum = 0 ; for ( $ i = 1 ; $ i <= $ N ; $ i ++ ) { if ( $ i == $ A $ i == $ B ) continue ; else { $ x = abs ( $ i - $ A ) ; $ y = abs ( $ i - $ B ) ; $ sum = $ x + $ y ; if ( $ sum < $ minisum ) { $ minisum = $ sum ; $ position = $ i ; } } } return $ position ; } $ N = 3 ; $ A = 1 ; $ B = 2 ; echo " Vertex = " ? >


"Determine whether a given number is a Hyperperfect Number | function to find the sum of all proper divisors ( excluding 1 and N ) ; Iterate only until sqrt N as we are going to generate pairs to produce divisors ; As divisors occur in pairs , we can take the values i and N / i as long as i divides N ; Function to check whether the given number is prime ; base and corner cases ; Since integers can be represented as some 6 * k + y where y >= 0 , we can eliminate all integers that can be expressed in this form ; start from 5 as this is the next prime number ; Returns true if N is a K - Hyperperfect number Else returns false . ; Condition from the definition of hyperperfect ; Driver Code ; First two statements test against the condition N = 1 + K * ( sum ( proper divisors ) )"
Solution in php:

 < ? php function divisorSum ( $ N , $ K ) { $ sum = 0 ; for ( $ i = 2 ; $ i <= ceil ( sqrt ( $ N ) ) ; $ i ++ ) if ( $ N % $ i == 0 ) $ sum += ( $ i + $ N / $ i ) ; return $ sum ; } function isPrime ( $ n ) { if ( $ n == 1 $ n == 0 ) return false ; if ( $ n <= 3 ) return true ; if ( $ n % 2 == 0 $ n % 3 == 0 ) return false ; for ( $ i = 5 ; $ i * $ i <= $ n ; $ i = $ i + 6 ) if ( $ n % $ i == 0 || $ n % ( $ i + 2 ) == 0 ) return false ; return true ; } function isHyperPerfect ( $ N , $ K ) { $ sum = divisorSum ( $ N , $ K ) ; if ( ( 1 + $ K * ( $ sum ) ) == $ N ) return true ; else return false ; } $ N1 = 1570153 ; $ K1 = 12 ; $ N2 = 321 ; $ K2 = 3 ; if ( isHyperPerfect ( $ N1 , $ K1 ) ) echo $ N1 , " ▁ is ▁ " , $ K1 , " - HyperPerfect " , "
 " ; else echo $ N1 , " ▁ is ▁ not ▁ " , $ K1 , " - HyperPerfect " , "
 " ; if ( isHyperPerfect ( $ N2 , $ K2 ) ) echo $ N2 , " ▁ is ▁ " , K2 , " - HyperPerfect " , "
 " ; else echo $ N2 , " ▁ is ▁ not ▁ " , $ K2 , " - HyperPerfect " , "
 " ; ? >


"Diagonal of a Regular Decagon | Function to return the diagonal of a regular decagon ; Side cannot be negative ; Length of the diagonal ; Driver code"
Solution in php:

< ? php function decdiagonal ( $ a ) { if ( $ a < 0 ) return -1 ; $ d = 1.902 * $ a ; return $ d ; } $ a = 9 ; echo decdiagonal ( $ a ) ; ? >


"Diagonal of a Regular Heptagon | Function to return the diagonal of a regular heptagon ; Side cannot be negative ; Length of the diagonal ; Driver code"
Solution in php:

< ? php function heptdiagonal ( $ a ) { if ( $ a < 0 ) return -1 ; $ d = 1.802 * $ a ; return $ d ; } $ a = 6 ; echo heptdiagonal ( $ a ) ;


"Diagonal of a Regular Hexagon | Function to find the diagonal of a regular hexagon ; Side cannot be negative ; Length of the diagonal ; Driver code"
Solution in php:

 < ? php function hexDiagonal ( $ a ) { if ( $ a < 0 ) return -1 ; $ d = 1.73 * $ a ; return $ d ; } $ a = 9 ; echo hexDiagonal ( $ a ) , "
 " ; ? >


"Diagonal of a Regular Pentagon | Function to find the diagonal of a regular pentagon ; Side cannot be negative ; Length of the diagonal ; Driver code"
Solution in php:

< ? php function pentdiagonal ( $ a ) { if ( $ a < 0 ) return -1 ; $ d = 1.22 * $ a ; return $ d ; } $ a = 6 ; echo pentdiagonal ( $ a ) ; ? >


"Dice Throw | DP | The main function that returns number of ways to get sum ' x ' with ' n ' dice and ' m ' with m faces . ; Create a table to store results of subproblems . One extra row and column are used for simpilicity ( Number of dice is directly used as row index and sum is directly used as column index ) . The entries in 0 th row and 0 th column are never used . ; Table entries for only one dice ; Fill rest of the entries in table using recursive relation i : number of dice , j : sum ; Return value ; Driver Code"
Solution in php:


"Difference between Recursion and Iteration | -- -- - Recursion -- -- - method to find factorial of given number ; recursion call ; -- -- - Iteration -- -- - Method to find the factorial of a given number ; using iteration ; Driver method"
Solution in php:

 < ? php function factorialUsingRecursion ( $ n ) { if ( $ n == 0 ) return 1 ; return $ n * factorialUsingRecursion ( $ n - 1 ) ; } function factorialUsingIteration ( $ n ) { $ res = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ res *= $ i ; return $ res ; } $ num = 5 ; print ( " Factorial ▁ of ▁ " . $ num . " ▁ using ▁ Recursion ▁ is : ▁ " . factorialUsingRecursion ( 5 ) . "
 " ) ; print ( " Factorial ▁ of ▁ " . $ num . " ▁ using ▁ Iteration ▁ is : ▁ " . factorialUsingIteration ( 5 ) . "
 " ) ; ? >


"Difference between highest and least frequencies in an array | PHP code to find the difference between highest and least frequencies function that returns difference ; sort the array ; checking consecutive elements ; Driver Code"
Solution in php:

< ? php function findDiff ( $ arr , $ n ) { sort ( $ arr ) ; $ count = 0 ; $ max_count = 0 ; $ min_count = $ n ; for ( $ i = 0 ; $ i < ( $ n - 1 ) ; $ i ++ ) { if ( $ arr [ $ i ] == $ arr [ $ i + 1 ] ) { $ count += 1 ; continue ; } else { $ max_count = max ( $ max_count , $ count ) ; $ min_count = min ( $ min_count , $ count ) ; $ count = 0 ; } } return ( $ max_count - $ min_count ) ; } $ arr = array ( 7 , 8 , 4 , 5 , 4 , 1 , 1 , 7 , 7 , 2 , 5 ) ; $ n = sizeof ( $ arr ) ; echo ( findDiff ( $ arr , $ n ) . " " ) ; ? >


"Difference of two large numbers | Returns true if str1 is smaller than str2 , else false . ; Calculate lengths of both string ; Function for finding difference of larger numbers ; Before proceeding further , make sure str1 is not smaller ; Take an empty string for storing result ; Calculate lengths of both string ; Initially take carry zero ; Traverse from end of both strings ; Do school mathematics , compute difference of current digits and carry ; subtract remaining digits of str1 [ ] ; if ( $i > 0 $sub > 0 ) remove preceding 0 's ; reverse resultant string ; Driver code ; Function call"
Solution in php:

< ? php function isSmaller ( $ str1 , $ str2 ) { $ n1 = strlen ( $ str1 ) ; $ n2 = strlen ( $ str2 ) ; if ( $ n1 < $ n2 ) return true ; if ( $ n2 < $ n1 ) return false ; for ( $ i = 0 ; $ i < $ n1 ; $ i ++ ) { if ( $ str1 [ $ i ] < $ str2 [ $ i ] ) return true ; else if ( $ str1 [ $ i ] > $ str2 [ $ i ] ) return false ; } return false ; } function findDiff ( $ str1 , $ str2 ) { if ( isSmaller ( $ str1 , $ str2 ) ) { $ t = $ str1 ; $ str1 = $ str2 ; $ str2 = $ t ; } $ str = " " ; $ n1 = strlen ( $ str1 ) ; $ n2 = strlen ( $ str2 ) ; $ diff = $ n1 - $ n2 ; $ carry = 0 ; for ( $ i = $ n2 - 1 ; $ i >= 0 ; $ i -- ) { $ sub = ( ( ord ( $ str1 [ $ i + $ diff ] ) - ord ( '0' ) ) - ( ord ( $ str2 [ $ i ] ) - ord ( '0' ) ) - $ carry ) ; if ( $ sub < 0 ) { $ sub = $ sub + 10 ; $ carry = 1 ; } else $ carry = 0 ; $ str . = chr ( $ sub + ord ( "0" ) ) ; } for ( $ i = $ n1 - $ n2 - 1 ; $ i >= 0 ; $ i -- ) { if ( $ str1 [ $ i ] == '0' && $ carry > 0 ) { $ str . = "9" ; continue ; } $ sub = ( ord ( $ str1 [ $ i ] ) - ord ( '0' ) - $ carry ) ; $ str . = chr ( $ sub + ord ( "0" ) ) ; $ carry = 0 ; } return strrev ( $ str ) ; } $ str1 = "88" ; $ str2 = "1079" ; print ( findDiff ( $ str1 , $ str2 ) ) ; ? >


"Different ways to sum n using numbers greater than or equal to m | PHP Program to find number of ways to which numbers that are greater than given number can be added to get sum . ; Return number of ways to which numbers that are greater than given number can be added to get sum . ; Filling the table . k is for numbers greater than or equal that are allowed . ; i is for sum ; initializing dp [ i ] [ k ] to number ways to get sum using numbers greater than or equal k + 1 ; if i > k ; Driver Program"
Solution in php:


"Digit | function to produce and print Digit Product Sequence ; Array which store sequence ; Temporary variable to store product ; Initialize first element of the array with 1 ; Run a loop from 1 to N . Check if previous number is single digit or not . If yes then product = 1 else take modulus . Then again check if previous number is single digit or not if yes then store previous number , else store its first value Then for every i store value in the array . ; Print sequence ; Value of N ; Calling function"
Solution in php:


"Direction at last square block | Function which tells the Current direction ; Driver Code"
Solution in php:


"Discrete logarithm ( Find an integer k such that a ^ k is congruent modulo b ) | Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; $x = $x % $p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now $y = $y >> 1 ; y = y / 2 ; Function to calculate k for given a , b , m ; Store all values of a ^ ( n * i ) of LHS ; Calculate ( a ^ j ) * b and check for collision ; If collision occurs i . e . , LHS = RHS ; Check whether ans lies below m or not ; Driver code"
Solution in php:

< ? php function powmod ( $ x , $ y , $ p ) { while ( $ y > 0 ) { if ( $ y & 1 ) $ res = ( $ res * $ x ) % $ p ; $ x = ( $ x * $ x ) % $ p ; } return $ res ; } function discreteLogarithm ( $ a , $ b , $ m ) { $ n = ( int ) sqrt ( $ m ) + 1 ; $ value = array_fill ( 0 , $ m , NULL ) ; for ( $ i = $ n ; $ i >= 1 ; -- $ i ) $ value [ powmod ( $ a , $ i * $ n , $ m ) ] = $ i ; for ( $ j = 0 ; $ j < $ n ; ++ $ j ) { $ cur = ( powmod ( $ a , $ j , $ m ) * $ b ) % $ m ; if ( $ value [ $ cur ] ) { $ ans = $ value [ $ cur ] * $ n - $ j ; if ( $ ans < $ m ) return $ ans ; } } return -1 ; } $ a = 2 ; $ b = 3 ; $ m = 5 ; echo discreteLogarithm ( $ a , $ b , $ m ) , " " ; $ a = 3 ; $ b = 7 ; $ m = 11 ; echo discreteLogarithm ( $ a , $ b , $ m ) , " " ; ? >


"Discrete logarithm ( Find an integer k such that a ^ k is congruent modulo b ) | PHP program to calculate discrete logarithm ; Calculate a ^ n ; Store all values of a ^ ( n * i ) of LHS ; Calculate ( a ^ j ) * b and check for collision ; Driver code"
Solution in php:

< ? php function discreteLogarithm ( $ a , $ b , $ m ) { $ n = ( int ) sqrt ( $ m ) + 1 ; $ an = 1 ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) $ an = ( $ an * $ a ) % $ m ; $ value = array_fill ( 0 , $ m , NULL ) ; for ( $ i = 1 , $ cur = $ an ; $ i <= $ n ; ++ $ i ) { if ( ! $ value [ $ cur ] ) $ value [ $ cur ] = $ i ; $ cur = ( $ cur * $ an ) % $ m ; } for ( $ i = 0 , $ cur = $ b ; $ i <= $ n ; ++ $ i ) { if ( $ value [ $ cur ] ) { $ ans = $ value [ $ cur ] * $ n - $ i ; if ( $ ans < $ m ) return $ ans ; } $ cur = ( $ cur * $ a ) % $ m ; } return -1 ; } $ a = 2 ; $ b = 3 ; $ m = 5 ; echo discreteLogarithm ( $ a , $ b , $ m ) , " " ; $ a = 3 ; $ b = 7 ; $ m = 11 ; echo discreteLogarithm ( $ a , $ b , $ m ) ; ? >


"Distance between two nodes of binary tree with node values from 1 to N | Function to get minimum path distance ; count bit length of n1 and n2 ; find bit difference and maxBit ; calculate result by formula ; Driver Code"
Solution in php:

< ? php function minDistance ( $ n1 , $ n2 ) { $ bitCount1 = floor ( log ( $ n1 , 2 ) ) + 1 ; $ bitCount2 = floor ( log ( $ n2 , 2 ) ) + 1 ; $ bitDiff = abs ( $ bitCount1 - $ bitCount2 ) ; $ maxBitCount = max ( $ bitCount1 , $ bitCount2 ) ; if ( $ bitCount1 > $ bitCount2 ) { $ n2 = $ n2 * pow ( 2 , $ bitDiff ) ; } else { $ n1 = $ n1 * pow ( 2 , $ bitDiff ) ; } $ xorValue = $ n1 ^ $ n2 ; $ bitCountXorValue = floor ( log ( $ xorValue , 2 ) ) + 1 ; $ disSimilarBitPosition = $ maxBitCount - $ bitCountXorValue ; $ result = $ bitCount1 + $ bitCount2 - 2 * $ disSimilarBitPosition ; return $ result ; } $ n1 = 12 ; $ n2 = 5 ; echo minDistance ( $ n1 , $ n2 ) ; ? >


"Distance between two parallel lines | Function to find the distance between parallel lines ; Driver Code"
Solution in php:


"Distance of nearest cell having 1 in a binary matrix | PHP program to find distance of nearest cell having 1 in a binary matrix . ; Print the distance of nearest cell having 1 for each cell . ; Initialize the answer matrix with INT_MAX . ; For each cell ; Traversing the whole matrix to find the minimum distance . ; If cell contain 1 , check for minimum distance . ; Printing the answer . ; Driver Code"
Solution in php:


"Distribute N candies among K people | Function to find out the number of candies every person received ; Count number of complete turns ; Get the last term ; Stores the number of candies ; Do a binary search to find the number whose sum is less than N . ; Get mide ; If sum is below N ; Find number of complete turns ; Right halve ; Left halve ; Last term of last complete series ; Subtract the sum till ; First term of incomplete series ; Count the total candies ; Print the total candies ; Driver Code"
Solution in php:


"Distribute N candies among K people | Function to find out the number of candies every person received ; Count number of complete turns ; Get the last term ; Stores the number of candies ; Last term of last and current series ; Sum of current and last series ; Sum of current series only ; If sum of current is less than N ; else Individually distribute ; First term ; Distribute candies till there ; Candies available ; Not available ; Count the total candies ; Print the total candies ; Driver Code"
Solution in php:


"Distributing M items in a circle of size N starting from K | n == > Size of circle m == > Number of items k == > Initial position ; n - k + 1 is number of positions before we reach beginning of circle . If m is less than this value , then we can simply return ( m - 1 ) th position ; Let us compute remaining items before we reach beginning . ; We compute m % n to skip all complete rounds . If we reach end , we return n else we return m % n ; Driver code"
Solution in php:


"Divide 1 to n into two groups with minimum sum difference | To print vector along size ; Print vector size ; Print vector elements ; To divide n in two groups such that absolute difference of their sum is minimum ; Find sum of all elements upto n ; Sum of elements of group1 ; If sum is greater then or equal to 0 include i in group 1 otherwise include in group2 ; Decrease sum of group1 ; Print both the groups ; Driver Code"
Solution in php:

 < ? php function printVector ( $ v ) { echo count ( $ v ) . "
 " ; for ( $ i = 0 ; $ i < count ( $ v ) ; $ i ++ ) echo $ v [ $ i ] . " ▁ " ; echo "
 " ; } function findTwoGroup ( $ n ) { $ sum = $ n * ( $ n + 1 ) / 2 ; $ group1Sum = ( int ) ( $ sum / 2 ) ; $ group1 ; $ group2 ; $ x = 0 ; $ y = 0 ; for ( $ i = $ n ; $ i > 0 ; $ i -- ) { if ( $ group1Sum - $ i >= 0 ) { $ group1 [ $ x ++ ] = $ i ; $ group1Sum -= $ i ; } else { $ group2 [ $ y ++ ] = $ i ; } } printVector ( $ group1 ) ; printVector ( $ group2 ) ; } $ n = 5 ; findTwoGroup ( $ n ) ; ? >


"Divide a number into two parts | Function to print the two parts ; Find the position of 4 ; If current character is not '4' but appears after the first occurrence of '4' ; Print both the parts ; Driver code"
Solution in php:


"Divide a string in N equal parts | Function to print n equal parts of str ; Check if string can be divided in n equal parts ; Calculate the size of parts to find the division point ; Driver Code length od string is 28 ; Print 4 equal parts of the string"
Solution in php:

 < ? php function divideString ( $ str , $ n ) { $ str_size = strlen ( $ str ) ; $ i ; $ part_size ; if ( $ str_size % $ n != 0 ) { echo " Invalid ▁ Input : ▁ String ▁ size " ; echo " ▁ is ▁ not ▁ divisible ▁ by ▁ n " ; return ; } $ part_size = $ str_size / $ n ; for ( $ i = 0 ; $ i < $ str_size ; $ i ++ ) { if ( $ i % $ part_size == 0 ) echo "
 " ; echo $ str [ $ i ] ; } } $ str = " a _ simple _ divide _ string _ quest " ; divideString ( $ str , 4 ) ; ? >


"Divide an array into k segments to maximize maximum of segment minimums | function to calculate the max of all the minimum segments ; if we have to divide it into 1 segment then the min will be the answer ; If k >= 3 , return maximum of all elements . ; Driver Code"
Solution in php:

< ? php function maxOfSegmentMins ( $ a , $ n , $ k ) { if ( $ k == 1 ) return min ( $ a ) ; if ( $ k == 2 ) return max ( $ a [ 0 ] , $ a [ $ n - 1 ] ) ; return max ( $ a ) ; } $ a = array ( -10 , -9 , -8 , 2 , 7 , -6 , -5 ) ; $ n = count ( $ a ) ; $ k = 2 ; echo maxOfSegmentMins ( $ a , $ n , $ k ) ; ? >


"Divide an isosceles triangle in two parts with ratio of areas as n : m | Function to return the height ; type cast the n , m into float ; calculate the height for cut ; Driver code"
Solution in php:

< ? php function heightCalculate ( $ H , $ n , $ m ) { $ N = $ n * 1.0 ; $ M = $ m * 1.0 ; $ h = $ H * sqrt ( $ N / ( $ N + $ M ) ) ; return $ h ; } $ H = 10 ; $ n = 3 ; $ m = 4 ; echo heightCalculate ( $ H , $ n , $ m ) ; ? >


"Divide array into increasing and decreasing subsequence without changing the order | Function to print strictly increasing and strictly decreasing sequence if possible ; Arrays to store strictly increasing and decreasing sequence ; Initializing last element of both sequence ; Iterating through the array ; If current element can be appended to both the sequences ; If next element is greater than the current element Then append it to the strictly increasing array ; Otherwise append it to the strictly decreasing array ; If current element can be appended to the increasing sequence only ; If current element can be appended to the decreasing sequence only ; Else we can not make such sequences from the given array ; Print the required sequences ; Driver code"
Solution in php:

< ? php function Find_Sequence ( $ arr , $ n ) { $ inc_arr = array ( ) ; $ dec_arr = array ( ) ; $ inc = -1 ; $ dec = 1e7 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ inc < $ arr [ $ i ] && $ arr [ $ i ] < $ dec ) { if ( $ arr [ $ i ] < $ arr [ $ i + 1 ] ) { $ inc = $ arr [ $ i ] ; array_push ( $ inc_arr , $ arr [ $ i ] ) ; } else { $ dec = $ arr [ $ i ] ; array_push ( $ dec_arr , $ arr [ $ i ] ) ; } } else if ( $ inc < $ arr [ $ i ] ) { $ inc = $ arr [ $ i ] ; array_push ( $ inc_arr , $ arr [ $ i ] ) ; } else if ( $ dec > $ arr [ $ i ] ) { $ dec = $ arr [ $ i ] ; array_push ( $ dec_arr , $ arr [ $ i ] ) ; } else { echo ' - 1' ; break ; } } print_r ( $ inc_arr ) ; print_r ( $ dec_arr ) ; } $ arr = array ( 5 , 1 , 3 , 6 , 8 , 2 , 9 , 0 , 10 ) ; $ n = count ( $ arr ) ; Find_Sequence ( $ arr , $ n ) ; ? >


"Divide array into two parts with equal sum according to the given constraints | Function that checks if the given conditions are satisfied ; To store the prefix $sum of the array elements ; Sort the array ; Compute the prefix sum array ; Maximum element in the array ; Variable to check if there exists any number ; Stores the index of the largest number present in the array smaller than i ; Stores the index of the smallest number present in the array greater than i ; Find index of smallest number greater than i ; Find index of smallest number greater than i ; If there exists a number ; If no such number exists print no ; Driver code"
Solution in php:

< ? php function IfExists ( $ arr , $ n ) { $ sum = array_fill ( 0 , $ n , 0 ) ; sort ( $ arr ) ; $ sum [ 0 ] = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ sum [ $ i ] = $ sum [ $ i - 1 ] + $ arr [ $ i ] ; $ max = $ arr [ $ n - 1 ] ; $ flag = false ; for ( $ i = 1 ; $ i <= $ max ; $ i ++ ) { $ findex = 0 ; $ lindex = 0 ; $ l = 0 ; $ r = $ n - 1 ; while ( $ l <= $ r ) { $ m = ( $ l + $ r ) / 2 ; if ( $ arr [ $ m ] < $ i ) { $ findex = $ m ; $ l = $ m + 1 ; } else $ r = $ m - 1 ; } $ l = 1 ; $ r = $ n ; $ flag = false ; while ( $ l <= $ r ) { $ m = ( $ r + $ l ) / 2 ; if ( $ arr [ $ m ] > $ i ) { $ lindex = $ m ; $ r = $ m - 1 ; } else $ l = $ m + 1 ; } if ( $ sum [ $ findex ] == $ sum [ $ n - 1 ] - $ sum [ $ lindex - 1 ] ) { $ flag = true ; break ; } } if ( $ flag == true ) echo " Yes " ; else echo " No " ; } $ arr = array ( 1 , 2 , 2 , 5 ) ; $ n = sizeof ( $ arr ) ; IfExists ( $ arr , $ n ) ; ? >


"Divide every element of one array by other array elements | Function to calculate the quotient of every element of the array ; Calculate the product of all elements ; To calculate the quotient of every array element ; Driver code"
Solution in php:


"Divide number into two parts divisible by given numbers | method prints divisible parts if possible , otherwise prints ' Not ▁ possible ' ; creating arrays to store reminder ; looping over all suffix and storing reminder with f ; getting suffix reminder from previous suffix reminder ; looping over all prefix and storing reminder with s ; getting prefix reminder from next prefix reminder ; updating base value ; now looping over all reminders to check partition condition ; if both reminders are 0 and digit itself is not 0 , then print result and return ; if we reach here , then string can ' be partitioned  under constraints ; Driver code"
Solution in php:

 < ? php function printTwoDivisibleParts ( $ num , $ f , $ s ) { $ N = strlen ( $ num ) ; $ prefixReminder = array_fill ( 0 , $ N + 1 , 0 ) ; $ suffixReminder = array_fill ( 0 , $ N + 1 , 0 ) ; for ( $ i = 1 ; $ i < $ N ; $ i ++ ) $ suffixReminder [ $ i ] = ( $ suffixReminder [ $ i - 1 ] * 10 + ( ord ( $ num [ $ i - 1 ] ) - 48 ) ) % $ f ; $ base = 1 ; for ( $ i = $ N - 1 ; $ i >= 0 ; $ i -- ) { $ prefixReminder [ $ i ] = ( $ prefixReminder [ $ i + 1 ] + ( ord ( $ num [ $ i ] ) - 48 ) * $ base ) % $ s ; $ base = ( $ base * 10 ) % $ s ; } for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { if ( $ prefixReminder [ $ i ] == 0 && $ suffixReminder [ $ i ] == 0 && $ num [ $ i ] != '0' ) { echo substr ( $ num , 0 , $ i ) . " ▁ " . substr ( $ num , $ i ) . "
 " ; return ; } } echo " Not ▁ Possible
 " ; } $ num = "246904096" ; $ f = 12345 ; $ s = 1024 ; printTwoDivisibleParts ( $ num , $ f , $ s ) ; ? >


"Divide the two given numbers by their common divisors | Function to calculate gcd of two numbers ; Function to calculate all common divisors of two given numbers a , b -- > input integer numbers ; find gcd of a , b ; Driver code"
Solution in php:

 < ? php function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; } function commDiv ( $ a , $ b ) { $ n = gcd ( $ a , $ b ) ; $ a = ( int ) ( $ a / $ n ) ; $ b = ( int ) ( $ b / $ n ) ; echo " A = " ▁ . ▁ $ a ▁ .
 	 	 " , B = " ▁ . ▁ $ b ▁ . ▁ " " } $ a = 10 ; $ b = 15 ; commDiv ( $ a , $ b ) ; ? >


"Divide the two given numbers by their common divisors | print the numbers after dividing them by their common factors ; iterate from 1 to minimum of a and b ; if i is the common factor of both the numbers ; Driver code ; divide A and B by their common factors"
Solution in php:


"Divide two integers without using multiplication , division and mod operator | Function to divide a by b and return floor value it ; Calculate sign of divisor i . e . , sign will be negative either one of them is negative only iff otherwise it will be positive ; remove sign of operands ; Initialize the quotient ; test down from the highest bit and accumulate the tentative value for valid bit ; if the sign value computed earlier is - 1 then negate the value of quotient ; Driver code"
Solution in php:

 < ? php function divide ( $ dividend , $ divisor ) { $ sign = ( ( $ dividend < 0 ) ^ ( $ divisor < 0 ) ) ? -1 : 1 ; $ dividend = abs ( $ dividend ) ; $ divisor = abs ( $ divisor ) ; $ quotient = 0 ; $ temp = 0 ; for ( $ i = 31 ; $ i >= 0 ; -- $ i ) { if ( $ temp + ( $ divisor << $ i ) <= $ dividend ) { $ temp += $ divisor << $ i ; $ quotient |= ( double ) ( 1 ) << $ i ; } } if ( $ sign == -1 ) $ quotient = - $ quotient ; return $ quotient ; } $ a = 10 ; $ b = 3 ; echo divide ( $ a , $ b ) . "
 " ; $ a = 43 ; $ b = -8 ; echo divide ( $ a , $ b ) ; ? >


"Divide two integers without using multiplication , division and mod operator | Set2 | Returns the quotient of dividend / divisor . ; Calculate sign of divisor i . e . , sign will be negative only if either one of them is negative otherwise it will be positive ; Remove signs of dividend and divisor ; Zero division Exception . ; Using Formula derived above . ; Driver code"
Solution in php:

 < ? php function Divide ( $ a , $ b ) { $ dividend = $ a ; $ divisor = $ b ; $ sign = ( $ dividend < 0 ) ^ ( $ divisor < 0 ) ? -1 : 1 ; $ dividend = abs ( $ dividend ) ; $ divisor = abs ( $ divisor ) ; if ( $ divisor == 0 ) { echo " Cannot ▁ Divide ▁ by ▁ 0" ; echo " " ; } if ( $ dividend == 0 ) { echo $ a , " ▁ / ▁ " , $ b , " ▁ is ▁ equal ▁ to ▁ : ▁ " , 0 ; echo " " ; } if ( $ divisor == 1 ) { echo $ a , " ▁ / ▁ " , $ b , " ▁ is ▁ equal ▁ to ▁ : ▁ " , $ sign * $ dividend . "
 " ; echo " " ; } echo $ a , " ▁ / ▁ " , $ b , " ▁ is ▁ equal ▁ to ▁ : ▁ " , $ sign * exp ( log ( $ dividend ) - log ( $ divisor ) ) . "
 " ; echo " " ; } $ a = 10 ; $ b = 5 ; Divide ( $ a , $ b ) ; $ a = 49 ; $ b = -7 ; Divide ( $ a , $ b ) ; ? >


"Divisibility by 12 for a large number | PHP Program to check if number is divisible by 12 ; if number greater then 3 ; find last digit ; no is odd ; find second last digit ; find sum of all digits ; if number is less then or equal to 100 ; Driver Code"
Solution in php:

< ? php function isDvisibleBy12 ( $ num ) { if ( strlen ( $ num ) >= 3 ) { $ d1 = ( int ) $ num [ strlen ( $ num ) - 1 ] ; if ( $ d1 % 2 != 0 ) return ( 0 ) ; $ d2 = ( int ) $ num [ strlen ( $ num ) - 2 ] ; $ sum = 0 ; for ( $ i = 0 ; $ i < strlen ( $ num ) ; $ i ++ ) $ sum += $ num [ $ i ] ; return ( $ sum % 3 == 0 && ( $ d2 * 10 + $ d1 ) % 4 == 0 ) ; } else { $ number = stoi ( $ num ) ; return ( $ number % 12 == 0 ) ; } } $ num = "12244824607284961224" ; if ( isDvisibleBy12 ( $ num ) ) echo ( " Yes " ) ; else echo ( " No " ) ; ? >


"Divisibility by 3 where each digit is the sum of all prefix digits modulo 10 | Function to check the divisibility ; Cycle ; no of residual terms ; sum of residual terms ; if no of residue term = 0 ; if no of residue term = 1 ; if no of residue term = 2 ; if no of residue term = 3 ; sum of all digits ; divisibility check ; Driver code"
Solution in php:


"Divisibility by 64 with removal of bits allowed | function to check if it is possible to make it a multiple of 64. ; counter to count 0 's ; length of the string ; loop which traverses right to left and calculates the number of zeros before 1. ; Driver Code"
Solution in php:

< ? php function checking ( $ s ) { $ c = 0 ; $ n = strlen ( $ s ) ; for ( $ i = $ n - 1 ; $ i >= 0 ; $ i -- ) { if ( $ s [ $ i ] == '0' ) $ c ++ ; if ( $ c >= 6 and $ s [ $ i ] == '1' ) return true ; } return false ; } $ s = "100010001" ; if ( checking ( $ s ) ) echo " Possible " ; else echo " Not ▁ possible " ; ? >


"Division without using ' / ' operator | Function to find division without using ' / ' operator ; Handling negative numbers ; if num1 is greater than equal to num2 subtract num2 from num1 and increase quotient by one . ; checking if neg equals to 1 then making quotient negative ; Driver program"
Solution in php:

< ? php function division ( $ num1 , $ num2 ) { if ( $ num1 == 0 ) return 0 ; if ( $ num2 == 0 ) return INT_MAX ; $ negResult = false ; if ( $ num1 < 0 ) { $ num1 = - $ num1 ; if ( $ num2 < 0 ) $ num2 = - $ num2 ; else $ negResult = true ; } else if ( $ num2 < 0 ) { $ num2 = - $ num2 ; $ negResult = true ; } $ quotient = 0 ; while ( $ num1 >= $ num2 ) { $ num1 = $ num1 - $ num2 ; $ quotient ++ ; } if ( $ negResult ) $ quotient = - $ quotient ; return $ quotient ; } $ num1 = 13 ; $ num2 = 2 ; echo division ( $ num1 , $ num2 ) ; ? >


"Dodecagonal number | function for Dodecagonal number ; formula for find Dodecagonal nth term ; Driver code"
Solution in php:

 < ? php function Dodecagonal_number ( $ n ) { return 5 * $ n * $ n - 4 * $ n ; } $ n = 7 ; echo Dodecagonal_number ( $ n ) , "
 " ; $ n = 12 ; echo Dodecagonal_number ( $ n ) , "
 " ; ? >


"Dodecahedral number | Function to find dodecahedral number ; Formula to calculate nth dodecahedral number and return it into main function . ; Drivers Code ; print result"
Solution in php:

< ? php function dodecahedral_num ( $ n ) { return $ n * ( 3 * $ n - 1 ) * ( 3 * $ n - 2 ) / 2 ; } $ n = 5 ; echo $ n , " th ▁ Dodecahedral ▁ number : ▁ " ; echo dodecahedral_num ( $ n ) ; ? >


"Doolittle Algorithm : LU Decomposition | PHP Program to decompose a matrix into lower and upper triangular matrix ; Decomposing matrix into Upper and Lower triangular matrix ; Upper Triangular ; Summation of L ( i , j ) * U ( j , k ) ; Evaluating U ( i , k ) ; Lower Triangular ; $lower [ $i ] [ $i ] = 1 ; Diagonal as 1 ; Summation of L ( k , j ) * U ( j , i ) ; Evaluating L ( k , i ) ; setw is for displaying nicely ; Displaying the result : ; Lower ; Upper ; Driver code"
Solution in php:


"Dudeney Numbers | Function that returns true if n is a Dudeney number ; If n is not a perfect cube ; Last digit ; Update the digit sum ; Remove the last digit ; If cube root of n is not equal to the sum of its digits ; Driver code"
Solution in php:

< ? php function isDudeney ( $ n ) { $ cube_rt = floor ( round ( ( pow ( $ n , 1.0 / 3.0 ) ) ) ) ; if ( $ cube_rt * $ cube_rt * $ cube_rt != $ n ) return false ; $ dig_sum = 0 ; $ temp = $ n ; while ( $ temp > 0 ) { $ rem = $ temp % 10 ; $ dig_sum += $ rem ; $ temp = $ temp / 10 ; } if ( $ cube_rt != $ dig_sum ) return false ; return true ; } $ n = 17576 ; if ( isDudeney ( $ n ) ) echo " Yes " ; else echo " No " ; ? >


"Duplicates in an array in O ( n ) time and by using O ( 1 ) extra space | Set | Function to find repeating elements ; Flag variable used to represent whether repeating element is found or not . ; Check if current element is repeating or not . If it is repeating then value will be greater than or equal to n . ; Check if it is first repetition or not . If it is first repetition then value at index arr [ i ] is less than 2 * n . Print arr [ i ] if it is first repetition . ; Add n to index arr [ i ] to mark presence of arr [ i ] or to mark repetition of arr [ i ] . ; If flag variable is not set then no repeating element is found . So print - 1. ; Driver Function"
Solution in php:

< ? php function printDuplicates ( $ arr , $ n ) { $ i ; $ fl = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ arr [ $ i ] % $ n ] >= $ n ) { if ( $ arr [ $ arr [ $ i ] % $ n ] < 2 * $ n ) { echo $ arr [ $ i ] % $ n . " " ; $ fl = 1 ; } } $ arr [ $ arr [ $ i ] % $ n ] += $ n ; } if ( ! $ fl ) echo " - 1" ; } $ arr = array ( 1 , 6 , 3 , 1 , 3 , 6 , 6 ) ; $ arr_size = sizeof ( $ arr ) ; printDuplicates ( $ arr , $ arr_size ) ;


"Dyck path | Returns count Dyck paths in n x n grid ; Compute value of 2 nCn ; return 2 nCn / ( n + 1 ) ; Driver Code"
Solution in php:

< ? php function countDyckPaths ( $ n ) { $ res = 1 ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { $ res *= ( 2 * $ n - $ i ) ; $ res /= ( $ i + 1 ) ; } return $ res / ( $ n + 1 ) ; } $ n = 4 ; echo " Number ▁ of ▁ Dyck ▁ Paths ▁ is ▁ " , countDyckPaths ( $ n ) ; ? >


"Edit distance and LCS ( Longest Common Subsequence ) | PHP program to find Edit Distance ( when only two operations are allowed , insert and delete ) using LCS . ; Find LCS ; Edit distance is delete operations + insert operations . ; Driver Code"
Solution in php:


"Efficient Program to Compute Sum of Series 1 / 1 ! + 1 / 2 ! + 1 / 3 ! + 1 / 4 ! + . . + 1 / n ! | An Efficient Function to return value of 1 / 1 ! + 1 / 2 ! + . . + 1 / n ! ; Update factorial ; Update series sum ; Driver Code"
Solution in php:

< ? php function sum ( $ n ) { $ sum = 0 ; $ fact = 1 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ fact *= $ i ; $ sum += 1.0 / $ fact ; } return $ sum ; } $ n = 5 ; echo sum ( $ n ) ; ? >


"Efficient Program to Compute Sum of Series 1 / 1 ! + 1 / 2 ! + 1 / 3 ! + 1 / 4 ! + . . + 1 / n ! | Utility function to find ; A Simple Function to return value of 1 / 1 ! + 1 / 2 ! + . . + 1 / n ! ; Driver Code"
Solution in php:

< ? php function factorial ( $ n ) { $ res = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ res *= $ i ; return $ res ; } function sum ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ sum += 1.0 / factorial ( $ i ) ; return $ sum ; } $ n = 5 ; echo ( sum ( $ n ) ) ; ? >


"Efficient method for 2 's complement of a binary string | Function to find two 's complement ; Traverse the string to get first '1' from the last of string ; If there exists no '1' concatenate 1 at the starting of string ; Continue traversal after the position of first '1' ; Just flip the values ; return the modified string ; Driver code"
Solution in php:

< ? php function findTwoscomplement ( $ str ) { $ n = strlen ( $ str ) ; $ i ; for ( $ i = $ n - 1 ; $ i >= 0 ; $ i -- ) if ( $ str [ $ i ] == '1' ) break ; if ( $ i == -1 ) return '1' + $ str ; for ( $ k = $ i - 1 ; $ k >= 0 ; $ k -- ) { if ( $ str [ $ k ] == '1' ) $ str [ $ k ] = '0' ; else $ str [ $ k ] = '1' ; } return $ str ; ; } $ str = "00000101" ; echo findTwoscomplement ( $ str ) ; ? >


"Efficient program to calculate e ^ x | Returns approximate value of e ^ x using sum of first n terms of Taylor Series ; initialize sum of series ; Driver Code"
Solution in php:

< ? php function exponential ( $ n , $ x ) { $ sum = 1.0 ; for ( $ i = $ n - 1 ; $ i > 0 ; -- $ i ) $ sum = 1 + $ x * $ sum / $ i ; return $ sum ; } $ n = 10 ; $ x = 1.0 ; echo ( " e ^ x ▁ = ▁ " . exponential ( $ n , $ x ) ) ; ? >


"Efficient program to print the number of factors of n numbers | PHP program to count number of factors of an array of integers ; function to generate all prime factors of numbers from 1 to 10 ^ 6 ; Initializes all the positions with their value . ; Initializes all multiples of 2 with 2 ; A modified version of Sieve of Eratosthenes to store the smallest prime factor that divides every number . ; check if it has no prime factor . ; Initializes of j starting from i * i ; if it has no prime factor before , then stores the smallest prime divisor ; function to calculate number of factors ; stores the smallest prime number that divides n ; stores the count of number of times a prime number divides n . ; reduces to the next number after prime factorization of n ; false when prime factorization is done ; if the same prime number is dividing n , then we increase the count ; if its a new prime factor that is factorizing n , then we again set c = 1 and change dup to the new prime factor , and apply the formula explained above . ; prime factorizes a number ; for the last prime factor ; generate prime factors of number upto 10 ^ 6"
Solution in php:

< ? php $ MAX = 1000001 ; $ factor = array_fill ( 0 , $ MAX + 1 , 0 ) ; function generatePrimeFactors ( ) { global $ factor ; global $ MAX ; $ factor [ 1 ] = 1 ; for ( $ i = 2 ; $ i < $ MAX ; $ i ++ ) $ factor [ $ i ] = $ i ; for ( $ i = 4 ; $ i < $ MAX ; $ i += 2 ) $ factor [ $ i ] = 2 ; for ( $ i = 3 ; $ i * $ i < $ MAX ; $ i ++ ) { if ( $ factor [ $ i ] == $ i ) { for ( $ j = $ i * $ i ; $ j < $ MAX ; $ j += $ i ) { if ( $ factor [ $ j ] == $ j ) $ factor [ $ j ] = $ i ; } } } } function calculateNoOFactors ( $ n ) { global $ factor ; if ( $ n == 1 ) return 1 ; $ ans = 1 ; $ dup = $ factor [ $ n ] ; $ c = 1 ; $ j = ( int ) ( $ n / $ factor [ $ n ] ) ; while ( $ j != 1 ) { if ( $ factor [ $ j ] == $ dup ) $ c += 1 ; else { $ dup = $ factor [ $ j ] ; $ ans = $ ans * ( $ c + 1 ) ; $ c = 1 ; } $ j = ( int ) ( $ j / $ factor [ $ j ] ) ; } $ ans = $ ans * ( $ c + 1 ) ; return $ ans ; } generatePrimeFactors ( ) ; $ a = array ( 10 , 30 , 100 , 450 , 987 ) ; $ q = sizeof ( $ a ) ; for ( $ i = 0 ; $ i < $ q ; $ i ++ ) echo calculateNoOFactors ( $ a [ $ i ] ) . " ▁ " ; ? >


"Efficiently check if a string has all unique characters without using any additional data structure | Returns true if all characters of str are unique . Assumptions : ( 1 ) str contains only characters from ' a ' to ' z ' ( 2 ) integers are stored using 32 bits ; An integer to store presence / absence of 26 characters using its 32 bits . ; If bit corresponding to current character is already set ; set bit in checker ; Driver code"
Solution in php:

< ? php function areChractersUnique ( $ str ) { $ checker = 0 ; for ( $ i = 0 ; $ i < $ len = strlen ( $ str ) ; ++ $ i ) { $ val = ( $ str [ $ i ] - ' a ' ) ; if ( ( $ checker & ( 1 << $ val ) ) > 0 ) return false ; $ checker |= ( 1 << $ val ) ; } return true ; } $ s = " aaabbccdaa " ; if ( areChractersUnique ( $ s ) ) echo " Yes " ; else echo " No " ; ? >


"Efficiently compute sums of diagonals of a matrix | A simple PHP program to find sum of diagonals ; Condition for principal diagonal ; Condition for secondary diagonal ; Driver code"
Solution in php:

 < ? php $ MAX = 100 ; function printDiagonalSums ( $ mat , $ n ) { global $ MAX ; $ principal = 0 ; $ secondary = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ i == $ j ) $ principal += $ mat [ $ i ] [ $ j ] ; if ( ( $ i + $ j ) == ( $ n - 1 ) ) $ secondary += $ mat [ $ i ] [ $ j ] ; } } echo " Principal ▁ Diagonal : " , $ principal , "
 " ; echo " Secondary ▁ Diagonal : " , $ secondary , "
 " ; } $ a = array ( array ( 1 , 2 , 3 , 4 ) , array ( 5 , 6 , 7 , 8 ) , array ( 1 , 2 , 3 , 4 ) , array ( 5 , 6 , 7 , 8 ) ) ; printDiagonalSums ( $ a , 4 ) ; ? >


"Efficiently compute sums of diagonals of a matrix | An efficient PHP program to find sum of diagonals ; Driver Code"
Solution in php:

 < ? php $ MAX = 100 ; function printDiagonalSums ( $ mat , $ n ) { global $ MAX ; $ principal = 0 ; $ secondary = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ principal += $ mat [ $ i ] [ $ i ] ; $ secondary += $ mat [ $ i ] [ $ n - $ i - 1 ] ; } echo " Principal ▁ Diagonal : " , $ principal , "
 " ; echo " Secondary ▁ Diagonal : " , $ secondary , "
 " ; } $ a = array ( array ( 1 , 2 , 3 , 4 ) , array ( 5 , 6 , 7 , 8 ) , array ( 1 , 2 , 3 , 4 ) , array ( 5 , 6 , 7 , 8 ) ) ; printDiagonalSums ( $ a , 4 ) ; ? >


"Efficiently find first repeated character in a string without using any additional data structure in one traversal | Returns - 1 if all characters of str are unique . Assumptions : ( 1 ) str contains only characters from ' a ' to ' z ' ( 2 ) integers are stored using 32 bits ; An integer to store presence / absence of 26 characters using its 32 bits . ; If bit corresponding to current character is already set ; set bit in checker ; Driver code"
Solution in php:

< ? php function FirstRepeated ( $ str ) { $ checker = 0 ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; ++ $ i ) { $ val = ( ord ( $ str [ $ i ] ) - ord ( ' a ' ) ) ; if ( ( $ checker & ( 1 << $ val ) ) > 0 ) return $ i ; $ checker |= ( 1 << $ val ) ; } return -1 ; } $ s = " abcfdeacf " ; $ i = FirstRepeated ( $ s ) ; if ( $ i != -1 ) echo " Char ▁ = ▁ " . $ s [ $ i ] . " ▁ and ▁ Index ▁ = ▁ " . $ i ; else echo " No ▁ repeated ▁ Char " ; ? >


"Efficiently merging two sorted arrays with O ( 1 ) extra space | Function to find next gap . ; comparing elements in the first array . ; comparing elements in both arrays . ; comparing elements in the second array . ; Driver code ; Function Call"
Solution in php:


"Egg Dropping Puzzle with 2 Eggs and K Floors | PHP program to find optimal number of trials for k floors and 2 eggs . ; Driver Code"
Solution in php:

< ? php function twoEggDrop ( $ k ) { return ceil ( ( -1.0 + sqrt ( 1 + 8 * $ k ) ) / 2.0 ) ; } $ k = 100 ; echo twoEggDrop ( $ k ) ;


"Egg Dropping Puzzle | DP | Function to get minimum number of trials needed in worst case with n eggs and k floors ; A 2D table where entry eggFloor [ i ] [ j ] will represent minimum number of trials needed for i eggs and j floors . ; We need one trial for one floor and0 trials for 0 floors ; We always need j trials for one egg and j floors . ; Fill rest of the entries in table using optimal substructure property ; eggFloor [ n ] [ k ] holds the result ; Driver Code"
Solution in php:

< ? php function eggDrop ( $ n , $ k ) { $ eggFloor = array ( array ( ) ) ; ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ eggFloor [ $ i ] [ 1 ] = 1 ; $ eggFloor [ $ i ] [ 0 ] = 0 ; } for ( $ j = 1 ; $ j <= $ k ; $ j ++ ) $ eggFloor [ 1 ] [ $ j ] = $ j ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { for ( $ j = 2 ; $ j <= $ k ; $ j ++ ) { $ eggFloor [ $ i ] [ $ j ] = 999999 ; for ( $ x = 1 ; $ x <= $ j ; $ x ++ ) { $ res = 1 + max ( $ eggFloor [ $ i - 1 ] [ $ x - 1 ] , $ eggFloor [ $ i ] [ $ j - $ x ] ) ; if ( $ res < $ eggFloor [ $ i ] [ $ j ] ) $ eggFloor [ $ i ] [ $ j ] = $ res ; } } } return $ eggFloor [ $ n ] [ $ k ] ; } $ n = 2 ; $ k = 36 ; echo " Minimum ▁ number ▁ of ▁ trials ▁ in ▁ worst ▁ case ▁ with ▁ " . $ n . " ▁ eggs ▁ and ▁ " . $ k . " ▁ floors ▁ is ▁ " . eggDrop ( $ n , $ k ) ; ? >


"Eggs dropping puzzle ( Binomial Coefficient and Binary Search Solution ) | Find sum of binomial coefficients xCi ( where i varies from 1 to n ) . If the sum becomes more than K ; Do binary search to find minimum number of trials in worst case . ; Initialize low and high as 1 st and last floors ; Do binary search , for every mid , find sum of binomial coefficients and check if the sum is greater than k or not . ; Driver Code"
Solution in php:

< ? php function binomialCoeff ( $ x , $ n , $ k ) { $ sum = 0 ; $ term = 1 ; for ( $ i = 1 ; $ i <= $ n && $ sum < $ k ; ++ $ i ) { $ term *= $ x - $ i + 1 ; $ term /= $ i ; $ sum += $ term ; } return $ sum ; } function minTrials ( $ n , $ k ) { $ low = 1 ; $ high = $ k ; while ( $ low < $ high ) { $ mid = ( $ low + $ high ) / 2 ; if ( binomialCoeff ( $ mid , $ n , $ k ) < $ k ) $ low = $ mid + 1 ; else $ high = $ mid ; } return ( int ) $ low ; } echo minTrials ( 2 , 10 ) ; ? >


"Element equal to the sum of all the remaining elements | Function to find the element ; sum is use to store sum of all elements of array ; iterate over all elements ; Driver code"
Solution in php:

< ? php function findEle ( $ arr , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += $ arr [ $ i ] ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] == $ sum - $ arr [ $ i ] ) return $ arr [ $ i ] ; return -1 ; } $ arr = array ( 1 , 2 , 3 , 6 ) ; $ n = sizeof ( $ arr ) ; echo findEle ( $ arr , $ n ) ; ? >


"Elements greater than the previous and next element in an Array | Function to print elements greater than the previous and next element in an Array ; Traverse array from index 1 to n - 2 and check for the given condition ; Driver Code"
Solution in php:

< ? php function printElements ( $ arr , $ n ) { for ( $ i = 1 ; $ i < $ n - 1 ; $ i ++ ) { if ( $ arr [ $ i ] > $ arr [ $ i - 1 ] and $ arr [ $ i ] > $ arr [ $ i + 1 ] ) echo $ arr [ $ i ] . " ▁ " ; } } $ arr = array ( 2 , 3 , 1 , 5 , 4 , 9 , 8 , 7 , 5 ) ; $ n = sizeof ( $ arr ) ; printElements ( $ arr , $ n ) ;


"Elements that occurred only once in the array | Function to find the elements that appeared only once in the array ; Check if the first and last element is equal . If yes , remove those elements ; Start traversing the remaining elements ; Check if current element is equal to the element at immediate previous index If yes , check the same for next element ; Else print the current element ; Check for the last element ; Driver code"
Solution in php:


"Elements that occurred only once in the array | Function to find the elements that appeared only once in the array ; Sort the array ; Check for first element ; Check for all the elements if it is different its adjacent elements ; Check for the last element ; Driver code"
Solution in php:


"Elements to be added so that all elements of a range are present in array | Function to count numbers to be added ; Sort the array ; Check if elements are consecutive or not . If not , update count ; Driver code"
Solution in php:

< ? php function countNum ( $ arr , $ n ) { $ count = 0 ; sort ( $ arr ) ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) if ( $ arr [ $ i ] != $ arr [ $ i + 1 ] && $ arr [ $ i ] != $ arr [ $ i + 1 ] - 1 ) $ count += $ arr [ $ i + 1 ] - $ arr [ $ i ] - 1 ; return $ count ; } $ arr = array ( 3 , 5 , 8 , 6 ) ; $ n = count ( $ arr ) ; echo countNum ( $ arr , $ n ) ; ? >


"Emirp numbers | Function to find reverse of any number ; Sieve method used for generating emirp number ( use of sieve ofEratosthenes ) ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Traverse all prime numbers ; Find reverse a number ; A number is emrip if it is not a palindrome number and its reverse is also prime . ; Mark reverse prime as false so that it 's  not printed again ; Driver Code"
Solution in php:


"Entringer Number | Return Entringer Number E ( n , k ) ; Base Case ; Base Case ; Recursive step ; Driven Code"
Solution in php:

< ? php function zigzag ( $ n , $ k ) { if ( $ n == 0 and $ k == 0 ) return 1 ; if ( $ k == 0 ) return 0 ; return zigzag ( $ n , $ k - 1 ) + zigzag ( $ n - 1 , $ n - $ k ) ; } $ n = 4 ; $ k = 3 ; echo zigzag ( $ n , $ k ) ; ? >


"Entringer Number | Return Entringer Number E ( n , k ) ; Base cases ; Finding dp [ i ] [ j ] ; Driven Code"
Solution in php:


"Equal Sum and XOR | function to count number of values less than equal to n that satisfy the given condition ; Traverse all numbers from 0 to n and increment result only when given condition is satisfied . ; Driver Code"
Solution in php:

< ? php function countValues ( $ n ) { $ countV = 0 ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) if ( ( $ n + $ i ) == ( $ n ^ $ i ) ) $ countV ++ ; return $ countV ; } $ n = 12 ; echo countValues ( $ n ) ; ? >


"Equal Sum and XOR | function to count number of values less than equal to n that satisfy the given condition ; unset_bits keeps track of count of un - set bits in binary representation of n ; Return 2 ^ unset_bits ; Driver code"
Solution in php:

< ? php function countValues ( $ n ) { $ unset_bits = 0 ; while ( $ n ) { if ( ( $ n & 1 ) == 0 ) $ unset_bits ++ ; $ n = $ n >> 1 ; } return 1 << $ unset_bits ; } $ n = 12 ; echo countValues ( $ n ) ; ? >


"Equally divide into two sets such that one set has maximum distinct elements | PHP program to equally divide n elements into two sets such that second set has maximum distinct elements . ; Driver code"
Solution in php:

< ? php function distribution ( $ arr , $ n ) { sort ( $ arr ) ; $ count = 1 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] > $ arr [ $ i - 1 ] ) $ count ++ ; return min ( $ count , $ n / 2 ) ; } $ arr = array ( 1 , 1 , 2 , 1 , 3 , 4 ) ; $ n = count ( $ arr ) ; echo ( distribution ( $ arr , $ n ) ) ; ? >


"Equation of circle from center and radius | Function to find the equation of circle ; Printing result ; Driver code"
Solution in php:


"Equation of ellipse from its focus , directrix , and eccentricity | Function to find equation of ellipse . ; Driver Code"
Solution in php:


"Equation of straight line passing through a given point which bisects it into two equal line segments | Function to print the equation of the required line ; Driver code"
Solution in php:


"Equidigital Numbers | PHP Program to find Equidigital Numbers till n ; Array to store all prime less than and equal to MAX . ; Utility function for sieve of sundaram ; In general Sieve of Sundaram , produces primes smaller than ( 2 * x + 2 ) for a number given number x . Since we want primes smaller than MAX , we reduce MAX to half This array is used to separate numbers of the form i + j + 2 ij from others where 1 <= i <= j ; Main logic of Sundaram . Mark all numbers which do not generate prime number by doing 2 * i + 1 ; Since 2 is a prime number ; Print other primes . Remaining primes are of the form 2 * i + 1 such that marked [ i ] is false . ; Returns true if n is a Equidigital number , else false . ; Count digits in original number ; Count all digits in prime factors of n pDigit is going to hold this value . ; Count powers of p in n ; If primes [ i ] is a prime factor , ; Count the power of prime factors ; Add its digits to pDigit . ; Add digits of power of prime factors to pDigit . ; If n != 1 then one prime factor still to be summed up ; ; If digits in prime factors and digits in original number are same , then return true . Else return false . ; Finding all prime numbers before limit . These numbers are used to find prime factors ."
Solution in php:

 < ? php $ MAX = 10000 ; $ primes = array ( ) ; function sieveSundaram ( ) { global $ primes , $ MAX ; $ marked = array_fill ( 0 , ( $ MAX / 2 + 1 ) , false ) ; for ( $ i = 1 ; $ i <= ( ( int ) sqrt ( $ MAX ) - 1 ) / 2 ; $ i ++ ) for ( $ j = ( $ i * ( $ i + 1 ) ) << 1 ; $ j <= ( int ) ( $ MAX / 2 ) ; $ j = $ j + 2 * $ i + 1 ) $ marked [ $ j ] = true ; array_push ( $ primes , 2 ) ; for ( $ i = 1 ; $ i <= ( int ) ( $ MAX / 2 ) ; $ i ++ ) if ( $ marked [ $ i ] == false ) array_push ( $ primes , 2 * $ i + 1 ) ; } function isEquidigital ( $ n ) { global $ primes , $ MAX ; if ( $ n == 1 ) return true ; $ original_no = $ n ; $ sumDigits = 0 ; while ( $ original_no > 0 ) { $ sumDigits ++ ; $ original_no = ( int ) ( $ original_no / 10 ) ; } $ pDigit = 0 ; $ count_exp = 0 ; $ p = 0 ; for ( $ i = 0 ; $ primes [ $ i ] <= ( int ) ( $ n / 2 ) ; $ i ++ ) { while ( $ n % $ primes [ $ i ] == 0 ) { $ p = $ primes [ $ i ] ; $ n = ( int ) ( $ n / $ p ) ; $ count_exp ++ ; } while ( $ p > 0 ) { $ pDigit ++ ; $ p = ( int ) ( $ p / 10 ) ; } while ( $ count_exp > 1 ) { $ pDigit ++ ; $ count_exp = ( int ) ( $ count_exp / 10 ) ; } } if ( $ n != 1 ) { while ( $ n > 0 ) { $ pDigit ++ ; $ n = ( int ) ( $ n / 10 ) ; } } return ( $ pDigit == $ sumDigits ) ; } sieveSundaram ( ) ; echo " Printing ▁ first ▁ few ▁ Equidigital ▁ Numbers ▁ using ▁ isEquidigital ( )
 " ; for ( $ i = 1 ; $ i < 20 ; $ i ++ ) if ( isEquidigital ( $ i ) ) echo $ i . " " ; ? >


"Equilibrium index of an array | function to find the equilibrium index ; Check for indexes one by one until an equilibrium index is found ; get left sum ; get right sum ; if leftsum and rightsum are same , then we are done ; return - 1 if no equilibrium index is found ; Driver code"
Solution in php:

< ? php function equilibrium ( $ arr , $ n ) { $ i ; $ j ; $ leftsum ; $ rightsum ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { $ leftsum = 0 ; $ rightsum = 0 ; for ( $ j = 0 ; $ j < $ i ; $ j ++ ) $ leftsum += $ arr [ $ j ] ; for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) $ rightsum += $ arr [ $ j ] ; if ( $ leftsum == $ rightsum ) return $ i ; } return -1 ; } $ arr = array ( -7 , 1 , 5 , 2 , -4 , 3 , 0 ) ; $ arr_size = sizeof ( $ arr ) ; echo equilibrium ( $ arr , $ arr_size ) ; ? >


"Equilibrium index of an array | function to find the equilibrium index ; initialize sum of whole array ; initialize leftsum ; Find sum of the whole array ; sum is now right sum for index i ; If no equilibrium index found , then return 0 ; Driver code"
Solution in php:

< ? php function equilibrium ( $ arr , $ n ) { $ sum = 0 ; $ leftsum = 0 ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) $ sum += $ arr [ $ i ] ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { $ sum -= $ arr [ $ i ] ; if ( $ leftsum == $ sum ) return $ i ; $ leftsum += $ arr [ $ i ] ; } return -1 ; } $ arr = array ( -7 , 1 , 5 , 2 , -4 , 3 , 0 ) ; $ arr_size = sizeof ( $ arr ) ; echo " First ▁ equilibrium ▁ index ▁ is ▁ " , equilibrium ( $ arr , $ arr_size ) ; ? >


"Euler 's Four Square Identity | function to check euler four square identity ; loops checking the sum of squares ; sum of 2 squares ; sum of 3 squares ; sum of 4 squares ; product of 2 numbers represented as sum of four squares i , j , k , l ; product of 2 numbers a and b represented as sum of four squares i , j , k , l ; given numbers can be represented as sum of 4 squares By euler 's four square identity product also can be represented as sum of 4 squares"
Solution in php:

 < ? php function check_euler_four_square_identity ( $ a , $ b , $ ab ) { $ s = 0 ; for ( $ i = 0 ; $ i * $ i <= $ ab ; $ i ++ ) { $ s = $ i * $ i ; for ( $ j = $ i ; $ j * $ j <= $ ab ; $ j ++ ) { $ s = $ j * $ j + $ i * $ i ; for ( $ k = $ j ; $ k * $ k <= $ ab ; $ k ++ ) { $ s = $ k * $ k + $ j * $ j + $ i * $ i ; for ( $ l = $ k ; $ l * $ l <= $ ab ; $ l ++ ) { $ s = $ l * $ l + $ k * $ k + $ j * $ j + $ i * $ i ; if ( $ s == $ ab ) { echo ( " i ▁ = ▁ " . $ i . "
 " ) ; echo ( " j ▁ = ▁ " . $ j . "
 " ) ; echo ( " k ▁ = ▁ " . $ k . "
 " ) ; echo ( " l ▁ = ▁ " . $ l . "
 " ) ; echo " " . " Product ▁ of ▁ " . $ a . " ▁ and ▁ " . $ b ; echo " ▁ can ▁ be ▁ written " . " ▁ as ▁ sum ▁ of ▁ squares ▁ of ▁ i , ▁ " . " j , ▁ k , ▁ l
 " ; echo $ ab . " ▁ = ▁ " ; echo $ i . " * " ▁ . ▁ $ i . ▁ " + " echo $ j . " * " . $ j . " ▁ + ▁ " ; echo $ k . " * " ▁ . ▁ $ k ▁ . ▁ " + " echo $ l . " * " . $ l . "
 " ; echo "
 " ; } } } } } } $ ab = $ a * $ b ; check_euler_four_square_identity ( $ a , $ b , $ ab ) ; ? >


"Euler 's Totient Function | Function to return gcd of a and b ; A simple method to evaluate Euler Totient Function ; Driver Code"
Solution in php:

 < ? php function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; } function phi ( $ n ) { $ result = 1 ; for ( $ i = 2 ; $ i < $ n ; $ i ++ ) if ( gcd ( $ i , $ n ) == 1 ) $ result ++ ; return $ result ; } for ( $ n = 1 ; $ n <= 10 ; $ n ++ ) echo " phi ( " . $ n . " ) ▁ = " . phi ( $ n ) . "
 " ; I >


"Euler 's Totient Function | PHP program to calculate Euler ' s ▁ Totient ▁ Function ▁ using ▁ Euler ' s product formula ; Initialize result as n ; Consider all prime factors of n and for every prime factor p , multiply result with ( 1 - 1 / p ) ; Check if p is a prime factor . ; If yes , then update n and result ; If n has a prime factor greater than sqrt ( n ) ( There can be at - most one such prime factor ) ; Driver Code"
Solution in php:


"Euler 's Totient function for all numbers smaller than or equal to n | Computes and prints totient of all numbers smaller than or equal to n . ; Create and initialize an array to store phi or totient values ; indicates not evaluated yet and initializes for product formula . ; Compute other Phi values ; If phi [ p ] is not computed already , then number p is prime ; Phi of a prime number p is always equal to p - 1. ; Update phi values of all multiples of p ; Add contribution of p to its multiple i by multiplying with ( 1 - 1 / $p ) ; Print precomputed phi values ; Driver Code"
Solution in php:


"Euler Method for solving differential equation | Consider a differential equation dy / dx = ( x + y + xy ) ; Function for Euler formula ; Iterating till the point at which we need approximation ; Printing approximation ; Initial Values ; Value of x at which we need approximation"
Solution in php:

 < ? php function func ( $ x , $ y ) { return ( $ x + $ y + $ x * $ y ) ; } function euler ( $ x0 , $ y , $ h , $ x ) { $ temp = -0 ; while ( $ x0 < $ x ) { $ temp = $ y ; $ y = $ y + $ h * func ( $ x0 , $ y ) ; $ x0 = $ x0 + $ h ; } echo " Approximate ▁ solution ▁ at ▁ x ▁ = ▁ " , $ x , " ▁ is ▁ " , $ y , "
 " ; } $ x0 = 0 ; $ y0 = 1 ; $ h = 0.025 ; $ x = 0.1 ; euler ( $ x0 , $ y0 , $ h , $ x ) ; ? >


"Eulerian Number | Return euleriannumber A ( n , m ) ; Driven Code"
Solution in php:

< ? php function eulerian ( $ n , $ m ) { if ( $ m >= $ n $ n == 0 ) return 0 ; if ( $ m == 0 ) return 1 ; return ( $ n - $ m ) * eulerian ( $ n - 1 , $ m - 1 ) + ( $ m + 1 ) * eulerian ( $ n - 1 , $ m ) ; } $ n = 3 ; $ m = 1 ; echo eulerian ( $ n , $ m ) ; ? >


"Eulerian Number | Return euleriannumber A ( n , m ) ; For each row from 1 to n ; For each column from 0 to m ; If i is greater than j ; If j is 0 , then make that state as 1. ; basic recurrence relation . ; Driver Code"
Solution in php:


"Evaluate a boolean expression represented as string | Evaluates boolean expression and returns the result ; Traverse all operands by jumping a character after every iteration . ; If operator next to current operand is AND . ; If operator next to current operand is OR . ; If operator next to current operand is XOR ( Assuming a valid input ) ; Driver code"
Solution in php:


"Evaluate an array expression with numbers , + and | Function to find the sum of given array ; if string is empty ; stoi function to convert string into integer ; cast to convert string into integer ; Find operator ; If operator is equal to ' + ' , add value in sum variable else subtract ; Driver code"
Solution in php:

< ? php function calculateSum ( $ arr , $ n ) { if ( $ n == 0 ) return 0 ; $ s = $ arr [ 0 ] ; $ value = ( int ) $ s ; $ sum = $ value ; for ( $ i = 2 ; $ i < $ n ; $ i = $ i + 2 ) { $ s = $ arr [ $ i ] ; $ value = ( int ) $ s ; $ operation = $ arr [ $ i - 1 ] ; if ( $ operation == ' + ' ) $ sum += $ value ; else if ( $ operation == ' - ' ) $ sum -= $ value ; } return $ sum ; } $ arr = array ( "3" , " + " , "4" , " - " , "7" , " + " , "13" ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; echo calculateSum ( $ arr , $ n ) ; ? >


"Even Fibonacci Numbers Sum | Returns sum of even Fibonacci numbers which are less than or equal to given limit . ; Initialize first two even prime numbers and their sum ; calculating sum of even Fibonacci value ; get next even value of Fibonacci sequence ; If we go beyond limit , we break loop ; Move to next even number and update sum ; Driver code"
Solution in php:

< ? php function evenFibSum ( $ limit ) { if ( $ limit < 2 ) return 0 ; $ ef1 = 0 ; $ ef2 = 2 ; $ sum = $ ef1 + $ ef2 ; while ( $ ef2 <= $ limit ) { $ ef3 = 4 * $ ef2 + $ ef1 ; if ( $ ef3 > $ limit ) break ; $ ef1 = $ ef2 ; $ ef2 = $ ef3 ; $ sum += $ ef2 ; } return $ sum ; } $ limit = 400 ; echo ( evenFibSum ( $ limit ) ) ; ? >


"Even | PHP program to find max ( X , Y ) / min ( X , Y ) after P turns ; 1 st test case ; 2 nd test case"
Solution in php:

 < ? php function findValue ( $ X , $ Y , $ P ) { if ( $ P % 2 == 0 ) return ( int ) ( max ( $ X , $ Y ) / min ( $ X , $ Y ) ) ; else return ( int ) ( max ( 2 * $ X , $ Y ) / min ( 2 * $ X , $ Y ) ) ; } $ X = 1 ; $ Y = 2 ; $ P = 1 ; echo findValue ( $ X , $ Y , $ P ) , "
 " ; $ X = 3 ; $ Y = 7 ; $ P = 2 ; echo findValue ( $ X , $ Y , $ P ) , "
 " ; ? >


"Evil Number | returns number of 1 s from the binary number ; counting 1 s ; Check if number is evil or not ; converting n to binary form ; calculating remainder ; storing the remainders in binary form as a number ; Calling the count_one function to count and return number of 1 s in bin ; Driver Code"
Solution in php:

 < ? php function count_one ( $ n ) { $ c_one = 0 ; while ( $ n != 0 ) { $ rem = $ n % 10 ; if ( $ rem == 1 ) $ c_one = $ c_one + 1 ; $ n = $ n / 10 ; } return $ c_one ; } function checkEvil ( $ n ) { $ i = 0 ; $ bin = 0 ; $ n_one = 0 ; while ( $ n != 0 ) { $ r = $ n % 2 ; $ bin = $ bin + $ r * ( pow ( 10 , $ i ) ) ; $ n = $ n / 2 ; } $ n_one = count_one ( $ bin ) ; if ( $ n_one % 2 == 0 ) return 1 ; else return 0 ; } $ i ; $ check ; $ num ; $ num = 32 ; $ check = checkEvil ( $ num ) ; if ( $ check == 1 ) echo $ num , " ▁ is ▁ Evil ▁ Number
 " ; else echo $ num , " ▁ is ▁ Odious ▁ Number
 " ; ? >


"Expectation or expected value of an array | Function to calculate expectation ; variable prb is for probability of each element which is same for each element ; calculating expectation overall ; returning expectation as sum ; Driver Code ; Function for calculating expectation ; Display expectation of given array"
Solution in php:

 < ? php function calc_Expectation ( $ a , $ n ) { $ prb = ( 1 / $ n ) ; $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += $ a [ $ i ] * $ prb ; return $ sum ; } $ n = 6.0 ; $ a = array ( 1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 6.0 ) ; $ expect = calc_Expectation ( $ a , $ n ) ; echo " Expectation ▁ of ▁ array ▁ E ( X ) ▁ is ▁ : ▁ " . $ expect . "
 " ; ? >


"Exponential Search | Returns position of first occurrence of x in array ; If x is present at first location itself ; Find range for binary search by repeated doubling ; Call binary search for the found range . ; A recursive binary search function . It returns location of x in given array arr [ l . . r ] is present , otherwise - 1 ; If the element is present at the middle itself ; If element is smaller than mid , then it can only be present n left subarray ; Else the element can only be present in right subarray ; We reach here when element is not present in array ; Driver code"
Solution in php:

< ? php function exponentialSearch ( $ arr , $ n , $ x ) { if ( $ arr [ 0 ] == $ x ) return 0 ; $ i = 1 ; while ( $ i < $ n and $ arr [ $ i ] <= $ x ) $ i = $ i * 2 ; return binarySearch ( $ arr , $ i / 2 , min ( $ i , $ n - 1 ) , $ x ) ; } function binarySearch ( $ arr , $ l , $ r , $ x ) { if ( $ r >= $ l ) { $ mid = $ l + ( $ r - $ l ) / 2 ; if ( $ arr [ $ mid ] == $ x ) return $ mid ; if ( $ arr [ $ mid ] > $ x ) return binarySearch ( $ arr , $ l , $ mid - 1 , $ x ) ; return binarySearch ( $ arr , $ mid + 1 , $ r , $ x ) ; } return -1 ; } $ arr = array ( 2 , 3 , 4 , 10 , 40 ) ; $ n = count ( $ arr ) ; $ x = 10 ; $ result = exponentialSearch ( $ arr , $ n , $ x ) ; if ( $ result == -1 ) echo " Element ▁ is ▁ not ▁ present ▁ in ▁ array " ; else echo " Element ▁ is ▁ present ▁ at ▁ index ▁ " , $ result ; ? >


"Exponential Squaring ( Fast Modulo Multiplication ) | prime modulo value ; for cases where exponent is not an even value ; Driver Code"
Solution in php:

< ? php $ N = 1000000007 ; function exponentiation ( $ bas , $ exp ) { global $ N ; $ t = 1 ; while ( $ exp > 0 ) { if ( $ exp % 2 != 0 ) $ t = ( $ t * $ bas ) % $ N ; $ bas = ( $ bas * $ bas ) % $ N ; $ exp = ( int ) $ exp / 2 ; } return $ t % $ N ; } $ bas = 5 ; $ exp = 100000 ; $ modulo = exponentiation ( $ bas , $ exp ) ; echo ( $ modulo ) ; ? >


"Express a number as sum of consecutive numbers | Print consecutive numbers from last to first ; Driver Code"
Solution in php:

< ? php function printConsecutive ( $ last , $ first ) { echo $ first ++ ; for ( $ x = $ first ; $ x <= $ last ; $ x ++ ) echo " + " } function findConsecutive ( $ N ) { for ( $ last = 1 ; $ last < $ N ; $ last ++ ) { for ( $ first = 0 ; $ first < $ last ; $ first ++ ) { if ( 2 * $ N == ( $ last - $ first ) * ( $ last + $ first + 1 ) ) { echo $ N , " ▁ = ▁ " ; printConsecutive ( $ last , $ first + 1 ) ; return ; } } } echo " - 1" ; } $ n = 12 ; findConsecutive ( $ n ) ; ? >


"Express an odd number as sum of prime numbers | Function to check if a number is prime or not . ; Prints at most three prime numbers whose sum is n . ; CASE - I ; CASE - II ; CASE - III ; Driver code"
Solution in php:

< ? php function isPrime ( $ x ) { if ( $ x == 0 $ x == 1 ) return false ; for ( $ i = 2 ; $ i * $ i <= $ x ; ++ $ i ) if ( $ x % $ i == 0 ) return false ; return true ; } function findPrimes ( $ n ) { if ( isPrime ( $ n ) ) echo ( $ n ) ; else if ( isPrime ( $ n - 2 ) ) echo ( 2 . " ▁ " . ( $ n - 2 ) ) ; else { echo ( 3 . " ▁ " ) ; $ n = $ n - 3 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( isPrime ( $ i ) && isPrime ( $ n - $ i ) ) { echo ( $ i . " " . ( $ n - $ i ) ) ; break ; } } } } $ n = 27 ; findPrimes ( $ n ) ; ? >


"Expressing a fraction as a natural number under modulo ' m ' | Function to return the GCD of given numbers ; Recursive function to return ( x ^ n ) % m ; Function to return the fraction modulo mod ; ( b ^ m - 2 ) % m ; Final answer ; Driver code"
Solution in php:

< ? php function abc ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return abc ( $ b % $ a , $ a ) ; } function modexp ( $ x , $ n ) { $ m = 1000000007 ; if ( $ n == 0 ) { return 1 ; } else if ( $ n % 2 == 0 ) { return modexp ( ( $ x * $ x ) % $ m , $ n / 2 ) ; } else { return ( $ x * modexp ( ( $ x * $ x ) % $ m , ( $ n - 1 ) / 2 ) % $ m ) ; } } function getFractionModulo ( $ a , $ b ) { $ m = 1000000007 ; $ c = abc ( $ a , $ b ) ; $ a = $ a / $ c ; $ b = $ b / $ c ; $ d = modexp ( $ b , $ m - 2 ) ; $ ans = ( ( $ a % $ m ) * ( $ d % $ m ) ) % $ m ; return $ ans ; } $ a = 2 ; $ b = 6 ; echo ( getFractionModulo ( $ a , $ b ) ) ; ? >


"Expressing a number as sum of consecutive | Set 2 ( Using odd factors ) | returns the number of odd factors ; If i is an odd factor and n is a perfect square ; If n is not perfect square ; N as sum of consecutive numbers"
Solution in php:

< ? php function countOddFactors ( $ n ) { $ odd_factors = 0 ; for ( $ i = 1 ; 1 * $ i * $ i <= $ n ; $ i ++ ) { if ( $ n % $ i == 0 ) { if ( 1 * $ i * $ i == $ n ) { if ( $ i & 1 ) $ odd_factors ++ ; } else { if ( $ i & 1 ) $ odd_factors ++ ; $ factor = $ n / $ i ; if ( $ factor & 1 ) $ odd_factors ++ ; } } } return $ odd_factors - 1 ; } $ N = 15 ; echo ( countOddFactors ( $ N ) . ( " " ) ) ; $ N = 10 ; echo ( countOddFactors ( $ N ) ) ; ? >


"Extract maximum numeric value from a given string | Set 1 ( General approach ) | Utility function to find maximum string ; If both having equal lengths ; Reach first unmatched character / value ; Return string with maximum value ; If different lengths return string with maximum length ; Function to extract the maximum value ; Start traversing the string ; Ignore leading zeroes ; Store numeric value into a string ; Update maximum string ; To handle the case if there is only 0 numeric value ; Return maximum string ; Drivers program"
Solution in php:

< ? php function maximumNum ( $ curr_num , $ res ) { $ len1 = strlen ( $ curr_num ) ; $ len2 = strlen ( $ res ) ; if ( $ len1 == $ len2 ) { $ i = 0 ; while ( $ curr_num [ $ i ] == $ res [ $ i ] ) $ i ++ ; if ( $ curr_num [ $ i ] < $ res [ $ i ] ) return $ res ; else return $ curr_num ; } return $ len1 < $ len2 ? $ res : $ curr_num ; } function extractMaximum ( $ str ) { $ n = strlen ( $ str ) ; $ curr_num = " " ; $ res = " " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { while ( $ i < $ n && $ str [ $ i ] == '0' ) $ i ++ ; while ( $ i < $ n && $ str [ $ i ] >= '0' && $ str [ $ i ] <= '9' ) { $ curr_num . = $ str [ $ i ] ; $ i ++ ; } if ( $ i == $ n ) break ; if ( strlen ( $ curr_num ) > 0 ) $ i -- ; $ res = maximumNum ( $ curr_num , $ res ) ; $ curr_num = " " ; } if ( strlen ( $ curr_num ) == 0 && strlen ( $ res ) == 0 ) $ res . = '0' ; return maximumNum ( $ curr_num , $ res ) ; } $ str = "100klh564abc365bg " ; echo extractMaximum ( $ str ) ; ? >


"Farey Sequence | Optimized function to print Farey sequence of order n ; We know first two terms are 0 / 1 and 1 / n ; For next terms to be evaluated ; Using recurrence relation to find the next term ; Print next term ; Update x1 , y1 , x2 and y2 for next iteration ; Driver Code"
Solution in php:


"Fascinating Number | function to check if number is fascinating or not ; frequency count array using 1 indexing ; obtaining the resultant number using string concatenation ; Traversing the string character by character ; gives integer value of a character digit ; To check if any digit has appeared multiple times ; Traversing through freq array to check if any digit was missing ; Input number ; Not a valid number ; Calling the function to check if input number is fascinating or not"
Solution in php:

< ? php function isFascinating ( $ num ) { $ freq = array_fill ( 0 , 10 , NULL ) ; $ val = " " . $ num . ( $ num * 2 ) . ( $ num * 3 ) ; for ( $ i = 0 ; $ i < strlen ( $ val ) ; $ i ++ ) { $ digit = $ val [ $ i ] - '0' ; if ( $ freq [ $ digit ] > 0 && $ digit != 0 ) return false ; else $ freq [ $ digit ] ++ ; } for ( $ i = 1 ; $ i < 10 ; $ i ++ ) { if ( $ freq [ $ i ] == 0 ) return false ; } return true ; } $ num = 192 ; if ( $ num < 100 ) echo " No " ; else { $ ans = isFascinating ( $ num ) ; if ( $ ans ) echo " Yes " ; else echo " No " ; } ? >


"Fermat 's Last Theorem | PHP program to verify fermat 's last theorem for a given range and n. ; Check if there exists a triplet such that a ^ n + b ^ n = c ^ n ; Driver Code"
Solution in php:

< ? php function testSomeNumbers ( $ limit , $ n ) { if ( $ n < 3 ) for ( $ a = 1 ; $ a <= $ limit ; $ a ++ ) for ( $ b = $ a ; $ b <= $ limit ; $ b ++ ) { $ pow_sum = pow ( $ a , $ n ) + pow ( $ b , $ n ) ; $ c = pow ( $ pow_sum , 1.0 / $ n ) ; $ c_pow = pow ( $ c , $ n ) ; if ( $ c_pow != $ pow_sum ) { echo " Count ▁ example ▁ found " ; return ; } } echo " No ▁ counter ▁ example ▁ within ▁ " . " given ▁ range ▁ and ▁ data " ; } testSomeNumbers ( 10 , 3 ) ; ? >


"Fibbinary Numbers ( No consecutive 1 s in binary ) | function to check if binary representation of an integer has consecutive 1 s ; stores the previous last bit initially as 0 ; if current last bit and previous last bit is 1 ; stores the last bit ; right shift the number ; Driver code"
Solution in php:

< ? php function checkFibinnary ( $ n ) { $ prev_last = 0 ; while ( $ n ) { if ( ( $ n & 1 ) && $ prev_last ) return false ; $ prev_last = $ n & 1 ; $ n >>= 1 ; } return true ; } $ n = 10 ; if ( checkFibinnary ( $ n ) ) echo " YES " ; else echo " NO " ; ? >


"Fibbinary Numbers ( No consecutive 1 s in binary ) | function to check whether a number is fibbinary or not ; if the number does not contain adjacent ones then ( n & ( n >> 1 ) ) operation results to 0 ; not a fibbinary number ; Driver code"
Solution in php:

< ? php function isFibbinaryNum ( $ n ) { if ( ( $ n & ( $ n >> 1 ) ) == 0 ) return true ; return false ; } $ n = 10 ; if ( isFibbinaryNum ( $ n ) ) echo " Yes " ; else echo " No " ; ? >


"Fibonacci modulo p | Returns position of first Fibonacci number whose modulo p is 0. ; add previous two remainders and then take its modulo p . ; Driver code"
Solution in php:

 < ? php function findMinZero ( $ p ) { $ first = 1 ; $ second = 1 ; $ number = 2 ; $ next = 1 ; while ( $ next ) { $ next = ( $ first + $ second ) % $ p ; $ first = $ second ; $ second = $ next ; $ number ++ ; } return $ number ; } $ p = 7 ; echo " Minimal ▁ zero ▁ is : ▁ " , findMinZero ( $ p ) , "
 " ; ? >


"Fibonacci problem ( Value of Fib ( N ) * Fib ( N ) | PHP implementation of the approach ; Driver code"
Solution in php:

< ? php function getResult ( $ n ) { if ( $ n & 1 ) return 1 ; return -1 ; } $ n = 3 ; echo getResult ( $ n ) ; ? >


"Fibonomial coefficient and Fibonomial triangle | PHP Program to print Fibonomial Triangle of height n . ; Function to produce Fibonacci Series . ; 0 th and 1 st number of the series are 0 and 1 ; Add the previous 2 numbers in the series and store it ; Function to produce fibonomial coefficient ; Function to print Fibonomial Triangle . ; Finding the fibonacci series . ; to store triangle value . ; initialising the 0 th element of each row and diagonal element equal to 0. ; for each row . ; for each column . ; finding each element using recurrence relation . ; printing the Fibonomial Triangle . ; Driven Program"
Solution in php:


"Fill array with 1 's using minimum iterations of filling neighbors | Returns count of iterations to fill arr [ ] with 1 s . ; Start traversing the array ; Traverse until a 0 is found ; Count contiguous 0 s ; Condition for Case 3 ; Condition to check if Case 1 satisfies : ; If count_zero is even ; If count_zero is odd ; Reset count_zero ; Case 2 ; Update res ; Driver code"
Solution in php:

< ? php function countIterations ( $ arr , $ n ) { $ oneFound = false ; $ res = 0 ; for ( $ i = 0 ; $ i < $ n ; ) { if ( $ arr [ $ i ] == 1 ) $ oneFound = true ; while ( $ i < $ n && $ arr [ $ i ] == 1 ) $ i ++ ; $ count_zero = 0 ; while ( $ i < $ n && $ arr [ $ i ] == 0 ) { $ count_zero ++ ; $ i ++ ; } if ( $ oneFound == false && $ i == $ n ) return -1 ; $ curr_count ; if ( $ i < $ n && $ oneFound == true ) { if ( $ count_zero & 1 == 0 ) $ curr_count = $ count_zero / 2 ; else $ curr_count = ( $ count_zero + 1 ) / 2 ; $ count_zero = 0 ; } else { $ curr_count = $ count_zero ; $ count_zero = 0 ; } $ res = max ( $ res , $ curr_count ) ; } return $ res ; } $ arr = array ( 0 , 1 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; echo countIterations ( $ arr , $ n ) ; ? >


"Final cell position in the matrix | function to find the final cell position in the given matrix ; to count up , down , left and cright movements ; to store the final coordinate position ; traverse the command array ; calculate final values ; Driver Code"
Solution in php:

< ? php function finalPos ( $ command , $ n , $ x , $ y ) { $ cup ; $ cdown ; $ cleft ; $ cright ; $ final_x ; $ final_y ; $ cup = $ cdown = $ cleft = $ cright = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ command [ $ i ] == ' U ' ) $ cup ++ ; else if ( $ command [ $ i ] == ' D ' ) $ cdown ++ ; else if ( $ command [ $ i ] == ' L ' ) $ cleft ++ ; else if ( $ command [ $ i ] == ' R ' ) $ cright ++ ; } $ final_x = $ x + ( $ cright - $ cleft ) ; $ final_y = $ y + ( $ cdown - $ cup ) ; echo " Final ▁ Position : ▁ " . " ( " . $ final_x . " , ▁ " . $ final_y . " ) " ; } $ command = " DDLRULL " ; $ n = strlen ( $ command ) ; $ x = 3 ; $ y = 4 ; finalPos ( $ command , $ n , $ x , $ y ) ;


"Final string after performing given operations | Function to return the modified string ; Count number of ' x ' ; Count number of ' y ' ; min ( x , y ) number of ' x ' and ' y ' will be deleted ; Driver Code"
Solution in php:

< ? php function printFinalString ( $ s ) { $ n = strlen ( $ s ) ; $ x = 0 ; $ y = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ s [ $ i ] == ' x ' ) $ x ++ ; else $ y ++ ; } $ finalString = ( string ) null ; if ( $ x > $ y ) for ( $ i = 0 ; $ i < $ x - $ y ; $ i ++ ) $ finalString . = " x " ; else for ( $ i = 0 ; $ i < $ y - $ x ; $ i ++ ) $ finalString . = " y " ; return $ finalString ; } $ s = " xxyyxyy " ; echo printFinalString ( $ s ) ; ? >


"Find ' N ' number of solutions with the given inequality equations | Function to calculate all the solutions ; there is no solutions ; print first element as y - n + 1 ; print rest n - 1 elements as 1 ; initialize the number of elements and the value of x an y"
Solution in php:

 < ? php function findsolution ( $ n , $ x , $ y ) { if ( ( $ y - $ n + 1 ) * ( $ y - $ n + 1 ) + $ n - 1 < $ x $ y < $ n ) { echo " No ▁ solution " ; return ; } echo $ y - $ n + 1 ; while ( $ n -- > 1 ) echo "
 " . 1 ; } $ n = 5 ; $ x = 15 ; $ y = 15 ; findsolution ( $ n , $ x , $ y ) ;


"Find ( 1 ^ n + 2 ^ n + 3 ^ n + 4 ^ n ) mod 5 | Set 2 | Function to return A mod B ; length of the string ; to store required answer ; Function to return ( 1 ^ n + 2 ^ n + 3 ^ n + 4 ^ n ) % 5 ; Calculate and return ans ; Driver code"
Solution in php:

< ? php function A_mod_B ( $ N , $ a ) { $ len = strlen ( $ N ) ; $ ans = 0 ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) $ ans = ( $ ans * 10 + ( int ) $ N [ $ i ] - '0' ) % $ a ; return $ ans % $ a ; } function findMod ( $ N ) { $ mod = A_mod_B ( $ N , 4 ) ; $ ans = ( 1 + pow ( 2 , $ mod ) + pow ( 3 , $ mod ) + pow ( 4 , $ mod ) ) ; return ( $ ans % 5 ) ; } $ N = "4" ; echo findMod ( $ N ) ; ? >


"Find 2 ^ ( 2 ^ A ) % B | Function to return 2 ^ ( 2 ^ A ) % B ; $Base case , 2 ^ ( 2 ^ 1 ) % B = 4 % B ; Driver code ; Print 2 ^ ( 2 ^ $A ) % $B"
Solution in php:

< ? php function F ( $ A , $ B ) { if ( $ A == 1 ) return ( 4 % $ B ) ; else { $ temp = F ( $ A - 1 , $ B ) ; return ( $ temp * $ temp ) % $ B ; } } $ A = 25 ; $ B = 50 ; echo F ( $ A , $ B ) ;


"Find A and B from list of divisors | Function to print A and B all of whose divisors are present in the given array ; Sort the array ; A is the largest element from the array ; Iterate from the second largest element ; If current element is not a divisor of A then it must be B ; If current element occurs more than once ; Print A and B ; Driver code"
Solution in php:


"Find First element in AP which is multiple of given prime | Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now $y = $y >> 1 ; y = y / 2 ; function to find nearest element in common ; base conditions ; Driver code ; module both A and D ; function call"
Solution in php:

< ? php function power ( $ x , $ y , $ p ) { $ res = 1 ; $ x = $ x % $ p ; while ( $ y > 0 ) { if ( $ y & 1 ) $ res = ( $ res * $ x ) % $ p ; $ x = ( $ x * $ x ) % $ p ; } return $ res ; } function NearestElement ( $ A , $ D , $ P ) { if ( $ A == 0 ) return 0 ; else if ( $ D == 0 ) return -1 ; else { $ X = power ( $ D , $ P - 2 , $ P ) ; return ( $ X * ( $ P - $ A ) ) % $ P ; } } $ A = 4 ; $ D = 9 ; $ P = 11 ; $ A %= $ P ; $ D %= $ P ; echo NearestElement ( $ A , $ D , $ P ) ; ? >


"Find GCD of factorial of elements of given array | Implementation of factorial function ; Function to find GCD of factorial of elements from array ; find the minimum element of array ; return the factorial of minimum element ; Driver Code"
Solution in php:

< ? php function factorial ( $ n ) { return ( $ n == 1 $ n == 0 ) ? 1 : factorial ( $ n - 1 ) * $ n ; } function gcdOfFactorial ( $ arr , $ n ) { $ minm = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ minm = $ minm > $ arr [ $ i ] ? $ arr [ $ i ] : $ minm ; return factorial ( $ minm ) ; } $ arr = array ( 9 , 12 , 122 , 34 , 15 ) ; $ n = count ( $ arr ) ; echo gcdOfFactorial ( $ arr , $ n ) ; ? >