"Number of digits to be removed to make a number divisible by 3 | function to count the no of removal of digits to make a very large number divisible by 3 ; add up all the digits of num ; if num is already is divisible by 3 then no digits are to be removed ; if there is single digit , then it is not possible to remove one digit . ; traverse through the number and find out if any number on removal makes the sum divisible by 3 ; if there are two numbers then it is not possible to remove two digits . ; Otherwise we can always make a number multiple of 2 by removing 2 digits . ; Driver Code"
How can the above be solved in PHP?

< ? php function divisible ( $ num ) { $ n = strlen ( $ num ) ; $ sum = ( $ num ) ; ( $ num ) ; 0 - '0' ; if ( $ sum % 3 == 0 ) return 0 ; if ( $ n == 1 ) return -1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ sum % 3 == ( $ num [ $ i ] - '0' ) % 3 ) return 1 ; if ( $ n == 2 ) return -1 ; return 2 ; } $ num = "1234" ; echo divisible ( $ num ) ; ? >


"Number of distinct integers obtained by lcm ( X , N ) / X | Function to count the number of distinct integers ontained by lcm ( x , num ) / x ; iterate to count the number of factors ; Driver Code"
How can the above be solved in PHP?

< ? php function numberOfDistinct ( $ n ) { $ ans = 0 ; for ( $ i = 1 ; $ i <= sqrt ( $ n ) ; $ i ++ ) { if ( $ n % $ i == 0 ) { $ ans ++ ; if ( ( $ n / $ i ) != $ i ) $ ans ++ ; } } return $ ans ; } $ n = 3 ; echo numberOfDistinct ( $ n ) ; ? >


"Number of distinct subsets of a set | Returns 2 ^ n ; Driver Code"
How can the above be solved in PHP?

< ? php function subsetCount ( $ arr , $ n ) { return 1 << $ n ; } $ A = array ( 1 , 2 , 3 ) ; $ n = sizeof ( $ A ) ; echo ( subsetCount ( $ A , $ n ) ) ; ? >


"Number of divisors of a given number N which are divisible by K | Function to count number of divisors of N which are divisible by K ; Variable to store count of divisors ; Traverse from 1 to n ; increase the count if both the conditions are satisfied ; Driver code"
How can the above be solved in PHP?

< ? php function countDivisors ( $ n , $ k ) { $ count = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { if ( $ n % $ i == 0 && $ i % $ k == 0 ) { $ count ++ ; } } return $ count ; } $ n = 12 ; $ k = 3 ; echo countDivisors ( $ n , $ k ) ;


"Number of elements that can be seen from right side | PHP program to find number of elements that can be seen from right side . ; Driver code"
How can the above be solved in PHP?

< ? php function numberOfElements ( $ height , $ n ) { $ max_so_far = 0 ; $ coun = 0 ; for ( $ i = $ n - 1 ; $ i >= 0 ; $ i -- ) { if ( $ height [ $ i ] > $ max_so_far ) { $ max_so_far = $ height [ $ i ] ; $ coun ++ ; } } return $ coun ; } $ n = 6 ; $ height = array ( 4 , 8 , 2 , 0 , 0 , 5 ) ; echo numberOfElements ( $ height , $ n ) ;


"Number of elements with even factors in the given range | Function to count the perfect squares ; Driver code"
How can the above be solved in PHP?

< ? php function countOddSquares ( $ n , $ m ) { return ( int ) pow ( $ m , 0.5 ) - ( int ) pow ( $ n - 1 , 0.5 ) ; } $ n = 5 ; $ m = 100 ; echo " Count ▁ is ▁ " , ( $ m - $ n + 1 ) - countOddSquares ( $ n , $ m ) ; ? >


"Number of elements with odd factors in given range | Function to count odd squares ; Driver code"
How can the above be solved in PHP?

< ? php function countOddSquares ( $ n , $ m ) { return pow ( $ m , 0.5 ) - pow ( $ n - 1 , 0.5 ) ; } $ n = 5 ; $ m = 100 ; echo " Count ▁ is ▁ " , countOddSquares ( $ n , $ m ) ; ? >


"Number of even substrings in a string of digits | Return the even number substrings . ; If current digit is even , add count of substrings ending with it . The count is ( i + 1 ) ; Driver Code"
How can the above be solved in PHP?

 < ? php function evenNumSubstring ( $ str ) { $ len = strlen ( $ str ) ; $ count = 0 ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { $ temp = $ str [ $ i ] - '0' ; if ( $ temp % 2 == 0 ) $ count += ( $ i + 1 ) ; } return $ count ; } $ str = "1234" ; echo evenNumSubstring ( $ str ) , "
 " ; ? >


"Number of flips to make binary string alternate | Set 1 | Utility method to flip a character ; Utility method to get minimum flips when alternate string starts with expected char ; if current character is not expected , increase flip count ; flip expected character each time ; method return minimum flip to make binary string alternate ; return minimum of following two 1 ) flips when alternate string starts with 0 2 ) flips when alternate string starts with 1 ; Driver code to test above method"
How can the above be solved in PHP?

< ? php function flip ( $ ch ) { return ( $ ch == '0' ) ? '1' : '0' ; } function getFlipWithStartingCharcter ( $ str , $ expected ) { $ flipCount = 0 ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) { if ( $ str [ $ i ] != $ expected ) $ flipCount ++ ; $ expected = flip ( $ expected ) ; } return $ flipCount ; } function minFlipToMakeStringAlternate ( $ str ) { return min ( getFlipWithStartingCharcter ( $ str , '0' ) , getFlipWithStartingCharcter ( $ str , '1' ) ) ; } $ str = "0001010111" ; echo minFlipToMakeStringAlternate ( $ str ) ; ? >


"Number of horizontal or vertical line segments to connect 3 points | Function to check if the third point forms a rectangle with other two points at corners ; Returns true if point k can be used as a joining point to connect using two line segments ; Check for the valid polyline with two segments ; Check whether the X - coordinates or Y - cocordinates are same . ; Iterate over all pairs to check for two line segments ; Otherwise answer is three . ; Driver code"
How can the above be solved in PHP?


"Number of indexes with equal elements in given range | PHP program to count the number of indexes in range L R such that Ai = Ai + 1 ; array to store count of index from 0 to i that obey condition ; precomputing prefixans [ ] array ; traverse to compute the prefixans [ ] array ; function that answers every query in O ( 1 ) ; Driver Code ; pre - computation ; 1 - st query ; 2 nd query"
How can the above be solved in PHP?

< ? php $ N = 1000 ; $ prefixans = array_fill ( 0 , $ N , 0 ) ; function countIndex ( $ a , $ n ) { global $ N , $ prefixans ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { if ( $ a [ $ i ] == $ a [ $ i + 1 ] ) $ prefixans [ $ i ] = 1 ; if ( $ i != 0 ) $ prefixans [ $ i ] += $ prefixans [ $ i - 1 ] ; } } function answer_query ( $ l , $ r ) { global $ N , $ prefixans ; if ( $ l == 0 ) return $ prefixans [ $ r - 1 ] ; else return ( $ prefixans [ $ r - 1 ] - $ prefixans [ $ l - 1 ] ) ; } $ a = array ( 1 , 2 , 2 , 2 , 3 , 3 , 4 , 4 , 4 ) ; $ n = count ( $ a ) ; countIndex ( $ a , $ n ) ; $ L = 1 ; $ R = 8 ; echo ( answer_query ( $ L , $ R ) . " " ) ; $ L = 0 ; $ R = 4 ; echo ( answer_query ( $ L , $ R ) . " " ) ; ? >


"Number of indexes with equal elements in given range | function that answers every query in O ( r - l ) ; traverse from l to r and count the required indexes ; Driver Code ; 1 - st query ; 2 nd query"
How can the above be solved in PHP?


"Number of integral solutions for equation x = b * ( sumofdigits ( x ) ^ a ) + c | This function returns the sum of the digits of a number ; This function creates the array of valid numbers ; this computes s ( x ) ^ a ; this gives the result of equation ; checking if the sum same as i ; counter to keep track of numbers ; resultant array ; prints the number ; Driver Code ; calculate which value of x are possible"
How can the above be solved in PHP?


"Number of leading zeros in binary representation of a given number | Function to count the no . of leading zeros ; Driver Code"
How can the above be solved in PHP?


"Number of local extrema in an array | function to find local extremum ; start loop from position 1 till n - 1 ; check if a [ i ] is greater than both its neighbours then add 1 to x ; check if a [ i ] is less than both its neighbours , then add 1 to x ; Driver Code"
How can the above be solved in PHP?


"Number of mismatching bits in the binary representation of two integers | compute number of different bits ; since , the numbers are less than 2 ^ 31 run the loop from '0' to '31' only ; right shift both the numbers by ' i ' and check if the bit at the 0 th position is different ; Driver code ; find number of different bits"
How can the above be solved in PHP?

< ? php function solve ( $ A , $ B ) { $ count = 0 ; for ( $ i = 0 ; $ i < 32 ; $ i ++ ) { if ( ( ( $ A >> $ i ) & 1 ) != ( ( $ B >> $ i ) & 1 ) ) { $ count ++ ; } } echo " Number ▁ of ▁ different ▁ bits ▁ : ▁ $ count " ; } $ A = 12 ; $ B = 15 ; solve ( $ A , $ B ) ; ? >


"Number of moves required to guess a permutation . | Function that returns the required moves ; Final move ; Driver Code"
How can the above be solved in PHP?

< ? php function countMoves ( $ n ) { $ ct = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ ct += $ i * ( $ n - $ i ) ; $ ct += $ n ; return $ ct ; } $ n = 3 ; echo countMoves ( $ n ) ; ? >


"Number of n digit stepping numbers | function that calculates the answer ; if n is 1 then answer will be 10. ; Compute values for count of digits more than 1. ; If ending digit is 0 ; If ending digit is 9 ; For other digits . ; stores the final answer ; Driver Code"
How can the above be solved in PHP?


"Number of n | Returns count of non - decreasing numbers with n digits . ; Initialization of all 0 - digit number ; Initialization of all i - digit non - decreasing number leading with 9 ; for all digits we should calculate number of ways depending upon leading digits ; Driver Code"
How can the above be solved in PHP?


"Number of non | Returns count of solutions of a + b + c = n ; Driver Code"
How can the above be solved in PHP?

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


"Number of non | Returns count of solutions of a + b + c = n ; Initialize result ; Consider all triplets and increment result whenever sum of a triplet is n . ; Driver Code"
How can the above be solved in PHP?

< ? php function countIntegralSolutions ( $ n ) { $ result = 0 ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) for ( $ j = 0 ; $ j <= $ n - $ i ; $ j ++ ) for ( $ k = 0 ; $ k <= ( $ n - $ i - $ j ) ; $ k ++ ) if ( $ i + $ j + $ k == $ n ) $ result ++ ; return $ result ; } $ n = 3 ; echo countIntegralSolutions ( $ n ) ; ? >


"Number of non | return number of non negative integral solutions ; initialize total = 0 ; Base Case if n = 1 and val >= 0 then it should return 1 ; iterate the loop till equal the val ; total solution of equations and again call the recursive function Solutions ( variable , value ) ; return the total no possible solution ; Driver Code"
How can the above be solved in PHP?

< ? php function countSolutions ( $ n , $ val ) { $ total = 0 ; if ( $ n == 1 && $ val >= 0 ) return 1 ; for ( $ i = 0 ; $ i <= $ val ; $ i ++ ) { $ total += countSolutions ( $ n - 1 , $ val - $ i ) ; } return $ total ; } $ n = 5 ; $ val = 20 ; echo countSolutions ( $ n , $ val ) ; ? >


"Number of occurrences of 2 as a digit in numbers from 0 to n | Counts the number of '2' digits in a single number ; Counts the number of '2' digits between 0 and n ; Initialize result ; Count 2 's in every number  from 2 to n ; Driver Code"
How can the above be solved in PHP?

 < ? php function number0f2s ( $ n ) { $ count = 0 ; while ( $ n > 0 ) { if ( $ n % 10 == 2 ) $ count ++ ; $ n = $ n / 10 ; } return $ count ; } function numberOf2sinRange ( $ n ) { $ count = 0 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ count += number0f2s ( $ i ) ; return $ count ; } echo ( numberOf2sinRange ( 22 ) ) ; echo "
 " ; echo numberOf2sinRange ( 100 ) ; ? >


"Number of occurrences of 2 as a digit in numbers from 0 to n | Counts the number of 2 s in a number at d - th digit ; if the digit in spot digit is ; Counts the number of '2' digits between 0 and n ; Convert integer to String to find its length ; Traverse every digit and count for every digit ; Driver Code"
How can the above be solved in PHP?

 < ? php function count2sinRangeAtDigit ( $ number , $ d ) { $ powerOf10 = ( int ) pow ( 10 , $ d ) ; $ nextPowerOf10 = $ powerOf10 * 10 ; $ right = $ number % $ powerOf10 ; $ roundDown = $ number - $ number % $ nextPowerOf10 ; $ roundup = $ roundDown + $ nextPowerOf10 ; $ digit = ( $ number / $ powerOf10 ) % 10 ; if ( $ digit < 2 ) return $ roundDown / 10 ; if ( $ digit == 2 ) return $ roundDown / 10 + $ right + 1 ; return $ roundup / 10 ; } function numberOf2sinRange ( $ number ) { $ s = strval ( $ number ) ; $ len = strlen ( $ s ) ; $ count = 0 ; for ( $ digit = 0 ; $ digit < $ len ; $ digit ++ ) $ count += count2sinRangeAtDigit ( $ number , $ digit ) ; return $ count ; } print ( numberOf2sinRange ( 22 ) . "
 " ) ; print ( numberOf2sinRange ( 100 ) . "
 " ) ; ? >


"Number of odd and even results for every value of x in range [ min , max ] after performing N steps | Function that prints the number of odd and even results ; If constant at layer i is even , beven is true , otherwise false . If the coefficient of x at layer i is even , aeven is true , otherwise false . ; If any of the coefficients at any layer is found to be even , then the product of all the coefficients will always be even . ; Checking whether the constant added after all layers is even or odd . ; Assuming input x is even . ; Assuming input x is odd . ; Displaying the counts . ; Driver Code"
How can the above be solved in PHP?

 < ? php function count_even_odd ( $ min , $ max , $ steps ) { $ beven = true ; $ aeven = false ; $ n = 2 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ a = $ steps [ $ i ] [ 0 ] ; $ b = $ steps [ $ i ] [ 1 ] ; if ( ! ( $ aeven $ a & 1 ) ) $ aeven = true ; if ( $ beven ) { if ( $ b & 1 ) $ beven = false ; } else if ( ! ( $ a & 1 ) ) { if ( ! ( $ b & 1 ) ) $ beven = true ; } else { if ( $ b & 1 ) $ beven = true ; } } if ( $ beven ) { $ even = ( int ) $ max / 2 - ( int ) ( $ min - 1 ) / 2 ; $ odd = 0 ; } else { $ even = ( int ) $ max / 2 - ( int ) ( $ min - 1 ) / 2 ; $ odd = 0 ; } if ( ! ( $ beven ^ $ aeven ) ) $ even += $ max - $ min + 1 - ( int ) $ max / 2 + ( int ) ( $ min - 1 ) / 2 ; else $ odd += $ max - $ min + 1 - ( int ) $ max / 2 + ( int ) ( $ min - 1 ) / 2 ; echo " even = " ▁ , ▁ $ even ,
 	 	 " , odd = " , ▁ $ odd , ▁ " " } $ min = 1 ; $ max = 4 ; $ steps = array ( array ( 1 , 2 ) , array ( 3 , 4 ) ) ; count_even_odd ( $ min , $ max , $ steps ) ; ? >


