"Constant time range add operation on an array | Utility method to add value val , to range [ lo , hi ] ; Utility method to get actual array from operation array ; convert array into prefix sum array ; method to print final updated array ; Driver Code ; Range add Queries"
How can the above be solved in PHP?

 < ? php function add ( & $ arr , $ N , $ lo , $ hi , $ val ) { $ arr [ $ lo ] += $ val ; if ( $ hi != $ N - 1 ) $ arr [ $ hi + 1 ] -= $ val ; } function updateArray ( & $ arr , $ N ) { for ( $ i = 1 ; $ i < $ N ; $ i ++ ) $ arr [ $ i ] += $ arr [ $ i - 1 ] ; } function printArr ( & $ arr , $ N ) { updateArray ( $ arr , $ N ) ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; echo "
 " ; } $ N = 6 ; $ arr = array_fill ( 0 , $ N , NULL ) ; add ( $ arr , $ N , 0 , 2 , 100 ) ; add ( $ arr , $ N , 1 , 5 , 100 ) ; add ( $ arr , $ N , 2 , 3 , 100 ) ; printArr ( $ arr , $ N ) ; ? >


"Construct a binary string following the given constraints | Function to print a binary string which has ' a ' number of 0 ' s , ▁ ' b ' ▁ number ▁ of ▁ 1' s and there are at least ' x ' indices such that s [ i ] != s [ i + 1 ] ; Divide index value by 2 and store it into d ; If index value x is even and x / 2 is not equal to a ; Loop for d for each d print 10 ; subtract d from a and b ; Loop for b to print remaining 1 's ; Loop for a to print remaining 0 's ; Driver code"
How can the above be solved in PHP?


"Construct an array from XOR of all elements of array except element at same index | function to construct new array ; calculate xor of array ; update array ; Driver code ; print result"
How can the above be solved in PHP?


"Construct an array from its pair | Fills element in arr [ ] from its pair sum array pair [ ] . n is size of arr [ ] ; Driver Code"
How can the above be solved in PHP?


"Construct lexicographically smallest palindrome | function for printing palindrome ; iterate till i < j ; continue if str [ i ] == str [ j ] ; update str [ i ] = str [ j ] = ' a ' if both are ' * ' ; update str [ i ] = str [ j ] if only str [ i ] = ' * ' ; update str [ j ] = str [ i ] if only str [ j ] = ' * ' ; else print not possible and return ; Driver Code"
How can the above be solved in PHP?

< ? php function constructPalin ( $ str , $ len ) { $ i = 0 ; $ j = $ len - 1 ; for ( ; $ i < $ j ; $ i ++ , $ j -- ) { if ( $ str [ $ i ] == $ str [ $ j ] && $ str [ $ i ] != ' * ' ) continue ; else if ( $ str [ $ i ] == $ str [ $ j ] && $ str [ $ i ] == ' * ' ) { $ str [ $ i ] = ' a ' ; $ str [ $ j ] = ' a ' ; continue ; } else if ( $ str [ $ i ] == ' * ' ) { $ str [ $ i ] = $ str [ $ j ] ; continue ; } else if ( $ str [ $ j ] == ' * ' ) { $ str [ $ j ] = $ str [ $ i ] ; continue ; } echo " Not ▁ Possible " ; return " " ; } return $ str ; } $ str = " bca * xc * * b " ; $ len = strlen ( $ str ) ; echo constructPalin ( $ str , $ len ) ; ? >


"Construction of Longest Increasing Subsequence ( N log N ) | Binary search ; Add boundary case , when array n is zero Depend on smart pointers ; initialized with - 1 ; it will always point to empty location ; new smallest value ; arr [ i ] wants to extend largest subsequence ; arr [ i ] wants to be a potential condidate of future subsequence It will replace ceil value in tailIndices ; Driver code"
How can the above be solved in PHP?

 < ? php function GetCeilIndex ( $ arr , $ T , $ l , $ r , $ key ) { while ( $ r - $ l > 1 ) { $ m = ( int ) ( $ l + ( $ r - $ l ) / 2 ) ; if ( $ arr [ $ T [ $ m ] ] >= $ key ) $ r = $ m ; else $ l = $ m ; } return $ r ; } function LongestIncreasingSubsequence ( $ arr , $ n ) { $ tailIndices = array_fill ( 0 , $ n + 1 , 0 ) ; $ prevIndices = array_fill ( 0 , $ n + 1 , -1 ) ; $ len = 1 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] < $ arr [ $ tailIndices [ 0 ] ] ) { $ tailIndices [ 0 ] = $ i ; } else if ( $ arr [ $ i ] > $ arr [ $ tailIndices [ $ len - 1 ] ] ) { $ prevIndices [ $ i ] = $ tailIndices [ $ len - 1 ] ; $ tailIndices [ $ len ++ ] = $ i ; } else { $ pos = GetCeilIndex ( $ arr , $ tailIndices , -1 , $ len - 1 , $ arr [ $ i ] ) ; $ prevIndices [ $ i ] = $ tailIndices [ $ pos - 1 ] ; $ tailIndices [ $ pos ] = $ i ; } } echo " LIS ▁ of ▁ given ▁ input
 " ; for ( $ i = $ tailIndices [ $ len - 1 ] ; $ i >= 0 ; $ i = $ prevIndices [ $ i ] ) echo $ arr [ $ i ] . " ▁ " ; echo "
 " ; return $ len ; } $ arr = array ( 2 , 5 , 3 , 7 , 11 , 8 , 10 , 13 , 6 ) ; $ n = count ( $ arr ) ; print ( " LIS ▁ size ▁ " . LongestIncreasingSubsequence ( $ arr , $ n ) ) ; ? >


"Container with Most Water | PHP code for Max Water Container ; Calculating the max area ; Driver code"
How can the above be solved in PHP?

 < ? php function maxArea ( $ A , $ len ) { $ l = 0 ; $ r = $ len - 1 ; $ area = 0 ; while ( $ l < $ r ) { $ area = max ( $ area , min ( $ A [ $ l ] , $ A [ $ r ] ) * ( $ r - $ l ) ) ; if ( $ A [ $ l ] < $ A [ $ r ] ) $ l += 1 ; else $ r -= 1 ; } return $ area ; } $ a = array ( 1 , 5 , 4 , 3 ) ; $ b = array ( 3 , 1 , 2 , 4 , 5 ) ; $ len1 = sizeof ( $ a ) / sizeof ( $ a [ 0 ] ) ; echo maxArea ( $ a , $ len1 ) . "
 " ; $ len2 = sizeof ( $ b ) / sizeof ( $ b [ 0 ] ) ; echo maxArea ( $ b , $ len2 ) ; ? >


"Convert String into Binary Sequence | utility function ; convert each char to ASCII value ; Convert ASCII value to binary ; Driver code"
How can the above be solved in PHP?

< ? php function strToBinary ( $ s ) { $ n = strlen ( $ s ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ val = ord ( $ s [ $ i ] ) ; $ bin = " " ; while ( $ val > 0 ) { ( $ val % 2 ) ? $ bin = $ bin . '1' : $ bin = $ bin . '0' ; $ val = floor ( $ val / 2 ) ; } for ( $ x = strlen ( $ bin ) - 1 ; $ x >= 0 ; $ x -- ) echo $ bin [ $ x ] ; echo " ▁ " ; } } $ s = " geeks " ; strToBinary ( $ s ) ; ? >


"Convert a sentence into its equivalent mobile numeric keypad sequence | Function which computes the sequence ; length of input string ; Checking for space ; Calculating index for each character ; Output sequence ; storing the sequence in array"
How can the above be solved in PHP?


"Convert decimal fraction to binary number | Function to convert decimal to binary upto k - precision after decimal point ; Fetch the integral part of decimal number ; Fetch the fractional part decimal number ; Conversion of integral part to binary equivalent ; Append 0 in binary ; Reverse string to get original binary equivalent ; Append point before conversion of fractional part ; Conversion of fractional part to binary equivalent ; Find next bit in fraction ; Driver code"
How can the above be solved in PHP?

 < ? php function decimalToBinary ( $ num , $ k_prec ) { $ binary = " " ; $ Integral = ( int ) ( $ num ) ; $ fractional = $ num - $ Integral ; while ( $ Integral ) { $ rem = $ Integral % 2 ; $ binary . = chr ( $ rem + 48 ) ; $ Integral = ( int ) ( $ Integral / 2 ) ; } $ binary = strrev ( $ binary ) ; $ binary . = ' . ' ; while ( $ k_prec -- ) { $ fractional *= 2 ; $ fract_bit = ( int ) $ fractional ; if ( $ fract_bit == 1 ) { $ fractional -= $ fract_bit ; $ binary . = chr ( 1 + 48 ) ; } else $ binary . = chr ( 0 + 48 ) ; } return $ binary ; } $ n = 4.47 ; $ k = 3 ; echo decimalToBinary ( $ n , $ k ) . "
 " ; $ n = 6.986 ; $ k = 5 ; echo decimalToBinary ( $ n , $ k ) ; ? >


"Convert from any base to decimal and vice versa | To return value of a char . For example , 2 is returned for '2' . 10 is returned for ' A ' , 11 for ' B ' ; Function to convert a number from given base ' b ' to decimal ; Initialize power of base ; Initialize result ; Decimal equivalent is str [ len - 1 ] * 1 + str [ len - 2 ] * base + str [ len - 3 ] * ( base ^ 2 ) + ... ; A digit in input number must be less than number 's base ; Driver code"
How can the above be solved in PHP?

< ? php function val ( $ c ) { if ( $ c >= '0' && $ c <= '9' ) return ord ( $ c ) - ord ( '0' ) ; else return ord ( $ c ) - ord ( ' A ' ) + 10 ; } function toDeci ( $ str , $ base ) { $ len = strlen ( $ str ) ; $ power = 1 ; $ num = 0 ; for ( $ i = $ len - 1 ; $ i >= 0 ; $ i -- ) { if ( val ( $ str [ $ i ] ) >= $ base ) { print ( " Invalid ▁ Number " ) ; return -1 ; } $ num += val ( $ str [ $ i ] ) * $ power ; $ power = $ power * $ base ; } return $ num ; } $ str = "11A " ; $ base = 16 ; print ( " Decimal ▁ equivalent ▁ of ▁ $ str ▁ " . " in ▁ base ▁ $ base ▁ is ▁ " . toDeci ( $ str , $ base ) ) ; ? >


"Convert given array to Arithmetic Progression by adding an element | Function to return the number to be added ; If difference of the current consecutive elements is different from the common difference ; If number has already been chosen then it 's not possible  to add another number ; If the current different is twice the common difference then a number can be added midway from current and previous element ; Number has been chosen ; It 's not possible to maintain  the common difference ; Return last element + common difference if no element is chosen and the array is already in AP ; Else return the chosen number ; Driver code"
How can the above be solved in PHP?

< ? php function getNumToAdd ( $ arr , $ n ) { sort ( $ arr ) ; $ d = $ arr [ 1 ] - $ arr [ 0 ] ; $ numToAdd = -1 ; $ numAdded = false ; for ( $ i = 2 ; $ i < $ n ; $ i ++ ) { $ diff = $ arr [ $ i ] - $ arr [ $ i - 1 ] ; if ( $ diff != $ d ) { if ( $ numAdded ) return -1 ; if ( $ diff == 2 * $ d ) { $ numToAdd = $ arr [ $ i ] - $ d ; $ numAdded = true ; } else return -1 ; } } if ( $ numToAdd == -1 ) return ( $ arr [ $ n - 1 ] + $ d ) ; return $ numToAdd ; } $ arr = array ( 1 , 3 , 5 , 7 , 11 , 13 , 15 ) ; $ n = sizeof ( $ arr ) ; echo getNumToAdd ( $ arr , $ n ) ; ? >


"Convert the ASCII value sentence to its equivalent string | Function to print the character sequence for the given ASCII sentence ; Append the current digit ; If num is within the required range ; Convert num to char ; Reset num to 0 ; Driver code"
How can the above be solved in PHP?

< ? php function asciiToSentence ( $ string , $ length ) { $ num = 0 ; for ( $ i = 0 ; $ i < $ length ; $ i ++ ) { $ num = $ num * 10 + ( ord ( $ string [ $ i ] ) - ord ( '0' ) ) ; if ( $ num >= 32 && $ num <= 122 ) { $ ch = chr ( $ num ) ; print ( $ ch ) ; $ num = 0 ; } } } $ string = "7110110110711510211111471101101107115" ; $ length = strlen ( $ string ) ; asciiToSentence ( $ string , $ length ) ; ? >


"Convert the string into palindrome string by changing only one character | Function to check if it is possible to convert the string into palindrome ; Counting number of characters that should be changed . ; If count of changes is less than or equal to 1 ; Driver Code"
How can the above be solved in PHP?

< ? php function checkPalindrome ( $ str ) { $ n = strlen ( $ str ) ; $ count = 0 ; for ( $ i = 0 ; $ i < $ n / 2 ; ++ $ i ) if ( $ str [ $ i ] != $ str [ $ n - $ i - 1 ] ) ++ $ count ; return ( $ count <= 1 ) ; } { $ str = " abccaa " ; if ( checkPalindrome ( $ str ) ) echo " Yes " ; else echo " No " ; return 0 ; } ? >


"Convert to Strictly increasing integer array with minimum changes | To find min elements to remove from array to make it strictly increasing ; Mark all elements of LIS as 1 ; Find LIS of array ; Return min changes for array to strictly increasing ; Driver Code"
How can the above be solved in PHP?

< ? php function minRemove ( $ arr , $ n ) { $ LIS = array ( ) ; $ len = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ LIS [ $ i ] = 1 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ i ; $ j ++ ) { if ( $ arr [ $ i ] > $ arr [ $ j ] ) $ LIS [ $ i ] = max ( $ LIS [ $ i ] , $ LIS [ $ j ] + 1 ) ; } $ len = max ( $ len , $ LIS [ $ i ] ) ; } return $ n - $ len ; } $ arr = array ( 1 , 2 , 6 , 5 , 4 ) ; $ n = count ( $ arr ) ; echo minRemove ( $ arr , $ n ) ; ? >


"Converting Decimal Number lying between 1 to 3999 to Roman Numerals | Function to calculate roman equivalent ; storing roman values of digits from 0 - 9 when placed at different places ; Converting to roman ; Driver Code"
How can the above be solved in PHP?

