"Most frequent element in an array | PHP program to find the most frequent element in an array . ; Sort the array ; find the max frequency using linear traversal ; If last element is most frequent ; Driver Code"
Solution in php:

< ? php function mostFrequent ( $ arr , $ n ) { sort ( $ arr ) ; sort ( $ arr , $ n ) ; $ max_count = 1 ; $ res = $ arr [ 0 ] ; $ curr_count = 1 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] == $ arr [ $ i - 1 ] ) $ curr_count ++ ; else { if ( $ curr_count > $ max_count ) { $ max_count = $ curr_count ; $ res = $ arr [ $ i - 1 ] ; } $ curr_count = 1 ; } } if ( $ curr_count > $ max_count ) { $ max_count = $ curr_count ; $ res = $ arr [ $ n - 1 ] ; } return $ res ; } { $ arr = array ( 1 , 5 , 2 , 1 , 3 , 2 , 1 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; echo mostFrequent ( $ arr , $ n ) ; return 0 ; } ? >


"Motzkin number | Return the nth Motzkin Number . ; Base Case ; Recursive step ; Driven Code"
Solution in php:

< ? php function motzkin ( $ n ) { if ( $ n == 0 $ n == 1 ) return 1 ; return ( ( 2 * $ n + 1 ) * motzkin ( $ n - 1 ) + ( 3 * $ n - 3 ) * motzkin ( $ n - 2 ) ) / ( $ n + 2 ) ; } $ n = 8 ; echo ( motzkin ( $ n ) ) ; ? >


"Motzkin number | Return the nth Motzkin Number . ; Base case ; Finding i - th Motzkin number . ; Driven Code"
Solution in php:


"Move all negative elements to end in order with extra space allowed | Moves all - ve element to end of array in same order . ; Create an empty array to store result ; Traversal array and store + ve element in temp array index of temp ; If array contains all positive or all negative . ; Store - ve element in temp array ; Copy contents of temp [ ] to arr [ ] ; Driver Code"
Solution in php:


"Move all negative numbers to beginning and positive to end with constant extra space | A PHP program to put all negative numbers before positive numbers ; A utility function to print an array ; Driver code"
Solution in php:

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


"Multiple of x closest to n | Function to calculate the smallest multiple ; Driver Code"
Solution in php:

< ? php function closestMultiple ( $ n , $ x ) { if ( $ x > $ n ) return $ x ; $ n = $ n + $ x / 2 ; $ n = $ n - ( $ n % $ x ) ; return $ n ; } $ n = 9 ; $ x = 4 ; echo closestMultiple ( $ n , $ x ) ; ? >


"Multiples of 3 or 7 | Returns count of all numbers smaller than or equal to n and multiples of 3 or 7 or both ; Driver code"
Solution in php:

< ? php function countMultiples ( $ n ) { return floor ( $ n / 3 + $ n / 7 - $ n / 21 ) ; } echo " Count = " ? >


"Multiples of 4 ( An Interesting Method ) | Returns true if n is a multiple of 4. ; Find XOR of all numbers from 1 to n ; If XOR is equal n , then return true ; Printing multiples of 4 using above method"
Solution in php:

< ? php function isMultipleOf4 ( $ n ) { if ( $ n == 1 ) return false ; $ XOR = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ XOR = $ XOR ^ $ i ; return ( $ XOR == $ n ) ; } for ( $ n = 0 ; $ n <= 42 ; $ n ++ ) if ( isMultipleOf4 ( $ n ) ) echo $ n , " ▁ " ; ? >


"Multiples of 4 ( An Interesting Method ) | Returns true if n is a multiple of 4. ; Printing multiples of 4 using above method"
Solution in php:

< ? php function isMultipleOf4 ( $ n ) { if ( $ n == 0 ) return true ; return ( ( ( $ n >> 2 ) << 2 ) == $ n ) ; } for ( $ n = 0 ; $ n <= 42 ; $ n ++ ) if ( isMultipleOf4 ( $ n ) ) echo $ n , " ▁ " ; ? >


"Multiplication of two complex numbers given as strings | PHP program to multiply two complex numbers given as strings . ; Spiting the real and imaginary parts of the given complex strings based on ' + ' and ' i ' symbols . ; Storing the real part of complex string a ; Storing the imaginary part of complex string a ; Storing the real part of complex string b ; Storing the imaginary part of complex string b ; Returns the product . ; Driver Code"
Solution in php:

< ? php function complexNumberMultiply ( $ a , $ b ) { $ x = preg_split ( " / [ \s + ] + ▁ i / " , $ a ) ; $ y = preg_split ( " / [ \s + ] + ▁ i / " , $ b ) ; $ a_real = intval ( $ x [ 0 ] ) ; $ a_img = intval ( $ x [ 1 ] ) ; $ b_real = intval ( $ y [ 0 ] ) ; $ b_img = intval ( $ y [ 1 ] ) ; return ( $ a_real * $ b_real - $ a_img * $ b_img ) . " + " ( $ a_real * $ b_img + $ a_img * $ b_real ) . " i " ; } $ str1 = "1 + 1i " ; $ str2 = "1 + 1i " ; echo complexNumberMultiply ( $ str1 , $ str2 ) ; ? >


"Multiplication of two numbers with shift operator | Function for multiplication ; check for set bit and left shift n , count times ; increment of place value ( count ) ; Driver code"
Solution in php:

< ? php function multiply ( $ n , $ m ) { $ ans = 0 ; $ count = 0 ; while ( $ m ) { if ( $ m % 2 == 1 ) $ ans += $ n << $ count ; $ count ++ ; $ m /= 2 ; } return $ ans ; } $ n = 20 ; $ m = 13 ; echo multiply ( $ n , $ m ) ; ? >


"Multiplication with a power of 2 | Efficient PHP program to compute x * ( 2 ^ n ) ; Driver Code"
Solution in php:

< ? php function multiply ( $ x , $ n ) { return $ x << $ n ; } $ x = 70 ; $ n = 2 ; echo multiply ( $ x , $ n ) ; ? >


"Multiplication with a power of 2 | Returns 2 raised to power n ; Driver Code"
Solution in php:

< ? php function power2 ( $ n ) { if ( $ n == 0 ) return 1 ; if ( $ n == 1 ) return 2 ; return power2 ( $ n / 2 ) * power2 ( $ n / 2 ) ; } function multiply ( $ x , $ n ) { return $ x * power2 ( $ n ) ; } $ x = 70 ; $ n = 2 ; echo multiply ( $ x , $ n ) ; ? >


"Multiplicative order | function for GCD ; Function return smallest + ve integer that holds condition A ^ k ( mod N ) = 1 ; result store power of A that rised to the power N - 1 ; modular arithmetic ; return smallest + ve integer ; increment power ; Driver Code"
Solution in php:

< ? php function GCD ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; return GCD ( $ b , $ a % $ b ) ; } function multiplicativeOrder ( $ A , $ N ) { if ( GCD ( $ A , $ N ) != 1 ) return -1 ; $ result = 1 ; $ K = 1 ; while ( $ K < $ N ) { $ result = ( $ result * $ A ) % $ N ; if ( $ result == 1 ) return $ K ; $ K ++ ; } return -1 ; } $ A = 4 ; $ N = 7 ; echo ( multiplicativeOrder ( $ A , $ N ) ) ; ? >


"Multiply a given Integer with 3.5 | PHP program to multiply a number with 3.5 ; Driver Code"
Solution in php:

< ? php function multiplyWith3Point5 ( $ x ) { return ( $ x << 1 ) + $ x + ( $ x >> 1 ) ; } $ x = 4 ; echo multiplyWith3Point5 ( $ x ) ; ? >


"Multiply a number by 15 without using * and / operators | Function to return ( 15 * N ) without using ' * ' or ' / ' operator ; prod = 16 * n ; ( ( 16 * n ) - n ) = 15 * n ; Driver code"
Solution in php:

< ? php function multiplyByFifteen ( $ n ) { $ prod = ( $ n << 4 ) ; $ prod = $ prod - $ n ; return $ prod ; } $ n = 7 ; echo multiplyByFifteen ( $ n ) ; ? >


"Multiply a number with 10 without using multiplication operator | Function to find multiplication of n with 10 without using multiplication operator ; Driver Code"
Solution in php:

< ? php function multiplyTen ( $ n ) { return ( $ n << 1 ) + ( $ n << 3 ) ; } $ n = 50 ; echo multiplyTen ( $ n ) ; ? >