"Number of ones in the smallest repunit | Function to find number of 1 s in smallest repunit multiple of the number ; to store number of 1 s in smallest repunit multiple of the number . ; initialize rem with 1 ; run loop until rem becomes zero ; rem * 10 + 1 here represents the repunit modulo n ; when remainder becomes 0 return count ; Driver Code ; Calling function"
How can the above be solved in PHP?

< ? php function countOnes ( $ n ) { $ count = 1 ; $ rem = 1 ; while ( $ rem != 0 ) { $ rem = ( $ rem * 10 + 1 ) % $ n ; $ count ++ ; } return $ count ; } $ n = 13 ; echo countOnes ( $ n ) ; ? >


"Number of ordered pairs such that ( Ai & Aj ) = 0 | Naive function to count the number of ordered pairs such that their bitwise and is 0 ; check for all possible pairs ; add 2 as ( i , j ) and ( j , i ) are considered different ; Driver Code"
How can the above be solved in PHP?

< ? php function countPairs ( $ a , $ n ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) if ( ( $ a [ $ i ] & $ a [ $ j ] ) == 0 ) $ count += 2 ; } return $ count ; } { $ a = array ( 3 , 4 , 2 ) ; $ n = sizeof ( $ a ) / sizeof ( $ a [ 0 ] ) ; echo countPairs ( $ a , $ n ) ; return 0 ; }


"Number of ordered points pair satisfying line equation | Checks if ( i , j ) is valid , a point ( i , j ) is valid if point ( arr [ i ] , arr [ j ] ) satisfies the equation y = mx + c And i is not equal to j ; check if i equals to j ; Equation LHS = y , and RHS = mx + c ; Returns the number of ordered pairs ( i , j ) for which point ( arr [ i ] , arr [ j ] ) satisfies the equation of the line y = mx + c ; for every possible ( i , j ) check if ( a [ i ] , a [ j ] ) satisfies the equation y = mx + c ; ( firstIndex , secondIndex ) is same as ( i , j ) ; check if ( firstIndex , secondIndex ) is a valid point ; Driver Code ; equation of line is y = mx + c"
How can the above be solved in PHP?

< ? php function isValid ( $ arr , $ i , $ j , $ m , $ c ) { if ( $ i == $ j ) return false ; $ lhs = $ arr [ $ j ] ; $ rhs = $ m * $ arr [ $ i ] + $ c ; return ( $ lhs == $ rhs ) ; } function findOrderedPoints ( $ arr , $ n , $ m , $ c ) { $ counter = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { $ firstIndex = $ i ; $ secondIndex = $ j ; if ( isValid ( $ arr , $ firstIndex , $ secondIndex , $ m , $ c ) ) $ counter ++ ; } } return $ counter ; } $ arr = array ( 1 , 2 , 3 , 4 , 2 ) ; $ n = count ( $ arr ) ; $ m = 1 ; $ c = 1 ; echo ( findOrderedPoints ( $ arr , $ n , $ m , $ c ) ) ; ? >


"Number of pairs from the first N natural numbers whose sum is divisible by K | Function to find the number of pairs from the set of natural numbers up to N whose sum is divisible by K ; Declaring a Hash to store count ; Storing the count of integers with a specific remainder in Hash array ; Check if K is even ; Count of pairs when both integers are divisible by K ; Count of pairs when one remainder is R and other remainder is K - R ; Count of pairs when both the remainders are K / 2 ; Count of pairs when both integers are divisible by K ; Count of pairs when one remainder is R and other remainder is K - R ; Driver code ; Print the count of pairs"
How can the above be solved in PHP?


"Number of pairs with Bitwise OR as Odd number | Function to count pairs with odd OR ; Count total even numbers in array ; Even pair count ; Total pairs ; Return Odd pair count ; Driver main"
How can the above be solved in PHP?

 < ? php function countOddPair ( $ A , $ N ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) if ( ! ( $ A [ $ i ] & 1 ) ) $ count ++ ; $ evenPairCount = $ count * ( $ count - 1 ) / 2 ; $ totPairs = $ N * ( $ N - 1 ) / 2 ; return ( $ totPairs - $ evenPairCount ) ; } $ A = array ( 5 , 6 , 2 , 8 ) ; $ N = sizeof ( $ A ) ; echo countOddPair ( $ A , $ N ) , "
 " ; ? >


"Number of pairs with Bitwise OR as Odd number | Function to count pairs with odd OR ; find OR operation check odd or odd ; return count of odd pair ; Driver Code"
How can the above be solved in PHP?

 < ? php function findOddPair ( $ A , $ N ) { $ oddPair = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { for ( $ j = $ i + 1 ; $ j < $ N ; $ j ++ ) { if ( ( $ A [ $ i ] $ A [ $ j ] ) % 2 != 0 ) $ oddPair ++ ; } } return $ oddPair ; } $ A = array ( 5 , 6 , 2 , 8 ) ; $ N = sizeof ( $ A ) / sizeof ( $ A [ 0 ] ) ; echo findOddPair ( $ A , $ N ) , "
 " ; #This  code is contributed by ajit
? >


"Number of pairs with Pandigital Concatenation | Checks if a given $is Pandigital ; digit i is not present thus not pandigital ; Returns number of pairs of strings resulting in Pandigital Concatenations ; iterate over all pair of strings ; Driver code"
How can the above be solved in PHP?

< ? php function isPanDigital ( $ s ) { $ digits = array ( ) ; $ digits = array_fill ( 0 , 10 , false ) ; for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) $ digits [ ord ( $ s [ $ i ] ) - ord ( '0' ) ] = true ; for ( $ i = 0 ; $ i <= 9 ; $ i ++ ) if ( $ digits [ $ i ] == false ) return false ; return true ; } function countPandigitalPairs ( & $ v ) { $ pairs = 0 ; for ( $ i = 0 ; $ i < count ( $ v ) ; $ i ++ ) { for ( $ j = $ i + 1 ; $ j < count ( $ v ) ; $ j ++ ) { if ( isPanDigital ( $ v [ $ i ] . $ v [ $ j ] ) ) { $ pairs ++ ; } } } return $ pairs ; } $ v = array ( "123567" , "098234" , "14765" , "19804" ) ; echo ( countPandigitalPairs ( $ v ) ) ; ? >


"Number of pairs with maximum sum | function to find the number of maximum pair sum ; traverse through all the pairs ; traverse through all pairs and keep a count of the number of maximum pairs ; Driver Code"
How can the above be solved in PHP?


"Number of pairs with maximum sum | function to find the number of maximum pair sums ; Find maximum and second maximum elements . Also find their counts . ; If maximum element appears more than once . ; If maximum element appears only once . ; Driver Code"
How can the above be solved in PHP?

< ? php function sum ( $ a , $ n ) { $ maxVal = $ a [ 0 ] ; $ maxCount = 1 ; $ secondMax = PHP_INT_MIN ; $ secondMaxCount ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] == $ maxVal ) $ maxCount ++ ; else if ( $ a [ $ i ] > $ maxVal ) { $ secondMax = $ maxVal ; $ secondMaxCount = $ maxCount ; $ maxVal = $ a [ $ i ] ; $ maxCount = 1 ; } else if ( $ a [ $ i ] == $ secondMax ) { $ secondMax = $ a [ $ i ] ; $ secondMaxCount ++ ; } else if ( $ a [ $ i ] > $ secondMax ) { $ secondMax = $ a [ $ i ] ; $ secondMaxCount = 1 ; } } if ( $ maxCount > 1 ) return $ maxCount * ( $ maxCount - 1 ) / 2 ; return $ secondMaxCount ; } $ array = array ( 1 , 1 , 1 , 2 , 2 , 2 , 3 ) ; $ n = count ( $ array ) ; echo sum ( $ array , $ n ) ; ? >


"Number of palindromic permutations | Set 1 | PHP program to find number of palindromic permutations of a given string ; Returns factorial of n ; Returns count of palindromic permutations of str . ; Count frequencies of all characters ; Since half of the characters decide count of palindromic permutations , we take ( n / 2 ) ! ; To make sure that there is at most one odd occurring char ; Traverse through all counts ; To make sure that the string can permute to form a palindrome ; If there are more than one odd occurring chars ; Divide all permutations with repeated characters ; Driver code"
How can the above be solved in PHP?

< ? php $ MAX = 256 ; function fact ( $ n ) { $ res = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ res = $ res * $ i ; return $ res ; } function countPalinPermutations ( & $ str ) { global $ MAX ; $ n = strlen ( $ str ) ; $ freq = ( 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ res = fact ( $ n / 2 ) ; $ oddFreq = false ; for ( $ i = 0 ; $ i < $ MAX ; $ i ++ ) { $ half = $ freq [ $ i ] / 2 ; if ( $ freq [ $ i ] % 2 != 0 ) { if ( $ oddFreq == true ) return 0 ; $ oddFreq = true ; } $ res = $ res / fact ( $ half ) ; } return $ res ; } $ str = " gffg " ; echo countPalinPermutations ( $ str ) ; ? >


"Number of palindromic subsequences of length k where k <= 3 | PHP program to count number of subsequences of given length . ; Precompute the prefix and suffix array . ; Precompute the prefix 2D array ; Precompute the Suffix 2D array . ; Find the number of palindromic subsequence of length k ; If k is 1. ; If k is 2 ; Adding all the products of prefix array ; For k greater than 2. Adding all the products of value of prefix and suffix array . ; Driver Code"
How can the above be solved in PHP?


"Number of perfect squares between two given numbers | Method to count square between a and b ; Driver code"
How can the above be solved in PHP?

< ? php function countSquares ( $ a , $ b ) { return ( floor ( sqrt ( $ b ) ) - ceil ( sqrt ( $ a ) ) + 1 ) ; } { $ a = 9 ; $ b = 25 ; echo " Count ▁ of ▁ squares ▁ is ▁ " , countSquares ( $ a , $ b ) ; return 0 ; } ? >


"Number of permutations of a string in which all the occurrences of a given character occurs together | Function to return factorial of the number passed as argument ; Function to get the total permutations which satisfy the given condition ; Create has to store count of each character ; Store character occurrences ; Count number of times Particular character comes ; If particular character isn 't  present in the string then return 0 ; Remove count of particular character ; Total length of the string ; Assume all occurrences of particular character as a single character . ; Compute factorial of the length ; Divide by the factorials of the no . of occurrences of all the characters . ; return the result ; Driver Code ; Assuming the string and the character are all in uppercase"
How can the above be solved in PHP?

 < ? php function fact ( $ n ) { $ result = 1 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ result *= $ i ; return $ result ; } function getResult ( $ str , $ ch ) { $ has = array_fill ( 0 , 26 , NULL ) ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) $ has [ ord ( $ str [ $ i ] ) - ord ( ' A ' ) ] ++ ; $ particular = $ has [ ord ( $ ch ) - ord ( ' A ' ) ] ; if ( $ particular == 0 ) return 0 ; $ has [ ord ( $ ch ) - ord ( ' A ' ) ] = 0 ; $ total = strlen ( $ str ) ; $ total = $ total - $ particular + 1 ; $ result = fact ( $ total ) ; for ( $ i = 0 ; $ i < 26 ; $ i ++ ) { if ( $ has [ $ i ] > 1 ) { $ result = $ result / fact ( $ has [ $ i ] ) ; } } return $ result ; } $ str = " MISSISSIPPI " ; echo getResult ( $ str , ' S ' ) . "
 " ; ? >


"Number of positions such that adding K to the element is greater than sum of all other elements | Function that will find out the valid position ; find sum of all the elements ; adding K to the element and check whether it is greater than sum of all other elements ; Driver code"
How can the above be solved in PHP?

< ? php function validPosition ( $ arr , $ N , $ K ) { $ count = 0 ; $ sum = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { $ sum += $ arr [ $ i ] ; } for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { if ( ( $ arr [ $ i ] + $ K ) > ( $ sum - $ arr [ $ i ] ) ) $ count ++ ; } return $ count ; } $ arr = array ( 2 , 1 , 6 , 7 ) ; $ K = 4 ; $ N = count ( $ arr ) ; echo validPosition ( $ arr , $ N , $ K ) ; ? >


"Number of positions where a letter can be inserted such that a string becomes palindrome | Function to check if the string is palindrome ; to know the length of string ; if the given string is a palindrome ( Case - I ) ; Sub - case - III ) ; if the length is even ; sub - case - I ; sub - case - II ; insertion point ; Case - I ; Case - II ; Driver code"
How can the above be solved in PHP?