< ? php function intToRoman ( $ num ) { $ m = array ( " " , " M " , " MM " , " MMM " ) ; $ c = array ( " " , " C " , " CC " , " CCC " , " CD " , " D " , " DC " , " DCC " , " DCCC " , " CM " ) ; $ x = array ( " " , " X " , " XX " , " XXX " , " XL " , " L " , " LX " , " LXX " , " LXXX " , " XC " ) ; $ i = array ( " " , " I " , " II " , " III " , " IV " , " V " , " VI " , " VII " , " VIII " , " IX " ) ; $ thousands = $ m [ $ num / 1000 ] ; $ hundereds = $ c [ ( $ num % 1000 ) / 100 ] ; $ tens = $ x [ ( $ num % 100 ) / 10 ] ; $ ones = $ i [ $ num % 10 ] ; $ ans = $ thousands . $ hundereds . $ tens . $ ones ; return $ ans ; } $ number = 3549 ; echo intToRoman ( $ number ) ;


"Converting a Real Number ( between 0 and 1 ) to Binary String | Function to convert Binary real number to String ; Check if the number is Between 0 to 1 or Not ; Setting a limit on length : 32 characters . ; compare the number to .5 ; Now it become 0.25 ; Input value"
How can the above be solved in PHP?

 < ? php function toBinary ( $ n ) { if ( $ n >= 1 $ n <= 0 ) return " ERROR " ; $ frac = 0.5 ; $ answer = " . " ; while ( $ n > 0 ) { if ( strlen ( $ answer ) >= 32 ) return " ERROR " ; if ( $ n >= $ frac ) { $ answer . = "1" ; $ n = $ n - $ frac ; } else { $ answer . = "0" ; } $ frac = ( $ frac / 2 ) ; } return $ answer ; } $ n = 0.625 ; $ result = toBinary ( $ n ) ; print ( " ( 0" . $ result . " ) ▁ in ▁ base ▁ 2
 " ) ; $ m = 0.72 ; $ result = toBinary ( $ m ) ; print ( " ( " . $ result . " ) " ) ; ? >


"Converting one string to other using append and delete last operations | Returns true if it is possible to convert str1 to str2 using k operations . ; Case A ( i ) ; finding common length of both string ; Case A ( ii ) - ; Case B ; Driver Code"
How can the above be solved in PHP?

 < ? php function isConvertible ( $ str1 , $ str2 , $ k ) { if ( ( strlen ( $ str1 ) + strlen ( $ str2 ) ) < $ k ) return true ; $ commonLength = 0 ; for ( $ i = 0 ; $ i < min ( strlen ( $ str1 ) , strlen ( $ str2 ) ) ; $ i ++ ) { if ( $ str1 == $ str2 ) $ commonLength += 1 ; else break ; } if ( ( $ k - strlen ( $ str1 ) - strlen ( $ str2 ) + 2 * $ commonLength ) % 2 == 0 ) return true ; return false ; } $ str1 = " geek " ; $ str2 = " geek " ; $ k = 7 ; if ( isConvertible ( $ str1 , $ str2 , $ k ) ) echo " Yes " . "
 " ; else echo " No " . "
 " ; $ str1 = " geeks " ; $ str2 = " geek " ; $ k = 5 ; if ( isConvertible ( $ str1 , $ str2 , $ k ) ) echo " Yes " . "
 " ; else echo " No " . "
 " ; ? >


"Coordinates of rectangle with given points lie inside | function to print coordinate of smallest rectangle ; find Xmax and Xmin ; find Ymax and Ymin ; print all four coordinates ; Driver Code"
How can the above be solved in PHP?


"Cost of painting n * m grid | Function to return the minimum cost ; Driver code"
How can the above be solved in PHP?

< ? php function getMinCost ( $ n , $ m ) { $ cost = ( $ n - 1 ) * $ m + ( $ m - 1 ) * $ n ; return $ cost ; } $ n = 4 ; $ m = 5 ; echo getMinCost ( $ n , $ m ) ; ? >


"Cost to make a string Panagram | Function to return the total cost required to make the string Pangram ; Mark all the alphabets that occurred in the string ; Calculate the total cost for the missing alphabets ; Driver Code"
How can the above be solved in PHP?

< ? php function pangramCost ( $ arr , $ str ) { $ cost = 0 ; $ occurred = array ( ) ; for ( $ i = 0 ; $ i < 26 ; $ i ++ ) $ occurred [ $ i ] = false ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) { $ idx = ord ( $ str [ $ i ] ) - 97 ; $ occurred [ $ idx ] = true ; } for ( $ i = 0 ; $ i < 26 ; $ i ++ ) { if ( $ occurred [ $ i ] == false ) $ cost += $ arr [ $ i ] ; } return $ cost ; } $ arr = array ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , 26 ) ; $ str = " abcdefghijklmopqrstuvwz " ; echo pangramCost ( $ arr , $ str ) ; ? >


"Count ' d ' digit positive integers with 0 as a digit | Returns count of ' d ' digit integers have 0 as a digit ; Driver Code"
How can the above be solved in PHP?

 < ? php function findCount ( $ d ) { return 9 * ( pow ( 10 , $ d - 1 ) - pow ( 9 , $ d - 1 ) ) ; } { $ d = 1 ; echo findCount ( $ d ) , "
 " ; $ d = 2 ; echo findCount ( $ d ) , "
 " ; $ d = 4 ; echo findCount ( $ d ) , "
 " ; return 0 ; } ? >


"Count 1 's in a sorted binary array | Returns counts of 1 's in arr[low..high]. The array is assumed to be sorted in non-increasing order  ; get the middle index ; check if the element at middle index is last 1 ; If element is not last 1 , recur for right side ; else recur for left side ; Driver code"
How can the above be solved in PHP?

< ? php function countOnes ( $ arr , $ low , $ high ) { if ( $ high >= $ low ) { $ mid = $ low + ( $ high - $ low ) / 2 ; if ( ( $ mid == $ high or $ arr [ $ mid + 1 ] == 0 ) and ( $ arr [ $ mid ] == 1 ) ) return $ mid + 1 ; if ( $ arr [ $ mid ] == 1 ) return countOnes ( $ arr , ( $ mid + 1 ) , $ high ) ; return countOnes ( $ arr , $ low , ( $ mid - 1 ) ) ; } return 0 ; } $ arr = array ( 1 , 1 , 1 , 1 , 0 , 0 , 0 ) ; $ n = count ( $ arr ) ; echo " Count ▁ of ▁ 1 ' s ▁ in ▁ given ▁ array ▁ is ▁ " , countOnes ( $ arr , 0 , $ n - 1 ) ; ? >


"Count All Palindrome Sub | Returns total number of palindrome substring of length greater then equal to 2 ; create empty 2 - D matrix that counts all palindrome substring . dp [ i ] [ j ] stores counts of palindromic substrings in st [ i . . j ] ; P [ i ] [ j ] = true if substring str [ i . . j ] is palindrome , else false ; palindrome of single length ; palindrome of length 2 ; Palindromes of length more then 2. This loop is similar to Matrix Chain Multiplication . We start with a gap of length 2 and fill DP table in a way that gap between starting and ending indexes increases one by one by outer loop . ; Pick starting point for current gap ; Set ending point ; If current string is palindrome ; Add current palindrome substring ( + 1 ) and rest palindrome substring ( dp [ i ] [ j - 1 ] + dp [ i + 1 ] [ j ] ) remove common palindrome substrings ( - dp [ i + 1 ] [ j - 1 ] ) ; return total palindromic substrings ; Driver Code"
How can the above be solved in PHP?


"Count All Palindromic Subsequence in a given String | Function return the total palindromic subsequence ; create a 2D array to store the count of palindromic subsequence ; palindromic subsequence of length 1 ; check subsequence of length L is palindrome or not ; return total palindromic subsequence ; Driver Code"
How can the above be solved in PHP?


"Count All Palindromic Subsequence in a given String | PHP program to counts Palindromic Subsequence in a given String using recursion ; Function return the total palindromic subsequence ; Driver code"
How can the above be solved in PHP?

< ? php $ dp = array_fill ( 0 , 100 , array_fill ( 0 , 1000 , -1 ) ) ; $ str = " abcb " ; $ n = strlen ( $ str ) ; function countPS ( $ i , $ j ) { global $ str , $ dp , $ n ; if ( $ i > $ j ) return 0 ; if ( $ dp [ $ i ] [ $ j ] != -1 ) return $ dp [ $ i ] [ $ j ] ; if ( $ i == $ j ) return $ dp [ $ i ] [ $ j ] = 1 ; else if ( $ str [ $ i ] == $ str [ $ j ] ) return $ dp [ $ i ] [ $ j ] = countPS ( $ i + 1 , $ j ) + countPS ( $ i , $ j - 1 ) + 1 ; else return $ dp [ $ i ] [ $ j ] = countPS ( $ i + 1 , $ j ) + countPS ( $ i , $ j - 1 ) - countPS ( $ i + 1 , $ j - 1 ) ; } echo " Total ▁ palindromic ▁ subsequence ▁ are ▁ : ▁ " . countPS ( 0 , $ n - 1 ) ; ? >


"Count Balanced Binary Trees of Height h | PHP program to count number of balanced ; base cases ; Driver Code"
How can the above be solved in PHP?

 < ? php $ mod = 1000000007 ; function countBT ( $ h ) { global $ mod ; $ dp [ 0 ] = $ dp [ 1 ] = 1 ; for ( $ i = 2 ; $ i <= $ h ; $ i ++ ) { $ dp [ $ i ] = ( $ dp [ $ i - 1 ] * ( ( 2 * $ dp [ $ i - 2 ] ) % $ mod + $ dp [ $ i - 1 ] ) % $ mod ) % $ mod ; } return $ dp [ $ h ] ; } $ h = 3 ; echo " No . ▁ of ▁ balanced ▁ binary ▁ trees " , " ▁ of ▁ height ▁ h ▁ is : ▁ " , countBT ( $ h ) , "
 " ; ? >


"Count Derangements ( Permutation such that no element appears in its original position ) | Function to count derangements ; Base cases ; Fill der [ 0. . n ] in bottom up manner using above recursive formula ; Return result for n ; Driver code"
How can the above be solved in PHP?

< ? php function countDer ( $ n ) { $ der [ 1 ] = 0 ; $ der [ 2 ] = 1 ; for ( $ i = 3 ; $ i <= $ n ; ++ $ i ) $ der [ $ i ] = ( $ i - 1 ) * ( $ der [ $ i - 1 ] + $ der [ $ i - 2 ] ) ; return $ der [ $ n ] ; } $ n = 4 ; echo " Count ▁ of ▁ Derangements ▁ is ▁ " , countDer ( $ n ) ; ? >


"Count Derangements ( Permutation such that no element appears in its original position ) | Function to count derangements ; Base cases ; countDer ( n ) = ( n - 1 ) [ countDer ( n - 1 ) + der ( n - 2 ) ] ; Driver Code"
How can the above be solved in PHP?

< ? php function countDer ( $ n ) { if ( $ n == 1 ) return 0 ; if ( $ n == 2 ) return 1 ; return ( $ n - 1 ) * ( countDer ( $ n - 1 ) + countDer ( $ n - 2 ) ) ; } $ n = 4 ; echo " Count ▁ of ▁ Derangements ▁ is ▁ " , countDer ( $ n ) ; ? >


"Count Distinct Non | This function counts number of pairs ( x , y ) that satisfy the inequality x * x + y * y < n . ; Find the count of different y values for x = 0. ; One by one increase value of x , and find yCount for current x . If yCount becomes 0 , then we have reached maximum possible value of x . ; Add yCount ( count of different possible values of y for current x ) to result ; Increment x ; Update yCount for current x . Keep reducing yCount while the inequality is not satisfied . ; Driver program to test above function"
How can the above be solved in PHP?

 < ? php function countSolutions ( $ n ) { $ x = 0 ; $ yCount ; $ res = 0 ; for ( $ yCount = 0 ; $ yCount * $ yCount < $ n ; $ yCount ++ ) ; while ( $ yCount != 0 ) { $ res += $ yCount ; $ x ++ ; while ( $ yCount != 0 and ( $ x * $ x + ( $ yCount - 1 ) * ( $ yCount - 1 ) >= $ n ) ) $ yCount -- ; } return $ res ; } echo " Total ▁ Number ▁ of ▁ distinct ▁ Non - Negative " , " pairs ▁ is ▁ " , countSolutions ( 6 ) , "
 " ; ? >


"Count Distinct Non | function counts number of pairs ( x , y ) that satisfy the inequality x * x + y * y < n . ; Driver Code"
How can the above be solved in PHP?

< ? php function countSolutions ( $ n ) { $ res = 0 ; for ( $ x = 0 ; $ x * $ x < $ n ; $ x ++ ) for ( $ y = 0 ; $ x * $ x + $ y * $ y < $ n ; $ y ++ ) $ res ++ ; return $ res ; } { echo " Total ▁ Number ▁ of ▁ distinct ▁ Non - Negative ▁ pairs ▁ is ▁ " ; echo countSolutions ( 6 ) ; return 0 ; } ? >


"Count Divisors of Factorial | allPrimes [ ] stores all prime numbers less than or equal to n . ; Fills above vector allPrimes [ ] for a given n ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is not a prime , else true . ; Loop to update prime [ ] ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Store primes in the vector allPrimes ; Function to find all result of factorial number ; Initialize result ; find exponents of all primes which divides n and less than n ; Current divisor ; Find the highest power ( stored in exp ) of allPrimes [ i ] that divides n using Legendre 's formula. ; Multiply exponents of all primes less than n ; return total divisors ; Driver Code"
How can the above be solved in PHP?

< ? php $ allPrimes = array ( ) ; function sieve ( $ n ) { global $ allPrimes ; $ prime = array_fill ( 0 , $ n + 1 , true ) ; for ( $ p = 2 ; $ p * $ p <= $ n ; $ p ++ ) { if ( $ prime [ $ p ] == true ) { for ( $ i = $ p * 2 ; $ i <= $ n ; $ i += $ p ) $ prime [ $ i ] = false ; } } for ( $ p = 2 ; $ p <= $ n ; $ p ++ ) if ( $ prime [ $ p ] ) array_push ( $ allPrimes , $ p ) ; } function factorialDivisors ( $ n ) { global $ allPrimes ; $ result = 1 ; for ( $ i = 0 ; $ i < count ( $ allPrimes ) ; $ i ++ ) { $ p = $ allPrimes [ $ i ] ; $ exp = 0 ; while ( $ p <= $ n ) { $ exp = $ exp + ( int ) ( $ n / $ p ) ; $ p = $ p * $ allPrimes [ $ i ] ; } $ result = $ result * ( $ exp + 1 ) ; } return $ result ; } echo factorialDivisors ( 6 ) ; ? >


"Count Divisors of n in O ( n ^ 1 / 3 ) | function to count the divisors ; If divisors are equal , count only one ; Otherwise count both ; Driver Code"
How can the above be solved in PHP?

< ? php function countDivisors ( $ n ) { $ cnt = 0 ; for ( $ i = 1 ; $ i <= sqrt ( $ n ) ; $ i ++ ) { if ( $ n % $ i == 0 ) { if ( $ n / $ i == $ i ) $ cnt ++ ; else $ cnt = $ cnt + 2 ; } } return $ cnt ; } echo " Total ▁ distinct ▁ divisors ▁ of ▁ 100 ▁ are ▁ : ▁ " , countDivisors ( 100 ) ; ? >


"Count Inversions in an array | Set 1 ( Using Merge Sort ) | PHP program to Count Inversions in an array ; Driver Code"
How can the above be solved in PHP?

< ? php function getInvCount ( & $ arr , $ n ) { $ inv_count = 0 ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) if ( $ arr [ $ i ] > $ arr [ $ j ] ) $ inv_count ++ ; return $ inv_count ; } $ arr = array ( 1 , 20 , 6 , 4 , 5 ) ; $ n = sizeof ( $ arr ) ; echo " Number ▁ of ▁ inversions ▁ are ▁ " , getInvCount ( $ arr , $ n ) ; ? >