"Multiply any Number with 4 using Bitwise Operator | function the return multiply a number with 4 using bitwise operator ; returning a number with multiply with 4 using2 bit shifting right ; Driver Code"
Solution in php:

 < ? php function multiplyWith4 ( $ n ) { return ( $ n << 2 ) ; } $ n = 4 ; echo multiplyWith4 ( $ n ) , "
 " ; ? >


"Multiply large integers under large modulo | Returns ( a * b ) % mod ; Update a if it is more than or equal to mod ; If b is odd , add a with result ; Here we assume that doing 2 * a doesn 't cause overflow ; $b >>= 1 ; b = b / 2 ; Driver Code"
Solution in php:


"MÃ ¼ nchhausen Number | pwr [ i ] is going to store i raised to power i . ; Function to check out whether the number is MA14nchhausen Number or not ; Precompute i raised to power i for every i ; The input here is fixed i . e . it will check up to n ; check the integer for MA14nchhausen Number , if yes then print out the number ; Driver Code"
Solution in php:

 < ? php $ pwr = array_fill ( 0 , 10 , 0 ) ; function isMunchhausen ( $ n ) { global $ pwr ; $ sm = 0 ; $ temp = $ n ; while ( $ temp ) { $ sm = $ sm + $ pwr [ ( $ temp % 10 ) ] ; $ temp = ( int ) ( $ temp / 10 ) ; } return ( $ sm == $ n ) ; } function printMunchhausenNumbers ( $ n ) { global $ pwr ; for ( $ i = 0 ; $ i < 10 ; $ i ++ ) $ pwr [ $ i ] = pow ( ( float ) ( $ i ) , ( float ) ( $ i ) ) ; for ( $ i = 1 ; $ i < $ n + 1 ; $ i ++ ) if ( isMunchhausen ( $ i ) ) print ( $ i . "
 " ) ; } $ n = 10000 ; printMunchhausenNumbers ( $ n ) ; ? >


"N 'th Smart Number | Limit on result ; Function to calculate n 'th smart number  ; Initialize all numbers as not prime ; iterate to mark all primes and smart number ; Traverse all numbers till maximum limit ; ' i ' is maked as prime number because it is not multiple of any other prime ; mark all multiples of ' i ' as non prime ; If i is the third prime factor of j then add it to result as it has at least three prime factors . ; Sort all smart numbers ; return n 'th smart number  ; Driver program to run the case"
Solution in php:

< ? php $ MAX = 3000 ; function smartNumber ( $ n ) { global $ MAX ; $ primes = array_fill ( 0 , $ MAX , 0 ) ; $ result = array ( ) ; for ( $ i = 2 ; $ i < $ MAX ; $ i ++ ) { if ( $ primes [ $ i ] == 0 ) { $ primes [ $ i ] = 1 ; for ( $ j = $ i * 2 ; $ j < $ MAX ; $ j = $ j + $ i ) { $ primes [ $ j ] -= 1 ; if ( ( $ primes [ $ j ] + 3 ) == 0 ) array_push ( $ result , $ j ) ; } } } sort ( $ result ) ; return $ result [ $ n - 1 ] ; } $ n = 50 ; echo smartNumber ( $ n ) ; ? >


"N 'th palindrome of K digits | PHP program of finding nth palindrome of k digit ; Determine the first half digits ; Print the first half digits of palindrome ; If k is odd , truncate the last digit ; print the last half digits of palindrome ; Driver code"
Solution in php:

 < ? php function nthPalindrome ( $ n , $ k ) { $ temp = ( $ k & 1 ) ? ( int ) ( $ k / 2 ) : ( int ) ( $ k / 2 - 1 ) ; $ palindrome = ( int ) pow ( 10 , $ temp ) ; $ palindrome += $ n - 1 ; print ( $ palindrome ) ; if ( $ k & 1 ) $ palindrome = ( int ) ( $ palindrome / 10 ) ; while ( $ palindrome > 0 ) { print ( $ palindrome % 10 ) ; $ palindrome = ( int ) ( $ palindrome / 10 ) ; } print ( "
 " ) ; } $ n = 6 ; $ k = 5 ; print ( $ n . " th ▁ palindrome ▁ of ▁ $ k ▁ digit ▁ = ▁ " ) ; nthPalindrome ( $ n , $ k ) ; $ n = 10 ; $ k = 6 ; print ( $ n . " th ▁ palindrome ▁ of ▁ $ k ▁ digit ▁ = ▁ " ) ; nthPalindrome ( $ n , $ k ) ; ? >


"N 'th palindrome of K digits | Utility function to reverse the number n ; Boolean Function to check for palindromic number ; Function for finding nth palindrome of k digits ; Get the smallest k digit number ; check the number is palindrom or not ; if n 'th palindrome found  break the loop ; Increment number for checking next palindrome ; Driver code"
Solution in php:

 < ? php function reverseNum ( $ n ) { $ rem ; $ rev = 0 ; while ( $ n ) { $ rem = $ n % 10 ; $ rev = ( $ rev * 10 ) + $ rem ; $ n = ( int ) ( $ n / 10 ) ; } return $ rev ; } function isPalindrom ( $ num ) { return $ num == reverseNum ( $ num ) ; } function nthPalindrome ( $ n , $ k ) { $ num = pow ( 10 , $ k - 1 ) ; while ( true ) { if ( isPalindrom ( $ num ) ) -- $ n ; if ( ! $ n ) break ; ++ $ num ; } return $ num ; } $ n = 6 ; $ k = 5 ; echo $ n , " th ▁ palindrome ▁ of ▁ " , $ k , " ▁ digit ▁ = ▁ " , nthPalindrome ( $ n , $ k ) , "
 " ; $ n = 10 ; $ k = 6 ; echo $ n , " th ▁ palindrome ▁ of ▁ " , $ k , " ▁ digit ▁ = ▁ " , nthPalindrome ( $ n , $ k ) , "
 " ; ? >


"N / 3 repeated number in an array with O ( 1 ) space | PHP program to find if any element appears more than n / 3. ; take the integers as the maximum value of integer hoping the integer would not be present in the array ; if this element is previously seen , increment count1 . ; if this element is previously seen , increment count2 . ; if current element is different from both the previously seen variables , decrement both the counts . ; Again traverse the array and find the actual counts . ; Driver code"
Solution in php:

< ? php function appearsNBy3 ( $ arr , $ n ) { $ count1 = 0 ; $ count2 = 0 ; $ first = PHP_INT_MAX ; $ second = PHP_INT_MAX ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ first == $ arr [ $ i ] ) $ count1 ++ ; else if ( $ second == $ arr [ $ i ] ) $ count2 ++ ; else if ( $ count1 == 0 ) { $ count1 ++ ; $ first = $ arr [ $ i ] ; } else if ( $ count2 == 0 ) { $ count2 ++ ; $ second = $ arr [ $ i ] ; } else { $ count1 -- ; $ count2 -- ; } } $ count1 = 0 ; $ count2 = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] == $ first ) $ count1 ++ ; else if ( $ arr [ $ i ] == $ second ) $ count2 ++ ; } if ( $ count1 > $ n / 3 ) return $ first ; if ( $ count2 > $ n / 3 ) return $ second ; return -1 ; } $ arr = array ( 1 , 2 , 3 , 1 , 1 ) ; $ n = count ( $ arr ) ; echo appearsNBy3 ( $ arr , $ n ) ; ? >


"N digit numbers divisible by 5 formed from the M digits | Function to find the count of all possible N digit numbers which are divisible by 5 formed from M digits ; If it is not possible to form n digit number from the given m digits without repetition ; If both zero and five exists ; Remaining N - 1 iterations ; Remaining N - 1 iterations ; Driver code"
Solution in php:

< ? php function numbers ( $ n , $ arr , $ m ) { $ isZero = 0 ; $ isFive = 0 ; $ result = 0 ; if ( $ m < $ n ) { return -1 ; } for ( $ i = 0 ; $ i < $ m ; $ i ++ ) { if ( $ arr [ $ i ] == 0 ) $ isZero = 1 ; if ( $ arr [ $ i ] == 5 ) $ isFive = 1 ; } if ( $ isZero && $ isFive ) { $ result = 2 ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { $ result = $ result * ( -- $ m ) ; } } else if ( $ isZero $ isFive ) { $ result = 1 ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { $ result = $ result * ( -- $ m ) ; } } else $ result = -1 ; return $ result ; } $ n = 3 ; $ m = 6 ; $ arr = array ( 2 , 3 , 5 , 6 , 7 , 9 ) ; echo numbers ( $ n , $ arr , $ m ) ; ? >


"N | Function to find the N - th term ; calculates the N - th term ; Driver Code"
Solution in php:

< ? php function numberSequence ( $ n ) { $ num = pow ( 4 , $ n ) - pow ( 2 , $ n ) - 1 ; return $ num ; } $ n = 4 ; echo numberSequence ( $ n ) ; ? >


"N | Function to return the decimal value of a binary number ; Initializing base value to 1 , i . e 2 ^ 0 ; find the binary representation of the N - th number in sequence ; base case ; answer string ; add n - 1 1 's ; add 0 ; add n 1 's at end ; Driver Code"
Solution in php:

< ? php function binaryToDecimal ( $ n ) { $ num = $ n ; $ dec_value = 0 ; $ base = 1 ; $ len = strlen ( $ num ) ; for ( $ i = $ len - 1 ; $ i >= 0 ; $ i -- ) { if ( $ num [ $ i ] == '1' ) $ dec_value += $ base ; $ base = $ base * 2 ; } return $ dec_value ; } function numberSequence ( $ n ) { if ( $ n == 1 ) return 1 ; $ s = " " ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ s . = '1' ; $ s . = '0' ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ s . = '1' ; $ num = binaryToDecimal ( $ s ) ; return $ num ; } $ n = 4 ; echo numberSequence ( $ n ) ; ? >


"N | PHP program to find N - th term in George Cantor set of rational numbers ; let i = numerator ; let j = denominator ; to keep the check of no . of terms ; loop till k is not equal to n ; check if k is already equal to N then the first term is the required rational number ; loop for traversing from right to left downwards diagonally ; loop for traversing from left to right upwards diagonally ; Driver Code"
Solution in php:


"N | PHP program to find n - th number which is both square and cube . ; Driver code"
Solution in php:

< ? php function nthSquareCube ( $ n ) { return $ n * $ n * $ n * $ n * $ n * $ n ; } $ n = 5 ; echo ( nthSquareCube ( $ n ) ) ; ? >


"N | Return the n - th number in the sorted list of multiples of two numbers . ; Generating first n multiple of a . ; Sorting the sequence . ; Generating and storing first n multiple of b and storing if not present in the sequence . ; If not present in the sequence ; Storing in the sequence . ; Driver Code"
Solution in php:

< ? php function nthElement ( $ a , $ b , $ n ) { $ seq = array ( ) ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) array_push ( $ seq , $ a * $ i ) ; sort ( $ seq ) ; for ( $ i = 1 , $ k = $ n ; $ i <= $ n && $ k > 0 ; $ i ++ ) { if ( array_search ( $ b * $ i , $ seq ) == 0 ) { array_push ( $ seq , $ b * $ i ) ; sort ( $ seq ) ; $ k -- ; } } return $ seq [ $ n - 1 ] ; } $ a = 3 ; $ b = 5 ; $ n = 5 ; echo nthElement ( $ a , $ b , $ n ) ; ? >


"N | function to evaluate Nth polite number ; Driver code"
Solution in php:

< ? php function polite ( $ n ) { $ n += 1 ; $ base = 2 ; return $ n + ( log ( ( $ n + ( log ( $ n ) / log ( $ base ) ) ) ) ) / log ( $ base ) ; } $ n = 7 ; echo ( ( int ) polite ( $ n ) ) ; ? >


"Naive algorithm for Pattern Searching | PHP program for Naive Pattern Searching algorithm ; A loop to slide pat [ ] one by one ; For current index i , check for pattern match ; if pat [ 0. . . M - 1 ] = txt [ i , i + 1 , ... i + M - 1 ] ; Driver Code"
Solution in php:

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


"Narcissistic number | Function to count digits ; Returns true if n is Narcissistic number ; count the number of digits ; calculates the sum of digits raised to power ; Driver Code"
Solution in php:

< ? php function countDigit ( $ n ) { if ( $ n == 0 ) return 0 ; return ( 1 + countDigit ( $ n / 10 ) ) ; } function check ( $ n ) { $ l = countDigit ( $ n ) ; $ dup = $ n ; $ sum = 0 ; while ( $ dup ) { $ sum += pow ( $ dup % 10 , $ l ) ; $ dup = ( int ) $ dup / 10 ; } return ( $ n == $ sum ) ; } $ n = 1634 ; if ( check ( ! $ n ) ) echo " yes " ; else echo " no " ; ? >


"Natural Numbers | Returns sum of first n natural numbers ; Driver code"
Solution in php:

< ? php function findSum ( $ n ) { $ sum = 0 ; for ( $ x = 1 ; $ x <= $ n ; $ x ++ ) $ sum = $ sum + $ x ; return $ sum ; } $ n = 5 ; echo findSum ( $ n ) ; ? >


"Nearest element with at | PHP program to print nearest element with at least one common prime factor . ; Loop covers the every element of arr [ ] ; Loop that covers from 0 to i - 1 and i + 1 to n - 1 indexes simultaneously ; print position of closest element ; Recursive function to return gcd of a and b ; Driver Code"
Solution in php:

< ? php function nearestGcd ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { $ closest = -1 ; for ( $ j = $ i - 1 , $ k = $ i + 1 ; $ j > 0 $ k <= $ n ; -- $ j , ++ $ k ) { if ( $ j >= 0 && __gcd ( $ arr [ $ i ] , $ arr [ $ j ] ) > 1 ) { $ closest = $ j + 1 ; break ; } if ( $ k < $ n && __gcd ( $ arr [ $ i ] , $ arr [ $ k ] ) > 1 ) { $ closest = $ k + 1 ; break ; } } echo $ closest . " " ; } } function __gcd ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; return __gcd ( $ b , $ a % $ b ) ; } $ arr = array ( 2 , 9 , 4 , 3 , 13 ) ; $ n = sizeof ( $ arr ) ; nearestGcd ( $ arr , $ n ) ; ? >


"Nesbitt 's Inequality | PHP code to verify Nesbitt 's Inequality ; 3 parts of the inequality sum ; Driver Code"
Solution in php:

 < ? php function isValidNesbitt ( $ a , $ b , $ c ) { $ A = $ a / ( $ b + $ c ) ; $ B = $ b / ( $ a + $ c ) ; $ C = $ c / ( $ a + $ b ) ; $ inequality = $ A + $ B + $ C ; return ( $ inequality >= 1.5 ) ; } $ a = 1.0 ; $ b = 2.0 ; $ c = 3.0 ; if ( isValidNesbitt ( $ a , $ b , $ c ) ) echo " Nesbitt ' s ▁ inequality ▁ satisfied . " , " for ▁ real ▁ numbers ▁ " , $ a , " , ▁ " , $ b , " , ▁ " , $ c , "
 " ; else cout << " Not ▁ satisfied " ; ? >


"Newman Shanks Williams prime | return nth Newman Shanks Williams prime ; Base case ; Finding nth Newman Shanks Williams prime ; Driver Code"
Solution in php:


"Newman | Function to find the n - th element ; Declare array to store sequence ; Driver Code"
Solution in php:


"Newman | Recursive Function to find the n - th element ; Driver Code"
Solution in php:

< ? php function sequence ( $ n ) { if ( $ n == 1 $ n == 2 ) return 1 ; else return sequence ( sequence ( $ n - 1 ) ) + sequence ( $ n - sequence ( $ n - 1 ) ) ; } $ n = 10 ; echo ( sequence ( $ n ) ) ; ? >


"Newmanâ €“ Shanksâ €“ Williams prime | return nth Newman a Shanks a Williams prime ; Base case ; Recursive step ; Driver Code"
Solution in php:

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


"Newton 's Divided Difference Interpolation Formula | Function to find the product term ; Function for calculating divided difference table ; Function for applying Newton 's  divided difference formula  ; Function for displaying divided difference table ; number of inputs given ; y [ ] [ ] is used for divided difference table where y [ ] [ 0 ] is used for input ; calculating divided difference table ; displaying divided difference table ; value to be interpolated ; printing the value"
Solution in php:


"Newton Forward And Backward Interpolation | Calculation of u mentioned in formula ; Calculating factorial of given n ; number of values given ; y [ ] [ ] is used for difference table and y [ ] [ 0 ] used for input ; Calculating the backward difference table ; Displaying the backward difference table ; Value to interpolate at ; Initializing u and sum"
Solution in php:


"Next Greater Element | prints element and NGE pair for all elements of arr [ ] of size n ; Driver Code"
Solution in php:


"Next Smaller Element | prints element and NSE pair for all elements of arr [ ] of size n ; Driver Code"
Solution in php:


"Next greater Number than N with the same quantity of digits A and B | Recursive function to find the required number ; If the resulting number is >= n and count of a = count of b , return the number ; select minimum of two and call the function again ; Function to find the number next greater Number than N with the same quantity of digits A and B ; Driver code"
Solution in php:

< ? php function findNumUtil ( $ res , $ a , $ aCount , $ b , $ bCount , $ n ) { if ( $ res > 100000000000 ) return 10000000000 ; if ( $ aCount == $ bCount && $ res >= $ n ) return $ res ; return min ( findNumUtil ( $ res * 10 + $ a , $ a , $ aCount + 1 , $ b , $ bCount , $ n ) , findNumUtil ( $ res * 10 + $ b , $ a , $ aCount , $ b , $ bCount + 1 , $ n ) ) ; } function findNum ( $ n , $ a , $ b ) { $ result = 0 ; $ aCount = 0 ; $ bCount = 0 ; return findNumUtil ( $ result , $ a , $ aCount , $ b , $ bCount , $ n ) ; } $ N = 4500 ; $ A = 4 ; $ B = 7 ; echo findNum ( $ N , $ A , $ B ) ; ? >


"Next greater integer having one more number of set bits | function to find the position of rightmost set bit . Returns - 1 if there are no set bits ; function to find the next greater integer ; position of rightmost unset bit of n by passing ~ n as argument ; if n consists of unset bits , then set the rightmost unset bit ; n does not consists of unset bits ; Driver Code"
Solution in php:

< ? php function getFirstSetBitPos ( $ n ) { return ( log ( $ n & - $ n + 1 ) ) - 1 ; } function nextGreaterWithOneMoreSetBit ( $ n ) { $ pos = getFirstSetBitPos ( ~ $ n ) ; if ( $ pos > -1 ) return ( 1 << $ pos ) | $ n ; return ( ( $ n << 1 ) + 1 ) ; } $ n = 10 ; echo " Next ▁ greater ▁ integer ▁ = ▁ " , nextGreaterWithOneMoreSetBit ( $ n ) ; ? >


"Next higher number using atmost one swap operation | function to find the next higher number using atmost one swap operation ; to store the index of the largest digit encountered so far from the right ; to store the index of rightmost digit which has a digit greater to it on its right side ; finding the ' index ' of rightmost digit which has a digit greater to it on its right side ; required digit found , store its ' index ' and break ; if no such digit is found which has a larger digit on its right side ; to store the index of the smallest digit greater than the digit at ' index ' and right to it ; finding the index of the smallest digit greater than the digit at ' index ' and right to it ; swapping the digits ; required number ; Driver Code"
Solution in php:

 < ? php function nxtHighUsingAtMostOneSwap ( $ num ) { $ l = strlen ( $ num ) ; $ posRMax = $ l - 1 ; $ index = -1 ; for ( $ i = $ l - 2 ; $ i >= 0 ; $ i -- ) { if ( $ num [ $ i ] >= $ num [ $ posRMax ] ) $ posRMax = $ i ; else { $ index = $ i ; break ; } } if ( $ index == -1 ) return " Not ▁ Possible " ; $ greatSmallDgt = -1 ; for ( $ i = $ l - 1 ; $ i > $ index ; $ i -- ) { if ( $ num [ $ i ] > $ num [ $ index ] ) { if ( $ greatSmallDgt == -1 ) $ greatSmallDgt = $ i ; else if ( $ num [ $ i ] <= $ num [ $ greatSmallDgt ] ) $ greatSmallDgt = $ i ; } } $ temp = $ num [ $ index ] ; $ num [ $ index ] = $ num [ $ greatSmallDgt ] ; $ num [ $ greatSmallDgt ] = $ temp ; return $ num ; } $ num = "218765" ; echo " Original ▁ number : ▁ " . $ num . "
 " ; echo " Next ▁ higher ▁ number : ▁ " . nxtHighUsingAtMostOneSwap ( $ num ) ; ? >


"Next higher number with same number of set bits | This function returns next higher number with same number of set bits as x . ; right most set bit ; reset the pattern and set next higher bit left part of x will be here ; nextHigherOneBit is now part [ D ] of the above explanation . isolate the pattern ; right adjust pattern ; correction factor ; rightOnesPattern is now part [ A ] of the above explanation . integrate new pattern ( Add [ D ] and [ A ] ) ; Driver Code"
Solution in php:

< ? php function snoob ( $ x ) { $ next = 0 ; if ( $ x ) { $ rightOne = $ x & - $ x ; $ nextHigherOneBit = $ x + $ rightOne ; $ rightOnesPattern = $ x ^ $ nextHigherOneBit ; $ rightOnesPattern = intval ( ( $ rightOnesPattern ) / $ rightOne ) ; $ rightOnesPattern >>= 2 ; $ next = $ nextHigherOneBit | $ rightOnesPattern ; } return $ next ; } $ x = 156 ; echo " Next ▁ higher ▁ number ▁ with ▁ same ▁ " . " number ▁ of ▁ set ▁ bits ▁ is ▁ " . snoob ( $ x ) ; ? >


"Next higher palindromic number using the same set of digits | function to reverse the digits in the range i to j in ' num ' ; function to find next higher palindromic number using the same set of digits ; if length of number is less than '3' then no higher palindromic number can be formed ; find the index of last digit in the 1 st half of ' num ' ; Start from the ( mid - 1 ) th digit and find the first digit that is smaller than the digit next to it . ; If no such digit is found , then all digits are in descending order which means there cannot be a greater palindromic number with same set of digits ; Find the smallest digit on right side of ith digit which is greater than num [ i ] up to index ' mid ' ; swap num [ i ] with num [ smallest ] ; as the number is a palindrome , the same swap of digits should be performed in the 2 nd half of ' num ' ; reverse digits in the range ( i + 1 ) to mid ; if n is even , then reverse digits in the range mid + 1 to n - i - 2 ; else if n is odd , then reverse digits in the range mid + 2 to n - i - 2 ; required next higher palindromic number ; Driver Code"
Solution in php:

< ? php function reverse ( & $ num , $ i , $ j ) { while ( $ i < $ j ) { $ t = $ num [ $ i ] ; $ num [ $ i ] = $ num [ $ j ] ; $ num [ $ j ] = $ t ; $ i ++ ; $ j -- ; } } function nextPalin ( $ num , $ n ) { if ( $ n <= 3 ) { echo " Not ▁ Possible " ; return ; } $ mid = ( $ n / 2 ) - 1 ; $ i = $ mid - 1 ; $ j ; for ( ; $ i >= 0 ; $ i -- ) if ( $ num [ $ i ] < $ num [ $ i + 1 ] ) break ; if ( $ i < 0 ) { echo " Not ▁ Possible " ; return ; } $ smallest = $ i + 1 ; $ j = 0 ; for ( $ j = $ i + 2 ; $ j <= $ mid ; $ j ++ ) if ( $ num [ $ j ] > $ num [ $ i ] && $ num [ $ j ] < $ num [ $ smallest ] ) $ smallest = $ j ; $ t = $ num [ $ i ] ; $ num [ $ i ] = $ num [ $ smallest ] ; $ num [ $ smallest ] = $ t ; $ t = $ num [ $ n - $ i - 1 ] ; $ num [ $ n - $ i - 1 ] = $ num [ $ n - $ smallest - 1 ] ; $ num [ $ n - $ smallest - 1 ] = $ t ; reverse ( $ num , $ i + 1 , $ mid ) ; if ( $ n % 2 == 0 ) reverse ( $ num , $ mid + 1 , $ n - $ i - 2 ) ; else reverse ( $ num , $ mid + 2 , $ n - $ i - 2 ) ; echo " Next ▁ Palindrome : ▁ " . $ num ; } $ num = "4697557964" ; $ n = strlen ( $ num ) ; nextPalin ( $ num , $ n ) ; ? >


"Nicomachu 's Theorem | PHP program to verify Nicomachu 's Theorem ; Compute sum of cubes ; Check if sum is equal to given formula . ; Driver Code"
Solution in php:

< ? php function NicomachuTheorum_sum ( $ n ) { $ sum = 0 ; for ( $ k = 1 ; $ k <= $ n ; $ k ++ ) $ sum += $ k * $ k * $ k ; $ triNo = $ n * ( $ n + 1 ) / 2 ; if ( $ sum == $ triNo * $ triNo ) echo " Yes " ; else echo " No " ; } $ n = 5 ; NicomachuTheorum_sum ( $ n ) ; ? >


"NicomachusÃƒÆ ’ Ã † â €™ Ãƒâ € Ã ¢ â ‚¬ â „¢¢ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒâ €¦¡¬ ÃƒÆ ’ Ã † â €™¢ ÃƒÆ ’¢ Ãƒ ¢ Ã ¢ â ‚¬ Å ¡¬ Ãƒâ €¦¾¢ s Theorem ( Sum of k | Return the sum of kth group of positive odd integer . ; Driven Code"
Solution in php:

< ? php function kthgroupsum ( $ k ) { return $ k * $ k * $ k ; } $ k = 3 ; echo kthgroupsum ( $ k ) ; ? >


"Nicomachusâ €™ s Theorem ( Sum of k | Return the sum of k - th group of positive odd integers . ; Finding first element of kth group . ; Finding the sum . ; Driver code"
Solution in php:

< ? php function kthgroupsum ( $ k ) { $ cur = ( $ k * ( $ k - 1 ) ) + 1 ; $ sum = 0 ; while ( $ k -- ) { $ sum += $ cur ; $ cur += 2 ; } return $ sum ; } $ k = 3 ; echo kthgroupsum ( $ k ) ; ? >


"Noble integers in an array ( count of greater elements is equal to value ) | Returns a Noble integer if present , else returns - 1. ; If count of greater elements is equal to arr [ i ] ; Driver code"
Solution in php:

< ? php function nobleInteger ( $ arr , $ size ) { for ( $ i = 0 ; $ i < $ size ; $ i ++ ) { $ count = 0 ; for ( $ j = 0 ; $ j < $ size ; $ j ++ ) if ( $ arr [ $ i ] < $ arr [ $ j ] ) $ count ++ ; if ( $ count == $ arr [ $ i ] ) return $ arr [ $ i ] ; } return -1 ; } $ arr = array ( 10 , 3 , 20 , 40 , 2 ) ; $ size = count ( $ arr ) ; $ res = nobleInteger ( $ arr , $ size ) ; if ( $ res != -1 ) echo " The ▁ noble ▁ integer ▁ is ▁ " , $ res ; else echo " No ▁ Noble ▁ Integer ▁ Found " ; ? >


"Noble integers in an array ( count of greater elements is equal to value ) | Returns a Noble integer if present , else returns - 1. ; Return a Noble element if present before last . ; In case of duplicates , we reach last occurrence here . ; Driver code"
Solution in php:

< ? php function nobleInteger ( $ arr ) { sort ( $ arr ) ; $ n = count ( $ arr ) ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { if ( $ arr [ $ i ] == $ arr [ $ i + 1 ] ) continue ; if ( $ arr [ $ i ] == $ n - $ i - 1 ) return $ arr [ $ i ] ; } if ( $ arr [ $ n - 1 ] == 0 ) return $ arr [ $ n - 1 ] ; return -1 ; } $ arr = array ( 10 , 3 , 20 , 40 , 2 ) ; $ res = nobleInteger ( $ arr ) ; if ( $ res != -1 ) echo " The ▁ noble ▁ integer ▁ is ▁ " , $ res ; else echo " No ▁ Noble ▁ Integer ▁ Found " ; ? >


"Non Fibonacci Numbers | Returns n 'th Non- Fibonacci number ; curr is to keep track of current fibonacci number , prev is previous , prevPrev is previous of previous . ; While count of non - fibonacci numbers doesn 't become  negative or zero ; Simple Fibonacci number logic ; ( curr - prev - 1 ) is count of non - Fibonacci numbers between curr and prev . ; n might be negative now . Make sure it becomes positive by removing last added gap . ; Now add the positive n to previous Fibonacci number to find the n 'th non-fibonacci. ; Driver code"
Solution in php:

< ? php function nonFibonacci ( $ n ) { $ prevPrev = 1 ; $ prev = 2 ; $ curr = 3 ; while ( $ n > 0 ) { $ prevPrev = $ prev ; $ prev = $ curr ; $ curr = $ prevPrev + $ prev ; $ n = $ n - ( $ curr - $ prev - 1 ) ; } $ n = $ n + ( $ curr - $ prev - 1 ) ; return $ prev + $ n ; } echo nonFibonacci ( 5 ) ; ? >


"Non | A dynamic programming based function to find nth Catalan number ; Initialize first two values in table ; Fill entries in catalan [ ] using recursive formula ; Return last entry ; Returns count of ways to connect n points on a circle such that no two connecting lines cross each other and every point is connected with one other point . ; Throw error if n is odd ; Else return n / 2 'th Catalan number ; Driver Code"
Solution in php:

< ? php function catalanDP ( $ n ) { $ catalan [ 0 ] = $ catalan [ 1 ] = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { $ catalan [ $ i ] = 0 ; for ( $ j = 0 ; $ j < $ i ; $ j ++ ) $ catalan [ $ i ] += $ catalan [ $ j ] * $ catalan [ $ i - $ j - 1 ] ; } return $ catalan [ $ n ] ; } function countWays ( $ n ) { if ( $ n & 1 ) { echo " Invalid " ; return 0 ; } return catalanDP ( $ n / 2 ) ; } echo countWays ( 6 ) , " " ; ? >


"Non | Simple PHP program to find first non - repeating element . ; Driver code"
Solution in php:

< ? php function firstNonRepeating ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ j ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) if ( $ i != $ j && $ arr [ $ i ] == $ arr [ $ j ] ) break ; if ( $ j == $ n ) return $ arr [ $ i ] ; } return -1 ; } $ arr = array ( 9 , 4 , 9 , 6 , 7 , 4 ) ; $ n = sizeof ( $ arr ) ; echo firstNonRepeating ( $ arr , $ n ) ; ? >


"Nonagonal number | Function to find nonagonal number series . ; Driver Code"
Solution in php:

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


"Nonagonal number | Function to find nth nonagonal number . ; Formula to find nth nonagonal number . ; Driver Code"
Solution in php:

< ? php function Nonagonal ( $ n ) { return $ n * ( 7 * $ n - 5 ) / 2 ; } $ n = 10 ; echo Nonagonal ( $ n ) ; ? >


"Nth Even Fibonacci Number | Function which return nth even fibonnaci number ; calculation of Fn = 4 * ( Fn - 1 ) + Fn - 2 ; Driver Code"
Solution in php:

< ? php function evenFib ( $ n ) { if ( $ n < 1 ) return $ n ; if ( $ n == 1 ) return 2 ; return ( ( 4 * evenFib ( $ n - 1 ) ) + evenFib ( $ n - 2 ) ) ; } $ n = 7 ; echo ( evenFib ( $ n ) ) ; ? >


"Nth Even length Palindrome | Function to find nth even length Palindrome ; string r to store resultant palindrome . Initialize same as s ; In this loop string r stores reverse of string s after the string s in consecutive manner . ; Driver code ; Function call"
Solution in php:

< ? php function evenlength ( $ n ) { $ res = $ n ; for ( $ j = strlen ( $ n ) - 1 ; $ j >= 0 ; -- $ j ) $ res = $ res . $ n [ $ j ] ; return $ res ; } $ n = "10" ; echo evenlength ( $ n ) ; ? >


"Nth Square free number | Function to find nth square free number ; To maintain count of square free number ; Loop for square free numbers ; Checking whether square of a number is divisible by any number which is a perfect square ; If number is square free ; If the cnt becomes n , return the number ; Driver Code"
Solution in php:

< ? php function squareFree ( $ n ) { $ cnt = 0 ; for ( $ i = 1 ; ; $ i ++ ) { $ isSqFree = true ; for ( $ j = 2 ; $ j * $ j <= $ i ; $ j ++ ) { if ( $ i % ( $ j * $ j ) == 0 ) { $ isSqFree = false ; break ; } } if ( $ isSqFree == true ) { $ cnt ++ ; if ( $ cnt == $ n ) return $ i ; } } return 0 ; } $ n = 10 ; echo ( squareFree ( $ n ) ) ; ? >


"Nth character in Concatenated Decimal String | Method to get dth digit of number N ; Method to return Nth character in concatenated decimal string ; sum will store character escaped till now ; dist will store numbers escaped till now ; loop for number lengths ; nine * len will be incremented characters and nine will be incremented numbers ; restore variables to previous correct state ; get distance from last one digit less maximum number ; d will store dth digit of current number ; method will return dth numbered digit of ( dist + diff ) number ; Driver code"
Solution in php:

< ? php function getDigit ( $ N , $ d ) { $ string = strval ( $ N ) ; return $ string [ $ d - 1 ] ; } function getNthChar ( $ N ) { $ sum = 0 ; $ nine = 9 ; $ dist = 0 ; for ( $ len = 1 ; $ len < $ N ; $ len ++ ) { $ sum += $ nine * $ len ; $ dist += $ nine ; if ( $ sum >= $ N ) { $ sum -= $ nine * $ len ; $ dist -= $ nine ; $ N -= $ sum ; break ; } $ nine *= 10 ; } $ diff = ( $ N / $ len ) + 1 ; $ d = $ N % $ len ; if ( $ d == 0 ) $ d = $ len ; return getDigit ( $ dist + $ diff , $ d ) ; } $ N = 251 ; echo getNthChar ( $ N ) ; ? >