< ? php function isPalindrome ( $ s , $ i , $ j ) { $ p = $ j ; for ( $ k = $ i ; $ k <= $ p ; $ k ++ ) { if ( $ s [ $ k ] != $ s [ $ p ] ) return false ; $ p -- ; } return true ; } function countWays ( $ s ) { $ n = strlen ( $ s ) ; $ count = 0 ; if ( isPalindrome ( $ s , 0 , $ n - 1 ) ) { for ( $ i = $ n / 2 ; $ i < $ n ; $ i ++ ) { if ( $ s [ $ i ] == $ s [ $ i + 1 ] ) $ count ++ ; else break ; } if ( $ n % 2 == 0 ) { $ count ++ ; $ count = 2 * $ count + 1 ; } else $ count = 2 * $ count + 2 ; } else { for ( $ i = 0 ; $ i < $ n / 2 ; $ i ++ ) { if ( $ s [ $ i ] != $ s [ $ n - 1 - $ i ] ) { $ j = $ n - 1 - $ i ; if ( isPalindrome ( $ s , $ i , $ n - 2 - $ i ) ) { for ( $ k = $ i - 1 ; $ k >= 0 ; $ k -- ) { if ( $ s [ $ k ] != $ s [ $ j ] ) break ; $ count ++ ; } $ count ++ ; } if ( isPalindrome ( $ s , $ i + 1 , $ n - 1 - $ i ) ) { for ( $ k = $ n - $ i ; $ k < $ n ; $ k ++ ) { if ( $ s [ $ k ] != $ s [ $ i ] ) break ; $ count ++ ; } $ count ++ ; } break ; } } } return $ count ; } $ s = " abca " ; echo countWays ( $ s ) ; ? >


"Number of rectangles in N * M grid | PHP program to count number of rectangles in a n x m grid ; Driver Code"
How can the above be solved in PHP?

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


"Number of rectangles in a circle of radius R | Function to return the total possible rectangles that can be cut from the circle ; Diameter = 2 * $Radius ; Square of diameter which is the square of the maximum length diagonal ; generate all combinations of a and b in the range ( 1 , ( 2 * Radius - 1 ) ) ( Both inclusive ) ; Calculate the Diagonal length of this rectangle ; If this rectangle 's Diagonal  Length is less than the  Diameter, it is a valid  rectangle, thus increment counter ; Radius of the circle"
How can the above be solved in PHP?

< ? php function countRectangles ( $ radius ) { $ rectangles = 0 ; $ diameter = 2 * $ radius ; $ diameterSquare = $ diameter * $ diameter ; for ( $ a = 1 ; $ a < 2 * $ radius ; $ a ++ ) { for ( $ b = 1 ; $ b < 2 * $ radius ; $ b ++ ) { $ diagonalLengthSquare = ( $ a * $ a + $ b * $ b ) ; if ( $ diagonalLengthSquare <= $ diameterSquare ) { $ rectangles ++ ; } } } return $ rectangles ; } $ radius = 2 ; $ totalRectangles ; $ totalRectangles = countRectangles ( $ radius ) ; echo $ totalRectangles , " ▁ rectangles ▁ can ▁ be ▁ " , " cut ▁ from ▁ a ▁ circle ▁ of ▁ Radius ▁ " , $ radius ; ? >


"Number of segments where all elements are greater than X | Function to count number of segments ; Iterate in the array ; check if array element greater then X or not ; if flag is true ; After iteration complete check for the last segment ; Driver Code"
How can the above be solved in PHP?

< ? php function countSegments ( $ a , $ n , $ x ) { $ flag = false ; $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] > $ x ) { $ flag = true ; } else { if ( $ flag ) $ count += 1 ; $ flag = false ; } } if ( $ flag ) $ count += 1 ; return $ count ; } $ a = array ( 8 , 25 , 10 , 19 , 19 , 18 , 20 , 11 , 18 ) ; $ n = sizeof ( $ a ) ; $ x = 13 ; echo countSegments ( $ a , $ n , $ x ) ; ? >


"Number of sequences which has HEAD at alternate positions to the right of the first HEAD | function to calculate total sequences possible ; Value of N is even ; Value of N is odd ; Driver code"
How can the above be solved in PHP?

< ? php function findAllSequence ( $ N ) { if ( $ N % 2 == 0 ) { return pow ( 2 , $ N / 2 + 1 ) + pow ( 2 , $ N / 2 ) - 2 ; } else { return pow ( 2 , ( $ N + 1 ) / 2 ) + pow ( 2 , ( $ N + 1 ) / 2 ) - 2 ; } } $ N = 2 ; echo findAllSequence ( $ N ) ; ? >


"Number of solutions for the equation x + y + z <= n | function to find the number of solutions for the equation x + y + z <= n , such that 0 <= x <= X , 0 <= y <= Y , 0 <= z <= Z . ; to store answer ; for values of x ; for values of y ; maximum possible value of z ; if z value greater than equals to 0 then only it is valid ; find minimum of temp and z ; return required answer ; Driver code"
How can the above be solved in PHP?

< ? php function NumberOfSolutions ( $ x , $ y , $ z , $ n ) { $ ans = 0 ; for ( $ i = 0 ; $ i <= $ x ; $ i ++ ) { for ( $ j = 0 ; $ j <= $ y ; $ j ++ ) { $ temp = $ n - $ i - $ j ; if ( $ temp >= 0 ) { $ temp = min ( $ temp , $ z ) ; $ ans += $ temp + 1 ; } } } return $ ans ; } $ x = 1 ; $ y = 2 ; $ z = 3 ; $ n = 4 ; echo NumberOfSolutions ( $ x , $ y , $ z , $ n ) ; ? >


"Number of solutions for x < y , where a <= x <= b and c <= y <= d and x , y are integers | function to Find the number of solutions for x < y , where a <= x <= b and c <= y <= d and x , y integers . ; to store answer ; iterate explicitly over all possible values of x ; return answer ; Driver code ; function call"
How can the above be solved in PHP?

< ? php function NumberOfSolutions ( $ a , $ b , $ c , $ d ) { $ ans = 0 ; for ( $ i = $ a ; $ i <= $ b ; $ i ++ ) if ( $ d >= max ( $ c , $ i + 1 ) ) $ ans += $ d - max ( $ c , $ i + 1 ) + 1 ; return $ ans ; } $ a = 2 ; $ b = 3 ; $ c = 3 ; $ d = 4 ; echo NumberOfSolutions ( $ a , $ b , $ c , $ d ) ; ? >


"Number of solutions of n = x + n âŠ • x | Function to find the number of solutions of n = n xor x ; Counter to store the number of solutions found ; Driver code"
How can the above be solved in PHP?

< ? php function numberOfSolutions ( $ n ) { $ c = 0 ; for ( $ x = 0 ; $ x <= $ n ; ++ $ x ) if ( $ n == $ x + $ n ^ $ x ) ++ $ c ; return $ c ; } $ n = 3 ; echo numberOfSolutions ( $ n ) ;


"Number of solutions to Modular Equations | Returns the number of divisors of ( A - B ) greater than B ; if N is divisible by i ; count only the divisors greater than B ; checking if a divisor isnt counted twice ; Utility function to calculate number of all possible values of X for which the modular equation holds true ; if A = B there are infinitely many solutions to equation or we say X can take infinitely many values > A . We return - 1 in this case ; if A < B , there are no possible values of X satisfying the equation ; the last case is when A > B , here we calculate the number of divisors of ( A - B ) , which are greater than B ; Wrapper function for numberOfPossibleWaysUtil ( ) ; if infinitely many solutions available ; Driver code"
How can the above be solved in PHP?

 < ? php function calculateDivisors ( $ A , $ B ) { $ N = ( $ A - $ B ) ; $ noOfDivisors = 0 ; for ( $ i = 1 ; $ i <= sqrt ( $ N ) ; $ i ++ ) { if ( ( $ N % $ i ) == 0 ) { if ( $ i > $ B ) $ noOfDivisors ++ ; if ( ( $ N / $ i ) != $ i && ( $ N / $ i ) > $ B ) $ noOfDivisors ++ ; } } return $ noOfDivisors ; } function numberOfPossibleWaysUtil ( $ A , $ B ) { if ( $ A == $ B ) return -1 ; if ( $ A < $ B ) return 0 ; $ noOfDivisors = 0 ; $ noOfDivisors = calculateDivisors ( $ A , $ B ) ; return $ noOfDivisors ; } function numberOfPossibleWays ( $ A , $ B ) { $ noOfSolutions = numberOfPossibleWaysUtil ( $ A , $ B ) ; if ( $ noOfSolutions == -1 ) { echo " For A = " ▁ , ▁ $ A , ▁ " and B = " ▁ , $ B ,
 	 	 	 " X can take Infinitely many values greater than " ▁ , ▁ $ A ▁ , ▁ " " ;
 	 }
 	 else ▁ {
 	 	 echo ▁ " For A = " , ▁ $ A ▁ , ▁ " and B = " ▁ , $ B ,
 	 	 	 " X can take " , $ noOfSolutions ,
 	 	 	 " values " } } $ A = 26 ; $ B = 2 ; numberOfPossibleWays ( $ A , $ B ) ; $ A = 21 ; $ B = 5 ; numberOfPossibleWays ( $ A , $ B ) ;


"Number of squares of side length required to cover an N * M rectangle | function to find number of squares of a * a required to cover n * m rectangle ; Driver code ; function call"
How can the above be solved in PHP?


"Number of steps required to reach point ( x , y ) from ( 0 , 0 ) using zig | Function to return the required position ; Driver Code"
How can the above be solved in PHP?


"Number of steps to convert to prime factors | PHP program to count number of steps required to convert an integer array to array of factors . ; array to store prime factors ; function to generate all prime factors of numbers from 1 to 10 ^ 6 ; Initializes all the positions with their value . ; Initializes all multiples of 2 with 2 ; A modified version of Sieve of Eratosthenes to store the smallest prime factor that divides every number . ; check if it has no prime factor . ; Initializes of j starting from i * i ; if it has no prime factor before , then stores the smallest prime divisor ; function to calculate the number of representations ; keep an count of prime factors ; traverse for every element ; count the no of factors ; subtract 1 if Ai is not 1 as the last step wont be taken into count ; call sieve to calculate the factors"
How can the above be solved in PHP?

< ? php $ MAX = 100001 ; $ factor = array_fill ( 0 , $ MAX + 1 , 0 ) ; function cal_factor ( ) { global $ factor , $ MAX ; $ factor [ 1 ] = 1 ; for ( $ i = 2 ; $ i < $ MAX ; $ i ++ ) $ factor [ $ i ] = $ i ; for ( $ i = 4 ; $ i < $ MAX ; $ i += 2 ) $ factor [ $ i ] = 2 ; for ( $ i = 3 ; $ i * $ i < $ MAX ; $ i ++ ) { if ( $ factor [ $ i ] == $ i ) { for ( $ j = $ i * $ i ; $ j < $ MAX ; $ j += $ i ) { if ( $ factor [ $ j ] == $ j ) $ factor [ $ j ] = $ i ; } } } } function no_of_representations ( $ a , $ n ) { global $ factor , $ MAX ; $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ temp = $ a [ $ i ] ; $ flag = 0 ; while ( $ factor [ $ temp ] != 1 ) { $ flag = -1 ; $ count ++ ; $ temp = ( int ) ( $ temp / $ factor [ $ temp ] ) ; } $ count += $ flag ; } return $ count ; } cal_factor ( ) ; $ a = array ( 4 , 4 , 4 ) ; $ n = count ( $ a ) ; echo no_of_representations ( $ a , $ n ) ; ? >


"Number of strings of length N with no palindromic sub string | Return the count of strings with no palindromic substring . ; Driver Code"
How can the above be solved in PHP?

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


"Number of sub arrays with odd sum | PHP code to find count of sub - arrays with odd sum ; Find sum of all subarrays and increment result if sum is odd ; Driver code"
How can the above be solved in PHP?

< ? php function countOddSum ( & $ ar , $ n ) { $ result = 0 ; for ( $ i = 0 ; $ i <= $ n - 1 ; $ i ++ ) { $ val = 0 ; for ( $ j = $ i ; $ j <= $ n - 1 ; $ j ++ ) { $ val = $ val + $ ar [ $ j ] ; if ( $ val % 2 != 0 ) $ result ++ ; } } return ( $ result ) ; } $ ar = array ( 5 , 4 , 4 , 5 , 1 , 3 ) ; $ n = sizeof ( $ ar ) ; echo " The ▁ Number ▁ of ▁ Subarrays ▁ with ▁ odd ▁ " ; echo " sum ▁ is ▁ " . countOddSum ( $ ar , $ n ) ; ? >


"Number of sub arrays with odd sum | PHP proggram to find count of sub - arrays with odd sum ; A temporary array of size 2. temp [ 0 ] is going to store count of even subarrays and temp [ 1 ] count of odd . temp [ 0 ] is initialized as 1 because there is a single odd element is also counted as a subarray ; Initialize count . sum is sum of elements under modulo 2 and ending with arr [ i ] . ; i ' th ▁ iteration ▁ computes ▁ sum ▁ ▁ of ▁ arr [ 0 . . i ] ▁ under ▁ modulo ▁ 2 ▁ ▁ and ▁ increments ▁ even / odd ▁ count ▁ ▁ according ▁ to ▁ sum ' s value ; 2 is added to handle negative numbers ; Increment even / odd count ; An odd can be formed by even - odd pair ; Driver code"
How can the above be solved in PHP?

< ? php function countOddSum ( $ ar , $ n ) { $ temp = array ( 1 , 0 ) ; $ result = 0 ; $ val = 0 ; for ( $ i = 0 ; $ i <= $ n - 1 ; $ i ++ ) { $ val = ( ( $ val + $ ar [ $ i ] ) % 2 + 2 ) % 2 ; $ temp [ $ val ] ++ ; } $ result = ( $ temp [ 0 ] * $ temp [ 1 ] ) ; return ( $ result ) ; } $ ar = array ( 5 , 4 , 4 , 5 , 1 , 3 ) ; $ n = sizeof ( $ ar ) ; echo " The ▁ Number ▁ of ▁ Subarrays ▁ with ▁ odd " . " ▁ sum ▁ is ▁ " . countOddSum ( $ ar , $ n ) ; ? >