"Count Inversions of size three in a given array | Returns count of inversions of size 3 ; Initialize result ; Count all smaller elements on right of arr [ i ] ; Count all greater elements on left of arr [ i ] ; Update inversion count by adding all inversions that have arr [ i ] as middle of three elements ; Driver Code"
How can the above be solved in PHP?

< ? php function getInvCount ( $ arr , $ n ) { $ invcount = 0 ; for ( $ i = 1 ; $ i < $ n - 1 ; $ i ++ ) { $ small = 0 ; for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) if ( $ arr [ $ i ] > $ arr [ $ j ] ) $ small ++ ; $ great = 0 ; for ( $ j = $ i - 1 ; $ j >= 0 ; $ j -- ) if ( $ arr [ $ i ] < $ arr [ $ j ] ) $ great ++ ; $ invcount += $ great * $ small ; } return $ invcount ; } $ arr = array ( 8 , 4 , 2 , 1 ) ; $ n = sizeof ( $ arr ) ; echo " Inversion ▁ Count ▁ : ▁ " , getInvCount ( $ arr , $ n ) ; ? >


"Count Number of animals in a zoo from given number of head and legs | Function that calculates Rabbits ; Driver code"
How can the above be solved in PHP?

 < ? php function countRabbits ( $ Heads , $ Legs ) { $ count = 0 ; $ count = ( $ Legs ) - 2 * ( $ Heads ) ; $ count = ( int ) $ count / 2 ; return $ count ; } $ Heads = 100 ; $ Legs = 300 ; $ Rabbits = countRabbits ( $ Heads , $ Legs ) ; echo " Rabbits = " ▁ , ▁ $ Rabbits ▁ , ▁ " " ;
 echo ▁ " Pigeons = " $ Rabbits , "
 " ; ? >


"Count Numbers with N digits which consists of even number of 0 ’ s | Function to count Numbers with N digits which consists of odd number of 0 's ; Driver code"
How can the above be solved in PHP?

 < ? php function countNumbers ( $ N ) { return ( pow ( 10 , $ N ) - 1 ) - ( pow ( 10 , $ N ) - pow ( 8 , $ N ) ) / 2 ; } $ n = 2 ; echo countNumbers ( $ n ) , "
 " ; ? >


"Count Numbers with N digits which consists of odd number of 0 's | Function to count Numbers with N digits which consists of odd number of 0 's ; Driver code"
How can the above be solved in PHP?

< ? php function countNumbers ( $ N ) { return ( pow ( 10 , $ N ) - pow ( 8 , $ N ) ) / 2 ; } $ n = 5 ; echo countNumbers ( $ n ) ; ? >


"Count Odd and Even numbers in a range from L to R | Return the number of odd numbers in the range [ L , R ] ; if either R or L is odd ; Driver code"
How can the above be solved in PHP?

 < ? php function countOdd ( $ L , $ R ) { $ N = ( $ R - $ L ) / 2 ; if ( $ R % 2 != 0 $ L % 2 != 0 ) $ N ++ ; return $ N ; } $ L = 3 ; $ R = 7 ; $ odds = countOdd ( $ L , $ R ) ; $ evens = ( $ R - $ L + 1 ) - $ odds ; echo " Count ▁ of ▁ odd ▁ numbers ▁ is ▁ " . $ odds . "
 " ; echo " Count ▁ of ▁ even ▁ numbers ▁ is ▁ " . $ evens ; ? >


"Count Pairs from two arrays with even sum | Function to return count of required pairs ; Count of odd and even numbers from both the arrays ; Find the count of odd and even elements in a [ ] ; Find the count of odd and even elements in b [ ] ; Count the number of pairs ; Return the number of pairs ; Driver code ; This code is contributes by AnkitRai01"
How can the above be solved in PHP?


"Count Primes in Ranges | PHP program to answer queries for count of primes in given range . ; prefix [ i ] is going to store count of primes till i ( including i ) . ; Create a boolean array value in prime [ i ] will " prime [ 0 . . n ] " . A 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 ; Build prefix array $prefix [ 0 ] = $prefix [ 1 ] = 0 ; ; Returns count of primes in range from L to R ( both inclusive ) . ; Driver code"
How can the above be solved in PHP?

 < ? php $ MAX = 10000 ; $ prefix = array_fill ( 0 , ( $ MAX + 1 ) , 0 ) ; function buildPrefix ( ) { global $ MAX , $ prefix ; $ prime = array_fill ( 0 , ( $ MAX + 1 ) , true ) ; for ( $ p = 2 ; $ p * $ p <= $ MAX ; $ p ++ ) { if ( $ prime [ $ p ] == true ) { for ( $ i = $ p * 2 ; $ i <= $ MAX ; $ i += $ p ) $ prime [ $ i ] = false ; } } for ( $ p = 2 ; $ p <= $ MAX ; $ p ++ ) { $ prefix [ $ p ] = $ prefix [ $ p - 1 ] ; if ( $ prime [ $ p ] ) $ prefix [ $ p ] ++ ; } } function query ( $ L , $ R ) { global $ prefix ; return $ prefix [ $ R ] - $ prefix [ $ L - 1 ] ; } buildPrefix ( ) ; $ L = 5 ; $ R = 10 ; echo query ( $ L , $ R ) . "
 " ; $ L = 1 ; $ R = 10 ; echo query ( $ L , $ R ) . "
 " ; ? >


"Count Strictly Increasing Subarrays | PHP program to count number of strictly increasing subarrays ; Initialize count of subarrays as 0 ; Pick starting point ; Pick ending point ; If subarray arr [ i . . j ] is not strictly increasing , then subarrays after it , i . e . , arr [ i . . j + 1 ] , arr [ i . . j + 2 ] , ... . cannot be strictly increasing ; Driver program"
How can the above be solved in PHP?

< ? php function countIncreasing ( $ arr , $ n ) { $ cnt = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) { if ( $ arr [ $ j ] > $ arr [ $ j - 1 ] ) $ cnt ++ ; else break ; } } return $ cnt ; } $ arr = array ( 1 , 2 , 2 , 4 ) ; $ n = count ( $ arr ) ; echo " Count ▁ of ▁ strictly ▁ increasing ▁ " , " subarrays ▁ is ▁ " , countIncreasing ( $ arr , $ n ) ; ? >


"Count Strictly Increasing Subarrays | PHP program to count number of strictly increasing subarrays in O ( n ) time . ; Initialize result ; Initialize length of current increasing subarray ; Traverse through the array ; If arr [ i + 1 ] is greater than arr [ i ] , then increment length ; Else Update count and reset length ; If last length is more than 1 ; Driver Code"
How can the above be solved in PHP?

< ? php function countIncreasing ( $ arr , $ n ) { $ cnt = 0 ; $ len = 1 ; for ( $ i = 0 ; $ i < $ n - 1 ; ++ $ i ) { if ( $ arr [ $ i + 1 ] > $ arr [ $ i ] ) $ len ++ ; else { $ cnt += ( ( ( $ len - 1 ) * $ len ) / 2 ) ; $ len = 1 ; } } if ( $ len > 1 ) $ cnt += ( ( ( $ len - 1 ) * $ len ) / 2 ) ; return $ cnt ; } $ arr = array ( 1 , 2 , 2 , 4 ) ; $ n = count ( $ arr ) ; echo " Count ▁ of ▁ strictly ▁ increasing ▁ subarrays ▁ is ▁ " , countIncreasing ( $ arr , $ n ) ; ? >


"Count Triplets such that one of the numbers can be written as sum of the other two | Function to count the number of ways to choose the triples ; compute the max value in the array and create frequency array of size max_val + 1. We can also use HashMap to store frequencies . We have used an array to keep remaining code simple . ; Case 1 : 0 , 0 , 0 ; Case 2 : 0 , x , x ; Case 3 : x , x , 2 * x ; Case 4 : x , y , x + y iterate through all pairs ( x , y ) ; Driver code"
How can the above be solved in PHP?


"Count all Prime Length Palindromic Substrings | Function that returns true if sub - string starting at i and ending at j in str is a palindrome ; Function to count all palindromic substring whose lwngth is a prime number ; 0 and 1 are non - primes ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; To store the required number of sub - strings ; Starting from the smallest prime till the largest length of the sub - string possible ; If j is prime ; Check all the sub - strings of length j ; If current sub - string is a palindrome ; Driver Code"
How can the above be solved in PHP?

< ? php function isPalindrome ( $ str , $ i , $ j ) { while ( $ i < $ j ) { if ( $ str [ $ i ] != $ str [ $ j ] ) return false ; $ i ++ ; $ j -- ; } return true ; } function countPrimePalindrome ( $ str , $ len ) { $ prime = array_fill ( 0 , $ len + 1 , true ) ; $ prime [ 0 ] = false ; $ prime [ 1 ] = false ; for ( $ p = 2 ; $ p * $ p <= $ len ; $ p ++ ) { if ( $ prime [ $ p ] ) { for ( $ i = $ p * $ p ; $ i <= $ len ; $ i += $ p ) $ prime [ $ i ] = false ; } } $ count = 0 ; for ( $ j = 2 ; $ j <= $ len ; $ j ++ ) { if ( $ prime [ $ j ] ) { for ( $ i = 0 ; $ i + $ j - 1 < $ len ; $ i ++ ) { if ( isPalindrome ( $ str , $ i , $ i + $ j - 1 ) ) $ count ++ ; } } } return $ count ; } $ s = " geeksforgeeks " ; $ len = strlen ( $ s ) ; echo countPrimePalindrome ( $ s , $ len ) ; ? >


"Count all distinct pairs with difference equal to k | A simple PHP program to count pairs with difference k ; Pick all elements one by one ; See if there is a pair of this picked element ; Driver Code"
How can the above be solved in PHP?

< ? php function countPairsWithDiffK ( $ arr , $ n , $ k ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) if ( $ arr [ $ i ] - $ arr [ $ j ] == $ k or $ arr [ $ j ] - $ arr [ $ i ] == $ k ) $ count ++ ; } return $ count ; } $ arr = array ( 1 , 5 , 3 , 4 , 2 ) ; $ n = count ( $ arr ) ; $ k = 3 ; echo " Count ▁ of ▁ pairs ▁ with ▁ given ▁ diff ▁ is ▁ " , countPairsWithDiffK ( $ arr , $ n , $ k ) ; ? >


"Count all distinct pairs with difference equal to k | Returns count of pairs with difference k in arr [ ] of size n . ; Sort array elements ; arr [ r ] - arr [ l ] < k ; Driver Code"
How can the above be solved in PHP?

< ? php function countPairsWithDiffK ( $ arr , $ n , $ k ) { $ count = 0 ; sort ( $ arr ) ; $ l = 0 ; $ r = 0 ; while ( $ r < $ n ) { if ( $ arr [ $ r ] - $ arr [ $ l ] == $ k ) { $ count ++ ; $ l ++ ; $ r ++ ; } else if ( $ arr [ $ r ] - $ arr [ $ l ] > $ k ) $ l ++ ; else $ r ++ ; } return $ count ; } $ arr = array ( 1 , 5 , 3 , 4 , 2 ) ; $ n = count ( $ arr ) ; $ k = 3 ; echo " Count ▁ of ▁ pairs ▁ with ▁ given ▁ diff ▁ is ▁ " , countPairsWithDiffK ( $ arr , $ n , $ k ) ; ? >


"Count all distinct pairs with difference equal to k | Standard binary search function ; Returns count of pairs with difference k in arr [ ] of size n . ; Sort array elements ; Pick a first element point ; Driver Code"
How can the above be solved in PHP?

< ? php function binarySearch ( $ arr , $ low , $ high , $ x ) { if ( $ high >= $ low ) { $ mid = $ low + ( $ high - $ low ) / 2 ; if ( $ x == $ arr [ $ mid ] ) return $ mid ; if ( $ x > $ arr [ $ mid ] ) return binarySearch ( $ arr , ( $ mid + 1 ) , $ high , $ x ) ; else return binarySearch ( $ arr , $ low , ( $ mid - 1 ) , $ x ) ; } return -1 ; } function countPairsWithDiffK ( $ arr , $ n , $ k ) { $ count = 0 ; $ i ; sort ( $ arr ) ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) if ( binarySearch ( $ arr , $ i + 1 , $ n - 1 , $ arr [ $ i ] + $ k ) != -1 ) $ count ++ ; return $ count ; } $ arr = array ( 1 , 5 , 3 , 4 , 2 ) ; $ n = count ( $ arr ) ; $ k = 3 ; echo " Count ▁ of ▁ pairs ▁ with ▁ given ▁ diff ▁ is ▁ " , countPairsWithDiffK ( $ arr , $ n , $ k ) ; ? >


"Count all pairs of an array which differ in K bits | Utility function to count total ones in a number ; Function to count pairs of K different bits ; initialize final answer ; Check for K differ bit ; Driver code"
How can the above be solved in PHP?

 < ? php function bitCount ( $ n ) { $ count = 0 ; while ( $ n ) { if ( $ n & 1 ) ++ $ count ; $ n >>= 1 ; } return $ count ; } function countPairsWithKDiff ( $ arr , $ n , $ k ) { $ ans = 0 ; for ( $ i = 0 ; $ i < $ n - 1 ; ++ $ i ) { for ( $ j = $ i + 1 ; $ j < $ n ; ++ $ j ) { $ xoredNum = $ arr [ $ i ] ^ $ arr [ $ j ] ; if ( $ k == bitCount ( $ xoredNum ) ) ++ $ ans ; } } return $ ans ; } $ k = 2 ; $ arr = array ( 2 , 4 , 1 , 3 , 1 ) ; $ n = count ( $ arr ) ; echo " Total ▁ pairs ▁ for ▁ k ▁ = ▁ " , $ k , " ▁ are ▁ " , countPairsWithKDiff ( $ arr , $ n , $ k ) , "
 " ; ? >


"Count all palindrome which is square of a palindrome | check if a number is a palindrome ; Function to return required count of palindromes ; Range [ L , R ] ; Upper limit ; count odd length palindromes ; if s = '1234' ; then , t = '1234321' ; count even length palindromes ; if s = '1234' ; then , t = '12344321' ; Return count of super - palindromes ; Driver Code ; function call to get required answer"
How can the above be solved in PHP?

< ? php function ispalindrome ( $ x ) { $ ans = 0 ; $ temp = $ x ; while ( $ temp > 0 ) { $ ans = ( 10 * $ ans ) + ( $ temp % 10 ) ; $ temp = ( int ) ( $ temp / 10 ) ; } return $ ans == $ x ; } function SuperPalindromes ( $ L , $ R ) { $ L = ( int ) $ L ; $ R = ( int ) $ R ; $ LIMIT = 100000 ; $ ans = 0 ; for ( $ i = 0 ; $ i < $ LIMIT ; $ i ++ ) { $ s = ( string ) $ i ; $ rs = substr ( $ s , 0 , strlen ( $ s ) - 1 ) ; $ p = $ s . strrev ( $ rs ) ; $ p_sq = ( int ) $ p * * 2 ; if ( $ p_sq > $ R ) { break ; } if ( $ p_sq >= $ L and ispalindrome ( $ p_sq ) ) { $ ans = $ ans + 1 ; } } for ( $ i = 0 ; $ i < $ LIMIT ; $ i ++ ) { $ s = ( string ) $ i ; $ p = $ s . strrev ( $ s ) ; $ p_sq = ( int ) $ p * * 2 ; if ( $ p_sq > $ R ) { break ; } if ( $ p_sq >= $ L and ispalindrome ( $ p_sq ) ) { $ ans = $ ans + 1 ; } } return $ ans ; } $ L = "4" ; $ R = "1000" ; echo SuperPalindromes ( $ L , $ R ) ; ? >


"Count all perfect divisors of a number | Below is PHP code to count total perfect divisors ; Pre - compute counts of all perfect divisors of all numbers upto MAX . ; Iterate through all the multiples of i * i ; Increment all such multiples by 1 ; Returns count of perfect divisors of n . ; Driver code"
How can the above be solved in PHP?

 < ? php $ MAX = 10001 ; $ perfectDiv = array_fill ( 0 , $ MAX , 0 ) ; function precomputeCounts ( ) { global $ MAX , $ perfectDiv ; for ( $ i = 1 ; $ i * $ i < $ MAX ; ++ $ i ) { for ( $ j = $ i * $ i ; $ j < $ MAX ; $ j += $ i * $ i ) ++ $ perfectDiv [ $ j ] ; } } function countPerfectDivisors ( $ n ) { global $ perfectDiv ; return $ perfectDiv [ $ n ] ; } precomputeCounts ( ) ; $ n = 16 ; echo " Total ▁ perfect ▁ divisors ▁ of ▁ " . $ n . " ▁ = ▁ " . countPerfectDivisors ( $ n ) . "
 " ; $ n = 12 ; echo " Total ▁ perfect ▁ divisors ▁ of ▁ " . $ n . " ▁ = ▁ " . countPerfectDivisors ( $ n ) ; ? >


"Count all perfect divisors of a number | function to check perfect square number ; Returns count all perfect divisors of n ; Initialize result ; Consider every number that can be a divisor of n ; If i is a divisor ; Driver Code"
How can the above be solved in PHP?

 < ? php function isPerfectSquare ( $ n ) { $ sq = sqrt ( $ n ) ; return ( $ n == $ sq * $ sq ) ; } function countPerfectDivisors ( $ n ) { $ count = 0 ; for ( $ i = 1 ; $ i * $ i <= $ n ; ++ $ i ) { if ( $ n % $ i == 0 ) { if ( isPerfectSquare ( $ i ) ) ++ $ count ; if ( $ n / $ i != $ i && isPerfectSquare ( $ n / $ i ) ) ++ $ count ; } } return $ count ; } $ n = 16 ; echo " Total ▁ perfect ▁ divisors ▁ of ▁ " , $ n , " ▁ = ▁ " , countPerfectDivisors ( $ n ) , "
 " ; $ n = 12 ; echo " Total ▁ perfect ▁ divisors ▁ of ▁ " , $ n , " ▁ = ▁ " , countPerfectDivisors ( $ n ) ; ? >


"Count all possible N digit numbers that satisfy the given condition | Function to return the count of required numbers ; If N is odd then return 0 ; Driver Code"
How can the above be solved in PHP?

< ? php function getCount ( $ N ) { if ( $ N % 2 == 1 ) return 0 ; $ result = "9" ; for ( $ i = 1 ; $ i <= $ N / 2 - 1 ; $ i ++ ) $ result . = "0" ; return $ result ; } $ N = 4 ; echo getCount ( $ N ) ; ? >


"Count all possible paths from top left to bottom right of a mXn matrix | PHP program to count all possible paths from top left to top bottom using combinatorics ; We have to calculate m + n - 2 C n - 1 here which will be ( m + n - 2 ) ! / ( n - 1 ) ! ( m - 1 ) ! ; Driver code"
How can the above be solved in PHP?

< ? php function numberOfPaths ( $ m , $ n ) { $ path = 1 ; for ( $ i = $ n ; $ i < ( $ m + $ n - 1 ) ; $ i ++ ) { $ path *= $ i ; $ path /= ( $ i - $ n + 1 ) ; } return $ path ; } { echo ( numberOfPaths ( 3 , 3 ) ) ; }


"Count all possible paths from top left to bottom right of a mXn matrix | Returns count of possible paths to reach cell at row number m and column number n from the topmost leftmost cell ( cell at 1 , 1 ) ; Create a 1D array to store results of subproblems ; Driver Code"
How can the above be solved in PHP?

< ? php function numberOfPaths ( $ m , $ n ) { $ dp = array ( ) ; $ dp [ 0 ] = 1 ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) { for ( $ j = 1 ; $ j < $ n ; $ j ++ ) { $ dp [ $ j ] += $ dp [ $ j - 1 ] ; } } return $ dp [ $ n - 1 ] ; } echo numberOfPaths ( 3 , 3 ) ; ? >


"Count all possible paths from top left to bottom right of a mXn matrix | Returns count of possible paths to reach cell at row number m and column number n from the topmost leftmost cell ( cell at 1 , 1 ) ; Create a 2D table to store results of subproblems ; Count of paths to reach any cell in first column is 1 ; Count of paths to reach any cell in first column is 1 ; Calculate count of paths for other cells in bottom - up manner using the recursive solution ; By uncommenting the last part the code calculated the total possible paths if the diagonal Movements are allowed ; Driver Code"
How can the above be solved in PHP?


"Count all possible paths from top left to bottom right of a mXn matrix | Returns count of possible paths to reach cell at row number m and column number n from the topmost leftmost cell ( cell at 1 , 1 ) ; If either given row number is first or given column number is first ; If diagonal movements are allowed then the last addition is required . ; Driver Code"
How can the above be solved in PHP?

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


"Count all prefixes of the given binary array which are divisible by x | Function to return the count of total binary prefix which are divisible by x ; Initialize with zero ; Convert all prefixes to decimal ; If number is divisible by x then increase count ; Driver code"
How can the above be solved in PHP?

< ? php function CntDivbyX ( $ arr , $ n , $ x ) { $ number = 0 ; $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ number = $ number * 2 + $ arr [ $ i ] ; if ( ( $ number % $ x == 0 ) ) $ count += 1 ; } return $ count ; } $ arr = array ( 1 , 0 , 1 , 0 , 1 , 1 , 0 ) ; $ n = sizeof ( $ arr ) ; $ x = 2 ; echo CntDivbyX ( $ arr , $ n , $ x ) ;


"Count all prefixes of the given binary array which are divisible by x | Function to return the count of total binary prefix which are divisible by x ; Initialize with zero ; Instead of converting all prefixes to decimal , take reminder with x ; If number is divisible by x then reminder = 0 ; Driver code"
How can the above be solved in PHP?

< ? php function CntDivbyX ( $ arr , $ n , $ x ) { $ number = 0 ; $ count1 = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ number = ( $ number * 2 + $ arr [ $ i ] ) % $ x ; if ( $ number == 0 ) $ count1 += 1 ; } return $ count1 ; } $ arr = array ( 1 , 0 , 1 , 0 , 1 , 1 , 0 ) ; $ n = sizeof ( $ arr ) ; $ x = 2 ; echo CntDivbyX ( $ arr , $ n , $ x ) ; ? >


"Count all sorted rows in a matrix | PHP program to find number of sorted rows ; Function to count all sorted rows in a matrix ; Initialize result ; Start from left side of matrix to count increasing order rows ; Check if there is any pair ofs element that are not in increasing order . ; If the loop didn 't break  (All elements of current row were in increasing order) ; Start from right side of matrix to count increasing order rows ( reference to left these are in decreasing order ) ; Check if there is any pair ofs element that are not in decreasing order . ; Note c > 1 condition is required to make sure that a single column row is not counted twice ( Note that a single column row is sorted both in increasing and decreasing order ) ; Driver Code"
How can the above be solved in PHP?


"Count all the numbers in a range with smallest factor as K | Function to check if k is a prime number or not ; Corner case ; Check from 2 to n - 1 ; Function to check if a number is not divisible by any number between 2 and K - 1 ; to check if the num is divisible by any numbers between 2 and k - 1 ; if not divisible by any number between 2 and k - 1 but divisible by k ; Function to find count of numbers in range [ a , b ] with smallest factor as K ; a number can be divisible only by k and not by any number less than k only if k is a prime ; to check if a number has smallest factor as K ; Driver code"
How can the above be solved in PHP?

< ? php function isPrime ( $ k ) { if ( $ k <= 1 ) return false ; for ( $ i = 2 ; $ i < $ k ; $ i ++ ) if ( $ k % $ i == 0 ) return false ; return true ; } function check ( $ num , $ k ) { $ flag = 1 ; for ( $ i = 2 ; $ i < $ k ; $ i ++ ) { if ( $ num % $ i == 0 ) $ flag = 0 ; } if ( $ flag == 1 ) { if ( $ num % $ k == 0 ) return 1 ; else return 0 ; } else return 0 ; } function findCount ( $ a , $ b , $ k ) { $ count = 0 ; if ( ! isPrime ( $ k ) ) return 0 ; else { for ( $ i = $ a ; $ i <= $ b ; $ i ++ ) { $ ans = check ( $ i , $ k ) ; if ( $ ans == 1 ) $ count ++ ; else continue ; } } return $ count ; } $ a = 2020 ; $ b = 6300 ; $ k = 29 ; echo ( findCount ( $ a , $ b , $ k ) ) ; ? >


"Count all the numbers less than 10 ^ 6 whose minimum prime factor is N | PHP implementation of above approach ; the sieve of prime number and count of minimum prime factor ; form the prime sieve ; 1 is not a prime number ; form the sieve ; if i is prime ; if i is the least prime factor ; mark the number j as non prime ; count the numbers whose least prime factor is i ; form the sieve ; display ; display"
How can the above be solved in PHP?

< ? php $ MAX = 1000000 ; $ sieve_Prime = array_fill ( 0 , $ MAX + 4 , NULL ) ; $ sieve_count = array_fill ( 0 , $ MAX + 4 , NULL ) ; function form_sieve ( ) { global $ sieve_Prime , $ sieve_count , $ MAX ; $ sieve_Prime [ 1 ] = 1 ; for ( $ i = 2 ; $ i <= $ MAX ; $ i ++ ) { if ( $ sieve_Prime [ $ i ] == 0 ) { for ( $ j = $ i * 2 ; $ j <= $ MAX ; $ j += $ i ) { if ( $ sieve_Prime [ $ j ] == 0 ) { $ sieve_Prime [ $ j ] = 1 ; $ sieve_count [ $ i ] ++ ; } } } } } form_sieve ( ) ; $ n = 2 ; echo " Count = " ▁ . ▁ ( $ sieve _ count [ $ n ] ▁ + ▁ 1 ) ▁ . ▁ " " $ n = 3 ; echo " Count = " ▁ . ▁ ( $ sieve _ count [ $ n ] ▁ + ▁ 1 ) ▁ . ▁ " " ? >


"Count all triplets whose sum is equal to a perfect cube | PHP program to calculate all triplets whose sum is perfect cube . ; Function to calculate all occurrence of a number in a given range ; if i == 0 assign 1 to present state ; else add + 1 to current state with previous state ; Function to calculate triplets whose sum is equal to the perfect cube ; Initialize answer ; count all occurrence of third triplet in range from j + 1 to n ; Driver code"
How can the above be solved in PHP?


"Count and Print the alphabets having ASCII value in the range [ l , r ] | Function to count the number of characters whose ascii value is in range [ l , r ] ; Initializing the count to 0 ; Increment the count if the value is less ; return the count ; Driver code"
How can the above be solved in PHP?

 < ? php function CountCharacters ( $ str , $ l , $ r ) { $ cnt = 0 ; $ len = strlen ( $ str ) ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { if ( $ l <= ord ( $ str [ $ i ] ) && ord ( $ str [ $ i ] ) <= $ r ) { $ cnt ++ ; echo $ str [ $ i ] . " " ; } } return $ cnt ; } $ str = " geeksforgeeks " ; $ l = 102 ; $ r = 111 ; echo " Characters ▁ with ▁ ASCII ▁ values " . " ▁ in ▁ the ▁ range ▁ [ l , ▁ r ] ▁ are ▁
 " ; echo " and their count is " . CountCharacters ( $ str , $ l , $ r ) ; ? >


"Count and Print the alphabets having ASCII value not in the range [ l , r ] | Function to count the number of characters whose ascii value not in range [ l , r ] ; Initializing the count to 0 ; using map to print a character only once ; Increment the count if the value is less ; return the count ; Driver code"
How can the above be solved in PHP?

< ? php function CountCharacters ( $ str , $ l , $ r ) { $ cnt = 0 ; $ m = array_fill ( 0 , 256 , NULL ) ; $ len = strlen ( $ str ) ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { if ( ! ( $ l <= ord ( $ str [ $ i ] ) and ord ( $ str [ $ i ] ) <= $ r ) ) { $ cnt ++ ; if ( isset ( $ m [ ord ( $ str [ $ i ] ) ] ) != 1 ) { echo $ str [ $ i ] . " " ; $ m [ ord ( $ str [ $ i ] ) ] ++ ; } } } return $ cnt ; } $ str = " geeksforgeeks " ; $ l = 102 ; $ r = 111 ; echo " Characters ▁ with ▁ ASCII ▁ values ▁ not ▁ in ▁ the ▁ " . " in the given string are : " echo " and their count is " . CountCharacters ( $ str , $ l , $ r ) ; ? >


"Count binary strings with k times appearing adjacent two set bits | PHP program to count number of binary strings with k times appearing consecutive 1 's. ; dp [ i ] [ j ] [ 0 ] stores count of binary strings of length i with j consecutive 1 ' s ▁ and ▁ ending ▁ at ▁ 0 . ▁ ▁ dp [ i ] [ j ] [1 ] ▁ stores ▁ count ▁ of ▁ binary ▁ ▁ strings ▁ of ▁ length ▁ i ▁ with ▁ j ▁ consecutive ▁ ▁ 1' s and ending at 1. ; If n = 1 and k = 0. ; number of adjacent 1 's can not exceed i-1 ; Driver code"
How can the above be solved in PHP?


"Count changes in Led Lights to display digits one by one | PHP program to count number of on offs to display digits of a number . ; store the led lights required to display a particular number . ; compute the change in led and keep on adding the change ; Driver code"
How can the above be solved in PHP?


"Count characters at same position as in English alphabet | Function to count the number of characters at same position as in English alphabets ; Traverse the input string ; Check that index of characters of string is same as of English alphabets by using ASCII values and the fact that all lower case alphabetic characters come together in same order in ASCII table . And same is true for upper case . ; Driver Code"
How can the above be solved in PHP?

< ? php function findCount ( $ str ) { $ result = 0 ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) { if ( ( $ i == ord ( $ str [ $ i ] ) - ord ( ' a ' ) ) or ( $ i == ord ( $ str [ $ i ] ) - ord ( ' A ' ) ) ) $ result += 1 ; } return $ result ; } $ str = " AbgdeF " ; print ( findCount ( $ str ) ) ? >


"Count characters with same neighbors | Function to count the characters with same adjacent characters ; if length is less than 3 then return length as there will be only two characters ; Traverse the string ; Increment the count if the previous and next character is same ; Return count ; Driver code"
How can the above be solved in PHP?

< ? php function countChar ( $ str ) { $ n = strlen ( $ str ) ; if ( $ n <= 2 ) return $ n ; $ count = 2 ; for ( $ i = 1 ; $ i < $ n - 1 ; $ i ++ ) if ( $ str [ $ i - 1 ] == $ str [ $ i + 1 ] ) $ count ++ ; return $ count ; } $ str = " egeeksk " ; echo countChar ( $ str ) ; ? >


"Count common subsequence in two strings | return the number of common subsequence in two strings ; for each character of S ; for each character in T ; if character are same in both the string ; Driver Code"
How can the above be solved in PHP?


"Count consecutive pairs of same elements | Function to return the count of consecutive elements in the array which are equal ; If consecutive elements are same ; Driver code"
How can the above be solved in PHP?


"Count consonants in a string ( Iterative and recursive methods ) | Function to check for consonant ; To handle lower case ; To check is character is Consonant ; Driver code"
How can the above be solved in PHP?

< ? php function isConsonant ( $ ch ) { $ ch = strtoupper ( $ ch ) ; return ! ( $ ch == ' A ' $ ch == ' E ' $ ch == ' I ' $ ch == ' O ' $ ch == ' U ' ) && ord ( $ ch ) >= 65 && ord ( $ ch ) <= 90 ; } function totalConsonants ( $ str ) { $ count = 0 ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) if ( isConsonant ( $ str [ $ i ] ) ) ++ $ count ; return $ count ; } $ str = " abc ▁ de " ; echo totalConsonants ( $ str ) ; return 0 ; ? >


"Count different numbers that can be generated such that there digits sum is equal to ' n ' | Function to count ' num ' as sum of digits ( 1 , 2 , 3 , 4 ) ; Initialize dp [ ] array ; Base case ; Initialize the current dp [ ] array as '0' ; if i == j then there is only one way to write with element itself ' i ' ; If j == 1 , then there exist two ways , one from '1' and other from '4' ; if i - j is positive then pick the element from ' i - j ' element of dp [ ] array ; Check for modulas ; return the final answer ; Driver code"
How can the above be solved in PHP?


"Count digit groupings of a number with given constraints | Function to find the subgroups ; Terminating Condition ; sum of digits ; Traverse all digits from current position to rest of the length of string ; If forward_sum is greater than the previous sum , then call the method again ; Note : We pass current sum as previous sum ; Total number of subgroups till current position ; Driver Code"
How can the above be solved in PHP?

< ? php function countGroups ( $ position , $ previous_sum , $ length , $ num ) { if ( $ position == $ length ) return 1 ; $ res = 0 ; $ sum = 0 ; for ( $ i = $ position ; $ i < $ length ; $ i ++ ) { $ sum += ( $ num [ $ i ] - '0' ) ; if ( $ sum >= $ previous_sum ) $ res += countGroups ( $ i + 1 , $ sum , $ length , $ num ) ; } return $ res ; } $ num = "1119" ; $ len = strlen ( $ num ) ; echo countGroups ( 0 , 0 , $ len , $ num ) ; ? >


"Count digits in a factorial | Set 1 | This function receives an integer n , and returns the number of digits present in n ! ; factorial exists only for n >= 0 ; base case ; else iterate through n and calculate the value ; Driver code"
How can the above be solved in PHP?

 < ? php function findDigits ( $ n ) { if ( $ n < 0 ) return 0 ; if ( $ n <= 1 ) return 1 ; $ digits = 0 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ digits += log10 ( $ i ) ; return floor ( $ digits ) + 1 ; } echo findDigits ( 1 ) , "
 " ; echo findDigits ( 5 ) , "
 " ; echo findDigits ( 10 ) , "
 " ; echo findDigits ( 120 ) , "
 " ; ? >


"Count digits in a factorial | Set 2 | Returns the number of digits present in n ! Since the result can be large long long is used as return type ; factorial of - ve number doesn 't exists ; base case ; Use Kamenetsky formula to calculate the number of digits ; Driver Code"
How can the above be solved in PHP?

 < ? php function findDigits ( $ n ) { if ( $ n < 0 ) return 0 ; if ( $ n <= 1 ) return 1 ; $ x = ( ( $ n * log10 ( $ n / M_E ) + log10 ( 2 * M_PI * $ n ) / 2.0 ) ) ; return floor ( $ x ) + 1 ; } echo findDigits ( 1 ) . "
 " ; echo findDigits ( 50000000 ) . "
 " ; echo findDigits ( 1000000000 ) . "
 " ; echo findDigits ( 120 ) ; ? >


"Count digits in given number N which divide N | Utility function to check divisibility by digit ; ( N [ i ] - '0' ) gives the digit value and form the number ; Function to count digits which appears in N and divide N divide [ 10 ] -- > array which tells that particular digit divides N or not count [ 10 ] -- > counts frequency of digits which divide N ; We initialize all digits of N as not divisible by N . ; start checking divisibility of N by digits 2 to 9 ; if digit divides N then mark it as true ; Now traverse the number string to find and increment result whenever a digit divides N . ; Driver Code"
How can the above be solved in PHP?

< ? php function divisible ( $ N , $ digit ) { $ ans = 0 ; for ( $ i = 0 ; $ i < strlen ( $ N ) ; $ i ++ ) { $ ans = ( $ ans * 10 + ( int ) ( $ N [ $ i ] - '0' ) ) ; $ ans %= $ digit ; } return ( $ ans == 0 ) ; } function allDigits ( $ N ) { $ divide = array_fill ( 0 , 10 , false ) ; for ( $ digit = 2 ; $ digit <= 9 ; $ digit ++ ) { if ( divisible ( $ N , $ digit ) ) $ divide [ $ digit ] = true ; } $ result = 0 ; for ( $ i = 0 ; $ i < strlen ( $ N ) ; $ i ++ ) { if ( $ divide [ ( int ) ( $ N [ $ i ] - '0' ) ] == true ) $ result ++ ; } return $ result ; } $ N = "122324" ; echo allDigits ( $ N ) ; ? >


"Count distinct elements in an array | PHP program to count all distinct elements in a given array ; First sort the array so that all occurrences become consecutive ; Traverse the sorted array ; Move the index ahead while there are duplicates ; Driver Code"
How can the above be solved in PHP?

< ? php function countDistinct ( $ arr , $ n ) { sort ( $ arr , 0 ) ; $ res = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { while ( $ i < $ n - 1 && $ arr [ $ i ] == $ arr [ $ i + 1 ] ) $ i ++ ; $ res ++ ; } return $ res ; } $ arr = array ( 6 , 10 , 5 , 4 , 9 , 120 , 4 , 6 , 10 ) ; $ n = sizeof ( $ arr ) ; echo countDistinct ( $ arr , $ n ) ; ? >


"Count distinct elements in an array | PHP program to count distinct elements in a given array ; Pick all elements one by one ; If not printed earlier , then print it ; Driver Code"
How can the above be solved in PHP?

< ? php function countDistinct ( & $ arr , $ n ) { $ res = 1 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ i ; $ j ++ ) if ( $ arr [ $ i ] == $ arr [ $ j ] ) break ; if ( $ i == $ j ) $ res ++ ; } return $ res ; } $ arr = array ( 12 , 10 , 9 , 45 , 2 , 10 , 10 , 45 ) ; $ n = count ( $ arr ) ; echo countDistinct ( $ arr , $ n ) ; ? >


"Count distinct elements in an array | This function prints all distinct elements ; Creates an empty hashset ; Traverse the input array ; If not present , then put it in hashtable and increment result ; Driver Code"
How can the above be solved in PHP?

< ? php function countDistinct ( $ arr , $ n ) { $ s = array ( ) ; $ res = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { array_push ( $ s , $ arr [ $ i ] ) ; } $ s = array_unique ( $ s ) ; return count ( $ s ) ; } $ arr = array ( 6 , 10 , 5 , 4 , 9 , 120 , 4 , 6 , 10 ) ; $ n = count ( $ arr ) ; echo countDistinct ( $ arr , $ n ) ; ? >


"Count distinct occurrences as a subsequence | PHP program to count number of times S appears as a subsequence in T ; T can 't appear as a subsequence in S ; mat [ i ] [ j ] stores the count of occurrences of T ( 1. . i ) in S ( 1. . j ) . ; Initializing first column with all 0 s . An empty string can 't have another  string as suhsequence ; Initializing first row with all 1 s . An empty string is subsequence of all . ; Fill mat [ ] [ ] in bottom up manner ; If last characters don 't match,  then value is same as the value  without last character in S. ; Else value is obtained considering two cases . a ) All substrings without last character in S b ) All substrings without last characters in both . ; uncomment this to print matrix mat for ( int i = 1 ; i <= m ; i ++ , cout << endl ) for ( int j = 1 ; j <= n ; j ++ ) cout << mat [ i ] [ j ] << " ▁ " ; ; Driver Code"
How can the above be solved in PHP?


"Count divisors of n that have at | function to return true if any digit of m is present in hash [ ] . ; check till last digit ; if number is also present in original number then return true ; if no number matches then return 1 ; Count the no of divisors that have at least 1 digits same ; Store digits present in n in a hash [ ] ; marks that the number is present ; last digit removed ; loop to traverse from 1 to sqrt ( n ) to count divisors ; if i is the factor ; call the function to check if any digits match or not ; if n / i != i then a different number , then check it also ; return the answer ; Driver code"
How can the above be solved in PHP?

< ? php function isDigitPresent ( $ m , $ hash ) { while ( $ m ) { if ( $ hash [ $ m % 10 ] ) return true ; $ m = ( int ) ( $ m / 10 ) ; } return false ; } function countDivisibles ( $ n ) { $ hash = array_fill ( 0 , 10 , false ) ; $ m = $ n ; while ( $ m ) { $ hash [ $ m % 10 ] = true ; $ m = ( int ) ( $ m / 10 ) ; } $ ans = 0 ; for ( $ i = 1 ; $ i <= sqrt ( $ n ) ; $ i ++ ) { if ( $ n % $ i == 0 ) { if ( isDigitPresent ( $ i , $ hash ) ) $ ans ++ ; if ( ( int ) ( $ n / $ i ) != $ i ) { if ( isDigitPresent ( ( int ) ( $ n / $ i ) , $ hash ) ) $ ans ++ ; } } } return $ ans ; } $ n = 15 ; echo countDivisibles ( $ n ) ; ? >


"Count elements in the given range which have maximum number of divisors | Function to count the elements with maximum number of divisors ; to store number of divisors initialise with zero ; to store the maximum number of divisors ; to store required answer ; Find the first divisible number ; Count number of divisors ; Find number of elements with maximum number of divisors ; Driver code"
How can the above be solved in PHP?

 < ? php function MaximumDivisors ( $ X , $ Y ) { $ arr = array_fill ( 0 , ( $ Y - $ X + 1 ) , NULL ) ; $ mx = PHP_INT_MIN ; $ cnt = 0 ; for ( $ i = 1 ; $ i * $ i <= $ Y ; $ i ++ ) { $ sq = $ i * $ i ; if ( ( $ X / $ i ) * $ i >= $ X ) $ first_divisible = ( $ X / $ i ) * $ i ; else $ first_divisible = ( $ X / $ i + 1 ) * $ i ; for ( $ j = $ first_divisible ; $ j < $ Y ; $ j += $ i ) { if ( $ j < $ sq ) continue ; else if ( $ j == $ sq ) $ arr [ $ j - $ X ] ++ ; else $ arr [ $ j - $ X ] += 2 ; } } for ( $ i = $ X ; $ i <= $ Y ; $ i ++ ) { if ( $ arr [ $ i - $ X ] > $ mx ) { $ cnt = 1 ; $ mx = $ arr [ $ i - $ X ] ; } else if ( $ arr [ $ i - $ X ] == $ mx ) $ cnt ++ ; } return $ cnt ; } $ X = 1 ; $ Y = 10 ; echo MaximumDivisors ( $ X , $ Y ) . "
 " ; ? >


"Count elements which divide all numbers in range L | function to count element Time complexity O ( n ^ 2 ) worst case ; answer for query ; 0 based index ; iterate for all elements ; check if the element divides all numbers in range ; no of elements ; if all elements are divisible by a [ i ] ; answer for every query ; Driver Code"
How can the above be solved in PHP?

 < ? php function answerQuery ( $ a , $ n , $ l , $ r ) { $ count = 0 ; $ l = $ l - 1 ; for ( $ i = $ l ; $ i < $ r ; $ i ++ ) { $ element = $ a [ $ i ] ; $ divisors = 0 ; for ( $ j = $ l ; $ j < $ r ; $ j ++ ) { if ( $ a [ $ j ] % $ a [ $ i ] == 0 ) $ divisors ++ ; else break ; } if ( $ divisors == ( $ r - $ l ) ) $ count ++ ; } return $ count ; } $ a = array ( 1 , 2 , 3 , 5 ) ; $ n = sizeof ( $ a ) ; $ l = 1 ; $ r = 4 ; echo answerQuery ( $ a , $ n , $ l , $ r ) . "
 " ; $ l = 2 ; $ r = 4 ; echo answerQuery ( $ a , $ n , $ l , $ r ) . "
 " ;


"Count even length binary sequences with same sum of first and second half bits | A memoization based PHP program to count even length binary sequences such that the sum of first and second half bits is same ; A lookup table to store the results of subproblems ; dif is difference between sums of first n bits and last n bits i . e . , dif = ( Sum of first n bits ) - ( Sum of last n bits ) ; We can 't cover difference of more  than n with 2n bits ; n == 1 , i . e . , 2 bit long sequences ; Check if this subproblem is already solved n is added to dif to make sure index becomes positive ; $res = First bit is 0 & last bit is 1 ; First and last bits are same ; First bit is 1 & last bit is 0 ; Store result in lookup table and return the result ; A Wrapper over countSeqUtil ( ) . It mainly initializes lookup table , then calls countSeqUtil ( ) ; call countSeqUtil ( ) ; Driver Code"
How can the above be solved in PHP?

< ? php $ MAX = 1000 ; $ lookup = array_fill ( 0 , $ MAX , array_fill ( 0 , $ MAX , -1 ) ) ; function countSeqUtil ( $ n , $ dif ) { global $ lookup ; if ( abs ( $ dif ) > $ n ) return 0 ; if ( $ n == 1 && $ dif == 0 ) return 2 ; if ( $ n == 1 && abs ( $ dif ) == 1 ) return 1 ; if ( $ lookup [ $ n ] [ $ n + $ dif ] != -1 ) return $ lookup [ $ n ] [ $ n + $ dif ] ; countSeqUtil ( $ n - 1 , $ dif + 1 ) + 2 * countSeqUtil ( $ n - 1 , $ dif ) + countSeqUtil ( $ n - 1 , $ dif - 1 ) ; return $ lookup [ $ n ] [ $ n + $ dif ] = $ res ; } function countSeq ( $ n ) { return countSeqUtil ( $ n , 0 ) ; } $ n = 2 ; echo " Count ▁ of ▁ sequences ▁ is ▁ " . countSeq ( $ n ) ; ? >


"Count even length binary sequences with same sum of first and second half bits | Returns the count of even length sequences ; Calculate SUM ( ( nCr ) ^ 2 ) ; Compute nCr using nC ( r - 1 ) nCr / nC ( r - 1 ) = ( n + 1 - r ) / r ; ; Driver Code"
How can the above be solved in PHP?

< ? php function countSeq ( $ n ) { $ nCr = 1 ; $ res = 1 ; for ( $ r = 1 ; $ r <= $ n ; $ r ++ ) { $ nCr = ( $ nCr * ( $ n + 1 - $ r ) ) / $ r ; $ res = $ res + ( $ nCr * $ nCr ) ; } return $ res ; } $ n = 2 ; echo ( " Count ▁ of ▁ sequences ▁ is ▁ " ) ; echo countSeq ( $ n ) ; ? >


"Count even length binary sequences with same sum of first and second half bits | diff is difference between sums first n bits and last n bits respectively ; We can 't cover difference of  more than n with 2n bits ; n == 1 , i . e . , 2 bit long sequences ; First bit is 0 & last bit is 1 ; First and last bits are same ; First bit is 1 & last bit is 0 ; Driver Code"
How can the above be solved in PHP?

< ? php function countSeq ( $ n , $ diff ) { if ( abs ( $ diff ) > $ n ) return 0 ; if ( $ n == 1 && $ diff == 0 ) return 2 ; if ( $ n == 1 && abs ( $ diff ) == 1 ) return 1 ; $ res = countSeq ( $ n - 1 , $ diff + 1 ) + 2 * countSeq ( $ n - 1 , $ diff ) + countSeq ( $ n - 1 , $ diff - 1 ) ; return $ res ; } $ n = 2 ; echo " Count ▁ of ▁ sequences ▁ is ▁ " , countSeq ( $ n , 0 ) ; ? >


"Count factorial numbers in a given range | Function to count factorial ; Find the first factorial number ' fact ' greater than or equal to ' low ' ; Count factorial numbers in range [ low , high ] ; Return the count ; Driver Code"
How can the above be solved in PHP?

< ? php function countFact ( $ low , $ high ) { $ fact = 1 ; $ x = 1 ; while ( $ fact < $ low ) { $ fact = $ fact * $ x ; $ x ++ ; } $ res = 0 ; while ( $ fact <= $ high ) { $ res ++ ; $ fact = $ fact * $ x ; $ x ++ ; } return $ res ; } echo " Count ▁ is ▁ " , countFact ( 2 , 720 ) ; ? >


"Count frequencies of all elements in array in O ( 1 ) extra space and O ( n ) time | Function to find counts of all elements present in arr [ 0. . n - 1 ] . The array elements must be range from 1 to n ; Subtract 1 from every element so that the elements become in range from 0 to n - 1 ; Use every element arr [ i ] as index and add ' n ' to element present at arr [ i ] % n to keep track of count of occurrences of arr [ i ] ; To print counts , simply print the number of times n was added at index corresponding to every element ; Driver Code"
How can the above be solved in PHP?


"Count frequency of k in a matrix of size n where matrix ( i , j ) = i + j | PHP program to find the frequency of k in matrix where m ( i , j ) = i + j ; Driver Code"
How can the above be solved in PHP?

 < ? php function find ( $ n , $ k ) { if ( $ n + 1 >= $ k ) return ( $ k - 1 ) ; else return ( 2 * $ n + 1 - $ k ) ; } $ n = 4 ; $ k = 7 ; $ freq = find ( $ n , $ k ) ; if ( $ freq < 0 ) echo " ▁ element ▁ not ▁ exist ▁
 ▁ " ; else echo " ▁ Frequency ▁ of ▁ " , $ k , " ▁ is ▁ " , $ freq , "
 " ; ? >


"Count integers in a range which are divisible by their euler totient value | Function to return a ^ n ; Function to return count of integers that satisfy n % phi ( n ) = 0 ; Driver Code"
How can the above be solved in PHP?

< ? php function power ( $ a , $ n ) { if ( $ n == 0 ) return 1 ; $ p = power ( $ a , $ n / 2 ) ; $ p = $ p * $ p ; if ( $ n & 1 ) $ p = $ p * $ a ; return $ p ; } function countIntegers ( $ l , $ r ) { $ ans = 0 ; $ i = 1 ; $ v = power ( 2 , $ i ) ; while ( $ v <= $ r ) { while ( $ v <= $ r ) { if ( $ v >= $ l ) $ ans ++ ; $ v = $ v * 3 ; } $ i ++ ; $ v = power ( 2 , $ i ) ; } if ( $ l == 1 ) $ ans ++ ; return $ ans ; } $ l = 12 ; $ r = 21 ; echo countIntegers ( $ l , $ r ) ; ? >


"Count minimum bits to flip such that XOR of A and B equal to C | PHP code to count the Minimum bits in A and B ; If both A [ i ] and B [ i ] are equal ; If Both A and B are unequal ; N represent total count of Bits"
How can the above be solved in PHP?


"Count minimum number of subsets ( or subsequences ) with consecutive numbers | Returns count of subsets with consecutive numbers ; Sort the array so that elements which are consecutive in nature became consecutive in the array . ; Initialize result ; Check if there is beginning of another subset of consecutive number ; Driver Code"
How can the above be solved in PHP?

< ? php function numofsubset ( $ arr , $ n ) { sort ( $ arr ) ; $ count = 1 ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { if ( $ arr [ $ i ] + 1 != $ arr [ $ i + 1 ] ) $ count ++ ; } return $ count ; } $ arr = array ( 100 , 56 , 5 , 6 , 102 , 58 , 101 , 57 , 7 , 103 , 59 ) ; $ n = sizeof ( $ arr ) ; echo numofsubset ( $ arr , $ n ) ; ? >


"Count minimum steps to get the given desired array | Returns count of minimum operations to convert a zero array to target array with increment and doubling operations . This function computes count by doing reverse steps , i . e . , convert target to zero array . ; Initialize result ( Count of minimum moves ) ; Keep looping while all elements of target don 't become 0. ; To store count of zeroes in current target array ; To find first odd element ; If odd number found ; If 0 , then increment zero_count ; All numbers are 0 ; All numbers are even ; Divide the whole array by 2 and increment result ; Make all odd numbers even by subtracting one and increment result . ; Driver Code"
How can the above be solved in PHP?

 < ? php function countMinOperations ( $ target , $ n ) { $ result = 0 ; while ( 1 ) { $ zero_count = 0 ; $ i = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ target [ $ i ] & 1 ) break ; else if ( $ target [ $ i ] == 0 ) $ zero_count ++ ; } if ( $ zero_count == $ n ) return $ result ; if ( $ i == $ n ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) $ target [ $ j ] = $ target [ $ j ] / 2 ; $ result ++ ; } for ( $ j = $ i ; $ j < $ n ; $ j ++ ) { if ( $ target [ $ j ] & 1 ) { $ target [ $ j ] -- ; $ result ++ ; } } } } $ arr = array ( 16 , 16 , 16 ) ; $ n = sizeof ( $ arr ) ; echo " Minimum ▁ number ▁ of ▁ steps ▁ required ▁ to ▁
 " . " get ▁ the ▁ given ▁ target ▁ array ▁ is ▁ " . countMinOperations ( $ arr , $ n ) ; ? >


"Count n digit numbers divisible by given number | Returns count of n digit numbers divisible by ' number ' ; compute the first and last term ; count total number of which having n digit and divisible by number ; Driver code"
How can the above be solved in PHP?

< ? php function numberofterm ( $ n , $ number ) { $ firstnum = pow ( 10 , $ n - 1 ) ; $ lastnum = pow ( 10 , $ n ) ; $ count = 0 ; for ( $ i = $ firstnum ; $ i < $ lastnum ; $ i ++ ) if ( $ i % $ number == 0 ) $ count ++ ; return $ count ; } $ n = 3 ; $ num = 7 ; echo numberofterm ( $ n , $ num ) ; ? >


"Count natural numbers whose factorials are divisible by x but not y | GCD function to compute the greatest divisor among a and b ; Returns first number whose factorial is divisible by x . ; Result ; Remove common factors ; We found first i . ; Count of natural numbers whose factorials are divisible by x but not y . ; Return difference between first natural number whose factorial is divisible by y and first natural number whose factorial is divisible by x . ; Driver code"
How can the above be solved in PHP?

< ? php function gcd ( $ a , $ b ) { if ( ( $ a % $ b ) == 0 ) return $ b ; return gcd ( $ b , $ a % $ b ) ; } function firstFactorialDivisibleNumber ( $ x ) { $ i = 1 ; $ new_x = $ x ; for ( $ i = 1 ; $ i < $ x ; $ i ++ ) { $ new_x /= gcd ( $ i , $ new_x ) ; if ( $ new_x == 1 ) break ; } return $ i ; } function countFactorialXNotY ( $ x , $ y ) { return ( firstFactorialDivisibleNumber ( $ y ) - firstFactorialDivisibleNumber ( $ x ) ) ; } $ x = 15 ; $ y = 25 ; echo ( countFactorialXNotY ( $ x , $ y ) ) ; ? >


"Count no . of ordered subsets having a particular XOR value | Returns count of ordered subsets of arr [ ] with XOR value = K ; Find maximum element in arr [ ] ; Maximum possible XOR value ; The value of dp [ i ] [ j ] [ k ] is the number of subsets of length k having XOR of their elements as j from the set arr [ 0. . . i - 1 ] ; Initializing all the values of dp [ i ] [ j ] [ k ] as 0 ; The xor of empty subset is 0 ; Fill the dp table ; The answer is the number of subsets of all lengths from set arr [ 0. . n - 1 ] having XOR of elements as k ; Driver Code"
How can the above be solved in PHP?


"Count number less than N which are product of perfect squares | Function to count number less than N which are product of any two perfect squares ; Driver Code"
How can the above be solved in PHP?

< ? php function countNumbers ( $ N ) { return ( int ) ( sqrt ( $ N ) ) - 1 ; } $ N = 36 ; echo countNumbers ( $ N ) ; ? >


"Count number of 1 s in the array after N moves | Function to count number of perfect squares ; Counting number of perfect squares between a and b ; Function to count number of 1 s in array after N moves ; Initialize array size ; Initialize all elements to 0"
How can the above be solved in PHP?

< ? php function perfectSquares ( $ a , $ b ) { return ( floor ( sqrt ( $ b ) ) - ceil ( sqrt ( $ a ) ) + 1 ) ; } function countOnes ( $ arr , $ n ) { return perfectSquares ( 1 , $ n ) ; } $ N = 10 ; $ arr [ 10 ] = array ( 0 ) ; echo countOnes ( $ arr , $ N ) ; ? >


"Count number of binary strings of length N having only 0 ' s ▁ and ▁ 1' s | 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 count the number of binary strings of length N having only 0 ' s ▁ and ▁ 1' s ; Driver code"
How can the above be solved in PHP?

< ? php function power ( $ x , $ y ) { $ p = 1000000007 ; while ( $ y > 0 ) { if ( $ y & 1 ) $ res = ( $ res * $ x ) % $ p ; $ x = ( $ x * $ x ) % $ p ; } return $ res ; } function findCount ( $ N ) { $ count = power ( 2 , $ N ) ; return $ count ; } $ N = 25 ; echo findCount ( $ N ) ; ? >


"Count number of bits to be flipped to convert A to B | Function that count set bits ; Function that return count of flipped number ; Return count of set bits in a XOR b ; Driver code"
How can the above be solved in PHP?

< ? php function countSetBits ( $ n ) { $ count = 0 ; while ( $ n ) { $ count += 1 ; $ n &= ( n - 1 ) ; } return $ count ; } function FlippedCount ( $ a , $ b ) { return countSetBits ( $ a ^ $ b ) ; } $ a = 10 ; $ b = 20 ; echo FlippedCount ( $ a , $ b ) ; ? >


"Count number of elements between two given elements in array | Function to count number of elements occurs between the elements . ; Find num1 ; If num1 is not present or present at end ; Find num2 ; If num2 is not present ; return number of elements betweenthe two elements . ; Driver Code"
How can the above be solved in PHP?


"Count number of integers less than or equal to N which has exactly 9 divisors | Function to count factors in O ( N ) ; iterate and check if factor or not ; Function to count numbers having exactly 9 divisors ; check for all numbers <= N ; check if exactly 9 factors or not ; Driver Code"
How can the above be solved in PHP?

< ? php Function numberOfDivisors ( $ num ) { $ c = 0 ; for ( $ i = 1 ; $ i <= $ num ; $ i ++ ) { if ( $ num % $ i == 0 ) { $ c += 1 ; } } return $ c ; } Function countNumbers ( $ n ) { $ c = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { if ( numberOfDivisors ( $ i ) == 9 ) $ c += 1 ; } return $ c ; } $ n = 1000 ; echo countNumbers ( $ n ) ; ? >


"Count number of integers less than or equal to N which has exactly 9 divisors | PHP implementation of above approach Function to count numbers having exactly 9 divisors ; Sieve array ; initially prime [ i ] = i ; use sieve concept to store the first prime factor of every number ; mark all factors of i ; check for all numbers if they can be expressed in form p * q ; p prime factor ; q prime factor ; if both prime factors are different if p * q <= n and q != ; Check if it can be expressed as p ^ 8 ; Driver Code"
How can the above be solved in PHP?


"Count number of islands where every island is row | This function takes a matrix of ' X ' and ' O ' and returns the number of rectangular islands of ' X ' where no two islands are row - wise or column - wise adjacent , the islands may be diagonaly adjacent ; Initialize result ; Traverse the input matrix ; If current cell is ' X ' , then check whether this is top - leftmost of a rectangle . If yes , then increment count ; Driver Code"
How can the above be solved in PHP?

< ? php function countIslands ( $ mat ) { $ M = 6 ; $ N = 3 ; $ count = 0 ; for ( $ i = 0 ; $ i < $ M ; $ i ++ ) { for ( $ j = 0 ; $ j < $ N ; $ j ++ ) { if ( $ mat [ $ i ] [ $ j ] == ' X ' ) { if ( ( $ i == 0 $ mat [ $ i - 1 ] [ $ j ] == ' O ' ) && ( $ j == 0 $ mat [ $ i ] [ $ j - 1 ] == ' O ' ) ) $ count ++ ; } } } return $ count ; } $ mat = array ( array ( ' O ' , ' O ' , ' O ' ) , array ( ' X ' , ' X ' , ' O ' ) , array ( ' X ' , ' X ' , ' O ' ) , array ( ' O ' , ' O ' , ' X ' ) , array ( ' O ' , ' O ' , ' X ' ) , array ( ' X ' , ' X ' , ' O ' ) ) ; echo " Number ▁ of ▁ rectangular ▁ islands ▁ is ▁ " , countIslands ( $ mat ) ; ? >


"Count number of occurrences ( or frequency ) in a sorted array | 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 in left subarray ; Else the element can only be present in right subarray ; Returns number of times x occurs in arr [ 0. . n - 1 ] ; If element is not present ; Count elements on left side . ; Count elements on right side . ; Driver code"
How can the above be solved in PHP?

< ? php function binarySearch ( & $ arr , $ l , $ r , $ x ) { if ( $ r < $ l ) return -1 ; $ 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 ) ; } function countOccurrences ( $ arr , $ n , $ x ) { $ ind = binarySearch ( $ arr , 0 , $ n - 1 , $ x ) ; if ( $ ind == -1 ) return 0 ; $ count = 1 ; $ left = $ ind - 1 ; while ( $ left >= 0 && $ arr [ $ left ] == $ x ) { $ count ++ ; $ left -- ; } $ right = $ ind + 1 ; while ( $ right < $ n && $ arr [ $ right ] == $ x ) { $ count ++ ; $ right ++ ; } return $ count ; } $ arr = array ( 1 , 2 , 2 , 2 , 2 , 3 , 4 , 7 , 8 , 8 ) ; $ n = sizeof ( $ arr ) ; $ x = 2 ; echo countOccurrences ( $ arr , $ n , $ x ) ; ? >


"Count number of occurrences ( or frequency ) in a sorted array | Returns number of times x occurs in arr [ 0. . n - 1 ] ; Driver code"
How can the above be solved in PHP?

< ? php function countOccurrences ( $ arr , $ n , $ x ) { $ res = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ x == $ arr [ $ i ] ) $ res ++ ; return $ res ; } $ arr = array ( 1 , 2 , 2 , 2 , 2 , 3 , 4 , 7 , 8 , 8 ) ; $ n = count ( $ arr ) ; $ x = 2 ; echo countOccurrences ( $ arr , $ n , $ x ) ; ? >


"Count number of ordered pairs with Even and Odd Sums | function to count odd sum pair ; if number is even ; if number is odd ; count of ordered pairs ; function to count even sum pair ; Driver code"
How can the above be solved in PHP?

 < ? php function count_odd_pair ( $ n , $ a ) { $ odd = 0 ; $ even = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] % 2 == 0 ) $ even ++ ; else $ odd ++ ; } $ ans = $ odd * $ even * 2 ; return $ ans ; } function count_even_pair ( $ odd_sum_pairs , $ n ) { $ total_pairs = ( $ n * ( $ n - 1 ) ) ; $ ans = $ total_pairs - $ odd_sum_pairs ; return $ ans ; } $ n = 6 ; $ a = array ( 2 , 4 , 5 , 9 , 1 , 8 ) ; $ odd_sum_pairs = count_odd_pair ( $ n , $ a ) ; $ even_sum_pairs = count_even_pair ( $ odd_sum_pairs , $ n ) ; echo " Even ▁ Sum ▁ Pairs ▁ = ▁ $ even _ sum _ pairs ▁
 " ; echo " Odd ▁ Sum ▁ Pairs = ▁ $ odd _ sum _ pairs ▁
 " ; ? >


"Count number of pairs ( A <= N , B <= N ) such that gcd ( A , B ) is B | returns number of valid pairs ; initialize k ; loop till imin <= n ; Initialize result ; max i with given k floor ( n / k ) ; adding k * ( number of i with floor ( n / i ) = k to ans ; set imin = imax + 1 and k = n / imin ; Driver Code"
How can the above be solved in PHP?

 < ? php function CountPairs ( $ n ) { $ k = $ n ; $ imin = 1 ; $ ans = 0 ; while ( $ imin <= $ n ) { $ imax = $ n / $ k ; $ ans += $ k * ( $ imax - $ imin + 1 ) ; $ imin = $ imax + 1 ; $ k = ( int ) ( $ n / $ imin ) ; } return $ ans ; } echo ( CountPairs ( 1 ) . "
 " ) ; echo ( CountPairs ( 2 ) . "
 " ) ; echo ( CountPairs ( 3 ) . "
 " ) ; ? >


"Count number of paths whose weight is exactly X and has at | PHP program to count the number of paths ; Function to find the number of paths ; If the summation is more than X ; If exactly X weights have reached ; Already visited ; Count paths ; Traverse in all paths ; If the edge weight is M ; else Edge 's weight is not M ; Driver Code ; Initialized the DP array with - 1 ; Function to count paths"
How can the above be solved in PHP?

< ? php $ max = 4 ; $ c = 2 ; function countPaths ( $ sum , $ get , $ m , $ n , & $ dp ) { global $ max , $ c ; if ( $ sum < 0 ) return 0 ; if ( $ sum == 0 ) return $ get ; if ( $ dp [ $ sum ] [ $ get ] != -1 ) return $ dp [ $ sum ] [ $ get ] ; $ res = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { if ( $ i == $ m ) $ res += countPaths ( $ sum - $ i , 1 , $ m , $ n , $ dp ) ; $ res += countPaths ( $ sum - $ i , $ get , $ m , $ n , $ dp ) ; } $ dp [ $ sum ] [ $ get ] = $ res ; return $ dp [ $ sum ] [ $ get ] ; } $ n = 3 ; $ m = 2 ; $ x = 3 ; $ dp = array_fill ( 0 , $ max + 1 , NULL ) ; for ( $ i = 0 ; $ i <= $ max ; $ i ++ ) for ( $ j = 0 ; $ j < 2 ; $ j ++ ) $ dp [ $ i ] [ $ j ] = -1 ; echo countPaths ( $ x , 0 , $ m , $ n , $ dp ) ; ? >


"Count number of solutions of x ^ 2 = 1 ( mod p ) in given range | Program to count number of values that satisfy x ^ 2 = 1 mod p where x lies in range [ 1 , n ] ; Initialize result ; Traverse all numbers smaller than given number p . Note that we don 't traverse from  1 to n, but 1 to p ; If x is a solution , then count all numbers of the form x + i * p such that x + i * p is in range [ 1 , n ] ; The largest number in the form of x + p * i in range [ 1 , n ] ; Add count of numbers of the form x + p * i . 1 is added for x itself . ; Driver code"
How can the above be solved in PHP?

< ? php function findCountOfSolutions ( $ n , $ p ) { $ ans = 0 ; for ( $ x = 1 ; $ x < $ p ; $ x ++ ) { if ( ( $ x * $ x ) % $ p == 1 ) { $ last = $ x + $ p * ( $ n / $ p ) ; if ( $ last > $ n ) $ last -= $ p ; $ ans += ( ( $ last - $ x ) / $ p + 1 ) ; } } return $ ans ; } $ n = 10 ; $ p = 5 ; echo findCountOfSolutions ( $ n , $ p ) ; ? >


"Count number of squares in a rectangle | Returns count of all squares in a rectangle of size m x n ; If n is smaller , swap m and n ; Now n is greater dimension , apply formula ; Driver Code"
How can the above be solved in PHP?

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


"Count number of subsets having a particular XOR value | Returns count of subsets of arr [ ] with XOR value equals to k . ; Find maximum element in arr [ ] ; Maximum possible XOR value ; Initializing all the values of dp [ i ] [ j ] as 0 ; The xor of empty subset is 0 ; Fill the dp table ; The answer is the number of subset from set arr [ 0. . n - 1 ] having XOR of elements as k ; Driver Code"
How can the above be solved in PHP?


"Count number of subsets whose median is also present in the same subset | PHP implementation of the approach ; Function to return the factorial of a number ; Function to return the value of nCr ; Function to return a raised to the power n with complexity O ( log ( n ) ) ; Function to return the number of sub - sets whose median is also present in the set ; Number of odd length sub - sets ; Sort the array ; Checking each element for leftmost middle element while they are equal ; Calculate the number of elements in right of rightmost middle element ; Calculate the number of elements in left of leftmost middle element ; Add selected even length subsets to the answer ; Driver code"
How can the above be solved in PHP?

< ? php $ mod = 1000000007 ; function fact ( $ n ) { $ res = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ res = $ res * $ i ; return $ res ; } function nCr ( $ n , $ r ) { return fact ( $ n ) / ( fact ( $ r ) * fact ( $ n - $ r ) ) ; } function powmod ( $ a , $ n ) { global $ mod ; if ( $ n == 0 ) return 1 ; $ pt = powmod ( $ a , $ n / 2 ) ; $ pt = ( $ pt * $ pt ) % $ mod ; if ( $ n % 2 == 1 ) return ( $ pt * $ a ) % $ mod ; else return $ pt ; } function CountSubset ( $ arr , $ n ) { global $ mod ; $ ans = powmod ( 2 , $ n - 1 ) ; sort ( $ arr , 0 ) ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { $ j = $ i + 1 ; while ( $ j < $ n && $ arr [ $ j ] == $ arr [ $ i ] ) { $ r = $ n - 1 - $ j ; $ l = $ i ; $ ans = ( $ ans + nCr ( $ l + $ r , $ l ) ) % $ mod ; $ j ++ ; } } return $ ans ; } { $ arr = array ( 2 , 3 , 2 ) ; $ n = sizeof ( $ arr ) ; echo ( CountSubset ( $ arr , $ n ) ) ; }


"Count number of substrings with exactly k distinct characters | Function to count number of substrings with exactly k unique characters ; Initialize result ; To store count of characters from ' a ' to ' z ' ; Consider all substrings beginning with str [ i ] ; Initializing count array with 0 ; Consider all substrings between str [ i . . j ] ; If this is a new character for this substring , increment dist_count . ; Increment count of current character ; If distinct character count becomes k , then increment result . ; Driver code"
How can the above be solved in PHP?

< ? php function countkDist ( $ str , $ k ) { $ res = 0 ; $ n = strlen ( $ str ) ; $ cnt = array ( ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ dist_count = 0 ; $ cnt = array_fill ( 0 , 0 , true ) ; for ( $ j = $ i ; $ j < $ n ; $ j ++ ) { if ( $ cnt [ ord ( $ str [ $ j ] ) - ord ( ' a ' ) ] == 0 ) $ dist_count ++ ; $ cnt [ ord ( $ str [ $ j ] ) - ord ( ' a ' ) ] ++ ; if ( $ dist_count == $ k ) $ res ++ ; } } return $ res ; } { $ ch = " abcbaa " ; $ k = 3 ; echo ( " Total ▁ substrings ▁ with ▁ exactly ▁ " . $ k . " ▁ distinct ▁ characters ▁ : ▁ " . countkDist ( $ ch , $ k ) ) ; }


"Count number of substrings with numeric value greater than X | Function that counts valid sub - strings ; Only take those numbers that do not start w$ith '0' . ; converting the sub - str$ing starting from index ' i ' and having length ' len ' to int and checking if it is greater than X or not ; Driver code"
How can the above be solved in PHP?

< ? php function countSubStr ( $ S , $ X ) { $ cnt = 0 ; $ N = strlen ( $ S ) ; for ( $ i = 0 ; $ i < $ N ; ++ $ i ) { if ( $ S [ $ i ] != '0' ) { for ( $ len = 1 ; ( $ i + $ len ) <= $ N ; ++ $ len ) { $ num = intval ( substr ( $ S , $ i , $ len ) ) ; if ( $ num > $ X ) $ cnt ++ ; } } } return $ cnt ; } $ S = "2222" ; $ X = 97 ; echo countSubStr ( $ S , $ X ) ; ? >


"Count number of trailing zeros in ( 1 ^ 1 ) * ( 2 ^ 2 ) * ( 3 ^ 3 ) * ( 4 ^ 4 ) * . . | Function to return the number of trailing zeros ; To store the number of 2 s and 5 s ; If we get a factor 2 then we have i number of 2 s because the power of the number is raised to i ; If we get a factor 5 then we have i number of 5 s because the power of the number is raised to i ; Take the minimum of them ; Driver code"
How can the above be solved in PHP?

< ? php function trailing_zeros ( $ N ) { $ count_of_two = 0 ; $ count_of_five = 0 ; for ( $ i = 1 ; $ i <= $ N ; $ i ++ ) { $ val = $ i ; while ( $ val % 2 == 0 && $ val > 0 ) { $ val /= 2 ; $ count_of_two += $ i ; } while ( $ val % 5 == 0 && $ val > 0 ) { $ val /= 5 ; $ count_of_five += $ i ; } } $ ans = min ( $ count_of_two , $ count_of_five ) ; return $ ans ; } $ N = 12 ; echo trailing_zeros ( $ N ) ; ? >


"Count number of trailing zeros in product of array | Returns count of zeros in product of array ; count number of 2 s in each element ; count number of 5 s in each element ; return the minimum ; Driver Code"
How can the above be solved in PHP?


"Count number of triplets ( a , b , c ) such that a ^ 2 + b ^ 2 = c ^ 2 and 1 <= a <= b <= c <= n | Function to ind number of Triplets 1 <= a <= b <= c <= n , Such that a ^ 2 + b ^ 2 = c ^ 2 ; to store required answer ; run nested loops for first two numbers . ; third number ; check if third number is perfect square and less than n ; Driver code ; function call"
How can the above be solved in PHP?


"Count number of triplets in an array having sum in the range [ a , b ] | Function to count triplets ; Initialize result ; Fix the first element as A [ i ] ; Fix the second element as A [ j ] ; Now look for the third number ; Driver Code"
How can the above be solved in PHP?


"Count number of triplets in an array having sum in the range [ a , b ] | Function to find count of triplets having sum less than or equal to val . ; sort the input array . ; Initialize result ; to store sum ; Fix the first element ; Initialize other two elements as corner elements of subarray arr [ j + 1. . k ] ; Use Meet in the Middle concept . ; If sum of current triplet is greater , then to reduce it decrease k . ; If sum is less than or equal to given value , then add possible triplets ( k - j ) to result . ; Function to return count of triplets having sum in range [ a , b ] . ; to store count of triplets . ; Find count of triplets having sum less than or equal to b and subtract count of triplets having sum less than or equal to a - 1. ; Driver Code"
How can the above be solved in PHP?

< ? php function countTripletsLessThan ( $ arr , $ n , $ val ) { sort ( $ arr ) ; $ ans = 0 ; $ sum ; for ( $ i = 0 ; $ i < $ n - 2 ; $ i ++ ) { $ j = $ i + 1 ; $ k = $ n - 1 ; while ( $ j != $ k ) { $ sum = $ arr [ $ i ] + $ arr [ $ j ] + $ arr [ $ k ] ; if ( $ sum > $ val ) $ k -- ; else { $ ans += ( $ k - $ j ) ; $ j ++ ; } } } return $ ans ; } function countTriplets ( $ arr , $ n , $ a , $ b ) { $ res ; $ res = countTripletsLessThan ( $ arr , $ n , $ b ) - countTripletsLessThan ( $ arr , $ n , $ a - 1 ) ; return $ res ; } $ arr = array ( 2 , 7 , 5 , 3 , 8 , 4 , 1 , 9 ) ; $ n = sizeof ( $ arr ) ; $ a = 8 ; $ b = 16 ; echo countTriplets ( $ arr , $ n , $ a , $ b ) , " " ; ? >


"Count number of triplets with product equal to given number with duplicates allowed | The target value for which we have to find the solution ; This variable contains the total count of triplets found ; Loop from the first to the third last integer in the list ; Check if arr [ i ] is a factor of target or not . If not , skip to the next element ; Check if the pair ( arr [ i ] , arr [ j ] ) can be a part of triplet whose product is equal to the target ; Find the remaining element of the triplet ; If element is found . increment the total count of the triplets"
How can the above be solved in PHP?

< ? php $ target = 93 ; $ arr = array ( 1 , 31 , 3 , 1 , 93 , 3 , 31 , 1 , 93 ) ; $ length = sizeof ( $ arr ) ; $ totalCount = 0 ; for ( $ i = 0 ; $ i < $ length - 2 ; $ i ++ ) { if ( $ target % $ arr [ $ i ] == 0 ) { for ( $ j = $ i + 1 ; $ j < $ length - 1 ; $ j ++ ) { if ( $ target % ( $ arr [ $ i ] * $ arr [ $ j ] ) == 0 ) { $ toFind = $ target / ( $ arr [ $ i ] * $ arr [ $ j ] ) ; for ( $ k = $ j + 1 ; $ k < $ length ; $ k ++ ) { if ( $ arr [ $ k ] == $ toFind ) { $ totalCount ++ ; } } } } } } echo ( " Total ▁ number ▁ of ▁ triplets ▁ found ▁ : ▁ " ) ; echo ( $ totalCount ) ; ? >


"Count number of triplets with product equal to given number | Function to count such triplets ; Consider all triplets and count if their product is equal to m ; Driver code"
How can the above be solved in PHP?

< ? php function countTriplets ( $ arr , $ n , $ m ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ n - 2 ; $ i ++ ) for ( $ j = $ i + 1 ; $ j < $ n - 1 ; $ j ++ ) for ( $ k = $ j + 1 ; $ k < $ n ; $ k ++ ) if ( $ arr [ $ i ] * $ arr [ $ j ] * $ arr [ $ k ] == $ m ) $ count ++ ; return $ count ; } $ arr = array ( 1 , 4 , 6 , 2 , 3 , 8 ) ; $ n = sizeof ( $ arr ) ; $ m = 24 ; echo countTriplets ( $ arr , $ n , $ m ) ; ? >