"Number Theory | Generators of finite cyclic group under addition | Function to return gcd of a and b ; Print generators of n ; 1 is always a generator ; A number x is generator of GCD is 1 ; Driver program to test above function"
Solution in php:

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


"Number expressed as sum of five consecutive integers | function to check if a number can be expressed as sum of five consecutive integers . ; if n is 0 ; if n is positive , increment loop by 1. ; if n is negative , decrement loop by 1. ; Running loop from 0 to n - 4 ; check if sum of five consecutive integer is equal to n . ; Driver Code"
Solution in php:


"Number expressed as sum of five consecutive integers | function to check if a number can be expressed as sum of five consecutive integers . ; if n is multiple of 5 ; else print " - 1" . ; Driver Code"
Solution in php:


"Number is divisible by 29 or not | Returns true if n is divisible by 29 else returns false . ; add the lastdigit * 3 to remaining number until number becomes of only 2 digit ; return true if number is divisible by 29 another ; Driver Code"
Solution in php:

< ? php function isDivisible ( $ n ) { while ( intval ( $ n / 100 ) ) { $ last_digit = $ n % 10 ; $ n = intval ( $ n / 10 ) ; $ n += $ last_digit * 3 ; } return ( $ n % 29 == 0 ) ; } $ n = 348 ; if ( isDivisible ( $ n ) ) echo " Yes " ; else echo " No " ; ? >


"Number of Digits in a ^ b | function to calculate number of digits in a ^ b ; Driver Code"
Solution in php:

< ? php function no_of_digit ( $ a , $ b ) { return ( ( int ) ( $ b * log10 ( $ a ) ) + 1 ) ; } $ a = 2 ; $ b = 100 ; echo ( " no . ▁ of ▁ digits ▁ = ▁ " . no_of_digit ( $ a , $ b ) ) ; ? >


"Number of Distinct Meeting Points on a Circular Road | Returns the GCD of two number . ; Returns the number of distinct meeting points . ; Find the relative speed . ; convert the negative value to positive . ; Driver Code"
Solution in php:

 < ? php function gcd ( $ a , $ b ) { $ c = $ a % $ b ; while ( $ c != 0 ) { $ a = $ b ; $ b = $ c ; $ c = $ a % $ b ; } return $ b ; } function numberOfmeet ( $ a , $ b ) { $ ans ; if ( $ a > $ b ) $ ans = $ a - $ b ; else $ ans = $ b - $ a ; if ( $ a < 0 ) $ a = $ a * ( -1 ) ; if ( $ b < 0 ) $ b = $ b * ( -1 ) ; return $ ans / gcd ( $ a , $ b ) ; } $ a = 1 ; $ b = -1 ; echo numberOfmeet ( $ a , $ b ) . "
 " ; ? >


"Number of Groups of Sizes Two Or Three Divisible By 3 | PHP Program to find groups of 2 or 3 whose sum is divisible by 3 ; Initialize groups to 0 ; Increment group with specified remainder ; Return groups using the formula ; Driver Code"
Solution in php:


"Number of Larger Elements on right side in a string | PHP program to count greater characters on right side of every character . ; Arrays to store result and character counts . ; start from right side of string ; Driver code"
Solution in php:

< ? php $ MAX_CHAR = 26 ; function printGreaterCount ( $ str ) { global $ MAX_CHAR ; $ len = strlen ( $ str ) ; $ ans = array_fill ( 0 , $ len , 0 ) ; $ count = array_fill ( 0 , $ MAX_CHAR , 0 ) ; for ( $ i = $ len - 1 ; $ i >= 0 ; $ i -- ) { $ count [ ord ( $ str [ $ i ] ) - ord ( ' a ' ) ] ++ ; for ( $ j = ord ( $ str [ $ i ] ) - ord ( ' a ' ) + 1 ; $ j < $ MAX_CHAR ; $ j ++ ) $ ans [ $ i ] += $ count [ $ j ] ; } for ( $ i = 0 ; $ i < $ len ; $ i ++ ) echo $ ans [ $ i ] . " ▁ " ; } $ str = " abcd " ; printGreaterCount ( $ str ) ; ? >


"Number of Larger Elements on right side in a string | PHP program to find counts of right greater characters for every character . ; Driver code"
Solution in php:

< ? php function printGreaterCount ( $ str ) { $ len = strlen ( $ str ) ; $ right = array_fill ( 0 , $ len , 0 ) ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { for ( $ j = $ i + 1 ; $ j < $ len ; $ j ++ ) if ( $ str [ $ i ] < $ str [ $ j ] ) $ right [ $ i ] ++ ; } for ( $ i = 0 ; $ i < $ len ; $ i ++ ) echo $ right [ $ i ] . " ▁ " ; } $ str = ' bcd ' printGreaterCount ( $ str ) ; ? >


"Number of Permutations such that no Three Terms forms Increasing Subsequence | 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"
Solution in php:

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


"Number of Simple Graph with N Vertices and M Edges | Function to return the value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate the value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * ... * 1 ] ; Driver Code"
Solution in php:

< ? php function binomialCoeff ( $ n , $ k ) { if ( $ k > $ n ) return 0 ; $ res = 1 ; if ( $ k > $ n - $ k ) $ k = $ n - $ k ; for ( $ i = 0 ; $ i < $ k ; ++ $ i ) { $ res *= ( $ n - $ i ) ; $ res /= ( $ i + 1 ) ; } return $ res ; } $ N = 5 ; $ M = 1 ; $ P = floor ( ( $ N * ( $ N - 1 ) ) / 2 ) ; echo binomialCoeff ( $ P , $ M ) ; ? >


"Number of Symmetric Relations on a Set | function find the square of n ; Base case ; Return 2 ^ ( n ( n + 1 ) / 2 ) ; Driver code"
Solution in php:

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


"Number of Unique BST with a given key | Dynamic Programming | Function to find number of unique BST ; DP to store the number of unique BST with key i ; Base case ; fill the dp table in top - down approach . ; n - i in right * i - 1 in left ; Driver Code"
Solution in php:

< ? php function numberOfBST ( $ n ) { $ dp = array ( $ n + 1 ) ; for ( $ i = 0 ; $ i <= $ n + 1 ; $ i ++ ) $ dp [ $ i ] = 0 ; $ dp [ 0 ] = 1 ; $ dp [ 1 ] = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { for ( $ j = 1 ; $ j <= $ i ; $ j ++ ) { $ dp [ $ i ] += ( ( $ dp [ $ i - $ j ] ) * ( $ dp [ $ j - 1 ] ) ) ; } } return $ dp [ $ n ] ; } $ n = 3 ; echo " Number ▁ of ▁ structurally ▁ " . " Unique ▁ BST ▁ with ▁ " , $ n , " ▁ keys ▁ are ▁ : ▁ " , numberOfBST ( $ n ) ; ? >


"Number of anomalies in an array | A simple PHP solution to count anomalies in an array . ; Driver Code"
Solution in php:

< ? php function countAnomalies ( & $ arr , $ n , $ k ) { $ res = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) if ( $ i != $ j && abs ( $ arr [ $ i ] - $ arr [ $ j ] ) <= $ k ) break ; if ( $ j == $ n ) $ res ++ ; } return $ res ; } $ arr = array ( 7 , 1 , 8 ) ; $ k = 5 ; $ n = sizeof ( $ arr ) ; echo countAnomalies ( $ arr , $ n , $ k ) ; ? >


"Number of array elements derivable from D after performing certain operations | Function to return gcd of a and b ; Function to Return the number of elements of arr [ ] which can be derived from D by performing ( + A , - A , + B , - B ) ; find the gcd of A and B ; counter stores the number of array elements which can be derived from D ; arr [ i ] can be derived from D only if | arr [ i ] - D | is divisible by gcd of A and B ; Driver Code"
Solution in php:

< ? php function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; } function findPossibleDerivables ( $ arr , $ n , $ D , $ A , $ B ) { $ gcdAB = gcd ( $ A , $ B ) ; $ counter = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( ( abs ( $ arr [ $ i ] - $ D ) % $ gcdAB ) == 0 ) { $ counter ++ ; } } return $ counter ; } $ arr = array ( 1 , 2 , 3 , 4 , 7 , 13 ) ; $ n = sizeof ( $ arr ) ; $ D = 5 ; $ A = 4 ; $ B = 2 ; echo findPossibleDerivables ( $ arr , $ n , $ D , $ A , $ B ) , " " ; $ a = array ( 1 , 2 , 3 ) ; $ n = sizeof ( $ a ) ; $ D = 6 ; $ A = 3 ; $ B = 2 ; echo findPossibleDerivables ( $ arr , $ n , $ D , $ A , $ B ) , " " ; ? >


"Number of arrays of size N whose elements are positive integers and sum is K | Return nCr ; $C [ 0 ] = 1 ; nC0 is 1 ; Compute next row of pascal triangle using the previous row ; Return the number of array that can be formed of size n and sum equals to k . ; Driver Code"
Solution in php:

< ? php function binomialCoeff ( $ n , $ k ) { $ C = array_fill ( 0 , ( $ k + 1 ) , 0 ) ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { for ( $ j = min ( $ i , $ k ) ; $ j > 0 ; $ j -- ) $ C [ $ j ] = $ C [ $ j ] + $ C [ $ j - 1 ] ; } return $ C [ $ k ] ; } function countArray ( $ N , $ K ) { return binomialCoeff ( $ K - 1 , $ N - 1 ) ; } $ N = 2 ; $ K = 3 ; echo countArray ( $ N , $ K ) ; ? >


"Number of buildings facing the sun | Returns count buildings that can see sunlight ; Initialuze result ( Note that first building always sees sunlight ) ; Start traversing element ; If curr_element is maximum or current element is equal , update maximum and increment count ; Driver code"
Solution in php:

< ? php function countBuildings ( $ arr , $ n ) { $ count = 1 ; $ curr_max = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] > $ curr_max $ arr [ $ i ] == $ curr_max ) { $ count ++ ; $ curr_max = $ arr [ $ i ] ; } } return $ count ; } $ arr = array ( 7 , 4 , 8 , 2 , 9 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; echo countBuildings ( $ arr , $ n ) ; ? >


"Number of cells a queen can move with obstacles on the chessborad | Return the number of position a Queen can move . ; d11 , d12 , d21 , d22 are for diagnoal distances . r1 , r2 are for vertical distance . c1 , c2 are for horizontal distance . ; Initialise the distance to end of the board . ; For each obstacle find the minimum distance . If obstacle is present in any direction , distance will be updated . ; Chessboard size ; number of obstacles ; Queen x position ; Queen y position ; x position of obstacles ; y position of obstacles"
Solution in php:

< ? php function numberofPosition ( $ n , $ k , $ x , $ y , $ obstPosx , $ obstPosy ) { $ d11 ; $ d12 ; $ d21 ; $ d22 ; $ r1 ; $ r2 ; $ c1 ; $ c2 ; $ d11 = min ( $ x - 1 , $ y - 1 ) ; $ d12 = min ( $ n - $ x , $ n - $ y ) ; $ d21 = min ( $ n - $ x , $ y - 1 ) ; $ d22 = min ( $ x - 1 , $ n - $ y ) ; $ r1 = $ y - 1 ; $ r2 = $ n - $ y ; $ c1 = $ x - 1 ; $ c2 = $ n - $ x ; for ( $ i = 0 ; $ i < $ k ; $ i ++ ) { if ( $ x > $ obstPosx [ $ i ] && $ y > $ obstPosy [ $ i ] && $ x - $ obstPosx [ $ i ] == $ y - $ obstPosy [ $ i ] ) $ d11 = min ( $ d11 , $ x - $ obstPosx [ $ i ] - 1 ) ; if ( $ obstPosx [ $ i ] > $ x && $ obstPosy [ $ i ] > $ y && $ obstPosx [ $ i ] - $ x == $ obstPosy [ $ i ] - $ y ) $ d12 = min ( $ d12 , $ obstPosx [ $ i ] - $ x - 1 ) ; if ( $ obstPosx [ $ i ] > $ x && $ y > $ obstPosy [ $ i ] && $ obstPosx [ $ i ] - $ x == $ y - $ obstPosy [ $ i ] ) $ d21 = min ( $ d21 , $ obstPosx [ $ i ] - $ x - 1 ) ; if ( $ x > $ obstPosx [ $ i ] && $ obstPosy [ $ i ] > $ y && $ x - $ obstPosx [ $ i ] == $ obstPosy [ $ i ] - $ y ) $ d22 = min ( $ d22 , $ x - $ obstPosx [ $ i ] - 1 ) ; if ( $ x == $ obstPosx [ $ i ] && $ obstPosy [ $ i ] < $ y ) $ r1 = min ( $ r1 , $ y - $ obstPosy [ $ i ] - 1 ) ; if ( $ x == $ obstPosx [ $ i ] && $ obstPosy [ $ i ] > $ y ) $ r2 = min ( $ r2 , $ obstPosy [ $ i ] - $ y - 1 ) ; if ( $ y == $ obstPosy [ $ i ] && $ obstPosx [ $ i ] < $ x ) $ c1 = min ( $ c1 , $ x - $ obstPosx [ $ i ] - 1 ) ; if ( $ y == $ obstPosy [ $ i ] && $ obstPosx [ $ i ] > $ x ) $ c2 = min ( $ c2 , $ obstPosx [ $ i ] - $ x - 1 ) ; } return $ d11 + $ d12 + $ d21 + $ d22 + $ r1 + $ r2 + $ c1 + $ c2 ; } $ n = 8 ; $ k = 1 ; $ Qposx = 4 ; $ Qposy = 4 ; $ obstPosx = array ( 3 ) ; $ obstPosy = array ( 5 ) ; echo numberofPosition ( $ n , $ k , $ Qposx , $ Qposy , $ obstPosx , $ obstPosy ) ; ? >


"Number of character corrections in the given strings to make them equal | Function to return the count of operations required ; To store the count of operations ; No operation required ; One operation is required when any two characters are equal ; Two operations are required when none of the characters are equal ; Return the minimum count of operations required ; Driver code"
Solution in php:

< ? php function minOperations ( $ n , $ a , $ b , $ c ) { $ ans = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ x = $ a [ $ i ] ; $ y = $ b [ $ i ] ; $ z = $ c [ $ i ] ; if ( $ x == $ y && $ y == $ z ) ; else if ( $ x == $ y $ y == $ z $ x == $ z ) { $ ans ++ ; } else { $ ans += 2 ; } } return $ ans ; } $ a = " place " ; $ b = " abcde " ; $ c = " plybe " ; $ n = strlen ( $ a ) ; echo minOperations ( $ n , $ a , $ b , $ c ) ; ? >


"Number of chocolates left after k iterations | Function to find the chocolates left ; Driver code"
Solution in php:

< ? php function results ( $ n , $ k ) { return round ( pow ( $ n , ( 1.0 / pow ( 2 , $ k ) ) ) ) ; } $ k = 3 ; $ n = 100000000 ; echo ( " Chocolates ▁ left ▁ after ▁ " ) ; echo ( $ k ) ; echo ( " ▁ iterations ▁ are ▁ " ) ; echo ( results ( $ n , $ k ) ) ; ? >


"Number of closing brackets needed to complete a regular bracket sequence | Function to find number of closing brackets and complete a regular bracket sequence ; Finding the length of sequence ; Counting opening brackets ; Counting closing brackets ; Checking if at any position the number of closing bracket is more then answer is impossible ; If possible , print ' s ' and required closing brackets . ; Driver Code"
Solution in php:

< ? php function completeSequence ( $ s ) { $ n = strlen ( $ s ) ; $ open = 0 ; $ close = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ s [ $ i ] == ' ( ' ) $ open ++ ; else $ close ++ ; if ( $ close > $ open ) { echo ( " IMPOSSIBLE " ) ; return ; } } echo ( $ s ) ; for ( $ i = 0 ; $ i < $ open - $ close ; $ i ++ ) echo ( " ) " ) ; } $ s = " ( ( ) ( ( ) ( " ; completeSequence ( $ s ) ; ? >