"Number of sub | Function to return the count of required sub - strings ; Left and right counters for characters on both sides of sub - string window ; Left and right pointer on both sides of sub - string window ; Initialize the frequency ; Result and length of string ; Initialize the left pointer ; Initialize the right pointer ; Traverse all the window sub - strings ; Counting the characters on left side of the sub - string window ; Counting the characters on right side of the sub - string window ; Add the possible sub - strings on both sides to result ; Setting the frequency for next sub - string window ; Reset the left and right counters ; Driver code"
How can the above be solved in PHP?

 < ? php function countSubString ( $ s , $ c , $ k ) { $ leftCount = 0 ; $ rightCount = 0 ; $ left = 0 ; $ right = 0 ; $ freq = 0 ; $ result = 0 ; $ len = strlen ( $ s ) ; while ( $ s [ $ left ] != $ c && $ left < $ len ) { $ left ++ ; $ leftCount ++ ; } $ right = $ left + 1 ; while ( $ freq != ( $ k - 1 ) && ( $ right - 1 ) < $ len ) { if ( $ s [ $ right ] == $ c ) $ freq ++ ; $ right ++ ; } while ( $ left < $ len && ( $ right - 1 ) < $ len ) { while ( $ s [ $ left ] != $ c && $ left < $ len ) { $ left ++ ; $ leftCount ++ ; } while ( $ right < $ len && $ s [ $ right ] != $ c ) { if ( $ s [ $ right ] == $ c ) $ freq ++ ; $ right ++ ; $ rightCount ++ ; } $ result = $ result + ( $ leftCount + 1 ) * ( $ rightCount + 1 ) ; $ freq = $ k - 1 ; $ leftCount = 0 ; $ rightCount = 0 ; $ left ++ ; $ right ++ ; } return $ result ; } $ s = " abada " ; $ c = ' a ' ; $ k = 2 ; echo countSubString ( $ s , $ c , $ k ) , "
 " ; ? >


"Number of subarrays have bitwise OR >= K | Function to return the count of required sub - arrays ; Traverse sub - array [ i . . j ] ; Driver code"
How can the above be solved in PHP?

< ? php function countSubArrays ( $ arr , $ n , $ K ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { $ bitwise_or = 0 ; for ( $ k = $ i ; $ k < $ j + 1 ; $ k ++ ) $ bitwise_or = $ bitwise_or | $ arr [ $ k ] ; if ( $ bitwise_or >= $ K ) $ count += 1 ; } } return $ count ; } $ arr = array ( 3 , 4 , 5 ) ; $ n = count ( $ arr ) ; $ k = 6 ; print ( countSubArrays ( $ arr , $ n , $ k ) ) ; ? >


"Number of subarrays having product less than K | PHP program to count subarrays having product less than k . ; Counter for single element ; Multiple subarray ; If this multiple is less than k , then increment ; Driver Code"
How can the above be solved in PHP?

< ? php function countsubarray ( $ array , $ n , $ k ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ array [ $ i ] < $ k ) $ count ++ ; $ mul = $ array [ $ i ] ; for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) { $ mul = $ mul * $ array [ $ j ] ; if ( $ mul < $ k ) $ count ++ ; else break ; } } return $ count ; } $ array = array ( 1 , 2 , 3 , 4 ) ; $ k = 10 ; $ size = sizeof ( $ array ) ; $ count = countsubarray ( $ array , $ size , $ k ) ; echo ( $ count . " " ) ; ? >


"Number of subarrays whose minimum and maximum are same | calculate the no of contiguous subarrays which has same minimum and maximum ; stores the answer ; loop to traverse from 0 - n ; start checking subarray from next element ; traverse for finding subarrays ; if the elements are same then we check further and keep a count of same numbers in ' r ' ; the no of elements in between r and i with same elements . ; the no of subarrays that can be formed between i and r ; again start checking from the next index ; returns answer ; Driver Code"
How can the above be solved in PHP?


"Number of subsequences as " ab " in a string repeated K times | PHP code to find number of subsequences of " ab " in the string S which is repeated K times . ; Count of ' a ' s ; Count of ' b ' s ; occurrence of " ab " s in string S ; Add following two : 1 ) K * ( Occurrences of " ab " in single string ) 2 ) a is from one string and b is from other . ; Driver code"
How can the above be solved in PHP?


"Number of subsequences of the form a ^ i b ^ j c ^ k | Returns count of subsequences of the form a ^ i b ^ j c ^ k ; Initialize counts of different subsequences caused by different combination of ' a ' ; Initialize counts of different subsequences caused by different combination of ' a ' and different combination of ' b ' ; Initialize counts of different subsequences caused by different combination of ' a ' , ' b ' and ' c ' . ; Traverse all characters of given string ; If current character is ' a ' , then there are following possibilities : a ) Current character begins a new subsequence . b ) Current character is part of aCount subsequences . c ) Current character is not part of aCount subsequences . ; If current character is ' b ' , then there are following possibilities : a ) Current character begins a new subsequence of b 's with aCount subsequences. b) Current character is part of bCount subsequences. c) Current character is not part of  bCount subsequences.  ; If current character is ' c ' , then there are following possibilities : a ) Current character begins a new subsequence of c 's with bCount subsequences. b) Current character is part of cCount subsequences. c) Current character is not part of  cCount subsequences.  ; Driver Code"
How can the above be solved in PHP?

< ? php function countSubsequences ( $ s ) { $ aCount = 0 ; $ bCount = 0 ; $ cCount = 0 ; for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) { if ( $ s [ $ i ] == ' a ' ) $ aCount = ( 1 + 2 * $ aCount ) ; else if ( $ s [ $ i ] == ' b ' ) $ bCount = ( $ aCount + 2 * $ bCount ) ; else if ( $ s [ $ i ] == ' c ' ) $ cCount = ( $ bCount + 2 * $ cCount ) ; } return $ cCount ; } $ s = " abbc " ; echo countSubsequences ( $ s ) ; ? >


"Number of substrings of a string | PHP program to count number of substrings of a string ; Driver Code"
How can the above be solved in PHP?

< ? php function countNonEmptySubstr ( $ str ) { $ n = strlen ( $ str ) ; return $ n * ( $ n + 1 ) / 2 ; } $ s = " abcde " ; echo countNonEmptySubstr ( $ s ) ; ? >


"Number of substrings of one string present in other | PHP program to count number of substrings of s1 present in s2 . ; s3 stores all substrings of s1 ; check the presence of s3 in s2 ; Driver code"
How can the above be solved in PHP?

< ? php function countSubstrs ( $ s1 , $ s2 ) { $ ans = 0 ; for ( $ i = 0 ; $ i < strlen ( $ s1 ) ; $ i ++ ) { $ s3 = " " ; for ( $ j = $ i ; $ j < strlen ( $ s1 ) ; $ j ++ ) { $ s3 += $ s1 [ $ j ] ; if ( stripos ( $ s2 , $ s3 , 0 ) != -1 ) $ ans ++ ; } } return $ ans ; } $ s1 = " aab " ; $ s2 = " aaaab " ; echo countSubstrs ( $ s1 , $ s2 ) ; ? >


"Number of substrings with count of each character as k | PHP program to count number of substrings with counts of distinct characters as k . ; Returns true if all values in freq [ ] are either 0 or k . ; Returns count of substrings where frequency of every present character is k ; Pick a starting point ; Initialize all frequencies as 0 for this starting point ; One by one pick ending points ; Increment frequency of current char ; If frequency becomes more than k , we can 't have more substrings  starting with i ; If frequency becomes k , then check other frequencies as well . ; Driver code"
How can the above be solved in PHP?

 < ? php $ MAX_CHAR = 26 ; function check ( & $ freq , $ k ) { global $ MAX_CHAR ; for ( $ i = 0 ; $ i < $ MAX_CHAR ; $ i ++ ) if ( $ freq [ $ i ] && $ freq [ $ i ] != $ k ) return false ; return true ; } function substrings ( $ s , $ k ) { global $ MAX_CHAR ; for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) { $ freq = array_fill ( 0 , $ MAX_CHAR , NULL ) ; for ( $ j = $ i ; $ j < strlen ( $ s ) ; $ j ++ ) { $ index = ord ( $ s [ $ j ] ) - ord ( ' a ' ) ; $ freq [ $ index ] ++ ; if ( $ freq [ $ index ] > $ k ) break ; else if ( $ freq [ $ index ] == $ k && check ( $ freq , $ k ) == true ) $ res ++ ; } } return $ res ; } $ s = " aabbcc " ; $ k = 2 ; echo substrings ( $ s , $ k ) . "
 " ; $ s = " aabbc " ; $ k = 2 ; echo substrings ( $ s , $ k ) . "
 " ; ? >


"Number of substrings with odd decimal value in a binary string | function to count number of substrings with odd decimal representation ; auxiliary array to store count of 1 's  before ith index ; store count of 1 's  before i-th index ; variable to store answer ; traverse the string reversely to calculate number of odd substrings before i - th index ; Driver code"
How can the above be solved in PHP?

< ? php function countSubstr ( $ s ) { $ n = strlen ( $ s ) ; $ auxArr = array ( ) ; if ( $ s [ 0 ] == '1' ) $ auxArr [ 0 ] = 1 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { if ( $ s [ $ i ] == '1' ) $ auxArr [ $ i ] = $ auxArr [ $ i - 1 ] + 1 ; else $ auxArr [ $ i ] = $ auxArr [ $ i - 1 ] ; } $ count = 0 ; for ( $ i = $ n - 1 ; $ i >= 0 ; $ i -- ) if ( $ s [ $ i ] == '1' ) $ count += $ auxArr [ $ i ] ; return $ count ; } $ s = "1101" ; echo countSubstr ( $ s ) ; ? >


"Number of swaps to sort when only adjacent swapping allowed | This function merges two sorted arrays and returns inversion count in the arrays . ; i is index for left subarray ; j is index for right subarray ; k is index for resultant merged subarray ; this is tricky -- see above explanation / diagram for merge ( ) ; Copy the remaining elements of left subarray ( if there are any ) to temp ; Copy the remaining elements of right subarray ( if there are any ) to temp ; Copy back the merged elements to original array ; An auxiliary recursive function that sorts the input array and returns the number of inversions in the array . ; Divide the array into two parts and call _mergeSortAndCountInv ( ) for each of the parts ; Inversion count will be sum of inversions in left - part , right - part and number of inversions in merging ; Merge the two parts ; This function sorts the input array and returns the number of inversions in the array ; Driver progra to test above functions"
How can the above be solved in PHP?

< ? php function merge ( & $ arr , & $ temp , $ left , $ mid , $ right ) { $ inv_count = 0 ; $ i = $ left ; $ j = $ mid ; $ k = $ left ; while ( ( $ i <= $ mid - 1 ) && ( $ j <= $ right ) ) { if ( $ arr [ $ i ] <= $ arr [ $ j ] ) $ temp [ $ k ++ ] = $ arr [ $ i ++ ] ; else { $ temp [ $ k ++ ] = $ arr [ $ j ++ ] ; $ inv_count = $ inv_count + ( $ mid - $ i ) ; } } while ( $ i <= $ mid - 1 ) $ temp [ $ k ++ ] = $ arr [ $ i ++ ] ; while ( $ j <= $ right ) $ temp [ $ k ++ ] = $ arr [ $ j ++ ] ; for ( $ i = $ left ; $ i <= $ right ; $ i ++ ) $ arr [ $ i ] = $ temp [ $ i ] ; return $ inv_count ; } function _mergeSort ( & $ arr , & $ temp , $ left , $ right ) { $ inv_count = 0 ; if ( $ right > $ left ) { $ mid = intval ( ( $ right + $ left ) / 2 ) ; $ inv_count = _mergeSort ( $ arr , $ temp , $ left , $ mid ) ; $ inv_count += _mergeSort ( $ arr , $ temp , $ mid + 1 , $ right ) ; $ inv_count += merge ( $ arr , $ temp , $ left , $ mid + 1 , $ right ) ; } return $ inv_count ; } function countSwaps ( & $ arr , $ n ) { $ temp = array_fill ( 0 , $ n , NULL ) ; return _mergeSort ( $ arr , $ temp , 0 , $ n - 1 ) ; } $ arr = array ( 1 , 20 , 6 , 4 , 5 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; echo " Number ▁ of ▁ swaps ▁ is ▁ " . countSwaps ( $ arr , $ n ) ; return 0 ; ? >


"Number of times a number can be replaced by the sum of its digits until it only contains one digit | PHP program to count number of times we need to add digits to get a single digit . ; Here the count variable store how many times we do sum of digits and temporary_sum always store the temporary sum we get at each iteration . ; In this loop we always compute the sum of digits in temporary_ sum variable and convert it into string str till its length become 1 and increase the count in each iteration . ; computing sum of its digits ; converting temporary_sum into string str again . ; increase the count ; Driver Code"
How can the above be solved in PHP?

< ? php function NumberofTimes ( $ str ) { $ temporary_sum = 0 ; $ count = 0 ; while ( strlen ( $ str ) > 1 ) { $ temporary_sum = 0 ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) $ temporary_sum += ( $ str [ $ i ] - '0' ) ; $ str = ( string ) ( $ temporary_sum ) ; $ count ++ ; } return $ count ; } $ s = "991" ; echo NumberofTimes ( $ s ) ; ? >


"Number of times the largest perfect square number can be subtracted from N | Function to return the count of steps ; Variable to store the count of steps ; Iterate while N > 0 ; Get the largest perfect square and subtract it from N ; Increment steps ; Return the required count ; Driver code"
How can the above be solved in PHP?

< ? php function countSteps ( $ n ) { $ steps = 0 ; while ( $ n ) { $ largest = ( int ) sqrt ( $ n ) ; $ n -= ( $ largest * $ largest ) ; $ steps ++ ; } return $ steps ; } $ n = 85 ; echo countSteps ( $ n ) ; ? >


"Number of triangles after N moves | function to calculate number of triangles in Nth step ; Driver Code"
How can the above be solved in PHP?

< ? php function numberOfTriangles ( $ n ) { $ ans = 2 * ( pow ( 3 , $ n ) ) - 1 ; return $ ans ; } $ n = 2 ; echo numberOfTriangles ( $ n ) ; ? >


"Number of triangles formed from a set of points on three lines | Returns factorial of a number ; calculate c ( n , r ) ; Driver code"
How can the above be solved in PHP?

 < ? php function factorial ( $ n ) { $ fact = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ fact = $ fact * $ i ; return $ fact ; } function ncr ( $ n , $ r ) { return factorial ( $ n ) / ( factorial ( $ r ) * factorial ( $ n - $ r ) ) ; } $ m = 3 ; $ n = 4 ; $ k = 5 ; $ totalTriangles = ncr ( $ m + $ n + $ k , 3 ) - ncr ( $ m , 3 ) - ncr ( $ n , 3 ) - ncr ( $ k , 3 ) ; echo $ totalTriangles . "
 " ;


"Number of triangles in a plane if no more than two points are collinear | Function to find number of triangles in a plane . ; Formula to find number of triangles nC3 = n * ( n - 1 ) * ( n - 2 ) / 6 ; Driver code"
How can the above be solved in PHP?

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


"Number of triangles possible with given lengths of sticks which are powers of 2 | Function to return the number of positive area triangles ; To store the count of total triangles ; To store the count of pairs of sticks with equal lengths ; Back - traverse and count the number of triangles ; Count the number of pairs ; If we have one remaining stick and we have a pair ; Count 1 triangle ; Reduce one pair ; Count the remaining triangles that can be formed ; Driver code"
How can the above be solved in PHP?

< ? php Function countTriangles ( $ a , $ n ) { $ cnt = 0 ; $ pairs = 0 ; for ( $ i = $ n - 1 ; $ i >= 0 ; $ i -- ) { $ pairs += $ a [ $ i ] / 2 ; if ( $ a [ $ i ] % 2 == 1 && $ pairs > 0 ) { $ cnt += 1 ; $ pairs -= 1 ; } } $ cnt += ( int ) ( ( 2 * $ pairs ) / 3 ) ; return $ cnt ; } $ a = array ( 1 , 2 , 2 , 2 , 2 ) ; $ n = sizeof ( $ a ) ; echo ( countTriangles ( $ a , $ n ) ) ; ? >


"Number of unique rectangles formed using N unit squares | height >= length is maintained ; Driver code"
How can the above be solved in PHP?

< ? php function countRect ( $ n ) { $ ans = 0 ; for ( $ length = 1 ; $ length <= sqrt ( $ n ) ; $ length ++ ) for ( $ height = $ length ; $ height * $ length <= $ n ; $ height ++ ) $ ans ++ ; return $ ans ; } $ n = 5 ; echo countRect ( $ n ) ; ? >


"Number of unmarked integers in a special sieve | PHP Program to determine the number of unmarked integers in a special sieve ; Driver Code"
How can the above be solved in PHP?

< ? php function countUnmarked ( $ N ) { if ( $ N % 2 == 0 ) return $ N / 2 ; else return $ N / 2 + 1 ; } $ N = 4 ; echo " Number ▁ of ▁ unmarked ▁ elements : ▁ " , countUnmarked ( $ N ) ; ? >


"Number of visible boxes after putting one inside another | return the minimum number of visible boxes ; New Queue of integers . ; sorting the array ; traversing the array ; checking if current element is greater than or equal to twice of front element ; Pushing each element of array ; Driver Code"
How can the above be solved in PHP?

< ? php function minimumBox ( $ arr , $ n ) { $ q = array ( ) ; sort ( $ arr ) ; array_push ( $ q , $ arr [ 0 ] ) ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ now = $ q [ 0 ] ; if ( $ arr [ $ i ] >= 2 * $ now ) array_pop ( $ q ) ; array_push ( $ q , $ arr [ $ i ] ) ; } return count ( $ q ) ; } $ arr = array ( 4 , 1 , 2 , 8 ) ; $ n = count ( $ arr ) ; echo minimumBox ( $ arr , $ n ) ; ? >


"Number of ways a convex polygon of n + 2 sides can split into triangles by connecting vertices | Returns value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; A Binomial coefficient based function to find nth catalan number in O ( n ) time ; Calculate value of 2 nCn ; return 2 nCn / ( n + 1 ) ; Driver code"
How can the above be solved in PHP?

< ? php function binomialCoeff ( $ n , $ k ) { $ res = 1 ; if ( $ k > $ n - $ k ) $ k = $ n - $ k ; for ( $ i = 0 ; $ i < $ k ; ++ $ i ) { $ res *= ( $ n - $ i ) ; $ res /= ( $ i + 1 ) ; } return $ res ; } function catalan ( $ n ) { $ c = binomialCoeff ( 2 * $ n , $ n ) ; return $ c / ( $ n + 1 ) ; } $ n = 3 ; echo catalan ( $ n ) ; ? >


"Number of ways in which the substring in range [ L , R ] can be formed using characters out of the range | Function to return the number of ways to form the sub - string ; Initialize a hash - table with 0 ; Iterate in the string and count the frequency of characters that do not lie in the range L and R ; Out of range characters ; Stores the final number of ways ; Iterate for the sub - string in the range L and R ; If exists then multiply the number of ways and decrement the frequency ; If does not exist the sub - string cannot be formed ; Return the answer ; Driver code"
How can the above be solved in PHP?


"Number of ways to arrange 2 * N persons on the two sides of a table with X and Y persons on opposite sides | Function to find factorial of a number ; Function to find nCr ; Function to find the number of ways to arrange 2 * N persons ; Driver code ; Function call"
How can the above be solved in PHP?

< ? php function factorial ( $ n ) { if ( $ n <= 1 ) return 1 ; return $ n * factorial ( $ n - 1 ) ; } function nCr ( $ n , $ r ) { return factorial ( $ n ) / ( factorial ( $ n - $ r ) * factorial ( $ r ) ) ; } function NumberOfWays ( $ n , $ x , $ y ) { return nCr ( 2 * $ n - $ x - $ y , $ n - $ x ) * factorial ( $ n ) * factorial ( $ n ) ; } $ n = 5 ; $ x = 4 ; $ y = 2 ; echo ( NumberOfWays ( $ n , $ x , $ y ) ) ; ? >


"Number of ways to arrange K different objects taking N objects at a time | PHP implementation of the approach ; Function to return n ! % p ; $res = 1 ; Initialize result ; 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 ; Returns n ^ ( - 1 ) mod p ; Returns nCr % p using Fermat 's little theorem. ; Base case ; Fill factorial array so that we can find all factorial of r , n and n - r ; Function to return the number of ways to arrange K different objects taking N objects at a time ; Driver Code ; Function call"
How can the above be solved in PHP?

< ? php $ mod = ( 1e9 + 7 ) ; function factorial ( $ n , $ p ) { for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ res = ( $ res * $ i ) % $ p ; return $ res ; } function power ( $ x , $ y , $ p ) { while ( $ y > 0 ) { if ( ( $ y & 1 ) == 1 ) $ res = ( $ res * $ x ) % $ p ; $ x = ( $ x * $ x ) % $ p ; } return $ res ; } function modInverse ( $ n , $ p ) { return power ( $ n , $ p - 2 , $ p ) ; } function nCrModP ( $ n , $ r , $ p ) { if ( $ r == 0 ) return 1 ; $ fac = array ( ( int ) $ n + 1 ) ; $ fac [ 0 ] = 1 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ fac [ $ i ] = $ fac [ $ i - 1 ] * $ i % $ p ; return ( $ fac [ ( int ) $ n ] * modInverse ( $ fac [ ( int ) $ r ] , $ p ) % $ p * modInverse ( $ fac [ ( int ) $ n - ( int ) $ r ] , $ p ) % $ p ) % $ p ; } function countArrangements ( $ n , $ k , $ p ) { return ( factorial ( $ n , $ p ) * nCrModP ( $ k , $ n , $ p ) ) % $ p ; } { $ N = 5 ; $ K = 8 ; echo ( countArrangements ( $ N , $ K , $ mod ) ) ; }


"Number of ways to arrange N items under given constraints | method returns number of ways with which items can be arranged ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; declare dp array to store result up to ith colored item ; variable to keep track of count of items considered till now ; loop over all different colors ; populate next value using current value and stated relation ; return value stored at last index ; Driver code"
How can the above be solved in PHP?


"Number of ways to form an array with distinct adjacent elements | Returns the total ways to form arrays such that every consecutive element is different and each element except the first and last can take values from 1 to M ; define the dp [ ] [ ] array ; if the first element is 1 ; there is only one way to place a 1 at the first index ; the value at first index needs to be 1 , thus there is no way to place a non - one integer ; if the first element was 1 then at index 1 , only non one integer can be placed thus there are M - 1 ways to place a non one integer at index 2 and 0 ways to place a 1 at the 2 nd index ; Else there is one way to place a one at index 2 and if a non one needs to be placed here , there are ( M - 2 ) options , i . e neither the element at this index should be 1 , neither should it be equal to the previous element ; Build the dp array in bottom up manner ; f ( i , one ) = f ( i - 1 , non - one ) ; f ( i , non - one ) = f ( i - 1 , one ) * ( M - 1 ) + f ( i - 1 , non - one ) * ( M - 2 ) ; last element needs to be one , so return dp [ n - 1 ] [ 0 ] ; Driver Code"
How can the above be solved in PHP?


"Number of ways to represent a number as sum of k fibonacci numbers | To store fibonacci numbers 42 second number in fibonacci series largest possible integer ; Function to generate fibonacci series ; Recursive function to return the number of ways ; base condition ; for recursive function call ; Driver code"
How can the above be solved in PHP?

< ? php $ fib = array_fill ( 0 , 43 , 0 ) ; function fibonacci ( ) { global $ fib ; $ fib [ 0 ] = 1 ; $ fib [ 1 ] = 2 ; for ( $ i = 2 ; $ i < 43 ; $ i ++ ) $ fib [ $ i ] = $ fib [ $ i - 1 ] + $ fib [ $ i - 2 ] ; } function rec ( $ x , $ y , $ last ) { global $ fib ; if ( $ y == 0 ) { if ( $ x == 0 ) return 1 ; return 0 ; } $ sum = 0 ; for ( $ i = $ last ; $ i >= 0 and $ fib [ $ i ] * $ y >= $ x ; $ i -- ) { if ( $ fib [ $ i ] > $ x ) continue ; $ sum += rec ( $ x - $ fib [ $ i ] , $ y - 1 , $ i ) ; } return $ sum ; } fibonacci ( ) ; $ n = 13 ; $ k = 3 ; echo " Possible ▁ ways ▁ are : ▁ " . rec ( $ n , $ k , 42 ) ; ? >


"Number of ways to swap two bit of s1 so that bitwise OR of s1 and s2 changes | Function to find number of ways ; initialise result that store No . of swaps required ; Traverse both strings and check the bits as explained ; calculate result ; Driver code"
How can the above be solved in PHP?


"Number which has the maximum number of distinct prime factors in the range M to N | Function to return the maximum number ; array to store the number of distinct primes ; true if index ' i ' is a prime ; initializing the number of factors to 0 and ; Used in Sieve ; condition works only when ' i ' is prime , hence for factors of all prime number , the prime status is changed to false ; Number is prime ; number of factor of a prime number is 1 ; incrementing factorCount all the factors of i ; and changing prime status to false ; Initialize the max and num ; Gets the maximum number ; Gets the maximum number ; Driver code ; Calling function"
How can the above be solved in PHP?

< ? php function maximumNumberDistinctPrimeRange ( $ m , $ n ) { $ factorCount = array ( ) ; $ prime = array ( ) ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) { $ factorCount [ $ i ] = 0 ; $ prime [ $ i ] = true ; } for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { if ( $ prime [ $ i ] == true ) { $ factorCount [ $ i ] = 1 ; for ( $ j = $ i * 2 ; $ j <= $ n ; $ j += $ i ) { $ factorCount [ $ j ] ++ ; $ prime [ $ j ] = false ; } } } $ max = $ factorCount [ $ m ] ; $ num = $ m ; for ( $ i = $ m ; $ i <= $ n ; $ i ++ ) { if ( $ factorCount [ $ i ] > $ max ) { $ max = $ factorCount [ $ i ] ; $ num = $ i ; } } return $ num ; } $ m = 4 ; $ n = 6 ; echo maximumNumberDistinctPrimeRange ( $ m , $ n ) ; ? >


"Number whose XOR sum with given array is a given number k | This function returns the number to be inserted in the given array ; initialise the answer with k ; XOR of all elements in the array ; Driver Code"
How can the above be solved in PHP?

 < ? php function findEletobeInserted ( $ A , $ n , $ k ) { $ ans = $ k ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ ans ^= $ A [ $ i ] ; return $ ans ; } $ A = array ( 1 , 2 , 3 , 4 , 5 ) ; $ n = count ( $ A ) ; $ k = 10 ; echo findEletobeInserted ( $ A , $ n , $ k ) ; echo " ▁ has ▁ to ▁ be ▁ inserted " ; echo " ▁ in ▁ the ▁ given ▁ array ▁ to ▁ make ▁ xor ▁ sum ▁ of ▁ " ; echo $ k , "
 " ; ? >


"Number whose sum of XOR with given array range is maximum | PHP program to find smallest integer X such that sum of its XOR with range is maximum . ; Function to make prefix array which counts 1 's of each bit up to that number ; Making a prefix array which sums number of 1 's up to  that position ; If j - th bit of a number is set then add one to previously counted 1 's ; Function to find X ; Initially taking maximum value all bits 1 ; Iterating over each bit ; get 1 ' s ▁ at ▁ ith ▁ bit ▁ between ▁ the ▁ ▁ range ▁ L - R ▁ by ▁ subtracting ▁ 1' s till Rth number - 1 's till L-1th number ; If 1 ' s ▁ are ▁ more ▁ than ▁ or ▁ equal ▁ ▁ to ▁ 0' s then unset the ith bit from answer ; Set ith bit to 0 by doing Xor with 1 ; Driver Code"
How can the above be solved in PHP?


"Number with even sum of digits | Function to find kth good number . ; Find the last digit of n . ; If last digit is between 0 to 4 then return 2 * n . ; If last digit is between 5 to 9 then return 2 * n + 1. ; Driver code"
How can the above be solved in PHP?

< ? php function findKthGoodNo ( $ n ) { $ lastDig = $ n % 10 ; if ( $ lastDig >= 0 && $ lastDig <= 4 ) return $ n << 1 ; else return ( $ n << 1 ) + 1 ; } $ n = 10 ; echo ( findKthGoodNo ( $ n ) ) ; ? >


"Number with set bits only between L | Function to return the integer with all the bits set in range L - R ; Driver Code"
How can the above be solved in PHP?

< ? php function setbitsfromLtoR ( $ L , $ R ) { return ( 1 << ( $ R + 1 ) ) - ( 1 << $ L ) ; } $ L = 2 ; $ R = 5 ; echo setbitsfromLtoR ( $ L , $ R ) ; ? >


"Number with set bits only between L | Function to return the integer with all the bits set in range L - R ; iterate from L to R and add all powers of 2 ; Driver Code"
How can the above be solved in PHP?

< ? php function getInteger ( $ L , $ R ) { $ number = 0 ; for ( $ i = $ L ; $ i <= $ R ; $ i ++ ) $ number += pow ( 2 , $ i ) ; return $ number ; } $ L = 2 ; $ R = 5 ; echo getInteger ( $ L , $ R ) ; ? >


"Numbers having Unique ( or Distinct ) digits | Function to print unique digit numbers in range from l to r . ; Start traversing the numbers ; Find digits and maintain its hash ; if a digit occurs more than 1 time then break ; num will be 0 only when above loop doesn 't get break that  means the number is unique  so print it. ; Driver code"
How can the above be solved in PHP?

< ? php function printUnique ( $ l , $ r ) { for ( $ i = $ l ; $ i <= $ r ; $ i ++ ) { $ num = $ i ; $ visited = ( false ) ; while ( $ num ) { if ( $ visited [ $ num % 10 ] ) $ visited [ $ num % 10 ] = true ; $ num = ( int ) $ num / 10 ; } if ( $ num == 0 ) echo $ i , " ▁ " ; } } $ l = 1 ; $ r = 20 ; printUnique ( $ l , $ r ) ; ? >


"Numbers having difference with digit sum more than s | function for digit sum ; function to calculate count of integer s . t . integer - digSum > s ; if n < s no integer possible ; iterate for s range and then calculate total count of such integer if starting integer is found ; if no integer found return 0 ; Driver Code"
How can the above be solved in PHP?

< ? php function digitSum ( $ n ) { $ digSum = 0 ; while ( $ n ) { $ digSum += $ n % 10 ; $ n /= 10 ; } return $ digSum ; } function countInteger ( $ n , $ s ) { if ( $ n < $ s ) return 0 ; for ( $ i = $ s ; $ i <= min ( $ n , $ s + 163 ) ; $ i ++ ) if ( ( $ i - digitSum ( $ i ) ) > $ s ) return ( $ n - $ i + 1 ) ; return 0 ; } $ n = 1000 ; $ s = 100 ; echo countInteger ( $ n , $ s ) ; ? >


"Numbers less than N that are perfect cubes and the sum of their digits reduced to a single digit is 1 | Function that returns true if the eventual digit sum of number nm is 1 ; if reminder will 1 then eventual sum is 1 ; Function to print the required numbers less than n ; If it is the required perfect cube ; Driver code"
How can the above be solved in PHP?

< ? php function isDigitSumOne ( $ nm ) { if ( $ nm % 9 == 1 ) return true ; else return false ; } function printValidNums ( $ n ) { $ cbrt_n = ceil ( pow ( $ n , 1 / 3 ) ) ; for ( $ i = 1 ; $ i <= $ cbrt_n ; $ i ++ ) { $ cube = pow ( $ i , 3 ) ; if ( $ cube >= 1 && $ cube <= $ n && isDigitSumOne ( $ cube ) ) echo $ cube , " ▁ " ; } } $ n = 1000 ; printValidNums ( $ n ) ; ? >


"Numbers less than N which are product of exactly two distinct prime numbers | Function to check whether a number is a PerfectSquare or not ; Function to check if a number is a product of exactly two distinct primes ; Function to find numbers that are product of exactly two distinct prime numbers . ; Vector to store such numbers ; insert in the vector ; Print all numbers till n from the vector ; Driver Code"
How can the above be solved in PHP?

< ? php function isPerfectSquare ( $ x ) { $ sr = sqrt ( $ x ) ; return ( ( $ sr - floor ( $ sr ) ) == 0 ) ; } function isProduct ( $ num ) { $ cnt = 0 ; for ( $ i = 2 ; $ cnt < 2 && $ i * $ i <= $ num ; ++ $ i ) { while ( $ num % $ i == 0 ) { $ num /= $ i ; ++ $ cnt ; } } if ( $ num > 1 ) ++ $ cnt ; return $ cnt == 2 ; } function findNumbers ( $ N ) { $ vec = array ( ) ; for ( $ i = 1 ; $ i <= $ N ; $ i ++ ) { if ( isProduct ( $ i ) && ! isPerfectSquare ( $ i ) ) { array_push ( $ vec , $ i ) ; } } for ( $ i = 0 ; $ i < sizeof ( $ vec ) ; $ i ++ ) { echo $ vec [ $ i ] . " " ; } } $ N = 30 ; findNumbers ( $ N ) ;


"Numbers that are not divisible by any number in the range [ 2 , 10 ] | Function to return the count of numbers from 1 to N which are not divisible by any number in the range [ 2 , 10 ] ; Driver code"
How can the above be solved in PHP?


"Numbers whose bitwise OR and sum with N are equal | Function to find total 0 bit in a number ; Function to find Count of non - negative numbers less than or equal to N , whose bitwise OR and SUM with N are equal . ; count number of zero bit in N ; power of 2 to count ; Driver code"
How can the above be solved in PHP?

< ? php function CountZeroBit ( $ n ) { $ count = 0 ; while ( $ n ) { if ( ! ( $ n & 1 ) ) $ count ++ ; $ n >>= 1 ; } return $ count ; } function CountORandSumEqual ( $ N ) { $ count = CountZeroBit ( $ N ) ; return ( 1 << $ count ) ; } $ N = 10 ; echo CountORandSumEqual ( $ N ) ; ? >


"Numbers whose factorials end with n zeros | Function to calculate trailing zeros ; binary search for first number with n trailing zeros ; Print all numbers after low with n trailing zeros . ; Print result ; Driver code"
How can the above be solved in PHP?

< ? php function trailingZeroes ( $ n ) { $ cnt = 0 ; while ( $ n > 0 ) { $ n = intval ( $ n / 5 ) ; $ cnt += $ n ; } return $ cnt ; } function binarySearch ( $ n ) { $ low = 0 ; while ( $ low < $ high ) { $ mid = intval ( ( $ low + $ high ) / 2 ) ; $ count = trailingZeroes ( $ mid ) ; if ( $ count < $ n ) $ low = $ mid + 1 ; else $ high = $ mid ; } $ result = array ( ) ; while ( trailingZeroes ( $ low ) == $ n ) { array_push ( $ result , $ low ) ; $ low ++ ; } for ( $ i = 0 ; $ i < sizeof ( $ result ) ; $ i ++ ) echo $ result [ $ i ] . " ▁ " ; } $ n = 2 ; binarySearch ( $ n ) ; ? >


"Numbers with exactly 3 divisors | Generates all primes upto n and prints their squares ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; print squares of primes upto n . ; Driver Code"
How can the above be solved in PHP?

 < ? php function numbersWith3Divisors ( $ n ) { $ prime = array_fill ( 0 , $ n + 1 , true ) ; $ prime [ 0 ] = $ prime [ 1 ] = false ; for ( $ p = 2 ; $ p * $ p <= $ n ; $ p ++ ) { if ( $ prime [ $ p ] == true ) { for ( $ i = $ p * 2 ; $ i <= $ n ; $ i += $ p ) $ prime [ $ i ] = false ; } } echo " Numbers ▁ with ▁ 3 ▁ divisors ▁ :
 " ; for ( $ i = 0 ; $ i * $ i <= $ n ; $ i ++ ) if ( $ prime [ $ i ] ) echo $ i * $ i . " ▁ " ; } $ n = 96 ; numbersWith3Divisors ( $ n ) ; ? >


"Occurrences of a pattern in binary representation of a number | Function to return the count of occurrence of pat in binary representation of n ; To store decimal value of the pattern ; To store a number that has all ones in its binary representation and length of ones equal to length of the pattern ; Find values of $pattern_int and $all_ones ; If the pattern occurs in the last digits of $n ; Right shift $n by 1 bit ; Driver code"
How can the above be solved in PHP?

< ? php function countPattern ( $ n , $ pat ) { $ pattern_int = 0 ; $ power_two = 1 ; $ all_ones = 0 ; for ( $ i = strlen ( $ pat ) - 1 ; $ i >= 0 ; $ i -- ) { $ current_bit = $ pat [ $ i ] - '0' ; $ pattern_int += ( $ power_two * $ current_bit ) ; $ all_ones = $ all_ones + $ power_two ; $ power_two = $ power_two * 2 ; } $ count = 0 ; while ( $ n && $ n >= $ pattern_int ) { if ( ( $ n & $ all_ones ) == $ pattern_int ) { $ count ++ ; } $ n = $ n >> 1 ; } return $ count ; } $ n = 500 ; $ pat = "10" ; echo countPattern ( $ n , $ pat ) ; ? >


"Octahedral Number | Function to find octahedral number ; Formula to calculate nth octahedral number and return it into main function . ; Drivers Code ; print result"
How can the above be solved in PHP?

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


"Odd numbers in N | Function to get no of set bits in binary representation of positive integer n ; Count number of 1 's in binary  representation of n. ; Number of odd numbers in n - th row is 2 raised to power the count . ; Driver code"
How can the above be solved in PHP?

< ? php function countSetBits ( $ n ) { $ count = 0 ; while ( $ n ) { $ count += $ n & 1 ; $ n >>= 1 ; } return $ count ; } function countOfOddsPascal ( $ n ) { $ c = countSetBits ( $ n ) ; return pow ( 2 , $ c ) ; } $ n = 20 ; echo countOfOddsPascal ( $ n ) ; ? >


"Odious number | Function to get no of set bits in binary ; Check if number is odious or not ; Driver Code"
How can the above be solved in PHP?

< ? php function countSetBits ( $ n ) { $ count = 0 ; while ( $ n ) { $ n &= ( $ n - 1 ) ; $ count ++ ; } return $ count ; } function checkOdious ( $ n ) { return ( countSetBits ( $ n ) % 2 == 1 ) ; } $ num = 32 ; if ( checkOdious ( $ num ) ) echo " Yes " ; else echo " No " ; ? >


"Optimal Strategy for a Game | DP | Returns optimal value possible that a player can collect from an array of coins of size n . Note than n must be even ; Create a table to store solutions of subproblems ; Fill table using above recursive formula . Note that the table is filled in diagonal fashion ( similar to http : goo . gl / PQqoS ) , from diagonal elements to table [ 0 ] [ n - 1 ] which is the result . ; Here x is value of F ( i + 2 , j ) , y is F ( i + 1 , j - 1 ) and z is F ( i , j - 2 ) in above recursive formula ; Driver Code"
How can the above be solved in PHP?

< ? php function optimalStrategyOfGame ( $ arr , $ n ) { $ table = array_fill ( 0 , $ n , array_fill ( 0 , $ n , 0 ) ) ; for ( $ gap = 0 ; $ gap < $ n ; ++ $ gap ) { for ( $ i = 0 , $ j = $ gap ; $ j < $ n ; ++ $ i , ++ $ j ) { $ x = ( ( $ i + 2 ) <= $ j ) ? $ table [ $ i + 2 ] [ $ j ] : 0 ; $ y = ( ( $ i + 1 ) <= ( $ j - 1 ) ) ? $ table [ $ i + 1 ] [ $ j - 1 ] : 0 ; $ z = ( $ i <= ( $ j - 2 ) ) ? $ table [ $ i ] [ $ j - 2 ] : 0 ; $ table [ $ i ] [ $ j ] = max ( $ arr [ $ i ] + min ( $ x , $ y ) , $ arr [ $ j ] + min ( $ y , $ z ) ) ; } } return $ table [ 0 ] [ $ n - 1 ] ; } $ arr1 = array ( 8 , 15 , 3 , 7 ) ; $ n = count ( $ arr1 ) ; print ( optimalStrategyOfGame ( $ arr1 , $ n ) . " " ) ; $ arr2 = array ( 2 , 2 , 2 , 2 ) ; $ n = count ( $ arr2 ) ; print ( optimalStrategyOfGame ( $ arr2 , $ n ) . " " ) ; $ arr3 = array ( 20 , 30 , 2 , 2 , 2 , 10 ) ; $ n = count ( $ arr3 ) ; print ( optimalStrategyOfGame ( $ arr3 , $ n ) . " " ) ; ? >


"Optimized Euler Totient Function for Multiple Evaluations | PHP program to efficiently compute values of euler totient function for multiple inputs . ; Stores prime numbers upto MAX - 1 values ; Finds prime numbers upto MAX - 1 and stores them in vector p ; if prime [ i ] is not marked before ; fill vector for every newly encountered prime ; run this loop till square root of MAX , mark the index i * j as not prime ; function to find totient of n ; this loop runs sqrt ( n / ln ( n ) ) times ; subtract multiples of p [ i ] from r ; Remove all occurrences of p [ i ] in n ; when n has prime factor greater than sqrt ( n ) ; preprocess all prime numbers upto 10 ^ 5"
How can the above be solved in PHP?


"Optimized Naive Algorithm for Pattern Searching | A modified Naive Pettern Searching algorithn that is optimized for the cases when all characters of pattern are different ; For current index i , check for pattern match ; if pat [ 0. . . M - 1 ] = txt [ i , i + 1 , ... i + M - 1 ] ; slide the pattern by j ; Driver Code"
How can the above be solved in PHP?

 < ? php function search ( $ pat , $ txt ) { $ M = strlen ( $ pat ) ; $ N = strlen ( $ txt ) ; $ i = 0 ; while ( $ i <= $ N - $ M ) { $ j ; for ( $ j = 0 ; $ j < $ M ; $ j ++ ) if ( $ txt [ $ i + $ j ] != $ pat [ $ j ] ) break ; if ( $ j == $ M ) { echo ( " Pattern ▁ found ▁ at ▁ index ▁ $ i " . "
 " ) ; $ i = $ i + $ M ; } else if ( $ j == 0 ) $ i = $ i + 1 ; else $ i = $ i + $ j ; } } $ txt = " ABCEABCDABCEABCD " ; $ pat = " ABCD " ; search ( $ pat , $ txt ) ; ? >


"Overall percentage change from successive changes | PHP implementation of above approach ; Calculate successive change of 1 st 2 change ; Calculate successive change for rest of the value ; Driver code ; Calling function"
How can the above be solved in PHP?

< ? php function successiveChange ( $ arr , $ N ) { $ result = 0 ; $ var1 = $ arr [ 0 ] ; $ var2 = $ arr [ 1 ] ; $ result = $ var1 + $ var2 + ( ( $ var1 * $ var2 ) / 100 ) ; for ( $ i = 2 ; $ i < $ N ; $ i ++ ) $ result = $ result + $ arr [ $ i ] + ( ( $ result * $ arr [ $ i ] ) / 100 ) ; return $ result ; } $ arr = array ( 10 , 20 , 30 , 10 ) ; $ N = count ( $ arr ) ; $ result = successiveChange ( $ arr , $ N ) ; echo " Percentage ▁ change ▁ is ▁ = ▁ " , $ result , " ▁ % " ; ? >


"P | function to check if number n is a P - smooth number or not ; prime factorise it by 2 ; if the number is divisible by 2 ; check for all the possible numbers that can divide it ; prime factorize it by i ; stores the maximum if maximum and i , if i divides the number ; if n at the end is a prime number , then it a divisor itself ; Driver Code"
How can the above be solved in PHP?

< ? php function check ( $ n , $ p ) { $ maximum = -1 ; while ( ! ( $ n % 2 ) ) { $ maximum = max ( $ maximum , 2 ) ; $ n = $ n / 2 ; } for ( $ i = 3 ; $ i <= sqrt ( $ n ) ; $ i += 2 ) { while ( $ n % $ i == 0 ) { $ maximum = max ( $ maximum , $ i ) ; $ n = $ n / $ i ; } } if ( $ n > 2 ) $ maximum = max ( $ maximum , $ n ) ; return ( $ maximum <= $ p ) ; } $ n = 24 ; $ p = 7 ; if ( check ( $ n , $ p ) ) echo ( " yes " ) ; else echo ( " no " ) ; ? >


"Padovan Sequence | Function to calculate padovan number P ( n ) ; 0 th , 1 st and 2 nd number of the series are 1 ; Driver Code"
How can the above be solved in PHP?

< ? php function pad ( $ n ) { $ pPrevPrev = 1 ; $ pPrev = 1 ; $ pCurr = 1 ; $ pNext = 1 ; for ( $ i = 3 ; $ i <= $ n ; $ i ++ ) { $ pNext = $ pPrevPrev + $ pPrev ; $ pPrevPrev = $ pPrev ; $ pPrev = $ pCurr ; $ pCurr = $ pNext ; } return $ pNext ; } $ n = 12 ; echo ( pad ( $ n ) ) ; ? >


"Painting Fence Algorithm | Returns count of ways to color k posts using k colors ; There are k ways to color first post ; There are 0 ways for single post to violate ( same color_ and k ways to not violate ( different color ) ; Fill for 2 posts onwards ; Current same is same as previous diff ; We always have k - 1 choices for next post ; Total choices till i . ; Driver code"
How can the above be solved in PHP?

 < ? php function countWays ( $ n , $ k ) { $ total = $ k ; $ mod = 1000000007 ; $ same = 0 ; $ diff = $ k ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { $ same = $ diff ; $ diff = $ total * ( $ k - 1 ) ; $ diff = $ diff % $ mod ; $ total = ( $ same + $ diff ) % $ mod ; } return $ total ; } $ n = 3 ; $ k = 2 ; echo countWays ( $ n , $ k ) . "
 " ; ? >


"Pair with given product | Set 1 ( Find if any pair exists ) | Returns true if there is a pair in arr [ 0. . n - 1 ] with product equal to x . ; Consider all possible pairs and check for every pair . ; Driver code"
How can the above be solved in PHP?

 < ? php function isProduct ( $ arr , $ n , $ x ) { for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) for ( $ j = $ i + 1 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] * $ arr [ $ j ] == $ x ) return true ; return false ; } $ arr = array ( 10 , 20 , 9 , 40 ) ; $ x = 400 ; $ n = count ( $ arr ) ; if ( isProduct ( $ arr , $ n , $ x ) ) echo " Yes
 " ; else echo " No
 " ; $ x = 190 ; if ( isProduct ( $ arr , $ n , $ x ) ) echo " Yes
 " ; else echo " No
 " ; ? >


"Pair with maximum GCD from two arrays | Find the maximum GCD pair with maximum sum ; array to keep a count of existing elements ; first [ i ] and second [ i ] are going to store maximum multiples of i in a [ ] and b [ ] respectively . ; traverse through the first array to mark the elements in cnt ; Find maximum multiple of every number in first array ; Find maximum multiple of every number in second array We re - initialise cnt [ ] and traverse through the second array to mark the elements in cnt ; if the multiple is present in the second array then store the max of number or the pre - existing element ; traverse for every elements and checks the maximum N that is present in both the arrays ; Driver code ; Maximum possible value of elements in both arrays ."
How can the above be solved in PHP?


"Pairs from an array that satisfy the given condition | Function to return the number of set bits in n ; Function to return the count of required pairs ; Set bits for first element of the pair ; Set bits for second element of the pair ; Set bits of the resultant number which is the XOR of both the elements of the pair ; If the condition is satisfied ; Increment the count ; Return the total count ; Driver code"
How can the above be solved in PHP?

< ? php function setBits ( $ n ) { $ count = 0 ; while ( $ n ) { $ n = $ n & ( $ n - 1 ) ; $ count ++ ; } return $ count ; } function countPairs ( & $ a , $ n ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { $ setbits_x = setBits ( $ a [ $ i ] ) ; for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) { $ setbits_y = setBits ( $ a [ $ j ] ) ; $ setbits_xor_xy = setBits ( $ a [ $ i ] ^ $ a [ $ j ] ) ; if ( $ setbits_x + $ setbits_y == $ setbits_xor_xy ) $ count ++ ; } } return $ count ; } $ a = array ( 2 , 3 , 4 , 5 , 6 ) ; $ n = sizeof ( $ a ) / sizeof ( $ a [ 0 ] ) ; echo countPairs ( $ a , $ n ) ; ? >


"Pairs of strings which on concatenating contains each character of " string " | PHP implementation of the approach ; Function to return the bitmask for the string ; Function to return the count of pairs ; bitMask [ i ] will store the count of strings from the array whose bitmask is i ; To store the count of pairs ; MAX - 1 = 63 i . e . 111111 in binary ; arr [ i ] cannot make s pair with itself i . e . ( arr [ i ] , arr [ i ] ) ; Driver code"
How can the above be solved in PHP?

< ? php $ MAX = 64 ; function getBitmask ( $ s ) { $ temp = 0 ; for ( $ j = 0 ; $ j < strlen ( $ s ) ; $ j ++ ) { if ( $ s [ $ j ] == ' s ' ) { $ temp = $ temp | ( 1 ) ; } else if ( $ s [ $ j ] == ' t ' ) { $ temp = $ temp | ( 2 ) ; } else if ( $ s [ $ j ] == ' r ' ) { $ temp = $ temp | ( 4 ) ; } else if ( $ s [ $ j ] == ' i ' ) { $ temp = $ temp | ( 8 ) ; } else if ( $ s [ $ j ] == ' n ' ) { $ temp = $ temp | ( 16 ) ; } else if ( $ s [ $ j ] == ' g ' ) { $ temp = $ temp | ( 32 ) ; } } return $ temp ; } function countPairs ( $ arr , $ n ) { $ bitMask = array_fill ( 0 , $ GLOBALS [ ' MAX ' ] , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ bitMask [ getBitmask ( $ arr [ $ i ] ) ] ++ ; $ cnt = 0 ; for ( $ i = 0 ; $ i < $ GLOBALS [ ' MAX ' ] ; $ i ++ ) { for ( $ j = $ i ; $ j < $ GLOBALS [ ' MAX ' ] ; $ j ++ ) { if ( ( $ i $ j ) == ( $ GLOBALS [ ' MAX ' ] - 1 ) ) { if ( $ i == $ j ) $ cnt += floor ( ( $ bitMask [ $ i ] * $ bitMask [ $ i ] - 1 ) / 2 ) ; else $ cnt += ( $ bitMask [ $ i ] * $ bitMask [ $ j ] ) ; } } } return $ cnt ; } $ arr = array ( " strrr " , " string " , " gstrin " ) ; $ n = count ( $ arr ) ; echo countPairs ( $ arr , $ n ) ; ? >


"Pairs such that one is a power multiple of other | function to count the required pairs ; sort the given array ; for each A [ i ] traverse rest array ; count Aj such that Ai * k ^ x = Aj ; increase x till Ai * k ^ x <= largest element ; Driver Code"
How can the above be solved in PHP?


"Pairs with Difference less than K | Function to count pairs ; Driver code"
How can the above be solved in PHP?


"Pairs with Difference less than K | PHP code to find count of Pairs with difference less than K . ; to sort the array . ; Keep incrementing result while subsequent elements are within limits . ; Driver code"
How can the above be solved in PHP?


"Pairs with GCD equal to one in the given range | Function to print all pairs ; check if even ; We can print all adjacent pairs for ( int i = l ; i < r ; i += 2 ) { cout << " { " << i << " , ▁ " << i + 1 << " } , ▁ " ; } ; Driver Code"
How can the above be solved in PHP?

< ? php function checkPairs ( $ l , $ r ) { if ( ( $ l - $ r ) % 2 == 0 ) return false ; return true ; } $ l = 1 ; $ r = 8 ; if ( checkPairs ( $ l , $ r ) ) echo ( " Yes " ) ; else echo ( " No " ) ; ? >


"Palindrome Partitioning | DP | Returns the minimum number of cuts needed to partition a string such that every part is a palindrome ; Get the length of the string ; Create two arrays to build the solution in bottom up manner C [ i ] [ j ] = Minimum number of cuts needed for palindrome partitioning of substring str [ i . . j ] P [ i ] [ j ] = true if substring str [ i . . j ] is palindrome , else false Note that C [ i ] [ j ] is 0 if P [ i ] [ j ] is true ; Every substring of length 1 is a palindrome ; L is substring length . Build the solution in a bottom - up manner by considering all substrings of length starting from 2 to n . The loop structure is same as Matrix Chain Multiplication problem ( See https : www . geeksforgeeks . org / matrix - chain - multiplication - dp - 8 / ) ; For substring of length L , set different possible starting indexes ; Set ending index ; If L is 2 , then we just need to compare two characters . Else need to check two corner characters and value of P [ i + 1 ] [ j - 1 ] ; IF str [ i . . j ] is palindrome , then C [ i ] [ j ] is 0 ; Make a cut at every possible location starting from i to j , and get the minimum cost cut . ; Return the min cut value for complete string . i . e . , str [ 0. . n - 1 ] ; Driver program to test the above function"
How can the above be solved in PHP?

< ? php function minPalPartion ( $ str ) { $ n = strlen ( $ str ) ; $ C = array_fill ( 0 , $ n , array_fill ( 0 , $ n , NULL ) ) ; $ P = array_fill ( false , $ n , array_fill ( false , $ n , NULL ) ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ P [ $ i ] [ $ i ] = true ; $ C [ $ i ] [ $ i ] = 0 ; } for ( $ L = 2 ; $ L <= $ n ; $ L ++ ) { for ( $ i = 0 ; $ i < $ n - $ L + 1 ; $ i ++ ) { $ j = $ i + $ L - 1 ; if ( $ L == 2 ) $ P [ $ i ] [ $ j ] = ( $ str [ $ i ] == $ str [ $ j ] ) ; else $ P [ $ i ] [ $ j ] = ( $ str [ $ i ] == $ str [ $ j ] ) && $ P [ $ i + 1 ] [ $ j - 1 ] ; if ( $ P [ $ i ] [ $ j ] == true ) $ C [ $ i ] [ $ j ] = 0 ; else { $ C [ $ i ] [ $ j ] = PHP_INT_MAX ; for ( $ k = $ i ; $ k <= $ j - 1 ; $ k ++ ) $ C [ $ i ] [ $ j ] = min ( $ C [ $ i ] [ $ j ] , $ C [ $ i ] [ $ k ] + $ C [ $ k + 1 ] [ $ j ] + 1 ) ; } } } return $ C [ 0 ] [ $ n - 1 ] ; } $ str = " ababbbabbababa " ; echo " Min ▁ cuts ▁ needed ▁ for ▁ Palindrome ▁ Partitioning ▁ is ▁ " . minPalPartion ( $ str ) ; return 0 ; ? >


"Palindromic Primes | A function that returns true only if num contains one digit ; comparison operation is faster than division operation . So using following instead of " return ▁ num ▁ / ▁ 10 ▁ = = ▁ 0 ; " ; A recursive function to find out whether num is palindrome or not . Initially , dupNum contains address of a copy of num . ; Base case ( needed for recursion termination ) : This statement / mainly compares the first digit with the last digit ; This is the key line in this method . Note that all recursive / calls have a separate copy of num , but they all share same copy of dupNum . We divide num while moving up the recursion tree ; The following statements are executed when we move up the recursion call tree ; At this point , if num % 10 contains ith digit from beginning , then ( dupNum ) % 10 contains ith digit from end ; The main function that uses recursive function isPalUtil ( ) to find out whether num is palindrome or not ; If num is negative , make it positive ; Create a separate copy of num , so that modifications made to address dupNum don 't change the input number.  $dupNum = $num; dupNum = num ; Function to generate all primes and checking whether number is palindromic or not ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Print all palindromic prime numbers ; checking whether the given number is prime palindromic or not ; Driver Code"
How can the above be solved in PHP?

 < ? php function oneDigit ( $ num ) { return ( $ num >= 0 && $ num < 10 ) ; } function isPalUtil ( $ num , $ dupNum ) { if ( oneDigit ( $ num ) ) return ( $ num == ( $ dupNum ) % 10 ) ; if ( ! isPalUtil ( ( int ) ( $ num / 10 ) , $ dupNum ) ) return false ; $ dupNum = ( int ) ( $ dupNum / 10 ) ; return ( $ num % 10 == ( $ dupNum ) % 10 ) ; } function isPal ( $ num ) { if ( $ num < 0 ) $ num = - $ num ; return isPalUtil ( $ num , $ dupNum ) ; } function printPalPrimesLessThanN ( $ n ) { $ prime = array_fill ( 0 , $ n + 1 , true ) ; for ( $ p = 2 ; $ p * $ p <= $ n ; $ p ++ ) { if ( $ prime [ $ p ] ) { for ( $ i = $ p * 2 ; $ i <= $ n ; $ i += $ p ) { $ prime [ $ i ] = false ; } } } for ( $ p = 2 ; $ p <= $ n ; $ p ++ ) { if ( $ prime [ $ p ] && isPal ( $ p ) ) { print ( $ p . " " ) ; } } } $ n = 100 ; print ( " Palindromic ▁ primes ▁ smaller ▁ " . " than ▁ or ▁ equal ▁ to ▁ " . $ n . " ▁ are ▁ :
 " ) ; printPalPrimesLessThanN ( $ n ) ; ? >


"Panalphabetic window in a string | Return if given string contain panalphabetic window . ; traversing the string ; if character of string is equal to ch , increment ch . ; if all characters are found , return true . ; Driver Code"
How can the above be solved in PHP?

< ? php function isPanalphabeticWindow ( $ s , $ n ) { $ ch = ' a ' ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ s [ $ i ] == $ ch ) $ ch ++ ; if ( $ ch == ' z ' ) return true ; } return false ; } $ s = " abujm ▁ zvcd ▁ acefc ▁ deghf ▁ gijkle " . " ▁ m ▁ n ▁ o ▁ p ▁ pafqrstuvwxyzfap " ; $ n = strlen ( $ s ) ; if ( isPanalphabeticWindow ( $ s , $ n ) ) echo ( " YES " ) ; else echo ( " NO " ) ; ? >


"Pancake sorting | Reverses arr [ 0. . i ] ; Returns index of the maximum element in arr [ 0. . n - 1 ] ; The main function that sorts given array using flip operations ; Start from the complete array and one by one reduce current size by one ; Find index of the maximum element in arr [ 0. . curr_size - 1 ] ; Move the maximum element to end of current array if it 's not already  at the end ; To move at the end , first move maximum number to beginning ; Now move the maximum number to end by reversing current array ; A utility function to print n array of size n ; Driver code"
How can the above be solved in PHP?

 < ? php function flip ( & $ arr , $ i ) { $ start = 0 ; while ( $ start < $ i ) { $ temp = $ arr [ $ start ] ; $ arr [ $ start ] = $ arr [ $ i ] ; $ arr [ $ i ] = $ temp ; $ start ++ ; $ i -- ; } } function findMax ( $ arr , $ n ) { $ mi = 0 ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) if ( $ arr [ $ i ] > $ arr [ $ mi ] ) $ mi = $ i ; return $ mi ; } function pancakeSort ( & $ arr , $ n ) { for ( $ curr_size = $ n ; $ curr_size > 1 ; -- $ curr_size ) { $ mi = findMax ( $ arr , $ curr_size ) ; if ( $ mi != $ curr_size - 1 ) { flip ( $ arr , $ mi ) ; flip ( $ arr , $ curr_size -1 ) ; } } } function printArray ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; ++ $ i ) print ( $ arr [ $ i ] . " ▁ " ) ; } $ arr = array ( 23 , 10 , 20 , 11 , 12 , 6 , 7 ) ; $ n = count ( $ arr ) ; pancakeSort ( $ arr , $ n ) ; echo ( " Sorted ▁ Array ▁
 " ) ; printArray ( $ arr , $ n ) ; return 0 ; ? >


"Pandigital Product | To check the string formed from multiplicand , multiplier and product is pandigital ; calculate the multiplicand , multiplier , and product eligible for pandigital ; Driver Code"
How can the above be solved in PHP?

< ? php function isPandigital ( $ str ) { if ( strlen ( $ str ) != 9 ) return false ; $ x = str_split ( $ str ) ; sort ( $ x ) ; $ x = implode ( $ x ) ; return strcmp ( $ x , "123456789" ) ; } function PandigitalProduct_1_9 ( $ n ) { for ( $ i = 1 ; $ i * $ i <= $ n ; $ i ++ ) if ( $ n % $ i == 0 && isPandigital ( strval ( $ n ) . strval ( $ i ) . strval ( ( int ) ( $ n / $ i ) ) ) ) return true ; return false ; } $ n = 6050 ; if ( PandigitalProduct_1_9 ( $ n ) ) echo " yes " ; else echo " no " ; ? >


"Partition an array such into maximum increasing segments | Returns the maximum number of sorted subarrays in a valid partition ; Find minimum value from right for every index ; Finding the shortest prefix such that all the elements in the prefix are less than or equal to the elements in the rest of the array . ; if current max is less than the right prefix min , we increase number of partitions . ; Driver code ; Find minimum value from right for every index"
How can the above be solved in PHP?

 < ? php function sorted_partitions ( $ arr , $ n ) { $ right_min [ $ n + 1 ] = array ( ) ; $ right_min [ $ n ] = PHP_INT_MAX ; for ( $ i = $ n - 1 ; $ i >= 0 ; $ i -- ) { $ right_min [ $ i ] = min ( $ right_min [ $ i + 1 ] , $ arr [ $ i ] ) ; } $ partitions = 0 ; for ( $ current_max = $ arr [ 0 ] , $ i = 0 ; $ i < $ n ; $ i ++ ) { $ current_max = max ( $ current_max , $ arr [ $ i ] ) ; if ( $ current_max <= $ right_min [ $ i + 1 ] ) $ partitions ++ ; } return $ partitions ; } $ arr = array ( 3 , 1 , 2 , 4 , 100 , 7 , 9 ) ; $ n = sizeof ( $ arr ) ; $ ans = sorted_partitions ( $ arr , $ n ) ; echo $ ans , "
 " ; ? >


"Partition into two subarrays of lengths k and ( N | Function to calculate max_difference ; Sum of the array ; Sort the array in descending order ; Calculating max_difference ; Driver Code"
How can the above be solved in PHP?

< ? php function maxDifference ( $ arr , $ N , $ k ) { $ M ; $ S = 0 ; $ S1 = 0 ; $ max_difference = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) $ S += $ arr [ $ i ] ; rsort ( $ arr ) ; $ M = max ( $ k , $ N - $ k ) ; for ( $ i = 0 ; $ i < $ M ; $ i ++ ) $ S1 += $ arr [ $ i ] ; $ max_difference = $ S1 - ( $ S - $ S1 ) ; return $ max_difference ; } $ arr = array ( 8 , 4 , 5 , 2 , 10 ) ; $ N = count ( $ arr ) ; $ k = 2 ; echo maxDifference ( $ arr , $ N , $ k ) ; ? >


"Partition the array into three equal sum segments | First segment 's end index ; Third segment 's start index ; This function returns true if the array can be divided into three equal sum segments ; Prefix Sum Array ; Suffix Sum Array ; Stores the total sum of the array ; We can also take pre [ pos2 - 1 ] - pre [ pos1 ] == total_sum / 3 here . ; Driver Code"
How can the above be solved in PHP?

 < ? php $ pos1 = -1 ; $ pos2 = -1 ; function equiSumUtil ( $ arr ) { global $ pos2 , $ pos1 ; $ n = count ( $ arr ) ; $ pre = array_fill ( 0 , $ n , 0 ) ; $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ sum += $ arr [ $ i ] ; $ pre [ $ i ] = $ sum ; } $ suf = array_fill ( 0 , $ n , 0 ) ; $ sum = 0 ; for ( $ i = $ n - 1 ; $ i >= 0 ; $ i -- ) { $ sum += $ arr [ $ i ] ; $ suf [ $ i ] = $ sum ; } $ total_sum = $ sum ; $ i = 0 ; $ j = $ n - 1 ; while ( $ i < $ j - 1 ) { if ( $ pre [ $ i ] == $ total_sum / 3 ) { $ pos1 = $ i ; } if ( $ suf [ $ j ] == $ total_sum / 3 ) { $ pos2 = $ j ; } if ( $ pos1 != -1 && $ pos2 != -1 ) { if ( $ suf [ $ pos1 + 1 ] - $ suf [ $ pos2 ] == $ total_sum / 3 ) { return true ; } else { return false ; } } if ( $ pre [ $ i ] < $ suf [ $ j ] ) { $ i ++ ; } else { $ j -- ; } } return false ; } function equiSum ( $ arr ) { global $ pos2 , $ pos1 ; $ ans = equiSumUtil ( $ arr ) ; if ( $ ans ) { print ( " First ▁ Segment ▁ : ▁ " ) ; for ( $ i = 0 ; $ i <= $ pos1 ; $ i ++ ) { print ( $ arr [ $ i ] . " " ) ; } print ( "
 " ) ; print ( " Second ▁ Segment ▁ : ▁ " ) ; for ( $ i = $ pos1 + 1 ; $ i < $ pos2 ; $ i ++ ) { print ( $ arr [ $ i ] . " " ) ; } print ( "
 " ) ; print ( " Third ▁ Segment ▁ : ▁ " ) ; for ( $ i = $ pos2 ; $ i < count ( $ arr ) ; $ i ++ ) { print ( $ arr [ $ i ] . " " ) ; } print ( "
 " ) ; } else { println ( " Array ▁ cannot ▁ be ▁ divided ▁ into ▁ " , " three ▁ equal ▁ sum ▁ segments " ) ; } } $ arr = array ( 1 , 3 , 6 , 2 , 7 , 1 , 2 , 8 ) ; equiSum ( $ arr ) ; ? >


"Path with maximum average value | method returns maximum average of all path of cost matrix ; Initialize first column of total cost ( dp ) array ; Initialize first row of dp array ; Construct rest of the dp array ; divide maximum sum by constant path length : ( 2 N - 1 ) for getting average ; Driver code"
How can the above be solved in PHP?


"Pentagonal Pyramidal Number | function to get nth Pentagonal pyramidal number . ; Driver Code"
How can the above be solved in PHP?

< ? php function pentagon_pyramidal ( $ n ) { return $ n * $ n * ( $ n + 1 ) / 2 ; } $ n = 4 ; echo pentagon_pyramidal ( $ n ) ; ? >


"Pentagonal Pyramidal Number | function to get nth Pentagonal pyramidal number . ; Running loop from 1 to n ; get nth pentagonal number ; add to sum ; Driver Code"
How can the above be solved in PHP?

< ? php function pentagon_pyramidal ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ p = ( 3 * $ i * $ i - $ i ) / 2 ; $ sum = $ sum + $ p ; } return $ sum ; } $ n = 4 ; echo pentagon_pyramidal ( $ n ) ; ? >


"Pentatope number | Function that returns nth pentatope number ; For 5 th PentaTope Number ; For 11 th PentaTope Number"
How can the above be solved in PHP?

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


"Pentatope number | function for Pentatope number ; formula for find Pentatope nth term ; Driver Code"
How can the above be solved in PHP?

 < ? php function Pentatope_number ( $ n ) { return $ n * ( $ n + 1 ) * ( $ n + 2 ) * ( $ n + 3 ) / 24 ; } $ n = 7 ; echo $ n , " th ▁ Pentatope ▁ number ▁ : " , Pentatope_number ( $ n ) , "
 " ; $ n = 12 ; echo $ n , " th ▁ Pentatope ▁ number ▁ : " , Pentatope_number ( $ n ) ; ? >


"Perfect Number | Returns true if n is perfect ; To store sum of divisors ; Find all divisors and add them ; If sum of divisors is equal to n , then n is a perfect number ; Driver Code"
How can the above be solved in PHP?

 < ? php function isPerfect ( $ n ) { $ sum = 1 ; for ( $ i = 2 ; $ i * $ i <= $ n ; $ i ++ ) { if ( $ n % $ i == 0 ) { if ( $ i * $ i != $ n ) $ sum = $ sum + $ i + ( int ) ( $ n / $ i ) ; else $ sum = $ sum + $ i ; } } if ( $ sum == $ n && $ n != 1 ) return true ; return false ; } echo " Below ▁ are ▁ all ▁ perfect ▁ numbers ▁ till ▁ 10000
 " ; for ( $ n = 2 ; $ n < 10000 ; $ n ++ ) if ( isPerfect ( $ n ) ) echo " $ n ▁ is ▁ a ▁ perfect ▁ number
 " ; ? >


"Perfect Square String | PHP program to find if string is a perfect square or not . ; calculating the length of the string ; calculating the ASCII value of the string ; Find floating point value of square root of x . ; If square root is an integer ; Driver code"
How can the above be solved in PHP?

< ? php function isPerfectSquareString ( $ str ) { $ sum = 0 ; $ len = strlen ( $ str ) ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) $ sum += ( int ) $ str [ $ i ] ; $ squareRoot = sqrt ( $ sum ) ; return ( ( $ squareRoot - floor ( $ squareRoot ) ) == 0 ) ; } $ str = " d " ; if ( isPerfectSquareString ( $ str ) ) echo " Yes " ; else echo " No " ; ? >


"Perfect cubes in a range | A Simple Method to count cubes between a and b ; Traverse through all numbers in given range and one by one check if number is prime ; Check if current number ' i ' is perfect cube ; Driver Code"
How can the above be solved in PHP?

 < ? php function printCubes ( $ a , $ b ) { for ( $ i = $ a ; $ i <= $ b ; $ i ++ ) { for ( $ j = 1 ; $ j * $ j * $ j <= $ i ; $ j ++ ) { if ( $ j * $ j * $ j == $ i ) { echo $ j * $ j * $ j , " " ; break ; } } } } $ a = 1 ; $ b = 100 ; echo " Perfect ▁ cubes ▁ in ▁ given ▁ range :
 ▁ " ; printCubes ( $ a , $ b ) ; ? >