"Count number of triplets with product equal to given number | Set 2 | Function to count such triplets ; Sort the array ; three pointer technique ; Calculate the product of a triplet ; Check if that product is greater than m , decrement mid ; Check if that product is smaller than m , increment start ; Check if that product is equal to m , decrement mid , increment start and increment the count of pairs ; Drivers code"
How can the above be solved in PHP?

 < ? php function countTriplets ( $ arr , $ n , $ m ) { $ count = 0 ; sort ( $ arr ) ; $ end ; $ start ; $ mid ; for ( $ end = $ n - 1 ; $ end >= 2 ; $ end -- ) { $ start = 0 ; $ mid = $ end - 1 ; while ( $ start < $ mid ) { $ prod = $ arr [ $ end ] * $ arr [ $ start ] * $ arr [ $ mid ] ; if ( $ prod > $ m ) $ mid -- ; else if ( $ prod < $ m ) $ start ++ ; else if ( $ prod == $ m ) { $ count ++ ; $ mid -- ; $ start ++ ; } } } return $ count ; } $ arr = array ( 1 , 1 , 1 , 1 , 1 , 1 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; $ m = 1 ; echo countTriplets ( $ arr , $ n , $ m ) ; #This  Code is Contributed by ajit
? >


"Count number of ways to cover a distance | Function returns count of ways to cover ' dist ' ; Initialize base values . There is one way to cover 0 and 1 distances and two ways to cover 2 distance ; Fill the count array in bottom up manner ; Driver Code"
How can the above be solved in PHP?

< ? php function printCountDP ( $ dist ) { $ count = array ( ) ; $ count [ 0 ] = 1 ; $ count [ 1 ] = 1 ; $ count [ 2 ] = 2 ; for ( $ i = 3 ; $ i <= $ dist ; $ i ++ ) $ count [ $ i ] = $ count [ $ i - 1 ] + $ count [ $ i - 2 ] + $ count [ $ i - 3 ] ; return $ count [ $ dist ] ; } $ dist = 4 ; echo printCountDP ( $ dist ) ; ? >


"Count number of ways to cover a distance | Returns count of ways to cover ' dist ' ; Base cases ; Recur for all previous 3 and add the results ; Driver Code"
How can the above be solved in PHP?

< ? php function printCountRec ( $ dist ) { if ( $ dist < 0 ) return 0 ; if ( $ dist == 0 ) return 1 ; return printCountRec ( $ dist - 1 ) + printCountRec ( $ dist - 2 ) + printCountRec ( $ dist - 3 ) ; } $ dist = 4 ; echo printCountRec ( $ dist ) ; ? >


"Count number of ways to divide a number in 4 parts | A Dynamic Programming based solution to count number of ways to represent n as sum of four numbers ; " parts " is number of parts left , n is the value left " nextPart " is starting point from where we start trying for next part . ; Base cases ; If this subproblem is already solved ; Count number of ways for remaining number n - i remaining parts " parts - 1" , and for all part varying from ' nextPart ' to ' n ' ; Store computed answer in table and return result ; This function mainly initializes dp table and calls countWaysUtil ( ) ; Driver Code"
How can the above be solved in PHP?

< ? php $ dp = array_fill ( 0 , 501 , array_fill ( 0 , 501 , array_fill ( 0 , 5 , -1 ) ) ) ; function countWaysUtil ( $ n , $ parts , $ nextPart ) { global $ dp ; if ( $ parts == 0 && $ n == 0 ) return 1 ; if ( $ n <= 0 $ parts <= 0 ) return 0 ; if ( $ dp [ $ n ] [ $ nextPart ] [ $ parts ] != -1 ) return $ dp [ $ n ] [ $ nextPart ] [ $ parts ] ; for ( $ i = $ nextPart ; $ i <= $ n ; $ i ++ ) $ ans += countWaysUtil ( $ n - $ i , $ parts - 1 , $ i ) ; return ( $ dp [ $ n ] [ $ nextPart ] [ $ parts ] = $ ans ) ; } function countWays ( $ n ) { return countWaysUtil ( $ n , 4 , 1 ) ; } $ n = 8 ; echo countWays ( $ n ) ; ? >


"Count number of ways to divide a number in 4 parts | Returns count of ways ; Initialize result ; Generate all possible quadruplet and increment counter when sum of a quadruplet is equal to n ; Driver Code"
How can the above be solved in PHP?

< ? php function countWays ( $ n ) { $ counter = 0 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) for ( $ j = $ i ; $ j < $ n ; $ j ++ ) for ( $ k = $ j ; $ k < $ n ; $ k ++ ) for ( $ l = $ k ; $ l < $ n ; $ l ++ ) if ( $ i + $ j + $ k + $ l == $ n ) $ counter ++ ; return $ counter ; } $ n = 8 ; echo countWays ( $ n ) ; ? >


"Count number of ways to fill a " n ▁ x ▁ 4" grid using "1 ▁ x ▁ 4" tiles | Returns count of count of ways to place 1 x 4 tiles on n x 4 grid . ; Create a table to store results of subproblems dp [ i ] stores count of ways for i x 4 grid . ; Fill the table from d [ 1 ] to dp [ n ] ; Base cases ; dp ( i - 1 ) : Place first tile horizontally dp ( n - 4 ) : Place first tile vertically which means 3 more tiles have to be placed vertically . ; Driver Code"
How can the above be solved in PHP?


"Count number of ways to jump to reach end | function to count ways to jump to reach end for each array element ; count_jump [ i ] store number of ways arr [ i ] can reach to the end ; Last element does not require to jump . Count ways to jump for remaining elements ; if the element can directly jump to the end ; add the count of all the elements that can reach to end and arr [ i ] can reach to them ; if element can reach to end then add its count to count_jump [ i ] ; if arr [ i ] cannot reach to the end ; print count_jump for each array element ; Driver Code"
How can the above be solved in PHP?

< ? php function countWaysToJump ( $ arr , $ n ) { $ count_jump ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ count_jump [ $ i ] = 0 ; for ( $ i = $ n - 2 ; $ i >= 0 ; $ i -- ) { if ( $ arr [ $ i ] >= $ n - $ i - 1 ) $ count_jump [ $ i ] ++ ; for ( $ j = $ i + 1 ; $ j < $ n - 1 && $ j <= $ arr [ $ i ] + $ i ; $ j ++ ) if ( $ count_jump [ $ j ] != -1 ) $ count_jump [ $ i ] += $ count_jump [ $ j ] ; if ( $ count_jump [ $ i ] == 0 ) $ count_jump [ $ i ] = -1 ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ count_jump [ $ i ] . " ▁ " ; } $ arr = array ( 1 , 3 , 5 , 8 , 9 , 1 , 0 , 7 , 6 , 8 , 9 ) ; $ n = count ( $ arr ) ; countWaysToJump ( $ arr , $ n ) ; ? >


"Count number of ways to partition a set into k subsets | Returns count of different partitions of n elements in k subsets ; Base cases ; S ( n + 1 , k ) = k * S ( n , k ) + S ( n , k - 1 ) ; Driver Code"
How can the above be solved in PHP?

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


"Count number of ways to partition a set into k subsets | Returns count of different partitions of n elements in k subsets ; Table to store results of subproblems ; Base cases ; Fill rest of the entries in dp [ ] [ ] in bottom up manner ; Driver Code"
How can the above be solved in PHP?


"Count number of ways to reach a given score in a Matrix | PHP implementation of the approach ; To store the states of dp ; To check whether a particular state of dp has been solved ; Function to find the ways using memoization ; Base cases ; If required score becomes negative ; If current state has been reached before ; Set current state to visited ; Driver code"
How can the above be solved in PHP?