"Number of common base strings for two strings | function for finding common divisor . ; Checking if ' base ' is base string of ' s1' ; Checking if ' base ' is base string of ' s2' ; Driver code"
Solution in php:

 < ? php function isCommonBase ( $ base , $ s1 , $ s2 ) { for ( $ j = 0 ; $ j < strlen ( $ s1 ) ; ++ $ j ) if ( $ base [ $ j % strlen ( $ base ) ] != $ s1 [ $ j ] ) return false ; for ( $ j = 0 ; $ j < strlen ( $ s2 ) ; ++ $ j ) if ( $ base [ $ j % strlen ( $ base ) ] != $ s2 [ $ j ] ) return false ; return true ; } function countCommonBases ( $ s1 , $ s2 ) { $ n1 = strlen ( $ s1 ) ; $ n2 = strlen ( $ s2 ) ; $ count = 0 ; for ( $ i = 1 ; $ i <= min ( $ n1 , $ n2 ) ; $ i ++ ) { $ base = substr ( $ s1 , 0 , $ i ) ; if ( isCommonBase ( $ base , $ s1 , $ s2 ) ) $ count ++ ; } return $ count ; } $ s1 = " pqrspqrs " ; $ s2 = " pqrspqrspqrspqrs " ; echo countCommonBases ( $ s1 , $ s2 ) . "
 " ; ? >


"Number of common tangents between two circles if their centers and radius is given | PHP program to find the number of common tangents between the two circles ; Driver code"
Solution in php:

< ? php function circle ( $ x1 , $ y1 , $ x2 , $ y2 , $ r1 , $ r2 ) { $ distSq = ( $ x1 - $ x2 ) * ( $ x1 - $ x2 ) + ( $ y1 - $ y2 ) * ( $ y1 - $ y2 ) ; $ radSumSq = ( $ r1 + $ r2 ) * ( $ r1 + $ r2 ) ; if ( $ distSq == $ radSumSq ) return 1 ; else if ( $ distSq > $ radSumSq ) return -1 ; else return 0 ; } $ x1 = -10 ; $ y1 = 8 ; $ x2 = 14 ; $ y2 = -24 ; $ r1 = 30 ; $ r2 = 10 ; $ t = circle ( $ x1 , $ y1 , $ x2 , $ y2 , $ r1 , $ r2 ) ; if ( $ t == 1 ) echo " There ▁ are ▁ 3 ▁ common ▁ tangents " , " ▁ between ▁ the ▁ circles . " ; else if ( $ t < 0 ) echo " There ▁ are ▁ 4 ▁ common ▁ tangents " , " ▁ between ▁ the ▁ circles . " ; else echo " There ▁ are ▁ 2 ▁ common ▁ tangents " , " ▁ between ▁ the ▁ circles . " ; ? >


"Number of compositions of a natural number | PHP program to find the total number of compositions of a natural number ; Return 2 raised to power ( n - 1 ) ; Driver Code"
Solution in php:

 < ? php function countCompositions ( $ n ) { return ( ( 1 ) << ( $ n - 1 ) ) ; } $ n = 4 ; echo countCompositions ( $ n ) , "
 " ; ? >


"Number of days after which tank will become empty | Method returns minimum number of days after which tank will become empty ; Driver code"
Solution in php:

 < ? php function minDaysToEmpty ( $ C , $ l ) { if ( $ l >= $ C ) return $ C ; $ eq_root = ( int ) sqrt ( 1 + 8 * ( $ C - $ l ) - 1 ) / 2 ; return ceil ( $ eq_root ) + $ l ; } echo minDaysToEmpty ( 5 , 2 ) , "
 " ; echo minDaysToEmpty ( 6514683 , 4965 ) , " " ; ? >


"Number of decimal numbers of length k , that are strict monotone | PHP program to count numbers of k digits that are strictly monotone . ; DP [ i ] [ j ] is going to store monotone numbers of length i + 1 considering j + 1 digits ( 1 , 2 , 3 , . .9 ) ; Unit length numbers ; Building dp [ ] in bottom up ; Driver code"
Solution in php:


"Number of different cyclic paths of length N in a tetrahedron | Function to count the number of steps in a tetrahedron ; initially coming to B is B -> B ; cannot reach A , D or C ; iterate for all steps ; recurrence relation ; memoize previous values ; returns steps ; Driver Code"
Solution in php:

< ? php function countPaths ( $ n ) { $ zB = 1 ; $ zADC = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ nzB = $ zADC * 3 ; $ nzADC = ( $ zADC * 2 + $ zB ) ; $ zB = $ nzB ; $ zADC = $ nzADC ; } return $ zB ; } $ n = 3 ; echo countPaths ( $ n ) ; ? >


"Number of different positions where a person can stand | Function to find the position ; Driver code"
Solution in php:

< ? php function findPosition ( $ n , $ f , $ b ) { return $ n - max ( $ f + 1 , $ n - $ b ) + 1 ; } $ n = 5 ; $ f = 2 ; $ b = 3 ; echo findPosition ( $ n , $ f , $ b ) ; ? >


"Number of digits before the decimal point in the division of two numbers | Function to return the number of digits before the decimal in a / b ; Absolute value of a / b ; If result is 0 ; Count number of digits in the result ; Return the required count of digits ; Driver code"
Solution in php:

< ? php function countDigits ( $ a , $ b ) { $ count = 0 ; $ p = abs ( $ a / $ b ) ; if ( $ p == 0 ) return 1 ; while ( $ p > 0 ) { $ count ++ ; $ p = ( int ) ( $ p / 10 ) ; } return $ count ; } $ a = 100 ; $ b = 10 ; echo countDigits ( $ a , $ b ) ; ? >


"Number of digits before the decimal point in the division of two numbers | Function to return the number of digits before the decimal in a / b ; Return the required count of digits ; Driver code"
Solution in php:

< ? php function countDigits ( $ a , $ b ) { return floor ( log10 ( abs ( $ a ) ) - log10 ( abs ( $ b ) ) ) + 1 ; } $ a = 100 ; $ b = 10 ; echo countDigits ( $ a , $ b ) ; ? >


"Number of digits in 2 raised to power n | Function to find number of digits in 2 ^ n ; Driver code"
Solution in php:

< ? php function countDigits ( $ n ) { return intval ( $ n * log10 ( 2 ) + 1 ) ; } $ n = 5 ; echo ( countDigits ( $ n ) ) ; ? >


"Number of digits in N factorial to the power N | PHP program to find count of digits in N factorial raised to N ; we take sum of logarithms as explained in the approach ; multiply the result with n ; Driver Code"
Solution in php:

 < ? php function countDigits ( $ n ) { $ ans = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ ans += log10 ( $ i ) ; $ ans = $ ans * $ n ; return 1 + floor ( $ ans ) ; } $ n = 4 ; echo countDigits ( $ n ) , "
 " ; ? >


"Number of digits in the nth number made of given four digits | Efficient function to calculate number of digits in the nth number constructed by using 6 , 1 , 4 and 9 as digits in the ascending order . ; Number of digits increase after every i - th number where i increases in powers of 4. ; Driver Code"
Solution in php:

 < ? php function number_of_digits ( $ n ) { $ i ; $ res ; $ sum = 0 ; for ( $ i = 4 , $ res = 1 ; ; $ i *= 4 , $ res ++ ) { $ sum += $ i ; if ( $ sum >= $ n ) break ; } return $ res ; } $ n = 21 ; echo number_of_digits ( $ n ) , "
 " ; ? >


"Number of digits in the product of two numbers | function to count number of digits in the product of two numbers ; absolute value of the product of two numbers ; if product is 0 ; count number of digits in the product ' p ' ; required count of digits ; Driver Code"
Solution in php:

< ? php function countDigits ( $ a , $ b ) { $ count = 0 ; $ p = abs ( $ a * $ b ) ; if ( $ p == 0 ) return 1 ; while ( $ p > 0 ) { $ count ++ ; $ p = ( int ) ( $ p / 10 ) ; } return $ count ; } $ a = 33 ; $ b = -24 ; echo " Number ▁ of ▁ digits ▁ = ▁ " . countDigits ( $ a , $ b ) ; ? >


"Number of digits in the product of two numbers | function to count number of digits in the product of two numbers ; if either of the number is 0 , then product will be 0 ; required count of digits ; Driver Code"
Solution in php:

< ? php function countDigits ( $ a , $ b ) { if ( $ a == 0 or $ b == 0 ) return 1 ; return floor ( log10 ( abs ( $ a ) ) + log10 ( abs ( $ b ) ) ) + 1 ; } $ a = 33 ; $ b = -24 ; echo countDigits ( $ a , $ b ) ; ? >


"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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution in php:


"Number of leading zeros in binary representation of a given number | Function to count the no . of leading zeros ; Driver Code"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution in php:


"Number of non | Returns count of solutions of a + b + c = n ; Driver Code"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution in php:


"Number of perfect squares between two given numbers | Method to count square between a and b ; Driver code"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution in php:


"Number of steps required to reach point ( x , y ) from ( 0 , 0 ) using zig | Function to return the required position ; Driver Code"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution 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"
Solution in php: