{"text":"Function to convert centimeter to pixels","code":"< ? php function Conversion ( $ centi ) { $ pixels = ( 96 * $ centi ) \/ 2.54 ; echo ( $ pixels . \" \" ) ; }"}
{"text":"Driver Code","code":"$ centi = 15 ; Conversion ( $ centi ) ; ? >"}
{"text":"Function to find the maximum possible value of the minimum value of the modified array","code":"< ? php function maxOfMin ( $ a , $ n , $ S ) {"}
{"text":"To store minimum value of array","code":"$ mi = PHP_INT_MAX ;"}
{"text":"To store sum of elements of array","code":"$ s1 = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ s1 += $ a [ $ i ] ; $ mi = min ( $ a [ $ i ] , $ mi ) ; }"}
{"text":"Solution is not possible","code":"if ( $ s1 < $ S ) return -1 ;"}
{"text":"zero is the possible value","code":"if ( $ s1 == $ S ) return 0 ;"}
{"text":"minimum possible value","code":"$ low = 0 ;"}
{"text":"maximum possible value","code":"$ high = $ mi ;"}
{"text":"to store a required answer","code":"$ ans ;"}
{"text":"Binary Search","code":"while ( $ low <= $ high ) { $ mid = ( $ low + $ high ) \/ 2 ;"}
{"text":"If mid is possible then try to increase required answer","code":"if ( $ s1 - ( $ mid * $ n ) >= $ S ) { $ ans = $ mid ; $ low = $ mid + 1 ; }"}
{"text":"If mid is not possible then decrease required answer","code":"else $ high = $ mid - 1 ; }"}
{"text":"Return required answer","code":"return $ ans ; }"}
{"text":"Driver Code","code":"$ a = array ( 10 , 10 , 10 , 10 , 10 ) ; $ S = 10 ; $ n = sizeof ( $ a ) ; echo maxOfMin ( $ a , $ n , $ S ) ; ? >"}
{"text":"Function to print the desired Alphabet N Pattern","code":"< ? php function Alphabet_N_Pattern ( $ N ) { $ index ; $ side_index ; $ size ;"}
{"text":"Declaring the values of Right , Left and Diagonal values","code":"$ Right = 1 ; $ Left = 1 ; $ Diagonal = 2 ;"}
{"text":"Main Loop for the rows","code":"for ( $ index = 0 ; $ index < $ N ; $ index ++ ) {"}
{"text":"For the left Values","code":"echo $ Left ++ ;"}
{"text":"Spaces for the diagonals","code":"for ( $ side_index = 0 ; $ side_index < 2 * ( $ index ) ; $ side_index ++ ) echo \" \u2581 \" ;"}
{"text":"Condition for the diagonals","code":"if ( $ index != 0 && $ index != $ N - 1 ) echo $ Diagonal ++ ; else echo \" \u2581 \" ;"}
{"text":"Spaces for the Right Values","code":"for ( $ side_index = 0 ; $ side_index < 2 * ( $ N - $ index - 1 ) ; $ side_index ++ ) echo \" \u2581 \" ;"}
{"text":"For the right values","code":"echo $ Right ++ ; echo \" STRNEWLINE \" ; } }"}
{"text":"Size of the Pattern","code":"$ Size = 6 ;"}
{"text":"Calling the function to print the desired Pattern","code":"Alphabet_N_Pattern ( $ Size ) ; ? >"}
{"text":"Function to check if sum of digits of a number divides it","code":"< ? php function isSumDivides ( $ N ) { $ temp = $ N ; $ sum = 0 ;"}
{"text":"Calculate sum of all of digits of N","code":"while ( $ temp ) { $ sum += $ temp % 10 ; $ temp = ( int ) $ temp \/ 10 ; } if ( $ N % $ sum == 0 ) return 1 ; else return 0 ; }"}
{"text":"Driver Code","code":"$ N = 12 ; if ( isSumDivides ( $ N ) ) echo \" YES \" ; else echo \" NO \" ; ? >"}
{"text":"Function to calculate the sum of numbers divisible by 3 or 4","code":"< ? php function sum ( $ N ) { $ S1 ; $ S2 ; $ S3 ; $ S1 = ( ( $ N \/ 3 ) ) * ( 2 * 3 + ( $ N \/ 3 - 1 ) * 3 ) \/ 2 ; $ S2 = ( ( $ N \/ 4 ) ) * ( 2 * 4 + ( $ N \/ 4 - 1 ) * 4 ) \/ 2 ; $ S3 = ( ( $ N \/ 12 ) ) * ( 2 * 12 + ( $ N \/ 12 - 1 ) * 12 ) \/ 2 ; return $ S1 + $ S2 - $ S3 ; }"}
{"text":"Driver Code","code":"$ N = 20 ; echo sum ( 12 ) ; ? >"}
{"text":"Function to find next greater number than N with exactly one bit different in binary representation of N","code":"< ? php function nextGreater ( $ N ) { $ power_of_2 = 1 ; $ shift_count = 0 ;"}
{"text":"It is guaranteed that there is a bit zero in the number","code":"while ( true ) {"}
{"text":"If the shifted bit is zero then break","code":"if ( ( ( $ N >> $ shift_count ) & 1 ) % 2 == 0 ) break ;"}
{"text":"increase the bit shift","code":"$ shift_count ++ ;"}
{"text":"increase the power of 2","code":"$ power_of_2 = $ power_of_2 * 2 ; }"}
{"text":"set the lowest bit of the number","code":"return ( $ N + $ power_of_2 ) ; }"}
{"text":"Driver code","code":"$ N = 11 ;"}
{"text":"display the next number","code":"echo \" The \u2581 next \u2581 number \u2581 is \u2581 = \u2581 \" , nextGreater ( $ N ) ; ? >"}
{"text":"Function to print the N - th tetranacci number","code":"< ? php function printTetra ( $ n ) { $ dp = array_fill ( 0 , $ n + 5 , 0 ) ;"}
{"text":"base cases","code":"$ dp [ 0 ] = 0 ; $ dp [ 1 ] = $ dp [ 2 ] = 1 ; $ dp [ 3 ] = 2 ; for ( $ i = 4 ; $ i <= $ n ; $ i ++ ) $ dp [ $ i ] = $ dp [ $ i - 1 ] + $ dp [ $ i - 2 ] + $ dp [ $ i - 3 ] + $ dp [ $ i - 4 ] ; echo $ dp [ $ n ] ; }"}
{"text":"Driver code","code":"$ n = 10 ; printTetra ( $ n ) ; ? >"}
{"text":"Function to calculate the sum from 0 th position to ( n - 2 ) th position","code":"< ? php function maxSum1 ( $ arr , $ n ) { $ dp [ $ n ] = array ( ) ; $ maxi = 0 ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) {"}
{"text":"copy the element of original array to dp [ ]","code":"$ dp [ $ i ] = $ arr [ $ i ] ;"}
{"text":"find the maximum element in the array","code":"if ( $ maxi < $ arr [ $ i ] ) $ maxi = $ arr [ $ i ] ; }"}
{"text":"start from 2 nd to n - 1 th pos","code":"for ( $ i = 2 ; $ i < $ n - 1 ; $ i ++ ) {"}
{"text":"traverse for all pairs bottom - up approach","code":"for ( $ j = 0 ; $ j < $ i - 1 ; $ j ++ ) {"}
{"text":"dp - condition","code":"if ( $ dp [ $ i ] < $ dp [ $ j ] + $ arr [ $ i ] ) { $ dp [ $ i ] = $ dp [ $ j ] + $ arr [ $ i ] ;"}
{"text":"find maximum sum","code":"if ( $ maxi < $ dp [ $ i ] ) $ maxi = $ dp [ $ i ] ; } } }"}
{"text":"return the maximum","code":"return $ maxi ; }"}
{"text":"Function to find the maximum sum from 1 st position to n - 1 - th position","code":"function maxSum2 ( $ arr , $ n ) { $ dp [ $ n ] = array ( ) ; $ maxi = 0 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ dp [ $ i ] = $ arr [ $ i ] ; if ( $ maxi < $ arr [ $ i ] ) $ maxi = $ arr [ $ i ] ; }"}
{"text":"Traverse from third to n - th pos","code":"for ( $ i = 3 ; $ i < $ n ; $ i ++ ) {"}
{"text":"bootom - up approach","code":"for ( $ j = 1 ; $ j < $ i - 1 ; $ j ++ ) {"}
{"text":"dp condition","code":"if ( $ dp [ $ i ] < $ arr [ $ i ] + $ dp [ $ j ] ) { $ dp [ $ i ] = $ arr [ $ i ] + $ dp [ $ j ] ;"}
{"text":"find max sum","code":"if ( $ maxi < $ dp [ $ i ] ) $ maxi = $ dp [ $ i ] ; } } }"}
{"text":"return max","code":"return $ maxi ; } function findMaxSum ( $ arr , $ n ) { return max ( maxSum1 ( $ arr , $ n ) , maxSum2 ( $ arr , $ n ) ) ; }"}
{"text":"Driver Code","code":"$ arr = array ( 1 , 2 , 3 , 1 ) ; $ n = sizeof ( $ arr ) ; echo findMaxSum ( $ arr , $ n ) ; ? >"}
{"text":"Returns value of Permutation Coefficient P ( n , k )","code":"< ? php function permutationCoeff ( $ n , $ k ) { $ P = array ( array ( ) ) ;"}
{"text":"Calculate value of Permutation Coefficient in bottom up manner","code":"for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) { for ( $ j = 0 ; $ j <= min ( $ i , $ k ) ; $ j ++ ) {"}
{"text":"Base Cases","code":"if ( $ j == 0 ) $ P [ $ i ] [ $ j ] = 1 ;"}
{"text":"Calculate value using previosly stored values","code":"else $ P [ $ i ] [ $ j ] = $ P [ $ i - 1 ] [ $ j ] + ( $ j * $ P [ $ i - 1 ] [ $ j - 1 ] ) ;"}
{"text":"This step is important as P ( i , j ) = 0 for j > i","code":"$ P [ $ i ] [ $ j + 1 ] = 0 ; } } return $ P [ $ n ] [ $ k ] ; }"}
{"text":"Driver Code","code":"$ n = 10 ; $ k = 2 ; echo \" Value \u2581 of \u2581 P ( \" , $ n , \" \u2581 , \" , $ k , \" ) \u2581 is \u2581 \" , permutationCoeff ( $ n , $ k ) ; ? >"}
{"text":"Returns value of Permutation Coefficient P ( n , k )","code":"< ? php function permutationCoeff ( $ n , $ k ) { $ fact = array ( ) ;"}
{"text":"base case","code":"$ fact [ 0 ] = 1 ;"}
{"text":"Calculate value factorials up to n","code":"for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ fact [ $ i ] = $ i * $ fact [ $ i - 1 ] ;"}
{"text":"P ( n , k ) = n ! \/ ( n - k ) !","code":"return $ fact [ $ n ] \/ $ fact [ $ n - $ k ] ; }"}
{"text":"Driver Code","code":"$ n = 10 ; $ k = 2 ; echo \" Value \u2581 of \u2581 P ( \" , $ n , \" \u2581 \" , $ k , \" ) \u2581 is \u2581 \" , permutationCoeff ( $ n , $ k ) ; ? >"}
{"text":"Returns true if there is a subset of set with sun equal to given sum","code":"< ? php function isSubsetSum ( $ set , $ n , $ sum ) {"}
{"text":"Base Cases","code":"if ( $ sum == 0 ) return true ; if ( $ n == 0 ) return false ;"}
{"text":"If last element is greater than sum , then ignore it","code":"if ( $ set [ $ n - 1 ] > $ sum ) return isSubsetSum ( $ set , $ n - 1 , $ sum ) ;"}
{"text":"else , check if sum can be obtained by any of the following ( a ) including the last element ( b ) excluding the last element","code":"return isSubsetSum ( $ set , $ n - 1 , $ sum ) || isSubsetSum ( $ set , $ n - 1 , $ sum - $ set [ $ n - 1 ] ) ; }"}
{"text":"Driver Code","code":"$ set = array ( 3 , 34 , 4 , 12 , 5 , 2 ) ; $ sum = 9 ; $ n = 6 ; if ( isSubsetSum ( $ set , $ n , $ sum ) == true ) echo \" Found \u2581 a \u2581 subset \u2581 with \u2581 given \u2581 sum \" ; else echo \" No \u2581 subset \u2581 with \u2581 given \u2581 sum \" ; ? >"}
{"text":"Function to return the number of ways of removing a sub - string from $s such that all the remaining characters are same","code":"< ? php function no_of_ways ( $ s ) { $ n = strlen ( $ s ) ;"}
{"text":"To store the count of prefix and suffix","code":"$ count_left = 0 ; $ count_right = 0 ;"}
{"text":"Loop to count prefix","code":"for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { if ( $ s [ $ i ] == $ s [ 0 ] ) { ++ $ count_left ; } else break ; }"}
{"text":"Loop to count suffix","code":"for ( $ i = $ n - 1 ; $ i >= 0 ; -- $ i ) { if ( $ s [ $ i ] == $ s [ $ n - 1 ] ) { ++ $ count_right ; } else break ; }"}
{"text":"First and last characters of the string are same","code":"if ( $ s [ 0 ] == $ s [ $ n - 1 ] ) return ( ( $ count_left + 1 ) * ( $ count_right + 1 ) ) ;"}
{"text":"Otherwise","code":"else return ( $ count_left + $ count_right + 1 ) ; }"}
{"text":"Driver Code","code":"$ s = \" geeksforgeeks \" ; echo no_of_ways ( $ s ) ; ? >"}
{"text":"Function to create prefix array","code":"< ? php function preCompute ( $ n , $ s , & $ pref ) { $ pref [ 0 ] = 0 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ pref [ $ i ] = $ pref [ $ i - 1 ] ; if ( $ s [ $ i - 1 ] == $ s [ $ i ] ) $ pref [ $ i ] ++ ; } }"}
{"text":"Function to return the result of the query","code":"function query ( & $ pref , $ l , $ r ) { return $ pref [ $ r ] - $ pref [ $ l ] ; }"}
{"text":"Driver Code","code":"$ s = \" ggggggg \" ; $ n = strlen ( $ s ) ; $ pref = array_fill ( 0 , $ n , NULL ) ; preCompute ( $ n , $ s , $ pref ) ;"}
{"text":"Query 1","code":"$ l = 1 ; $ r = 2 ; echo query ( $ pref , $ l , $ r ) . \" STRNEWLINE \" ;"}
{"text":"Query 2","code":"$ l = 1 ; $ r = 5 ; echo query ( $ pref , $ l , $ r ) . \" STRNEWLINE \" ; ? >"}
{"text":"Function to find the final direction","code":"< ? php function findDirection ( $ s ) { $ count = 0 ; $ d = \" \" ; for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) { if ( $ s [ 0 ] == ' ' ) return null ; if ( $ s [ $ i ] == ' L ' ) $ count -= 1 ; else { if ( $ s [ $ i ] == ' R ' ) $ count += 1 ; } }"}
{"text":"if count is positive that implies resultant is clockwise direction","code":"if ( $ count > 0 ) { if ( $ count % 4 == 0 ) $ d = \" N \" ; else if ( $ count % 4 == 1 ) $ d = \" E \" ; else if ( $ count % 4 == 2 ) $ d = \" S \" ; else if ( $ count % 4 == 3 ) $ d = \" W \" ; }"}
{"text":"if count is negative that implies resultant is anti - clockwise direction","code":"if ( $ count < 0 ) { if ( $ count % 4 == 0 ) $ d = \" N \" ; else if ( $ count % 4 == -1 ) $ d = \" W \" ; else if ( $ count % 4 == -2 ) $ d = \" S \" ; else if ( $ count % 4 == -3 ) $ d = \" E \" ; } return $ d ; }"}
{"text":"Driver code","code":"$ s = \" LLRLRRL \" ; echo findDirection ( $ s ) . \" STRNEWLINE \" ; $ s = \" LL \" ; echo findDirection ( $ s ) . \" STRNEWLINE \" ; ? >"}
{"text":"Function to move string character","code":"< ? php function encode ( $ s , $ k ) {"}
{"text":"changed string","code":"$ newS = \" \" ;"}
{"text":"iterate for every characters","code":"for ( $ i = 0 ; $ i < strlen ( $ s ) ; ++ $ i ) {"}
{"text":"ASCII value","code":"$ val = ord ( $ s [ $ i ] ) ;"}
{"text":"store the duplicate","code":"$ dup = $ k ;"}
{"text":"if k - th ahead character exceed ' z '","code":"if ( $ val + $ k > 122 ) { $ k -= ( 122 - $ val ) ; $ k = $ k % 26 ; $ newS = $ newS . chr ( 96 + $ k ) ; } else $ newS = $ newS . chr ( $ val + $ k ) ; $ k = $ dup ; }"}
{"text":"print the new string","code":"echo $ newS ; }"}
{"text":"Driver code","code":"$ str = \" abc \" ; $ k = 28 ;"}
{"text":"function call","code":"encode ( $ str , $ k ) ; ? >"}
{"text":"Function to check if the character x is a vowel or not","code":"< ? php function isVowel ( $ x ) { if ( $ x == ' a ' $ x == ' e ' $ x == ' i ' $ x == ' o ' $ x == ' u ' ) return true ; else return false ; }"}
{"text":"Returns the updated string formed after removing all the Sandwiched Vowels from the given string","code":"function updateSandwichedVowels ( $ a ) { $ n = strlen ( $ a ) ;"}
{"text":"string to store the Updated String after removing the Sandwiched Vowels","code":"$ updatedString = \" \" ;"}
{"text":"traverse the string from left to right","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) {"}
{"text":"if the current character is the first or the last character of the string then , this needs to be appended to the updatedString , since the corner alphabet irrespective of it being a vowel or a consonant , is never ' Sandwiched '","code":"if ( ! $ i $ i == $ n - 1 ) { $ updatedString . = $ a [ $ i ] ; continue ; }"}
{"text":"Check if the current character of the string is a vowel and both the previous and the next characters are consonants , if so then this is a sandwiched vowel , thus is ignored and not appended to the updated string","code":"if ( isVowel ( $ a [ $ i ] ) && ! isVowel ( $ a [ $ i - 1 ] ) && ! isVowel ( $ a [ $ i + 1 ] ) ) { continue ; }"}
{"text":"if this character is not a sandwiched Vowel append it to the updated String","code":"$ updatedString . = $ a [ $ i ] ; } return $ updatedString ; }"}
{"text":"Driver Code","code":"$ str = \" geeksforgeeks \" ;"}
{"text":"Remove all the Sandwitched Vowels","code":"$ updatedString = updateSandwichedVowels ( $ str ) ; echo $ updatedString ; ? >"}
{"text":"Function to find total possible numbers with n digits and weight w","code":"< ? php function findNumbers ( $ n , $ w ) { $ x = 0 ; $ sum = 0 ;"}
{"text":"When Weight of an integer is Positive","code":"if ( $ w >= 0 && $ w <= 8 ) {"}
{"text":"Subtract the weight from 9","code":"$ x = 9 - $ w ; }"}
{"text":"When weight of an integer is negative","code":"else if ( $ w >= -9 && $ w <= -1 ) {"}
{"text":"add the weight to 10 to make it positive","code":"$ x = 10 + $ w ; } $ sum = pow ( 10 , $ n - 2 ) ; $ sum = ( $ x * $ sum ) ; return $ sum ; }"}
{"text":"number of digits in an integer and w as weight","code":"$ n = 3 ; $ w = 4 ;"}
{"text":"print the total possible numbers with n digits and weight w","code":"echo findNumbers ( $ n , $ w ) ;"}
{"text":"PHP program to find the maximum height of Pyramidal Arrangement of array values","code":"< ? php function MaximumHeight ( $ a , $ n ) { $ result = 1 ; for ( $ i = 1 ; $ i <= $ n ; ++ $ i ) {"}
{"text":"Just checking whether ith level is possible or not if possible then we must have atleast ( i * ( i + 1 ) ) \/ 2 elements in the array","code":"$ y = ( $ i * ( $ i + 1 ) ) \/ 2 ;"}
{"text":"updating the result value each time","code":"if ( $ y < $ n ) $ result = $ i ;"}
{"text":"otherwise we have exceeded n value","code":"else break ; } return $ result ; }"}
{"text":"Driver Code","code":"$ arr = array ( 40 , 100 , 20 , 30 ) ; $ n = count ( $ arr ) ; echo MaximumHeight ( $ arr , $ n ) ; ? >"}
{"text":"PHP program to find k - th element in the Odd - Even sequence .","code":"< ? php function findK ( $ n , $ k ) { $ a ; $ index = 0 ;"}
{"text":"insert all the odd numbers from 1 to n .","code":"for ( $ i = 1 ; $ i < $ n ; $ i ++ ) if ( $ i % 2 == 1 ) $ a [ $ index ++ ] = $ i ;"}
{"text":"insert all the even numbers from 1 to n .","code":"for ( $ i = 1 ; $ i < $ n ; $ i ++ ) if ( $ i % 2 == 0 ) $ a [ $ index ++ ] = $ i ; return ( $ a [ $ k - 1 ] ) ; }"}
{"text":"Driver code","code":"$ n = 10 ; $ k = 3 ; echo findK ( $ n , $ k ) ; ? >"}
{"text":"PHP program to find factorial of given number","code":"< ? php function factorial ( $ n ) {"}
{"text":"single line to find factorial","code":"return ( $ n == 1 $ n == 0 ) ? 1 : $ n * factorial ( $ n - 1 ) ; }"}
{"text":"Driver Code","code":"$ num = 5 ; echo \" Factorial \u2581 of \u2581 \" , $ num , \" \u2581 is \u2581 \" , factorial ( $ num ) ; ? >"}
{"text":"calculate nth pell number","code":"< ? php function pell ( $ n ) { if ( $ n <= 2 ) return $ n ; $ a = 1 ; $ b = 2 ; $ c ; $ i ; for ( $ i = 3 ; $ i <= $ n ; $ i ++ ) { $ c = 2 * $ b + $ a ; $ a = $ b ; $ b = $ c ; } return $ b ; }"}
{"text":"Driver Code","code":"$ n = 4 ; echo ( pell ( $ n ) ) ; ? >"}
{"text":"Returns true if n - th Fibonacci number is multiple of 10.","code":"< ? php function isMultipleOf10 ( $ n ) { return ( $ n % 15 == 0 ) ; }"}
{"text":"Driver Code","code":"$ n = 30 ; if ( isMultipleOf10 ( $ n ) ) echo \" Yes STRNEWLINE \" ; else echo \" No STRNEWLINE \" ; ? >"}
{"text":"A function to count all odd prime factors of a given number n","code":"< ? php function countOddPrimeFactors ( $ n ) { $ result = 1 ;"}
{"text":"Eliminate all even prime factor of number of n","code":"while ( $ n % 2 == 0 ) $ n \/= 2 ;"}
{"text":"n must be odd at this point , so iterate for only odd numbers till sqrt ( n )","code":"for ( $ i = 3 ; $ i * $ i <= $ n ; $ i += 2 ) { $ divCount = 0 ;"}
{"text":"if i divides n , then start counting of Odd divisors","code":"while ( $ n % $ i == 0 ) { $ n \/= $ i ; ++ $ divCount ; } $ result *= $ divCount + 1 ; }"}
{"text":"If n odd prime still remains then count it","code":"if ( $ n > 2 ) $ result *= 2 ; return $ result ; } function politness ( $ n ) { return countOddPrimeFactors ( $ n ) - 1 ; }"}
{"text":"Driver Code","code":"$ n = 90 ; echo \" Politness \u2581 of \u2581 \" , $ n , \" \u2581 = \u2581 \" , politness ( $ n ) , \" STRNEWLINE \" ; $ n = 15 ; echo \" Politness \u2581 of \u2581 \" , $ n , \" \u2581 = \u2581 \" , politness ( $ n ) , \" STRNEWLINE \" ; ? >"}
{"text":"PHP program to find the nearest prime to n .","code":"< ? php $ MAX = 10000 ;"}
{"text":"array to store all primes less than 10 ^ 6","code":"$ primes = array ( ) ;"}
{"text":"Utility function of Sieve of Sundaram","code":"function Sieve ( ) { global $ MAX , $ primes ; $ n = $ MAX ;"}
{"text":"In general Sieve of Sundaram , produces primes smaller than ( 2 * x + 2 ) for a number given number x","code":"$ nNew = ( int ) ( sqrt ( $ n ) ) ;"}
{"text":"This array is used to separate numbers of the form i + j + 2 ij from others where 1 <= i <= j","code":"$ marked = array_fill ( 0 , ( int ) ( $ n \/ 2 + 500 ) , 0 ) ;"}
{"text":"eliminate indexes which does not produce primes","code":"for ( $ i = 1 ; $ i <= ( $ nNew - 1 ) \/ 2 ; $ i ++ ) for ( $ j = ( $ i * ( $ i + 1 ) ) << 1 ; $ j <= $ n \/ 2 ; $ j = $ j + 2 * $ i + 1 ) $ marked [ $ j ] = 1 ;"}
{"text":"Since 2 is a prime number","code":"array_push ( $ primes , 2 ) ;"}
{"text":"Remaining primes are of the form 2 * i + 1 such that marked [ i ] is false .","code":"for ( $ i = 1 ; $ i <= $ n \/ 2 ; $ i ++ ) if ( $ marked [ $ i ] == 0 ) array_push ( $ primes , 2 * $ i + 1 ) ; }"}
{"text":"modified binary search to find nearest prime less than N","code":"function binarySearch ( $ left , $ right , $ n ) { global $ primes ; if ( $ left <= $ right ) { $ mid = ( int ) ( ( $ left + $ right ) \/ 2 ) ;"}
{"text":"base condition is , if we are reaching at left corner or right corner of primes [ ] array then return that corner element because before or after that we don 't have any prime number in  primes array","code":"if ( $ mid == 0 || $ mid == count ( $ primes ) - 1 ) return $ primes [ $ mid ] ;"}
{"text":"now if n is itself a prime so it will be present in primes array and here we have to find nearest prime less than n so we will return primes [ mid - 1 ]","code":"if ( $ primes [ $ mid ] == $ n ) return $ primes [ $ mid - 1 ] ;"}
{"text":"now if primes [ mid ] < n and primes [ mid + 1 ] > n that means we reached at nearest prime","code":"if ( $ primes [ $ mid ] < $ n && $ primes [ $ mid + 1 ] > $ n ) return $ primes [ $ mid ] ; if ( $ n < $ primes [ $ mid ] ) return binarySearch ( $ left , $ mid - 1 , $ n ) ; else return binarySearch ( $ mid + 1 , $ right , $ n ) ; } return 0 ; }"}
{"text":"Driver Code","code":"Sieve ( ) ; $ n = 17 ; echo binarySearch ( 0 , count ( $ primes ) - 1 , $ n ) ; ? >"}
{"text":"function to find factorial of given number","code":"< ? php function factorial ( $ n ) { if ( $ n == 0 ) return 1 ; return $ n * factorial ( $ n - 1 ) ; }"}
{"text":"Driver Code","code":"$ num = 5 ; echo \" Factorial \u2581 of \u2581 \" , $ num , \" \u2581 is \u2581 \" , factorial ( $ num ) ; ? >"}
{"text":"Returns k - th distinct element in arr .","code":"< ? php function printKDistinct ( $ arr , $ n , $ k ) { $ dist_count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) {"}
{"text":"Check if current element is present somewhere else .","code":"$ j ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) if ( $ i != $ j && $ arr [ $ j ] == $ arr [ $ i ] ) break ;"}
{"text":"If element is unique","code":"if ( $ j == $ n ) $ dist_count ++ ; if ( $ dist_count == $ k ) return $ arr [ $ i ] ; } return -1 ; }"}
{"text":"Driver Code","code":"$ ar = array ( 1 , 2 , 1 , 3 , 4 , 2 ) ; $ n = sizeof ( $ ar ) \/ sizeof ( $ ar [ 0 ] ) ; $ k = 2 ; echo printKDistinct ( $ ar , $ n , $ k ) ; ? >"}
{"text":"Function to calculate the count","code":"< ? php function calculate ( $ a , $ n ) {"}
{"text":"Sorting the list using built in function","code":"sort ( $ a ) ; $ count = 1 ; $ answer = 0 ;"}
{"text":"Traversing through the elements","code":"for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] == $ a [ $ i - 1 ] ) {"}
{"text":"Counting frequency of each elements","code":"$ count += 1 ; } else {"}
{"text":"Adding the contribution of the frequency to the answer","code":"$ answer = $ answer + ( $ count * ( $ count - 1 ) ) \/ 2 ; $ count = 1 ; } } $ answer = $ answer + ( $ count * ( $ count - 1 ) ) \/ 2 ; return $ answer ; }"}
{"text":"Driver Code","code":"$ a = array ( 1 , 2 , 1 , 2 , 4 ) ; $ n = count ( $ a ) ;"}
{"text":"Print the count","code":"echo calculate ( $ a , $ n ) ; ? >"}
{"text":"Function to calculate the answer","code":"< ? php function calculate ( $ a , $ n ) {"}
{"text":"Finding the maximum of the array","code":"$ maximum = max ( $ a ) ;"}
{"text":"Creating frequency array With initial value 0","code":"$ frequency = array_fill ( 0 , $ maximum + 1 , 0 ) ;"}
{"text":"Traversing through the array","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) {"}
{"text":"Counting frequency","code":"$ frequency [ $ a [ $ i ] ] += 1 ; } $ answer = 0 ;"}
{"text":"Traversing through the frequency array","code":"for ( $ i = 0 ; $ i < ( $ maximum ) + 1 ; $ i ++ ) {"}
{"text":"Calculating answer","code":"$ answer = $ answer + $ frequency [ $ i ] * ( $ frequency [ $ i ] - 1 ) ; } return $ answer \/ 2 ; }"}
{"text":"Driver Code","code":"$ a = array ( 1 , 2 , 1 , 2 , 4 ) ; $ n = count ( $ a ) ;"}
{"text":"Function calling","code":"echo ( calculate ( $ a , $ n ) ) ; ? >"}
{"text":"This function Prints the starting and ending indexes of the largest subarray with equal number of 0 s and 1 s . Also returns the size of such subarray .","code":"< ? php function findSubArray ( & $ arr , $ n ) { $ sum = 0 ; $ maxsize = -1 ;"}
{"text":"Pick a starting point as i","code":"for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { $ sum = ( $ arr [ $ i ] == 0 ) ? -1 : 1 ;"}
{"text":"Consider all subarrays starting from i","code":"for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) { ( $ arr [ $ j ] == 0 ) ? ( $ sum += -1 ) : ( $ sum += 1 ) ;"}
{"text":"If this is a 0 sum subarray , then compare it with maximum size subarray calculated so far","code":"if ( $ sum == 0 && $ maxsize < $ j - $ i + 1 ) { $ maxsize = $ j - $ i + 1 ; $ startindex = $ i ; } } } if ( $ maxsize == -1 ) echo \" No \u2581 such \u2581 subarray \" ; else echo $ startindex . \" \u2581 to \u2581 \" . ( $ startindex + $ maxsize - 1 ) ; return $ maxsize ; }"}
{"text":"Driver Code","code":"$ arr = array ( 1 , 0 , 0 , 1 , 0 , 1 , 1 ) ; $ size = sizeof ( $ arr ) ; findSubArray ( $ arr , $ size ) ; ? >"}
{"text":"Function to return the maximum element","code":"< ? php function findMax ( $ arr , $ low , $ high ) {"}
{"text":"This condition is for the case when array is not rotated at all","code":"if ( $ high <= $ low ) return $ arr [ $ low ] ;"}
{"text":"Find mid","code":"$ mid = $ low + ( $ high - $ low ) \/ 2 ;"}
{"text":"Check if mid reaches 0 , it is greater than next element or not","code":"if ( $ mid == 0 && $ arr [ $ mid ] > $ arr [ $ mid - 1 ] ) return $ arr [ 0 ] ;"}
{"text":"Check if mid itself is maximum element","code":"if ( $ mid < $ high && $ arr [ $ mid + 1 ] < $ arr [ $ mid ] && $ mid > 0 && $ arr [ $ mid ] > $ arr [ $ mid - 1 ] ) { return $ arr [ $ mid ] ; }"}
{"text":"Decide whether we need to go to the left half or the right half","code":"if ( $ arr [ $ low ] > $ arr [ $ mid ] ) { return findMax ( $ arr , $ low , $ mid - 1 ) ; } else { return findMax ( $ arr , $ mid + 1 , $ high ) ; } }"}
{"text":"Driver code","code":"$ arr = array ( 5 , 6 , 1 , 2 , 3 , 4 ) ; $ n = sizeof ( $ arr ) ; echo findMax ( $ arr , 0 , $ n - 1 ) ;"}
{"text":"Returns index of key in arr [ l . . h ] if key is present , otherwise returns - 1","code":"< ? php function search ( $ arr , $ l , $ h , $ key ) { if ( $ l > $ h ) return -1 ; $ mid = ( $ l + $ h ) \/ 2 ; if ( $ arr [ $ mid ] == $ key ) return $ mid ;"}
{"text":"If arr [ l ... mid ] is sorted","code":"if ( $ arr [ $ l ] <= $ arr [ $ mid ] ) {"}
{"text":"As this subarray is sorted , we can quickly check if key lies in half or other half","code":"if ( $ key >= $ arr [ $ l ] && $ key <= $ arr [ $ mid ] ) return search ( $ arr , $ l , $ mid - 1 , $ key ) ;"}
{"text":"If key not lies in first half subarray , Divide other half into two subarrays , such that we can quickly check if key lies in other half","code":"return search ( $ arr , $ mid + 1 , $ h , $ key ) ; }"}
{"text":"If arr [ l . . mid ] is not sorted , then arr [ mid ... r ] must be sorted","code":"if ( $ key >= $ arr [ $ mid ] && $ key <= $ arr [ $ h ] ) return search ( $ arr , $ mid + 1 , $ h , $ key ) ; return search ( $ arr , $ l , $ mid - 1 , $ key ) ; }"}
{"text":"Driver Code","code":"$ arr = array ( 4 , 5 , 6 , 7 , 8 , 9 , 1 , 2 , 3 ) ; $ n = sizeof ( $ arr ) ; $ key = 6 ; $ i = search ( $ arr , 0 , $ n - 1 , $ key ) ; if ( $ i != -1 ) echo \" Index : \u2581 \" , floor ( $ i ) , \" \u2581 STRNEWLINE \" ; else echo \" Key \u2581 not \u2581 found \" ; ? >"}
{"text":"PHP program to find minimum element in a sorted and rotated array","code":"< ? php function findMin ( $ arr , $ low , $ high ) {"}
{"text":"This condition is needed to handle the case when array is not rotated at all","code":"if ( $ high < $ low ) return $ arr [ 0 ] ;"}
{"text":"If there is only one element left","code":"if ( $ high == $ low ) return $ arr [ $ low ] ;"}
{"text":"Find mid","code":"$ mid = $ low + ( $ high - $ low ) \/ 2 ;"}
{"text":"Check if element ( mid + 1 ) is minimum element . Consider the cases like ( 3 , 4 , 5 , 1 , 2 )","code":"if ( $ mid < $ high && $ arr [ $ mid + 1 ] < $ arr [ $ mid ] ) return $ arr [ $ mid + 1 ] ;"}
{"text":"Check if mid itself is minimum element","code":"if ( $ mid > $ low && $ arr [ $ mid ] < $ arr [ $ mid - 1 ] ) return $ arr [ $ mid ] ;"}
{"text":"Decide whether we need to go to left half or right half","code":"if ( $ arr [ $ high ] > $ arr [ $ mid ] ) return findMin ( $ arr , $ low , $ mid - 1 ) ; return findMin ( $ arr , $ mid + 1 , $ high ) ; }"}
{"text":"Driver Code","code":"$ arr1 = array ( 5 , 6 , 1 , 2 , 3 , 4 ) ; $ n1 = sizeof ( $ arr1 ) ; echo \" The \u2581 minimum \u2581 element \u2581 is \u2581 \" . findMin ( $ arr1 , 0 , $ n1 - 1 ) . \" STRNEWLINE \" ; $ arr2 = array ( 1 , 2 , 3 , 4 ) ; $ n2 = sizeof ( $ arr2 ) ; echo \" The \u2581 minimum \u2581 element \u2581 is \u2581 \" . findMin ( $ arr2 , 0 , $ n2 - 1 ) . \" STRNEWLINE \" ; $ arr3 = array ( 1 ) ; $ n3 = sizeof ( $ arr3 ) ; echo \" The \u2581 minimum \u2581 element \u2581 is \u2581 \" . findMin ( $ arr3 , 0 , $ n3 - 1 ) . \" STRNEWLINE \" ; $ arr4 = array ( 1 , 2 ) ; $ n4 = sizeof ( $ arr4 ) ; echo \" The \u2581 minimum \u2581 element \u2581 is \u2581 \" . findMin ( $ arr4 , 0 , $ n4 - 1 ) . \" STRNEWLINE \" ; $ arr5 = array ( 2 , 1 ) ; $ n5 = sizeof ( $ arr5 ) ; echo \" The \u2581 minimum \u2581 element \u2581 is \u2581 \" . findMin ( $ arr5 , 0 , $ n5 - 1 ) . \" STRNEWLINE \" ; $ arr6 = array ( 5 , 6 , 7 , 1 , 2 , 3 , 4 ) ; $ n6 = sizeof ( $ arr6 ) ; echo \" The \u2581 minimum \u2581 element \u2581 is \u2581 \" . findMin ( $ arr6 , 0 , $ n6 - 1 ) . \" STRNEWLINE \" ; $ arr7 = array ( 1 , 2 , 3 , 4 , 5 , 6 , 7 ) ; $ n7 = sizeof ( $ arr7 ) ; echo \" The \u2581 minimum \u2581 element \u2581 is \u2581 \" . findMin ( $ arr7 , 0 , $ n7 - 1 ) . \" STRNEWLINE \" ; $ arr8 = array ( 2 , 3 , 4 , 5 , 6 , 7 , 8 , 1 ) ; $ n8 = sizeof ( $ arr8 ) ; echo \" The \u2581 minimum \u2581 element \u2581 is \u2581 \" . findMin ( $ arr8 , 0 , $ n8 - 1 ) . \" STRNEWLINE \" ; $ arr9 = array ( 3 , 4 , 5 , 1 , 2 ) ; $ n9 = sizeof ( $ arr9 ) ; echo \" The \u2581 minimum \u2581 element \u2581 is \u2581 \" . findMin ( $ arr9 , 0 , $ n9 - 1 ) . \" STRNEWLINE \" ; ? >"}
{"text":"Function to print first smallest and second smallest elements","code":"< ? php function print2Smallest ( $ arr , $ arr_size ) { $ INT_MAX = 2147483647 ;"}
{"text":"There should be atleast two elements","code":"if ( $ arr_size < 2 ) { echo ( \" \u2581 Invalid \u2581 Input \u2581 \" ) ; return ; } $ first = $ second = $ INT_MAX ; for ( $ i = 0 ; $ i < $ arr_size ; $ i ++ ) {"}
{"text":"If current element is smaller than first then update both first and second","code":"if ( $ arr [ $ i ] < $ first ) { $ second = $ first ; $ first = $ arr [ $ i ] ; }"}
{"text":"If arr [ i ] is in between first and second then update second","code":"else if ( $ arr [ $ i ] < $ second && $ arr [ $ i ] != $ first ) $ second = $ arr [ $ i ] ; } if ( $ second == $ INT_MAX ) echo ( \" There \u2581 is \u2581 no \u2581 second \u2581 smallest \u2581 element STRNEWLINE \" ) ; else echo \" The \u2581 smallest \u2581 element \u2581 is \u2581 \" , $ first , \" \u2581 and \u2581 second \u2581 Smallest \u2581 element \u2581 is \u2581 \" , $ second ; }"}
{"text":"Driver Code","code":"$ arr = array ( 12 , 13 , 1 , 10 , 34 , 1 ) ; $ n = count ( $ arr ) ; print2Smallest ( $ arr , $ n ) ? >"}
{"text":"Returns true if there exists a subset with given sum in arr [ ]","code":"< ? php function isSubsetSum ( $ arr , $ n , $ sum ) {"}
{"text":"The value of subset [ i % 2 ] [ j ] will be true if there exists a subset of sum j in arr [ 0 , 1 , ... . , i - 1 ]","code":"$ subset [ 2 ] [ $ sum + 1 ] = array ( ) ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) { for ( $ j = 0 ; $ j <= $ sum ; $ j ++ ) {"}
{"text":"A subset with sum 0 is always possible","code":"if ( $ j == 0 ) $ subset [ $ i % 2 ] [ $ j ] = true ;"}
{"text":"If there exists no element no sum is possible","code":"else if ( $ i == 0 ) $ subset [ $ i % 2 ] [ $ j ] = false ; else if ( $ arr [ $ i - 1 ] <= $ j ) $ subset [ $ i % 2 ] [ $ j ] = $ subset [ ( $ i + 1 ) % 2 ] [ $ j - $ arr [ $ i - 1 ] ] || $ subset [ ( $ i + 1 ) % 2 ] [ $ j ] ; else $ subset [ $ i % 2 ] [ $ j ] = $ subset [ ( $ i + 1 ) % 2 ] [ $ j ] ; } } return $ subset [ $ n % 2 ] [ $ sum ] ; }"}
{"text":"Driver code","code":"$ arr = array ( 6 , 2 , 5 ) ; $ sum = 7 ; $ n = sizeof ( $ arr ) ; if ( isSubsetSum ( $ arr , $ n , $ sum ) == true ) echo ( \" There \u2581 exists \u2581 a \u2581 subset \u2581 with \u2581 given \u2581 sum \" ) ; else echo ( \" No \u2581 subset \u2581 exists \u2581 with \u2581 given \u2581 sum \" ) ; ? >"}
{"text":"Function to find maximum equilibrium sum .","code":"< ? php function findMaxSum ( $ arr , $ n ) { $ res = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ prefix_sum = $ arr [ $ i ] ; for ( $ j = 0 ; $ j < $ i ; $ j ++ ) $ prefix_sum += $ arr [ $ j ] ; $ suffix_sum = $ arr [ $ i ] ; for ( $ j = $ n - 1 ; $ j > $ i ; $ j -- ) $ suffix_sum += $ arr [ $ j ] ; if ( $ prefix_sum == $ suffix_sum ) $ res = max ( $ res , $ prefix_sum ) ; } return $ res ; }"}
{"text":"Driver Code","code":"$ arr = array ( -2 , 5 , 3 , 1 , 2 , 6 , -4 , 2 ) ; $ n = count ( $ arr ) ; echo findMaxSum ( $ arr , $ n ) ; ? >"}
{"text":"Function to find maximum equilibrium sum .","code":"< ? php function findMaxSum ( $ arr , $ n ) {"}
{"text":"Array to store prefix sum .","code":"$ preSum [ $ n ] = array ( ) ;"}
{"text":"Array to store suffix sum .","code":"$ suffSum [ $ n ] = array ( ) ;"}
{"text":"Variable to store maximum sum .","code":"$ ans = PHP_INT_MIN ;"}
{"text":"Calculate prefix sum .","code":"$ preSum [ 0 ] = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ preSum [ $ i ] = $ preSum [ $ i - 1 ] + $ arr [ $ i ] ;"}
{"text":"Calculate suffix sum and compare it with prefix sum . Update ans accordingly .","code":"$ suffSum [ $ n - 1 ] = $ arr [ $ n - 1 ] ; if ( $ preSum [ $ n - 1 ] == $ suffSum [ $ n - 1 ] ) $ ans = max ( $ ans , $ preSum [ $ n - 1 ] ) ; for ( $ i = $ n - 2 ; $ i >= 0 ; $ i -- ) { $ suffSum [ $ i ] = $ suffSum [ $ i + 1 ] + $ arr [ $ i ] ; if ( $ suffSum [ $ i ] == $ preSum [ $ i ] ) $ ans = max ( $ ans , $ preSum [ $ i ] ) ; } return $ ans ; }"}
{"text":"Driver Code","code":"$ arr = array ( -2 , 5 , 3 , 1 , 2 , 6 , -4 , 2 ) ; $ n = sizeof ( $ arr ) ; echo findMaxSum ( $ arr , $ n ) ;"}
{"text":"Function to find Majority element in an array","code":"< ? php function findMajority ( $ arr , $ n ) { $ maxCount = 0 ;"}
{"text":"sentinels","code":"$ index = -1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ count = 0 ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ arr [ $ i ] == $ arr [ $ j ] ) $ count ++ ; }"}
{"text":"update maxCount if count of current element is greater","code":"if ( $ count > $ maxCount ) { $ maxCount = $ count ; $ index = $ i ; } }"}
{"text":"if maxCount is greater than n \/ 2 return the corresponding element","code":"if ( $ maxCount > $ n \/ 2 ) echo $ arr [ $ index ] . \" STRNEWLINE \" ; else echo \" No \u2581 Majority \u2581 Element \" . \" STRNEWLINE \" ; }"}
{"text":"Driver code","code":"$ arr = array ( 1 , 1 , 2 , 1 , 3 , 5 , 1 ) ; $ n = sizeof ( $ arr ) ;"}
{"text":"Function calling","code":"findMajority ( $ arr , $ n ) ;"}
{"text":"Function to find the candidate for Majority","code":"< ? php function findCandidate ( $ a , $ size ) { $ maj_index = 0 ; $ count = 1 ; for ( $ i = 1 ; $ i < $ size ; $ i ++ ) { if ( $ a [ $ maj_index ] == $ a [ $ i ] ) $ count ++ ; else $ count -- ; if ( $ count == 0 ) { $ maj_index = $ i ; $ count = 1 ; } } return $ a [ $ maj_index ] ; }"}
{"text":"Function to check if the candidate occurs more than n \/ 2 times","code":"function isMajority ( $ a , $ size , $ cand ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ size ; $ i ++ ) if ( $ a [ $ i ] == $ cand ) $ count ++ ; if ( $ count > $ size \/ 2 ) return 1 ; else return 0 ; }"}
{"text":"Function to print Majority Element","code":"function printMajority ( $ a , $ size ) {"}
{"text":"Find the candidate for Majority","code":"$ cand = findCandidate ( $ a , $ size ) ;"}
{"text":"Print the candidate if it is Majority","code":"if ( isMajority ( $ a , $ size , $ cand ) ) echo \" \" , \u2581 $ cand , \u2581 \" \" else echo \" No \u2581 Majority \u2581 Element \" ; }"}
{"text":"Driver Code","code":"$ a = array ( 1 , 3 , 3 , 1 , 2 ) ; $ size = sizeof ( $ a ) ;"}
{"text":"Function calling","code":"printMajority ( $ a , $ size ) ; ? >"}
{"text":"Returns true if there is a subset of set [ ] with sum equal to given sum","code":"< ? php function isSubsetSum ( $ set , $ n , $ sum ) {"}
{"text":"The value of subset [ i ] [ j ] will be true if there is a subset of set [ 0. . j - 1 ] with sum equal to i","code":"$ subset = array ( array ( ) ) ;"}
{"text":"If sum is 0 , then answer is true","code":"for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) $ subset [ $ i ] [ 0 ] = true ;"}
{"text":"If sum is not 0 and set is empty , then answer is false","code":"for ( $ i = 1 ; $ i <= $ sum ; $ i ++ ) $ subset [ 0 ] [ $ i ] = false ;"}
{"text":"Fill the subset table in bottom up manner","code":"for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { for ( $ j = 1 ; $ j <= $ sum ; $ j ++ ) { if ( $ j < $ set [ $ i - 1 ] ) $ subset [ $ i ] [ $ j ] = $ subset [ $ i - 1 ] [ $ j ] ; if ( $ j >= $ set [ $ i - 1 ] ) $ subset [ $ i ] [ $ j ] = $ subset [ $ i - 1 ] [ $ j ] || $ subset [ $ i - 1 ] [ $ j - $ set [ $ i - 1 ] ] ; } }"}
{"text":"uncomment this code to print table","code":"for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= sum ; j ++ ) printf ( \" % 4d \" , subset [ i ] [ j ] ) ; printf ( \" n \" ) ; } return $ subset [ $ n ] [ $ sum ] ; }"}
{"text":"Driver code","code":"$ set = array ( 3 , 34 , 4 , 12 , 5 , 2 ) ; $ sum = 9 ; $ n = count ( $ set ) ; if ( isSubsetSum ( $ set , $ n , $ sum ) == true ) echo \" Found \u2581 a \u2581 subset \u2581 with \u2581 given \u2581 sum \" ; else echo \" No \u2581 subset \u2581 with \u2581 given \u2581 sum \" ; ? >"}
{"text":"PHP implementation of the approach returns the gcd after all updates in the array","code":"< ? php function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; }"}
{"text":"Function to calculate gcd of onine queries","code":"function print_gcd_online ( $ n , $ m , $ query , $ arr ) {"}
{"text":"stores the gcd of the initial array elements","code":"$ max_gcd = 0 ; $ i = 0 ;"}
{"text":"calculates the gcd","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ max_gcd = gcd ( $ max_gcd , $ arr [ $ i ] ) ;"}
{"text":"performing online queries","code":"for ( $ i = 0 ; $ i < $ m ; $ i ++ ) {"}
{"text":"index is 1 based","code":"$ query [ $ i ] [ 0 ] -- ;"}
{"text":"divide the array element","code":"$ arr [ $ query [ $ i ] [ 0 ] ] \/= $ query [ $ i ] [ 1 ] ;"}
{"text":"calculates the current gcd","code":"$ max_gcd = gcd ( $ arr [ $ query [ $ i ] [ 0 ] ] , $ max_gcd ) ;"}
{"text":"print the gcd after each step","code":"echo ( $ max_gcd ) , \" STRNEWLINE \" ; } }"}
{"text":"Driver code","code":"$ n = 3 ; $ m = 3 ; $ query ; $ arr = array ( 36 , 24 , 72 ) ; $ query [ 0 ] [ 0 ] = 1 ; $ query [ 0 ] [ 1 ] = 3 ; $ query [ 1 ] [ 0 ] = 3 ; $ query [ 1 ] [ 1 ] = 12 ; $ query [ 2 ] [ 0 ] = 2 ; $ query [ 2 ] [ 1 ] = 4 ; print_gcd_online ( $ n , $ m , $ query , $ arr ) ; ? >"}
{"text":"PHP implementation of the approach","code":"< ? php $ MAX = 100000 ;"}
{"text":"stores whether the number is prime or not","code":"$ prime = array_fill ( 0 , $ MAX + 1 , true ) ;"}
{"text":"stores the count of prime numbers less than or equal to the index","code":"$ sum = array_fill ( 0 , $ MAX + 1 , 0 ) ;"}
{"text":"create the sieve","code":"function SieveOfEratosthenes ( ) { global $ MAX , $ sum , $ prime"}
{"text":"Create a boolean array \" prime [ 0 . . n ] \" and initialize all the entries as true . A value in prime [ i ] will finally be false if ' i ' is Not a prime , else true .","code":"; $ prime [ 1 ] = false ; for ( $ p = 2 ; $ p * $ p <= $ MAX ; $ p ++ ) {"}
{"text":"If prime [ p ] is not changed , then it is a prime","code":"if ( $ prime [ $ p ] ) {"}
{"text":"Update all multiples of p","code":"for ( $ i = $ p * 2 ; $ i <= $ MAX ; $ i += $ p ) $ prime [ $ i ] = false ; } }"}
{"text":"stores the prefix sum of number of primes less than or equal to ' i '","code":"for ( $ i = 1 ; $ i <= $ MAX ; $ i ++ ) { if ( $ prime [ $ i ] == true ) $ sum [ $ i ] = 1 ; $ sum [ $ i ] += $ sum [ $ i - 1 ] ; } }"}
{"text":"create the sieve","code":"SieveOfEratosthenes ( ) ;"}
{"text":"' l ' and ' r ' are the lower and upper bounds of the range","code":"$ l = 3 ; $ r = 9 ;"}
{"text":"get the value of count","code":"$ c = ( $ sum [ $ r ] - $ sum [ $ l - 1 ] ) ;"}
{"text":"display the count","code":"echo \" Count : \" \u2581 . \u2581 $ c \u2581 . \u2581 \" \" ? >"}
{"text":"Function to find the area of the circle","code":"< ? php function area ( $ r ) {"}
{"text":"radius cannot be negative","code":"if ( $ r < 0 ) return -1 ;"}
{"text":"area of the circle","code":"$ area = 3.14 * pow ( $ r \/ ( 2 * sqrt ( 2 ) ) , 2 ) ; return $ area ; }"}
{"text":"Driver code","code":"$ a = 5 ; echo area ( $ a ) ;"}
{"text":"PHP program to count almost prime numbers from 1 to n","code":"< ? php $ N = 100005 ;"}
{"text":"Create a boolean array \" prime [ 0 . . n ] \" and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true .","code":"$ prime = array_fill ( 0 , $ N , true ) ; function SieveOfEratosthenes ( ) { global $ N , $ prime ; $ prime [ 1 ] = false ; for ( $ p = 2 ; $ p < ( int ) ( sqrt ( $ N ) ) ; $ p ++ ) {"}
{"text":"If prime [ p ] is not changed , then it is a prime","code":"if ( $ prime [ $ p ] == true )"}
{"text":"Update all multiples of p","code":"for ( $ i = 2 * $ p ; $ i < $ N ; $ i += $ p ) $ prime [ $ i ] = false ; } }"}
{"text":"Function to count almost prime numbers from 1 to n","code":"function almostPrimes ( $ n ) { global $ prime ;"}
{"text":"to store required answer","code":"$ ans = 0 ;"}
{"text":"6 is first almost prime number","code":"for ( $ i = 6 ; $ i < $ n + 1 ; $ i ++ ) {"}
{"text":"to count prime factors","code":"$ c = 0 ; for ( $ j = 2 ; $ i >= $ j * $ j ; $ j ++ ) {"}
{"text":"if it is perfect square","code":"if ( $ i % $ j == 0 ) { if ( $ j * $ j == $ i ) { if ( $ prime [ $ j ] ) $ c += 1 ; } else { if ( $ prime [ $ j ] ) $ c += 1 ; if ( $ prime [ ( $ i \/ $ j ) ] ) $ c += 1 ; } } }"}
{"text":"if I is almost prime number","code":"if ( $ c == 2 ) $ ans += 1 ; } return $ ans ; }"}
{"text":"Driver Code","code":"SieveOfEratosthenes ( ) ; $ n = 21 ; print ( almostPrimes ( $ n ) ) ; ? >"}
{"text":"Returns sum of digits of x","code":"< ? php function sumOfDigitsSingle ( $ x ) { $ ans = 0 ; while ( $ x ) { $ ans += $ x % 10 ; $ x \/= 10 ; } return $ ans ; }"}
{"text":"Returns closest number to x in terms of 9 's.","code":"function closest ( $ x ) { $ ans = 0 ; while ( $ ans * 10 + 9 <= $ x ) $ ans = $ ans * 10 + 9 ; return $ ans ; } function sumOfDigitsTwoParts ( $ N ) { $ A = closest ( $ N ) ; return sumOfDigitsSingle ( $ A ) + sumOfDigitsSingle ( $ N - $ A ) ; }"}
{"text":"Driver code","code":"$ N = 35 ; echo sumOfDigitsTwoParts ( $ N ) ; ? >"}
{"text":"Function to check whether \/ ( 2 ^ p - 1 ) is prime or not .","code":"< ? php function isPrime ( $ p ) {"}
{"text":"generate the number","code":"$ checkNumber = pow ( 2 , $ p ) - 1 ;"}
{"text":"First number of the series","code":"$ nextval = 4 % $ checkNumber ;"}
{"text":"Generate the rest ( p - 2 ) terms of the series .","code":"for ( $ i = 1 ; $ i < $ p - 1 ; $ i ++ ) $ nextval = ( $ nextval * $ nextval - 2 ) % $ checkNumber ;"}
{"text":"now if the ( p - 1 ) th term is 0 return true else false .","code":"return ( $ nextval == 0 ) ; }"}
{"text":"Driver Code Check whether 2 ^ p - 1 is prime or not .","code":"$ p = 7 ; $ checkNumber = pow ( 2 , $ p ) - 1 ; if ( isPrime ( $ p ) ) echo $ checkNumber , \" \u2581 is \u2581 Prime . \" ; else echo $ checkNumber , \" \u2581 is \u2581 not \u2581 Prime . \" ; ? >"}
{"text":"function to detect prime number here we have used sieve method https : www . geeksforgeeks . org \/ sieve - of - eratosthenes \/ to detect prime number","code":"< ? php function sieve ( $ n , & $ prime ) { for ( $ p = 2 ; $ p * $ p <= $ n ; $ p ++ ) {"}
{"text":"If prime [ p ] is not changed , then it is a prime","code":"if ( $ prime [ $ p ] == true ) {"}
{"text":"Update all multiples of p","code":"for ( $ i = $ p * 2 ; $ i <= $ n ; $ i += $ p ) $ prime [ $ i ] = false ; } } } function printSophieGermanNumber ( $ n ) {"}
{"text":"We have made array till 2 * n + 1 so that we can check prime number till that and conclude about sophie german prime .","code":"$ prime = array ( ) ; for ( $ i = 0 ; $ i < ( 2 * $ n + 1 ) ; $ i ++ ) $ prime [ $ i ] = true ; sieve ( 2 * $ n + 1 , $ prime ) ; for ( $ i = 2 ; $ i <= $ n ; ++ $ i ) {"}
{"text":"checking every i whether it is sophie german prime or not .","code":"if ( $ prime [ $ i ] && $ prime [ 2 * $ i + 1 ] ) echo ( $ i . \" \u2581 \" ) ; } }"}
{"text":"Driver code","code":"$ n = 25 ; printSophieGermanNumber ( $ n ) ; ? >"}
{"text":"calculating u mentioned in the formula","code":"< ? php function ucal ( $ u , $ n ) { if ( $ n == 0 ) return 1 ; $ temp = $ u ; for ( $ i = 1 ; $ i <= ( int ) ( $ n \/ 2 ) ; $ i ++ ) $ temp = $ temp * ( $ u - $ i ) ; for ( $ i = 1 ; $ i < ( int ) ( $ n \/ 2 ) ; $ i ++ ) $ temp = $ temp * ( $ u + $ i ) ; return $ temp ; }"}
{"text":"calculating factorial of given number n","code":"function fact ( $ n ) { $ f = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ f *= $ i ; return $ f ; }"}
{"text":"Number of values given","code":"$ n = 6 ; $ x = array ( 25 , 26 , 27 , 28 , 29 , 30 ) ;"}
{"text":"y [ ] [ ] is used for difference table with y [ ] [ 0 ] used for input","code":"$ y ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ n ; $ j ++ ) $ y [ $ i ] [ $ j ] = 0.0 ; $ y [ 0 ] [ 0 ] = 4.000 ; $ y [ 1 ] [ 0 ] = 3.846 ; $ y [ 2 ] [ 0 ] = 3.704 ; $ y [ 3 ] [ 0 ] = 3.571 ; $ y [ 4 ] [ 0 ] = 3.448 ; $ y [ 5 ] [ 0 ] = 3.333 ;"}
{"text":"Calculating the central difference table","code":"for ( $ i = 1 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ n - $ i ; $ j ++ ) $ y [ $ j ] [ $ i ] = $ y [ $ j + 1 ] [ $ i - 1 ] - $ y [ $ j ] [ $ i - 1 ] ;"}
{"text":"Displaying the central difference table","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n - $ i ; $ j ++ ) echo str_pad ( $ y [ $ i ] [ $ j ] , 4 ) . \" TABSYMBOL \" ; echo \" STRNEWLINE \" ; }"}
{"text":"value to interpolate at","code":"$ value = 27.4 ;"}
{"text":"Initializing u and sum","code":"$ sum = ( $ y [ 2 ] [ 0 ] + $ y [ 3 ] [ 0 ] ) \/ 2 ;"}
{"text":"k is origin thats is f ( 0 )","code":"$ k ;"}
{"text":"if ( $n % 2 ) origin for odd","code":"$ k = $ n \/ 2 ; else"}
{"text":"$k = $n \/ 2 - 1 ; origin for even","code":"$ u = ( $ value - $ x [ $ k ] ) \/ ( $ x [ 1 ] - $ x [ 0 ] ) ;"}
{"text":"Solving using bessel 's formula","code":"for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { if ( $ i % 2 ) $ sum = $ sum + ( ( $ u - 0.5 ) * ucal ( $ u , $ i - 1 ) * $ y [ $ k ] [ $ i ] ) \/ fact ( $ i ) ; else $ sum = $ sum + ( ucal ( $ u , $ i ) * ( $ y [ $ k ] [ $ i ] + $ y [ -- $ k ] [ $ i ] ) \/ ( fact ( $ i ) * 2 ) ) ; } echo \" Value \u2581 at \u2581 \" . $ value . \" \u2581 is \u2581 \" . $ sum . \" STRNEWLINE \" ; ? >"}
{"text":"A simple PHP program to check if n - th Fibonacci number is multiple of 10.","code":"< ? php function fibonacci ( $ n ) { $ a = 0 ; $ b = 1 ; $ c ; if ( $ n <= 1 ) return $ n ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { $ c = $ a + $ b ; $ a = $ b ; $ b = $ c ; } return $ c ; }"}
{"text":"Returns true if n - th Fibonacci number is multiple of 10.","code":"function isMultipleOf10 ( $ n ) { $ f = fibonacci ( 30 ) ; return ( $ f % 10 == 0 ) ; }"}
{"text":"Driver code","code":"$ n = 30 ; if ( isMultipleOf10 ( $ n ) ) echo \" Yes STRNEWLINE \" ; else echo \" No STRNEWLINE \" ; ? >"}
{"text":"Function to check if x is power of 2","code":"< ? php function isPowerOfTwo ( $ x ) {"}
{"text":"First x in the below expression is for the case when x is 0","code":"return $ x && ( ! ( $ x & ( $ x - 1 ) ) ) ; }"}
{"text":"Driver Code","code":"if ( isPowerOfTwo ( 31 ) ) echo \" Yes STRNEWLINE \" ; else echo \" No STRNEWLINE \" ; if ( isPowerOfTwo ( 64 ) ) echo \" Yes STRNEWLINE \" ; else echo \" No STRNEWLINE \" ; ? >"}
{"text":"Function to find the nearest power of 2","code":"< ? php function nextPowerOf2 ( $ n ) {"}
{"text":"The number","code":"$ p = 1 ;"}
{"text":"If already a power of 2","code":"if ( $ n && ! ( $ n & ( $ n - 1 ) ) ) return $ n ;"}
{"text":"Find the next power of 2","code":"while ( $ p < $ n ) $ p <<= 1 ; return $ p ; }"}
{"text":"Function to find the memory used","code":"function memoryUsed ( & $ arr , $ n ) {"}
{"text":"Sum of array","code":"$ sum = 0 ;"}
{"text":"Traverse and find the sum of array","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += $ arr [ $ i ] ;"}
{"text":"Function call to find the nearest power of 2","code":"$ nearest = nextPowerOf2 ( $ sum ) ; return $ nearest ; }"}
{"text":"Driver Code","code":"$ arr = array ( 1 , 2 , 3 , 2 ) ; $ n = sizeof ( $ arr ) ; echo ( memoryUsed ( $ arr , $ n ) ) ; ? >"}
{"text":"Php program to toggle k - th bit of n","code":"< ? php function toggleKthBit ( $ n , $ k ) { return ( $ n ^ ( 1 << ( $ k - 1 ) ) ) ; }"}
{"text":"Driver code","code":"$ n = 5 ; $ k = 1 ; echo toggleKthBit ( $ n , $ k ) ; ? >"}
{"text":"PHP program to find smallest power of 2 greater than or equal to n","code":"< ? php function nextPowerOf2 ( $ n ) { $ count = 0 ;"}
{"text":"First n in the below condition is for the case where n is 0","code":"if ( $ n && ! ( $ n & ( $ n - 1 ) ) ) return $ n ; while ( $ n != 0 ) { $ n >>= 1 ; $ count += 1 ; } return 1 << $ count ; }"}
{"text":"Driver Code","code":"$ n = 0 ; echo ( nextPowerOf2 ( $ n ) ) ; ? >"}
{"text":"Function to print the N - th tetranacci number","code":"< ? php function printTetra ( $ n ) { if ( $ n < 0 ) return ;"}
{"text":"Initialize first four numbers to base cases","code":"$ first = 0 ; $ second = 1 ; $ third = 1 ; $ fourth = 2 ;"}
{"text":"declare a current variable","code":"$ curr ; if ( $ n == 0 ) echo $ first ; else if ( $ n == 1 $ n == 2 ) echo $ second ; else if ( $ n == 3 ) echo $ fourth ; else {"}
{"text":"Loop to add previous four numbers for each number starting from 4 and then assign first , second , third to second , third , fourth and curr to fourth respectively","code":"for ( $ i = 4 ; $ i <= $ n ; $ i ++ ) { $ curr = $ first + $ second + $ third + $ fourth ; $ first = $ second ; $ second = $ third ; $ third = $ fourth ; $ fourth = $ curr ; } echo $ curr ; } }"}
{"text":"Driver code","code":"$ n = 10 ; printTetra ( $ n ) ; ? >"}
{"text":"A recursive function used by countWays","code":"< ? php function countWays ( $ n ) { $ res [ 0 ] = 1 ; $ res [ 1 ] = 1 ; $ res [ 2 ] = 2 ; for ( $ i = 3 ; $ i <= $ n ; $ i ++ ) $ res [ $ i ] = $ res [ $ i - 1 ] + $ res [ $ i - 2 ] + $ res [ $ i - 3 ] ; return $ res [ $ n ] ; }"}
{"text":"Driver Code","code":"$ n = 4 ; echo countWays ( $ n ) ; ? >"}
{"text":"Returns maximum amount of task that can be done till day n","code":"< ? php function maxTasks ( $ high , $ low , $ n ) {"}
{"text":"If n is less than equal to 0 , then no solution exists","code":"if ( $ n <= 0 ) return 0 ;"}
{"text":"Determines which task to choose on day n , then returns the maximum till that day","code":"return max ( $ high [ $ n - 1 ] + maxTasks ( $ high , $ low , ( $ n - 2 ) ) , $ low [ $ n - 1 ] + maxTasks ( $ high , $ low , ( $ n - 1 ) ) ) ; }"}
{"text":"Driver Code","code":"$ n = 5 ; $ high = array ( 3 , 6 , 8 , 7 , 6 ) ; $ low = array ( 1 , 5 , 4 , 5 , 3 ) ; print ( maxTasks ( $ high , $ low , $ n ) ) ; ? >"}
{"text":"function to count substrings starting with character X and ending with character Y","code":"< ? php function countSubstr ( $ str , $ n , $ x , $ y ) {"}
{"text":"to store total count of required substrings","code":"$ tot_count = 0 ;"}
{"text":"to store count of character ' x ' up to the point the string ' str ' has been traversed so far","code":"$ count_x = 0 ;"}
{"text":"traverse ' str ' form left to right","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) {"}
{"text":"if true , increment ' count _ x '","code":"if ( $ str [ $ i ] == $ x ) $ count_x ++ ;"}
{"text":"if true accumulate ' count _ x ' to ' tot _ count '","code":"if ( $ str [ $ i ] == $ y ) $ tot_count += $ count_x ; }"}
{"text":"required count","code":"return $ tot_count ; }"}
{"text":"Driver code","code":"$ str = \" abbcaceghcak \" ; $ n = strlen ( $ str ) ; $ x = ' a ' ; $ y = ' c ' ; echo \" Count = \""}
{"text":"PHP program to count no of words from given input string","code":"< ? php $ OUT = 0 ; $ IN = 1 ;"}
{"text":"returns number of words in str","code":"function countWords ( $ str ) { global $ OUT , $ IN ; $ state = $ OUT ;"}
{"text":"word count","code":"$ wc = 0 ; $ i = 0 ;"}
{"text":"Scan all characters one by one","code":"while ( $ i < strlen ( $ str ) ) {"}
{"text":"If next character is a separator , set the state as OUT","code":"if ( $ str [ $ i ] == \" \u2581 \" $ str [ $ i ] == \" STRNEWLINE \" $ str [ $ i ] == \" TABSYMBOL \" ) $ state = $ OUT ;"}
{"text":"If next character is not a word separator and state is OUT , then set the state as IN and increment word count","code":"else if ( $ state == $ OUT ) { $ state = $ IN ; ++ $ wc ; }"}
{"text":"Move to next character","code":"++ $ i ; } return $ wc ; }"}
{"text":"Driver Code","code":"$ str = \" One \u2581 twothree STRNEWLINE \u2581 four TABSYMBOL five \u2581 \" ; echo \" No \u2581 of \u2581 words \u2581 : \u2581 \" . countWords ( $ str ) ; ? >"}
{"text":"Function to calculate Enneadecagonal number","code":"< ? php function nthEnneadecagonal ( $ n ) {"}
{"text":"Formula for finding nth Enneadecagonal number","code":"return ( 17 * $ n * $ n - 15 * $ n ) \/ 2 ; }"}
{"text":"Driver Code","code":"$ n = 6 ; echo $ n , \" th \u2581 Enneadecagonal \u2581 number \u2581 : \" , nthEnneadecagonal ( $ n ) ; ? >"}
{"text":"PHP Program to find the area of a circumscribed circle","code":"< ? php $ PI = 3.14159265 ;"}
{"text":"function returns the area","code":"function areacircumscribed ( $ a ) { global $ PI ; return ( $ a * $ a * ( $ PI \/ 2 ) ) ; }"}
{"text":"Driver code","code":"$ a = 6 ; echo \" \u2581 Area \u2581 of \u2581 an \u2581 circumscribed \u2581 circle \u2581 is \u2581 : \u2581 \" , areacircumscribed ( $ a ) ; ? >"}
{"text":"Function to return the N - th tetranacci number","code":"< ? php function printTetraRec ( $ n ) {"}
{"text":"base cases","code":"if ( $ n == 0 ) return 0 ;"}
{"text":"base cases","code":"if ( $ n == 1 $ n == 2 ) return 1 ;"}
{"text":"base cases","code":"if ( $ n == 3 ) return 2 ; else return printTetraRec ( $ n - 1 ) + printTetraRec ( $ n - 2 ) + printTetraRec ( $ n - 3 ) + printTetraRec ( $ n - 4 ) ; }"}
{"text":"function to print the nth tetranacci number","code":"function printTetra ( $ n ) { echo printTetraRec ( $ n ) . \" \" ; }"}
{"text":"Driver code","code":"$ n = 10 ; printTetra ( $ n ) ; ? >"}
{"text":"A DP based PHP program to find maximum tasks . Returns the maximum among the 2 numbers","code":"< ? php function max1 ( $ x , $ y ) { return ( $ x > $ y ? $ x : $ y ) ; }"}
{"text":"Returns the maximum among the 2 numbers","code":"return ( $ x > $ y ? $ x : $ y ) ; }"}
{"text":"Returns maximum amount of task that can be done till day n","code":"function maxTasks ( $ high , $ low , $ n ) {"}
{"text":"An array task_dp that stores the maximum task done","code":"$ task_dp = array ( $ n + 1 ) ;"}
{"text":"If n = 0 , no solution exists","code":"$ task_dp [ 0 ] = 0 ;"}
{"text":"If n = 1 , high effort task on that day will be the solution","code":"$ task_dp [ 1 ] = $ high [ 0 ] ;"}
{"text":"Fill the entire array determining which task to choose on day i","code":"for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ task_dp [ $ i ] = max ( $ high [ $ i - 1 ] + $ task_dp [ $ i - 2 ] , $ low [ $ i - 1 ] + $ task_dp [ $ i - 1 ] ) ; return $ task_dp [ $ n ] ; }"}
{"text":"Driver code","code":"{ $ n = 5 ; $ high = array ( 3 , 6 , 8 , 7 , 6 ) ; $ low = array ( 1 , 5 , 4 , 5 , 3 ) ; echo ( maxTasks ( $ high , $ low , $ n ) ) ; }"}
{"text":"A O ( n ) time and O ( 1 ) extra space PHP solution to calculate the Permutation Coefficient","code":"< ? php function PermutationCoeff ( $ n , $ k ) { $ Fn = 1 ; $ Fk ;"}
{"text":"Compute n ! and ( n - k ) !","code":"for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ Fn *= $ i ; if ( $ i == $ n - $ k ) $ Fk = $ Fn ; } $ coeff = $ Fn \/ $ Fk ; return $ coeff ; }"}
{"text":"Driver Code","code":"$ n = 10 ; $ k = 2 ; echo \" Value \u2581 of \u2581 P ( \" , $ n , \" , \u2581 \" , $ k , \" ) STRNEWLINE is \u2581 \" , PermutationCoeff ( $ n , $ k ) ; ? >"}
{"text":"dfa tells the number associated with the present state","code":"< ? php $ dfa = 0 ;"}
{"text":"This function is for the starting state ( zeroth ) of DFA","code":"function start ( $ c ) { global $ dfa ;"}
{"text":"On receiving ' T ' or ' t ' goto first state ( 1 )","code":"if ( $ c == ' t ' $ c == ' T ' ) $ dfa = 1 ; }"}
{"text":"This function is for the first state of DFA","code":"function state1 ( $ c ) { global $ dfa ;"}
{"text":"On receiving ' T ' or ' t ' goto first state ( 1 )","code":"if ( $ c == ' t ' $ c == ' T ' ) $ dfa = 1 ;"}
{"text":"On receiving ' H ' or ' h ' goto second state ( 2 )","code":"else if ( $ c == ' h ' $ c == ' H ' ) $ dfa = 2 ;"}
{"text":"else goto starting state ( 0 )","code":"else $ dfa = 0 ; }"}
{"text":"This function is for the second state of DFA","code":"function state2 ( $ c ) { global $ dfa ;"}
{"text":"On receiving ' E ' or ' e ' goto third state ( 3 ) else goto starting state ( 0 )","code":"if ( $ c == ' e ' $ c == ' E ' ) $ dfa = 3 ; else $ dfa = 0 ; }"}
{"text":"This function is for the third state of DFA","code":"function state3 ( $ c ) { global $ dfa ;"}
{"text":"On receiving ' T ' or ' t ' goto first state ( 1 ) else goto starting state ( 0 )","code":"if ( $ c == ' t ' $ c == ' T ' ) $ dfa = 1 ; else $ dfa = 0 ; } function isAccepted ( $ str ) { global $ dfa ;"}
{"text":"store length of string","code":"$ len = strlen ( $ str ) ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { if ( $ dfa == 0 ) start ( $ str [ $ i ] ) ; else if ( $ dfa == 1 ) state1 ( $ str [ $ i ] ) ; else if ( $ dfa == 2 ) state2 ( $ str [ $ i ] ) ; else state3 ( $ str [ $ i ] ) ; } return ( $ dfa != 3 ) ; }"}
{"text":"Driver Code","code":"$ str = \" forTHEgeeks \" ; if ( isAccepted ( $ str ) == true ) echo \" ACCEPTED STRNEWLINE \" ; else echo \" NOT \u2581 ACCEPTED STRNEWLINE \" ; ? >"}
{"text":"Function that return true if pre is a prefix of str","code":"< ? php function startsWith ( $ str , $ pre ) { $ strLen = strlen ( $ str ) ; $ preLen = strlen ( $ pre ) ; $ i = 0 ; $ j = 0 ;"}
{"text":"While there are characters to match","code":"while ( $ i < $ strLen && $ j < $ preLen ) {"}
{"text":"If characters differ at any position","code":"if ( $ str [ $ i ] != $ pre [ $ j ] ) return false ; $ i ++ ; $ j ++ ; }"}
{"text":"str starts with pre","code":"return true ; }"}
{"text":"Function that return true if suff is a suffix of str","code":"function endsWith ( $ str , $ suff ) { $ i = strlen ( $ str ) - 0 ; $ j = strlen ( $ suff ) - 0 ;"}
{"text":"While there are characters to match","code":"while ( $ i >= 0 && $ j >= 0 ) {"}
{"text":"I$f characters differ at any position","code":"if ( $ str [ $ i ] != $ suff [ $ j ] ) return false ; $ i -- ; $ j -- ; }"}
{"text":"str ends with suff","code":"return true ; }"}
{"text":"Function that returns true if str = a + b or str = b + a","code":"function checkString ( $ str , $ a , $ b ) {"}
{"text":"str cannot be generated by concatenating a and b","code":"if ( strlen ( $ str ) != strlen ( $ a ) + strlen ( $ b ) ) return false ;"}
{"text":"If str starts with a i . e . a is a prefix of str","code":"if ( startsWith ( $ str , $ a ) ) {"}
{"text":"Check if the rest of the characters are equal to b i . e . b is a suffix of str","code":"if ( endsWith ( $ str , $ b ) ) return true ; }"}
{"text":"If str starts with b i . e . b is a prefix of str","code":"if ( startsWith ( $ str , $ b ) ) {"}
{"text":"Check if the rest of the characters are equal to a i . e . a is a suffix of str","code":"if ( endsWith ( $ str , $ a ) ) return true ; } return false ; }"}
{"text":"Driver code","code":"$ str = \" GeeksforGeeks \" ; $ a = \" Geeksfo \" ; $ b = \" rGeeks \" ; if ( checkString ( $ str , $ a , $ b ) ) echo \" Yes \" ; else echo \" No \" ; ? >"}
{"text":"Function to return the minimum number of operations required","code":"< ? php function minOperations ( $ str , $ n ) {"}
{"text":"To store the indices of the last uppercase and the first lowercase character","code":"$ i ; $ lastUpper = -1 ; $ firstLower = -1 ;"}
{"text":"Find the last uppercase character","code":"for ( $ i = $ n - 1 ; $ i >= 0 ; $ i -- ) { if ( ctype_upper ( $ str [ $ i ] ) ) { $ lastUpper = $ i ; break ; } }"}
{"text":"Find the first lowercase character","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( ctype_lower ( $ str [ $ i ] ) ) { $ firstLower = $ i ; break ; } }"}
{"text":"If all the characters are either uppercase or lowercase","code":"if ( $ lastUpper == -1 $ firstLower == -1 ) return 0 ;"}
{"text":"Count of uppercase characters that appear after the first lowercase character","code":"$ countUpper = 0 ; for ( $ i = $ firstLower ; $ i < $ n ; $ i ++ ) { if ( ctype_upper ( $ str [ $ i ] ) ) { $ countUpper ++ ; } }"}
{"text":"Count of lowercase characters that appear before the last uppercase character","code":"$ countLower = 0 ; for ( $ i = 0 ; $ i < $ lastUpper ; $ i ++ ) { if ( ctype_lower ( $ str [ $ i ] ) ) { $ countLower ++ ; } }"}
{"text":"Return the minimum operations required","code":"return min ( $ countLower , $ countUpper ) ; }"}
{"text":"Driver Code","code":"{ $ str = \" geEksFOrGEekS \" ; $ n = strlen ( $ str ) ; echo ( minOperations ( $ str , $ n ) ) ; } ? >"}
{"text":"Function to find the probability","code":"< ? php function rainDayProbability ( $ a , $ n ) { $ count = 0 ; $ m ;"}
{"text":"count 1","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] == 1 ) $ count ++ ; }"}
{"text":"find probability","code":"$ m = $ count \/ $ n ; return $ m ; }"}
{"text":"Driver Code","code":"$ a = array ( 1 , 0 , 1 , 0 , 1 , 1 , 1 , 1 ) ; $ n = count ( $ a ) ; echo rainDayProbability ( $ a , $ n ) ; ? >"}
{"text":"Function to calculate the following series","code":"< ? php function Series ( $ n ) { $ i ; $ sums = 0.0 ; $ ser ; for ( $ i = 1 ; $ i <= $ n ; ++ $ i ) { $ ser = 1 \/ pow ( $ i , $ i ) ; $ sums += $ ser ; } return $ sums ; }"}
{"text":"Driver Code","code":"$ n = 3 ; $ res = Series ( $ n ) ; echo $ res ; ? >"}
{"text":"Function to perform Ternary Search","code":"< ? php function ternarySearch ( $ l , $ r , $ key , $ ar ) { if ( $ r >= $ l ) {"}
{"text":"Find the mid1 and mid2","code":"$ mid1 = ( int ) ( $ l + ( $ r - $ l ) \/ 3 ) ; $ mid2 = ( int ) ( $ r - ( $ r - $ l ) \/ 3 ) ;"}
{"text":"Check if key is present at any mid","code":"if ( $ ar [ $ mid1 ] == $ key ) { return $ mid1 ; } if ( $ ar [ $ mid2 ] == $ key ) { return $ mid2 ; }"}
{"text":"Since key is not present at mid , check in which region it is present then repeat the Search operation in that region","code":"if ( $ key < $ ar [ $ mid1 ] ) {"}
{"text":"The key lies in between l and mid1","code":"return ternarySearch ( $ l , $ mid1 - 1 , $ key , $ ar ) ; } else if ( $ key > $ ar [ $ mid2 ] ) {"}
{"text":"The key lies in between mid2 and r","code":"return ternarySearch ( $ mid2 + 1 , $ r , $ key , $ ar ) ; } else {"}
{"text":"The key lies in between mid1 and mid2","code":"return ternarySearch ( $ mid1 + 1 , $ mid2 - 1 , $ key , $ ar ) ; } }"}
{"text":"Key not found","code":"return -1 ; }"}
{"text":"Get the array Sort the array if not sorted","code":"$ ar = array ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ) ;"}
{"text":"Starting index","code":"$ l = 0 ;"}
{"text":"length of array","code":"$ r = 9 ;"}
{"text":"Key to be searched in the array","code":"$ key = 5 ;"}
{"text":"Search the key using ternarySearch","code":"$ p = ternarySearch ( $ l , $ r , $ key , $ ar ) ;"}
{"text":"Print the result","code":"echo \" Index \u2581 of \u2581 \" , $ key , \" \u2581 is \u2581 \" , ( int ) $ p , \" STRNEWLINE \" ;"}
{"text":"Key to be searched in the array","code":"$ key = 50 ;"}
{"text":"Search the key using ternarySearch","code":"$ p = ternarySearch ( $ l , $ r , $ key , $ ar ) ;"}
{"text":"Print the result","code":"echo \" Index \u2581 of \u2581 \" , $ key , \" \u2581 is \u2581 \" , ( int ) $ p , \" STRNEWLINE \" ; ? >"}
{"text":"PHP implementation to print the character and its frequency in order of its occurrence","code":"< ? php $ SIZE = 26 ;"}
{"text":"function to print the character and its frequency in order of its occurrence","code":"function printCharWithFreq ( $ str ) { global $ SIZE ;"}
{"text":"size of the string ' str '","code":"$ n = strlen ( $ str ) ;"}
{"text":"' freq [ ] ' implemented as hash table","code":"$ freq = array_fill ( 0 , $ SIZE , NULL ) ;"}
{"text":"accumulate frequency of each character in ' str '","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ freq [ ord ( $ str [ $ i ] ) - ord ( ' a ' ) ] ++ ;"}
{"text":"traverse ' str ' from left to right","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) {"}
{"text":"if frequency of character str [ i ] is not equal to 0","code":"if ( $ freq [ ord ( $ str [ $ i ] ) - ord ( ' a ' ) ] != 0 ) {"}
{"text":"print the character along with its frequency","code":"echo $ str [ $ i ] . $ freq [ ord ( $ str [ $ i ] ) - ord ( ' a ' ) ] . \" \" ;"}
{"text":"update frequency of str [ i ] to 0 so that the same character is not printed again","code":"$ freq [ ord ( $ str [ $ i ] ) - ord ( ' a ' ) ] = 0 ; } } }"}
{"text":"Driver Code","code":"$ str = \" geeksforgeeks \" ; printCharWithFreq ( $ str ) ; ? >"}
{"text":"PHP program to find if a matrix is symmetric .","code":"< ? php function checkHV ( $ arr , $ N , $ M ) {"}
{"text":"Initializing as both horizontal and vertical symmetric .","code":"$ horizontal = true ; $ vertical = true ;"}
{"text":"Checking for Horizontal Symmetry . We compare first row with last row , second row with second last row and so on .","code":"for ( $ i = 0 , $ k = $ N - 1 ; $ i < $ N \/ 2 ; $ i ++ , $ k -- ) {"}
{"text":"Checking each cell of a column .","code":"for ( $ j = 0 ; $ j < $ M ; $ j ++ ) {"}
{"text":"check if every cell is identical","code":"if ( $ arr [ $ i ] [ $ j ] != $ arr [ $ k ] [ $ j ] ) { $ horizontal = false ; break ; } } }"}
{"text":"Checking for Vertical Symmetry . We compare first column with last column , second xolumn with second last column and so on .","code":"for ( $ i = 0 , $ k = $ M - 1 ; $ i < $ M \/ 2 ; $ i ++ , $ k -- ) {"}
{"text":"Checking each cell of a row .","code":"for ( $ j = 0 ; $ j < $ N ; $ j ++ ) {"}
{"text":"check if every cell is identical","code":"if ( $ arr [ $ i ] [ $ j ] != $ arr [ $ k ] [ $ j ] ) { $ horizontal = false ; break ; } } } if ( ! $ horizontal && ! $ vertical ) echo \" NO STRNEWLINE \" ; else if ( $ horizontal && ! $ vertical ) cout << \" HORIZONTAL STRNEWLINE \" ; else if ( $ vertical && ! $ horizontal ) echo \" VERTICAL STRNEWLINE \" ; else echo \" BOTH STRNEWLINE \" ; }"}
{"text":"Driver Code","code":"$ mat = array ( array ( 1 , 0 , 1 ) , array ( 0 , 0 , 0 ) , array ( 1 , 0 , 1 ) ) ; checkHV ( $ mat , 3 , 3 ) ; ? >"}
{"text":"PHP program for addition of two matrices","code":"< ? php $ N = 4 ;"}
{"text":"This function adds A [ ] [ ] and B [ ] [ ] , and stores the result in C [ ] [ ]","code":"function add ( & $ A , & $ B , & $ C ) { for ( $ i = 0 ; $ i < $ N ; $ i ++ ) for ( $ j = 0 ; $ j < $ N ; $ j ++ ) $ C [ $ i ] [ $ j ] = $ A [ $ i ] [ $ j ] + $ B [ $ i ] [ $ j ] ; }"}
{"text":"Driver code","code":"$ A = array ( array ( 1 , 1 , 1 , 1 ) , array ( 2 , 2 , 2 , 2 ) , array ( 3 , 3 , 3 , 3 ) , array ( 4 , 4 , 4 , 4 ) ) ; $ B = array ( array ( 1 , 1 , 1 , 1 ) , array ( 2 , 2 , 2 , 2 ) , array ( 3 , 3 , 3 , 3 ) , array ( 4 , 4 , 4 , 4 ) ) ; $ N = 4 ; add ( $ A , $ B , $ C ) ; echo \" Result \u2581 matrix \u2581 is \u2581 STRNEWLINE \" ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { for ( $ j = 0 ; $ j < $ N ; $ j ++ ) { echo $ C [ $ i ] [ $ j ] ; echo \" \u2581 \" ; } echo \" STRNEWLINE \" ; } ? >"}
{"text":"This function subtracts B [ ] [ ] from A [ ] [ ] , and stores the result in C [ ] [ ]","code":"< ? php function subtract ( & $ A , & $ B , & $ C ) { $ N = 4 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) for ( $ j = 0 ; $ j < $ N ; $ j ++ ) $ C [ $ i ] [ $ j ] = $ A [ $ i ] [ $ j ] - $ B [ $ i ] [ $ j ] ; }"}
{"text":"Driver code","code":"$ N = 4 ; $ A = array ( array ( 1 , 1 , 1 , 1 ) , array ( 2 , 2 , 2 , 2 ) , array ( 3 , 3 , 3 , 3 ) , array ( 4 , 4 , 4 , 4 ) ) ; $ B = array ( array ( 1 , 1 , 1 , 1 ) , array ( 2 , 2 , 2 , 2 ) , array ( 3 , 3 , 3 , 3 ) , array ( 4 , 4 , 4 , 4 ) ) ; subtract ( $ A , $ B , $ C ) ; echo \" Result \u2581 matrix \u2581 is \u2581 STRNEWLINE \" ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { for ( $ j = 0 ; $ j < $ N ; $ j ++ ) { echo $ C [ $ i ] [ $ j ] ; echo \" \u2581 \" ; } echo \" STRNEWLINE \" ; } ? >"}
{"text":"PHP program to check fixed point in an array using linear search","code":"< ? php function linearSearch ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] == $ i ) return $ i ; }"}
{"text":"If no fixed point present then return - 1","code":"return -1 ; }"}
{"text":"Driver Code","code":"$ arr = array ( -10 , -1 , 0 , 3 , 10 , 11 , 30 , 50 , 100 ) ; $ n = count ( $ arr ) ; echo \" Fixed \u2581 Point \u2581 is \u2581 \" . linearSearch ( $ arr , $ n ) ; ? >"}
{"text":"PHP program to check fixed po in an array using binary search","code":"< ? php function binarySearch ( $ arr , $ low , $ high ) { if ( $ high >= $ low ) {"}
{"text":"low + ( high - low ) \/ 2 ;","code":"$ mid = ( int ) ( ( $ low + $ high ) \/ 2 ) ; if ( $ mid == $ arr [ $ mid ] ) return $ mid ; if ( $ mid > $ arr [ $ mid ] ) return binarySearch ( $ arr , ( $ mid + 1 ) , $ high ) ; else return binarySearch ( $ arr , $ low , ( $ mid - 1 ) ) ; }"}
{"text":"Return - 1 if there is no Fixed Po","code":"return -1 ; }"}
{"text":"Driver Code","code":"$ arr = array ( -10 , -1 , 0 , 3 , 10 , 11 , 30 , 50 , 100 ) ; $ n = count ( $ arr ) ; echo \" Fixed \u2581 Point \u2581 is : \u2581 \" . binarySearch ( $ arr , 0 , $ n - 1 ) ; ? >"}
{"text":"PHP code to find maximum triplet sum","code":"< ? php function maxTripletSum ( $ arr , $ n ) {"}
{"text":"Initialize sum with INT_MIN","code":"$ sum = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) for ( $ k = $ j + 1 ; $ k < $ n ; $ k ++ ) if ( $ sum < $ arr [ $ i ] + $ arr [ $ j ] + $ arr [ $ k ] ) $ sum = $ arr [ $ i ] + $ arr [ $ j ] + $ arr [ $ k ] ; return $ sum ; }"}
{"text":"Driver Code","code":"$ arr = array ( 1 , 0 , 8 , 6 , 4 , 2 ) ; $ n = count ( $ arr ) ; echo maxTripletSum ( $ arr , $ n ) ; ? >"}
{"text":"This function assumes that there are at least three elements in arr [ ] .","code":"< ? php function maxTripletSum ( $ arr , $ n ) {"}
{"text":"sort the given array","code":"sort ( $ arr ) ;"}
{"text":"After sorting the array . Add last three element of the given array","code":"return $ arr [ $ n - 1 ] + $ arr [ $ n - 2 ] + $ arr [ $ n - 3 ] ; }"}
{"text":"Driver code","code":"$ arr = array ( 1 , 0 , 8 , 6 , 4 , 2 ) ; $ n = count ( $ arr ) ; echo maxTripletSum ( $ arr , $ n ) ; ? >"}
{"text":"This function assumes that there are at least three elements in arr [ ] .","code":"< ? php function maxTripletSum ( $ arr , $ n ) {"}
{"text":"Initialize Maximum , second maximum and third maximum element","code":"$ maxA = PHP_INT_MIN ; $ maxB = PHP_INT_MIN ; $ maxC = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) {"}
{"text":"Update Maximum , second maximum and third maximum element","code":"if ( $ arr [ $ i ] > $ maxA ) { $ maxC = $ maxB ; $ maxB = $ maxA ; $ maxA = $ arr [ $ i ] ; }"}
{"text":"Update second maximum and third maximum element","code":"else if ( $ arr [ $ i ] > $ maxB ) { $ maxC = $ maxB ; $ maxB = $ arr [ $ i ] ; }"}
{"text":"Update third maximum element","code":"else if ( $ arr [ $ i ] > $ maxC ) $ maxC = $ arr [ $ i ] ; } return ( $ maxA + $ maxB + $ maxC ) ; }"}
{"text":"Driven code","code":"$ arr = array ( 1 , 0 , 8 , 6 , 4 , 2 ) ; $ n = count ( $ arr ) ; echo maxTripletSum ( $ arr , $ n ) ; ? >"}
{"text":"PHP code for linearly search x in arr [ ] . If x is present then return its location , otherwise return - 1","code":"< ? php function search ( $ arr , $ x ) { $ n = sizeof ( $ arr ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] == $ x ) return $ i ; } return -1 ; }"}
{"text":"Driver Code","code":"$ arr = array ( 2 , 3 , 4 , 10 , 40 ) ; $ x = 10 ;"}
{"text":"Function call","code":"$ result = search ( $ arr , $ x ) ; if ( $ result == -1 ) echo \" Element \u2581 is \u2581 not \u2581 present \u2581 in \u2581 array \" ; else echo \" Element \u2581 is \u2581 present \u2581 at \u2581 index \u2581 \" , $ result ; ? >"}
{"text":"PHP Program for counting sort","code":"< ? php $ RANGE = 255 ;"}
{"text":"The main function that sort the given string arr [ ] in alphabatical order","code":"function countSort ( $ arr ) { global $ RANGE ;"}
{"text":"The output character array that will have sorted arr","code":"$ output = array ( strlen ( $ arr ) ) ; $ len = strlen ( $ arr ) ;"}
{"text":"Create a count array to store count of inidividul characters and initialize count array as 0","code":"$ count = array_fill ( 0 , $ RANGE + 1 , 0 ) ;"}
{"text":"Store count of each character","code":"for ( $ i = 0 ; $ i < $ len ; ++ $ i ) ++ $ count [ ord ( $ arr [ $ i ] ) ] ;"}
{"text":"Change count [ i ] so that count [ i ] now contains actual position of this character in output array","code":"for ( $ i = 1 ; $ i <= $ RANGE ; ++ $ i ) $ count [ $ i ] += $ count [ $ i - 1 ] ;"}
{"text":"Build the output character array To make it stable we are operating in reverse order .","code":"for ( $ i = $ len - 1 ; $ i >= 0 ; $ i -- ) { $ output [ $ count [ ord ( $ arr [ $ i ] ) ] - 1 ] = $ arr [ $ i ] ; -- $ count [ ord ( $ arr [ $ i ] ) ] ; }"}
{"text":"Copy the output array to arr , so that arr now contains sorted characters","code":"for ( $ i = 0 ; $ i < $ len ; ++ $ i ) $ arr [ $ i ] = $ output [ $ i ] ; return $ arr ; }"}
{"text":"Driver Code","code":"$ arr = \" geeksforgeeks \" ; $ arr = countSort ( $ arr ) ; echo \" Sorted \u2581 character \u2581 array \u2581 is \u2581 \" . $ arr ; ? >"}
{"text":"Returns value of Binomial Coefficient C ( n , k )","code":"< ? php function binomialCoeff ( $ n , $ k ) {"}
{"text":"Base Cases","code":"if ( $ k > $ n ) return 0 ; if ( $ k == 0 $ k == $ n ) return 1 ;"}
{"text":"Recur","code":"return binomialCoeff ( $ n - 1 , $ k - 1 ) + binomialCoeff ( $ n - 1 , $ k ) ; }"}
{"text":"Driver Code","code":"$ n = 5 ; $ k = 2 ; echo \" Value \u2581 of \u2581 C \" , \" ( \" , $ n , $ k , \" ) \u2581 is \u2581 \" , binomialCoeff ( $ n , $ k ) ; ? >"}
{"text":"PHP program for space optimized Dynamic Programming Solution of Binomial Coefficient","code":"< ? php function binomialCoeff ( $ n , $ k ) { $ C = array_fill ( 0 , $ k + 1 , 0 ) ;"}
{"text":"nC0 is 1","code":"$ C [ 0 ] = 1 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) {"}
{"text":"Compute next row of pascal triangle using the previous row","code":"for ( $ j = min ( $ i , $ k ) ; $ j > 0 ; $ j -- ) $ C [ $ j ] = $ C [ $ j ] + $ C [ $ j - 1 ] ; } return $ C [ $ k ] ; }"}
{"text":"Driver Code","code":"$ n = 5 ; $ k = 2 ; echo \" Value \u2581 of \u2581 C [ $ n , \u2581 $ k ] \u2581 is \u2581 \" . binomialCoeff ( $ n , $ k ) ; ? >"}
{"text":"Returns true if there is a subset of set with sun equal to given sum","code":"< ? php function isSubsetSum ( $ set , $ n , $ sum ) {"}
{"text":"Base Cases","code":"if ( $ sum == 0 ) return true ; if ( $ n == 0 ) return false ;"}
{"text":"If last element is greater than sum , then ignore it","code":"if ( $ set [ $ n - 1 ] > $ sum ) return isSubsetSum ( $ set , $ n - 1 , $ sum ) ;"}
{"text":"else , check if sum can be obtained by any of the following ( a ) including the last element ( b ) excluding the last element","code":"return isSubsetSum ( $ set , $ n - 1 , $ sum ) || isSubsetSum ( $ set , $ n - 1 , $ sum - $ set [ $ n - 1 ] ) ; }"}
{"text":"Driver Code","code":"$ set = array ( 3 , 34 , 4 , 12 , 5 , 2 ) ; $ sum = 9 ; $ n = 6 ; if ( isSubsetSum ( $ set , $ n , $ sum ) == true ) echo \" Found \u2581 a \u2581 subset \u2581 with \u2581 given \u2581 sum \" ; else echo \" No \u2581 subset \u2581 with \u2581 given \u2581 sum \" ; ? >"}
{"text":"Returns true if there is a subset of set [ ] with sun equal to given sum","code":"< ? php function isSubsetSum ( $ set , $ n , $ sum ) {"}
{"text":"The value of subset [ i ] [ j ] will be true if there is a subset of set [ 0. . j - 1 ] with sum equal to i","code":"$ subset = array ( array ( ) ) ;"}
{"text":"If sum is 0 , then answer is true","code":"for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) $ subset [ $ i ] [ 0 ] = true ;"}
{"text":"If sum is not 0 and set is empty , then answer is false","code":"for ( $ i = 1 ; $ i <= $ sum ; $ i ++ ) $ subset [ 0 ] [ $ i ] = false ;"}
{"text":"Fill the subset table in botton up manner","code":"for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { for ( $ j = 1 ; $ j <= $ sum ; $ j ++ ) { if ( $ j < $ set [ $ i - 1 ] ) $ subset [ $ i ] [ $ j ] = $ subset [ $ i - 1 ] [ $ j ] ; if ( $ j >= $ set [ $ i - 1 ] ) $ subset [ $ i ] [ $ j ] = $ subset [ $ i - 1 ] [ $ j ] || $ subset [ $ i - 1 ] [ $ j - $ set [ $ i - 1 ] ] ; } }"}
{"text":"print table","code":"for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= sum ; j ++ ) printf ( \" % 4d \" , subset [ i ] [ j ] ) ; printf ( \" n \" ) ; } return $ subset [ $ n ] [ $ sum ] ; }"}
{"text":"Driver code","code":"$ set = array ( 3 , 34 , 4 , 12 , 5 , 2 ) ; $ sum = 9 ; $ n = count ( $ set ) ; if ( isSubsetSum ( $ set , $ n , $ sum ) == true ) echo \" Found \u2581 a \u2581 subset \u2581 with \u2581 given \u2581 sum \" ; else echo \" No \u2581 subset \u2581 with \u2581 given \u2581 sum \" ; ? >"}
{"text":"A recursive function that returns the optimal length string for N keystrokes","code":"< ? php function findoptimal ( $ N ) {"}
{"text":"The optimal string length is N when N is smaller than 7","code":"if ( $ N <= 6 ) return $ N ;"}
{"text":"Initialize result","code":"$ max = 0 ;"}
{"text":"TRY ALL POSSIBLE BREAK - POINTS For any keystroke N , we need to loop from N - 3 keystrokes back to 1 keystroke to find a breakpoint ' b ' after which we will have Ctrl - A , Ctrl - C and then only Ctrl - V all the way .","code":"$ b ; for ( $ b = $ N - 3 ; $ b >= 1 ; $ b -= 1 ) {"}
{"text":"If the breakpoint is s at b 'th keystroke then  the optimal string would have length  (n-b-1)*screen[b-1];","code":"$ curr = ( $ N - $ b - 1 ) * findoptimal ( $ b ) ; if ( $ curr > $ max ) $ max = $ curr ; } return $ max ; }"}
{"text":"Driver code","code":"$ N ;"}
{"text":"for the rest of the array we will rely on the previous entries to compute new ones","code":"for ( $ N = 1 ; $ N <= 20 ; $ N += 1 ) echo ( \" Maximum \u2581 Number \u2581 of \u2581 A ' s \u2581 with \" . $ N . \" keystrokes \u2581 is \u2581 \" . findoptimal ( $ N ) . \" STRNEWLINE \" ) ; ? >"}
{"text":"Function to calculate x raised to the power y","code":"< ? php function power ( $ x , $ y ) { if ( $ y == 0 ) return 1 ; else if ( $ y % 2 == 0 ) return power ( $ x , ( int ) $ y \/ 2 ) * power ( $ x , ( int ) $ y \/ 2 ) ; else return $ x * power ( $ x , ( int ) $ y \/ 2 ) * power ( $ x , ( int ) $ y \/ 2 ) ; }"}
{"text":"Driver Code","code":"$ x = 2 ; $ y = 3 ; echo power ( $ x , $ y ) ; ? >"}
{"text":"Extended version of power function that can work for float x and negative y","code":"< ? php function power ( $ x , $ y ) { $ temp ; if ( $ y == 0 ) return 1 ; $ temp = power ( $ x , $ y \/ 2 ) ; if ( $ y % 2 == 0 ) return $ temp * $ temp ; else { if ( $ y > 0 ) return $ x * $ temp * $ temp ; else return ( $ temp * $ temp ) \/ $ x ; } }"}
{"text":"Driver Code","code":"$ x = 2 ; $ y = -3 ; echo power ( $ x , $ y ) ; ? >"}
{"text":"Returns the square root of n . Note that the function","code":"< ? php function squareRoot ( $ n ) {"}
{"text":"We are using n itself as initial approximation This can definitely be improved","code":"$ x = $ n ; $ y = 1 ;"}
{"text":"e decides the accuracy level","code":"$ e = 0.000001 ; while ( $ x - $ y > $ e ) { $ x = ( $ x + $ y ) \/ 2 ; $ y = $ n \/ $ x ; } return $ x ; }"}
{"text":"Driver Code","code":"{ $ n = 50 ; echo \" Square \u2581 root \u2581 of \u2581 $ n \u2581 is \u2581 \" , squareRoot ( $ n ) ; } ? >"}
{"text":"Returns the new average after including x","code":"< ? php function getAvg ( $ prev_avg , $ x , $ n ) { return ( $ prev_avg * $ n + $ x ) \/ ( $ n + 1 ) ; }"}
{"text":"Prints average of a stream of numbers","code":"function streamAvg ( $ arr , $ n ) { $ avg = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ avg = getAvg ( $ avg , $ arr [ $ i ] , $ i ) ; echo \" Average \u2581 of \u2581 \" , $ i + 1 , \" numbers \u2581 is \u2581 \" , $ avg , \" STRNEWLINE \" ; } return ; }"}
{"text":"Driver Code","code":"$ arr = array ( 10 , 20 , 30 , 40 , 50 , 60 ) ; $ n = sizeof ( $ arr ) ; streamAvg ( $ arr , $ n ) ; ? >"}
{"text":"Returns the new average after including x","code":"< ? php function getAvg ( $ x ) { static $ sum ; static $ n ; $ sum += $ x ; return ( ( ( float ) $ sum ) \/ ++ $ n ) ; }"}
{"text":"Prints average of a stream of numbers","code":"function streamAvg ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ avg = getAvg ( $ arr [ $ i ] ) ; echo \" Average \u2581 of \u2581 \" . ( $ i + 1 ) . \" \u2581 numbers \u2581 is \u2581 \" . $ avg . \" \u2581 STRNEWLINE \" ; } return ; }"}
{"text":"Driver Code","code":"$ arr = array ( 10 , 20 , 30 , 40 , 50 , 60 ) ; $ n = sizeof ( $ arr ) \/ sizeof ( $ arr [ 0 ] ) ; streamAvg ( $ arr , $ n ) ; ? >"}
{"text":"Returns value of Binomial Coefficient C ( n , k )","code":"< ? php function binomialCoeff ( $ n , $ k ) { $ res = 1 ;"}
{"text":"Since C ( n , k ) = C ( n , n - k )","code":"if ( $ k > $ n - $ k ) $ k = $ n - $ k ;"}
{"text":"Calculate value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] \/ [ k * ( k - 1 ) * -- -- * 1 ]","code":"for ( $ i = 0 ; $ i < $ k ; ++ $ i ) { $ res *= ( $ n - $ i ) ; $ res \/= ( $ i + 1 ) ; } return $ res ; }"}
{"text":"Driver Code","code":"$ n = 8 ; $ k = 2 ; echo \" \u2581 Value \u2581 of \u2581 C \u2581 ( $ n , \u2581 $ k ) \u2581 is \u2581 \" , binomialCoeff ( $ n , $ k ) ; ? >"}
{"text":"function to print all prime factors of a given number n","code":"< ? php function primeFactors ( $ n ) {"}
{"text":"Print the number of 2 s that divide n","code":"while ( $ n % 2 == 0 ) { echo 2 , \" \u2581 \" ; $ n = $ n \/ 2 ; }"}
{"text":"n must be odd at this point . So we can skip one element ( Note i = i + 2 )","code":"for ( $ i = 3 ; $ i <= sqrt ( $ n ) ; $ i = $ i + 2 ) {"}
{"text":"While i divides n , print i and divide n","code":"while ( $ n % $ i == 0 ) { echo $ i , \" \" ; $ n = $ n \/ $ i ; } }"}
{"text":"This condition is to handle the case when n is a prime number greater than 2","code":"if ( $ n > 2 ) echo $ n , \" \u2581 \" ; }"}
{"text":"Driver Code","code":"$ n = 315 ; primeFactors ( $ n ) ; ? >"}
{"text":"The main function that prints all combinations of size r in arr [ ] of size n . This function mainly uses combinationUtil ( )","code":"< ? php function printCombination ( $ arr , $ n , $ r ) {"}
{"text":"A temporary array to store all combination one by one","code":"$ data = array ( ) ;"}
{"text":"Print all combination using temprary array ' data [ ] '","code":"combinationUtil ( $ arr , $ data , 0 , $ n - 1 , 0 , $ r ) ; }"}
{"text":"arr [ ] -- -> Input Array data [ ] -- -> Temporary array to store current combination start & end -- -> Staring and Ending indexes in arr [ ] index -- -> Current index in data [ ] r -- -> Size of a combination to be printed","code":"function combinationUtil ( $ arr , $ data , $ start , $ end , $ index , $ r ) {"}
{"text":"Current combination is ready to be printed , print it","code":"if ( $ index == $ r ) { for ( $ j = 0 ; $ j < $ r ; $ j ++ ) echo $ data [ $ j ] ; echo \" STRNEWLINE \" ; return ; }"}
{"text":"replace index with all possible elements . The condition \" end - i + 1 \u2581 > = \u2581 \u2581 r - index \" makes sure that including one element at index will make a combination with remaining elements at remaining positions","code":"for ( $ i = $ start ; $ i <= $ end && $ end - $ i + 1 >= $ r - $ index ; $ i ++ ) { $ data [ $ index ] = $ arr [ $ i ] ; combinationUtil ( $ arr , $ data , $ i + 1 , $ end , $ index + 1 , $ r ) ; } }"}
{"text":"Driver Code","code":"$ arr = array ( 1 , 2 , 3 , 4 , 5 ) ; $ r = 3 ; $ n = sizeof ( $ arr ) ; printCombination ( $ arr , $ n , $ r ) ; ? >"}
{"text":"The main function that prints all combinations of size r in arr [ ] of size n . This function mainly uses combinationUtil ( )","code":"< ? php function printCombination ( $ arr , $ n , $ r ) {"}
{"text":"A temporary array to store all combination one by one","code":"$ data = Array ( ) ;"}
{"text":"Print all combination using temprary array ' data [ ] '","code":"combinationUtil ( $ arr , $ n , $ r , 0 , $ data , 0 ) ; }"}
{"text":"arr [ ] -- -> Input Array n -- -> Size of input array r -- -> Size of a combination to be printed index -- -> Current index in data [ ] data [ ] -- -> Temporary array to store current combination i -- -> index of current element in arr [ ]","code":"function combinationUtil ( $ arr , $ n , $ r , $ index , $ data , $ i ) {"}
{"text":"Current cobination is ready , print it","code":"if ( $ index == $ r ) { for ( $ j = 0 ; $ j < $ r ; $ j ++ ) echo $ data [ $ j ] , \" \u2581 \" ; echo \" STRNEWLINE \" ; return ; }"}
{"text":"When no more elements are there to put in data [ ]","code":"if ( $ i >= $ n ) return ;"}
{"text":"current is included , put next at next location","code":"$ data [ $ index ] = $ arr [ $ i ] ; combinationUtil ( $ arr , $ n , $ r , $ index + 1 , $ data , $ i + 1 ) ;"}
{"text":"current is excluded , replace it with next ( Note that i + 1 is passed , but index is not changed )","code":"combinationUtil ( $ arr , $ n , $ r , $ index , $ data , $ i + 1 ) ; }"}
{"text":"Driver Code","code":"$ arr = array ( 1 , 2 , 3 , 4 , 5 ) ; $ r = 3 ; $ n = sizeof ( $ arr ) ; printCombination ( $ arr , $ n , $ r ) ; ? >"}
{"text":"Returns count of all possible groups that can be formed from elements of a [ ] .","code":"< ? php function findgroups ( $ arr , $ n ) {"}
{"text":"Create an array C [ 3 ] to store counts of elements with remainder 0 , 1 and 2. c [ i ] would store count of elements with remainder i","code":"$ c = array ( 0 , 0 , 0 ) ;"}
{"text":"To store the result","code":"$ res = 0 ;"}
{"text":"Count elements with remainder 0 , 1 and 2","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ c [ $ arr [ $ i ] % 3 ] += 1 ;"}
{"text":"Case 3. a : Count groups of size 2 from 0 remainder elements","code":"$ res += ( ( $ c [ 0 ] * ( $ c [ 0 ] - 1 ) ) >> 1 ) ;"}
{"text":"Case 3. b : Count groups of size 2 with one element with 1 remainder and other with 2 remainder","code":"$ res += $ c [ 1 ] * $ c [ 2 ] ;"}
{"text":"Case 4. a : Count groups of size 3 with all 0 remainder elements","code":"$ res += ( $ c [ 0 ] * ( $ c [ 0 ] - 1 ) * ( $ c [ 0 ] - 2 ) ) \/ 6 ;"}
{"text":"Case 4. b : Count groups of size 3 with all 1 remainder elements","code":"$ res += ( $ c [ 1 ] * ( $ c [ 1 ] - 1 ) * ( $ c [ 1 ] - 2 ) ) \/ 6 ;"}
{"text":"Case 4. c : Count groups of size 3 with all 2 remainder elements","code":"$ res += ( ( $ c [ 2 ] * ( $ c [ 2 ] - 1 ) * ( $ c [ 2 ] - 2 ) ) \/ 6 ) ;"}
{"text":"Case 4. c : Count groups of size 3 with different remainders","code":"$ res += $ c [ 0 ] * $ c [ 1 ] * $ c [ 2 ] ;"}
{"text":"Return total count stored in res","code":"return $ res ; }"}
{"text":"Driver Code","code":"$ arr = array ( 3 , 6 , 7 , 2 , 9 ) ; $ n = count ( $ arr ) ; echo \" Required \u2581 number \u2581 of \u2581 groups \u2581 are \u2581 \" . ( int ) ( findgroups ( $ arr , $ n ) ) ; ? >"}
{"text":"PHP program to find smallest power of 2 greater than or equal to n","code":"< ? php function nextPowerOf2 ( $ n ) { $ count = 0 ;"}
{"text":"First n in the below condition is for the case where n is 0","code":"if ( $ n && ! ( $ n & ( $ n - 1 ) ) ) return $ n ; while ( $ n != 0 ) { $ n >>= 1 ; $ count += 1 ; } return 1 << $ count ; }"}
{"text":"Driver Code","code":"$ n = 0 ; echo ( nextPowerOf2 ( $ n ) ) ; ? >"}
{"text":"Program to find smallest power of 2 greater than or equal to n","code":"< ? php function nextPowerOf2 ( $ n ) { $ count = 0 ; if ( $ n && ! ( $ n & ( $ n - 1 ) ) ) return $ n ; while ( $ n != 0 ) { $ n >>= 1 ; $ count += 1 ; } return 1 << $ count ; }"}
{"text":"Driver Code","code":"$ n = 5 ; echo ( nextPowerOf2 ( $ n ) ) ; ? >"}
{"text":"Finds next power of two for n . If n itself is a power of two then returns n","code":"< ? php function nextPowerOf2 ( $ n ) { $ n -- ; $ n |= $ n >> 1 ; $ n |= $ n >> 2 ; $ n |= $ n >> 4 ; $ n |= $ n >> 8 ; $ n |= $ n >> 16 ; $ n ++ ; return $ n ; }"}
{"text":"Driver Code","code":"$ n = 5 ; echo nextPowerOf2 ( $ n ) ; ? >"}
{"text":"Function to segregate 0 s and 1 s","code":"< ? php function segregate0and1 ( & $ arr , $ n ) {"}
{"text":"Counts the no of zeros in arr","code":"$ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] == 0 ) $ count ++ ; }"}
{"text":"Loop fills the arr with 0 until count","code":"for ( $ i = 0 ; $ i < $ count ; $ i ++ ) $ arr [ $ i ] = 0 ;"}
{"text":"Loop fills remaining arr space with 1","code":"for ( $ i = $ count ; $ i < $ n ; $ i ++ ) $ arr [ $ i ] = 1 ; }"}
{"text":"Function to print segregated array","code":"function toprint ( & $ arr , $ n ) { echo ( \" Array \u2581 after \u2581 segregation \u2581 is \u2581 \" ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo ( $ arr [ $ i ] . \" \u2581 \" ) ; }"}
{"text":"Driver Code","code":"$ arr = array ( 0 , 1 , 0 , 1 , 1 , 1 ) ; $ n = sizeof ( $ arr ) ; segregate0and1 ( $ arr , $ n ) ; toprint ( $ arr , $ n ) ; ? >"}
{"text":"Function to put all 0 s on left and all 1 s on right","code":"< ? php function segregate0and1 ( & $ arr , $ size ) {"}
{"text":"Initialize left and right indexes","code":"$ left = 0 ; $ right = $ size - 1 ; while ( $ left < $ right ) {"}
{"text":"Increment left index while we see 0 at left","code":"while ( $ arr [ $ left ] == 0 && $ left < $ right ) $ left ++ ;"}
{"text":"Decrement right index while we see 1 at right","code":"while ( $ arr [ $ right ] == 1 && $ left < $ right ) $ right -- ;"}
{"text":"If left is smaller than right then there is a 1 at left and a 0 at right . Exchange arr [ left ] and arr [ right ]","code":"if ( $ left < $ right ) { $ arr [ $ left ] = 0 ; $ arr [ $ right ] = 1 ; $ left ++ ; $ right -- ; } } }"}
{"text":"Driver code","code":"$ arr = array ( 0 , 1 , 0 , 1 , 1 , 1 ) ; $ arr_size = sizeof ( $ arr ) ; segregate0and1 ( $ arr , $ arr_size ) ; printf ( \" Array \u2581 after \u2581 segregation \u2581 is \u2581 \" ) ; for ( $ i = 0 ; $ i < 6 ; $ i ++ ) echo ( $ arr [ $ i ] . \" \u2581 \" ) ; ? >"}
{"text":"Function to put all 0 s on left and all 1 s on right","code":"< ? php function segregate0and1 ( & $ arr , $ size ) { $ type0 = 0 ; $ type1 = $ size - 1 ; while ( $ type0 < $ type1 ) { if ( $ arr [ $ type0 ] == 1 ) { $ temp = $ arr [ $ type0 ] ; $ arr [ $ type0 ] = $ arr [ $ type1 ] ; $ arr [ $ type1 ] = $ temp ; $ type1 -- ; } else $ type0 ++ ; } }"}
{"text":"Driver Code","code":"$ arr = array ( 0 , 1 , 0 , 1 , 1 , 1 ) ; $ arr_size = sizeof ( $ arr ) ; segregate0and1 ( $ arr , $ arr_size ) ; echo ( \" Array \u2581 after \u2581 segregation \u2581 is \u2581 \" ) ; for ( $ i = 0 ; $ i < $ arr_size ; $ i ++ ) echo ( $ arr [ $ i ] . \" \u2581 \" ) ; ? >"}
{"text":"For a given array arr [ ] , returns the maximum j a i such that arr [ j ] > arr [ i ]","code":"< ? php function maxIndexDiff ( $ arr , $ n ) { $ maxDiff = -1 ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { for ( $ j = $ n - 1 ; $ j > $ i ; -- $ j ) { if ( $ arr [ $ j ] > $ arr [ $ i ] && $ maxDiff < ( $ j - $ i ) ) $ maxDiff = $ j - $ i ; } } return $ maxDiff ; }"}
{"text":"Driver Code","code":"$ arr = array ( 9 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 18 , 0 ) ; $ n = count ( $ arr ) ; $ maxDiff = maxIndexDiff ( $ arr , $ n ) ; echo $ maxDiff ; ? >"}
{"text":"Function to find k - th missing element","code":"< ? php function missingK ( & $ a , $ k , $ n ) { $ difference = 0 ; $ ans = 0 ; $ count = $ k ; $ flag = 0 ;"}
{"text":"interating over the array","code":"for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { $ difference = 0 ;"}
{"text":"check if i - th and ( i + 1 ) - th element are not consecutive","code":"if ( ( $ a [ $ i ] + 1 ) != $ a [ $ i + 1 ] ) {"}
{"text":"save their difference","code":"$ difference += ( $ a [ $ i + 1 ] - $ a [ $ i ] ) - 1 ;"}
{"text":"check for difference and given k","code":"if ( $ difference >= $ count ) { $ ans = $ a [ $ i ] + $ count ; $ flag = 1 ; break ; } else $ count -= $ difference ; } }"}
{"text":"if found","code":"if ( $ flag ) return $ ans ; else return -1 ; }"}
{"text":"Input array","code":"$ a = array ( 1 , 5 , 11 , 19 ) ;"}
{"text":"k - th missing element to be found in the array","code":"$ k = 11 ; $ n = count ( $ a ) ;"}
{"text":"calling function to find missing element","code":"$ missing = missingK ( $ a , $ k , $ n ) ; echo $ missing ; ? >"}
{"text":"Returns count of rotations to get the same string back .","code":"< ? php function findRotations ( $ str ) {"}
{"text":"tmp is the concatenated string .","code":"$ tmp = ( $ str + $ str ) ; $ n = strlen ( $ str ) ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) {"}
{"text":"substring from i index of original string size .","code":"$ substring = $ tmp . substr ( $ i , strlen ( $ str ) ) ;"}
{"text":"if substring matches with original string then we will come out of the loop .","code":"if ( $ str == $ substring ) return $ i ; } return $ n ; }"}
{"text":"Driver code","code":"$ str = \" abc \" ; echo findRotations ( $ str ) , \" STRNEWLINE \" ; ? >"}
{"text":"Function to find the sum of minimum of all subarrays","code":"< ? php function findKth ( $ arr , $ n , $ k ) { $ missing = array ( ) ; $ count = 0 ;"}
{"text":"Insert all the elements in a set","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) array_push ( $ missing , $ arr [ $ i ] ) ; $ missing = array_unique ( $ missing ) ;"}
{"text":"Find the maximum and minimum element","code":"$ maxm = max ( $ arr ) ; $ minm = min ( $ arr ) ;"}
{"text":"Traverse from the minimum to maximum element","code":"for ( $ i = $ minm + 1 ; $ i < $ maxm ; $ i ++ ) {"}
{"text":"Check if \" i \" is missing","code":"if ( ! in_array ( $ i , $ missing , false ) ) $ count += 1 ;"}
{"text":"Check if it is kth missing","code":"if ( $ count == $ k ) return $ i ; }"}
{"text":"If no kth element is missing","code":"return -1 ; }"}
{"text":"Driver code","code":"$ arr = array ( 2 , 10 , 9 , 4 ) ; $ n = sizeof ( $ arr ) ; $ k = 5 ; echo findKth ( $ arr , $ n , $ k ) ; ? >"}
{"text":"Function to find the number of Bit Strings of length N with K adjacent set bits","code":"< ? php function waysToKAdjacentSetBits ( $ n , $ k , $ currentIndex , $ adjacentSetBits , $ lastBit ) {"}
{"text":"Base Case when we form bit string of length n","code":"if ( $ currentIndex == $ n ) {"}
{"text":"if f ( bit string ) = k , count this way","code":"if ( $ adjacentSetBits == $ k ) return 1 ; return 0 ; } $ noOfWays = 0 ;"}
{"text":"Check if the last bit was set , if it was set then call for next index by incrementing the adjacent bit count else just call the next index with same value of adjacent bit count and either set the bit at current index or let it remain unset","code":"if ( $ lastBit == 1 ) {"}
{"text":"set the bit at currentIndex","code":"$ noOfWays += waysToKAdjacentSetBits ( $ n , $ k , $ currentIndex + 1 , $ adjacentSetBits + 1 , 1 ) ;"}
{"text":"unset the bit at currentIndex","code":"$ noOfWays += waysToKAdjacentSetBits ( $ n , $ k , $ currentIndex + 1 , $ adjacentSetBits , 0 ) ; } else if ( ! $ lastBit ) { $ noOfWays += waysToKAdjacentSetBits ( $ n , $ k , $ currentIndex + 1 , $ adjacentSetBits , 1 ) ; $ noOfWays += waysToKAdjacentSetBits ( $ n , $ k , $ currentIndex + 1 , $ adjacentSetBits , 0 ) ; } return $ noOfWays ; }"}
{"text":"Driver Code","code":"$ n = 5 ; $ k = 2 ;"}
{"text":"total ways = ( ways by placing 1 st bit as 1 + ways by placing 1 st bit as 0 )","code":"$ totalWays = waysToKAdjacentSetBits ( $ n , $ k , 1 , 0 , 1 ) + waysToKAdjacentSetBits ( $ n , $ k , 1 , 0 , 0 ) ; echo \" Number \u2581 of \u2581 ways \u2581 = \u2581 \" , $ totalWays , \" STRNEWLINE \" ; ? >"}
{"text":"Returns count of ways to reach n - th stair using 1 or 2 or 3 steps .","code":"< ? php function findStep ( $ n ) { if ( $ n == 1 $ n == 0 ) return 1 ; else if ( $ n == 2 ) return 2 ; else return findStep ( $ n - 3 ) + findStep ( $ n - 2 ) + findStep ( $ n - 1 ) ; }"}
{"text":"Driver code","code":"$ n = 4 ; echo findStep ( $ n ) ; ? >"}
{"text":"A utility function that returns true if there is a subset of arr [ ] with sun equal to given sum","code":"< ? php function isSubsetSum ( $ arr , $ n , $ sum ) {"}
{"text":"Base Cases","code":"if ( $ sum == 0 ) return true ; if ( $ n == 0 && $ sum != 0 ) return false ;"}
{"text":"If last element is greater than sum , then ignore it","code":"if ( $ arr [ $ n - 1 ] > $ sum ) return isSubsetSum ( $ arr , $ n - 1 , $ sum ) ;"}
{"text":"else , check if sum can be obtained by any of the following ( a ) including the last element ( b ) excluding the last element","code":"return isSubsetSum ( $ arr , $ n - 1 , $ sum ) || isSubsetSum ( $ arr , $ n - 1 , $ sum - $ arr [ $ n - 1 ] ) ; }"}
{"text":"Returns true if arr [ ] can be partitioned in two subsets of equal sum , otherwise false","code":"function findPartiion ( $ arr , $ n ) {"}
{"text":"Calculate sum of the elements in array","code":"$ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += $ arr [ $ i ] ;"}
{"text":"If sum is odd , there cannot be two subsets with equal sum","code":"if ( $ sum % 2 != 0 ) return false ;"}
{"text":"Find if there is subset with sum equal to half of total sum","code":"return isSubsetSum ( $ arr , $ n , $ sum \/ 2 ) ; }"}
{"text":"Driver Code","code":"$ arr = array ( 3 , 1 , 5 , 9 , 12 ) ; $ n = count ( $ arr ) ;"}
{"text":"Function call","code":"if ( findPartiion ( $ arr , $ n ) == true ) echo \" Can \u2581 be \u2581 divided \u2581 into \u2581 two \u2581 subsets \u2581 of \u2581 equal \u2581 sum \" ; else echo \" Can \u2581 not \u2581 be \u2581 divided \u2581 into \u2581 two \u2581 subsets \u2581 of \u2581 equal \u2581 sum \" ; ? >"}
{"text":"PHP program to find the first character that is repeated","code":"< ? php function findRepeatFirstN2 ( $ s ) {"}
{"text":"this is O ( N ^ 2 ) method","code":"$ p = -1 ; for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) { for ( $ j = ( $ i + 1 ) ; $ j < strlen ( $ s ) ; $ j ++ ) { if ( $ s [ $ i ] == $ s [ $ j ] ) { $ p = $ i ; break ; } } if ( $ p != -1 ) break ; } return $ p ; }"}
{"text":"Driver code","code":"$ str = \" geeksforgeeks \" ; $ pos = findRepeatFirstN2 ( $ str ) ; if ( $ pos == -1 ) echo ( \" Not \u2581 found \" ) ; else echo ( $ str [ $ pos ] ) ; ? >"}
{"text":"Function to calculate number of strings","code":"< ? php function possibleStrings ( $ n , $ r , $ b , $ g ) {"}
{"text":"Store factorial of numbers up to n for further computation","code":"$ fact [ 0 ] = 1 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ fact [ $ i ] = $ fact [ $ i - 1 ] * $ i ;"}
{"text":"Find the remaining values to be added","code":"$ left = $ n - ( $ r + $ g + $ b ) ; $ sum = 0 ;"}
{"text":"Make all possible combinations of R , B and G for the remaining value","code":"for ( $ i = 0 ; $ i <= $ left ; $ i ++ ) { for ( $ j = 0 ; $ j <= $ left - $ i ; $ j ++ ) { $ k = $ left - ( $ i + $ j ) ;"}
{"text":"Compute permutation of each combination one by one and add them .","code":"$ sum = $ sum + $ fact [ $ n ] \/ ( $ fact [ $ i + $ r ] * $ fact [ $ j + $ b ] * $ fact [ $ k + $ g ] ) ; } }"}
{"text":"Return total no . of strings \/ permutation","code":"return $ sum ; }"}
{"text":"Driver Code","code":"$ n = 4 ; $ r = 2 ; $ b = 0 ; $ g = 1 ; echo possibleStrings ( $ n , $ r , $ b , $ g ) ; ? >"}
{"text":"function to calculate minimum numbers of characters to be removed to make two strings anagram","code":"< ? php function remAnagram ( $ str1 , $ str2 ) {"}
{"text":"make hash array for both string and calculate frequency of each character","code":"$ count1 = array ( 26 ) ; $ count2 = array ( 26 ) ;"}
{"text":"count frequency of each character in first string","code":"for ( $ i = 0 ; $ i < strlen ( $ str1 ) ; $ i ++ ) $ count1 [ $ str1 [ $ i ] - ' a ' ] ++ ;"}
{"text":"count frequency of each character in second string","code":"for ( $ i = 0 ; $ i < strlen ( $ str2 ) ; $ i ++ ) $ count2 [ $ str2 [ $ i ] - ' a ' ] ++ ;"}
{"text":"traverse count arrays to find number of characters to be removed","code":"$ result = 0 ; for ( $ i = 0 ; $ i < 26 ; $ i ++ ) $ result += abs ( $ count1 [ $ i ] - $ count2 [ $ i ] ) ; return $ result ; }"}
{"text":"Driver Code","code":"{ $ str1 = \" bcadeh \" ; $ str2 = \" hea \" ; echo ( remAnagram ( $ str1 , $ str2 ) ) ; }"}
{"text":"Function to print path of all the nodes nth node represent as given node kth node represents as left and right node","code":"< ? php function printPath ( $ res , $ nThNode , $ kThNode ) {"}
{"text":"base condition if kth node value is greater then nth node then its means kth node is not valid so we not store it into the res simply we just return","code":"if ( $ kThNode > $ nThNode ) return ;"}
{"text":"Storing node into res","code":"array_push ( $ res , $ kThNode ) ;"}
{"text":"Print the path from root to node","code":"for ( $ i = 0 ; $ i < count ( $ res ) ; $ i ++ ) echo $ res [ $ i ] . \" \u2581 \" ; echo \" STRNEWLINE \" ;"}
{"text":"store left path of a tree So for left we will go node ( kThNode * 2 )","code":"printPath ( $ res , $ nThNode , $ kThNode * 2 ) ;"}
{"text":"right path of a tree and for right we will go node ( kThNode * 2 + 1 )","code":"printPath ( $ res , $ nThNode , $ kThNode * 2 + 1 ) ; }"}
{"text":"Function to print path from root to all of the nodes","code":"function printPathToCoverAllNodeUtil ( $ nThNode ) {"}
{"text":"res is for store the path from root to particulate node","code":"$ res = array ( ) ;"}
{"text":"Print path from root to all node . third argument 1 because of we have to consider root node is 1","code":"printPath ( $ res , $ nThNode , 1 ) ; }"}
{"text":"Given Node","code":"$ nThNode = 7 ;"}
{"text":"Print path from root to all node .","code":"printPathToCoverAllNodeUtil ( $ nThNode ) ; ? >"}
{"text":"function to get the minimum length of the shorter side of the triangle","code":"< ? php function shortestLength ( $ n , & $ x , & $ y ) { $ answer = 0 ;"}
{"text":"traversing through each points on the plane","code":"$ i = 0 ; while ( $ n -- ) {"}
{"text":"if sum of a points is greater than the previous one , the maximum gets replaced","code":"if ( $ x [ $ i ] + $ y [ $ i ] > $ answer ) $ answer = $ x [ $ i ] + $ y [ $ i ] ; $ i ++ ; }"}
{"text":"print the length","code":"echo \" Length \u2581 - > \u2581 \" . $ answer . \" STRNEWLINE \" ; echo \" Path \u2581 - > \u2581 \" . \" ( \u2581 1 , \u2581 \" . $ answer . \" \u2581 ) \" . \" and \u2581 ( \u2581 \" . $ answer . \" , \u2581 1 \u2581 ) \" ; }"}
{"text":"initialize the number of points","code":"$ n = 4 ;"}
{"text":"points on the plane","code":"$ x = array ( 1 , 4 , 2 , 1 ) ; $ y = array ( 4 , 1 , 1 , 2 ) ; shortestLength ( $ n , $ x , $ y ) ; ? >"}
{"text":"function to find intersection rectangle of given two rectangles .","code":"< ? php function FindPoints ( $ x1 , $ y1 , $ x2 , $ y2 , $ x3 , $ y3 , $ x4 , $ y4 ) {"}
{"text":"gives bottom - left point of intersection rectangle","code":"$ x5 = max ( $ x1 , $ x3 ) ; $ y5 = max ( $ y1 , $ y3 ) ;"}
{"text":"gives top - right point of intersection rectangle","code":"$ x6 = min ( $ x2 , $ x4 ) ; $ y6 = min ( $ y2 , $ y4 ) ;"}
{"text":"no intersection","code":"if ( $ x5 > $ x6 $ y5 > $ y6 ) { echo \" No \u2581 intersection \" ; return ; } echo \" ( \" . $ x5 . \" , \" \u2581 . \u2581 $ y5 \u2581 . \u2581 \" ) \" ; echo \" ( \" . $ x6 . \" , \" \u2581 . \u2581 $ y6 \u2581 . \u2581 \" ) \" ;"}
{"text":"gives top - left point of intersection rectangle","code":"$ x7 = $ x5 ; $ y7 = $ y6 ; echo \" ( \" . $ x7 . \" , \u2581 \" . $ y7 . \" ) \u2581 \" ;"}
{"text":"gives bottom - right point of intersection rectangle","code":"$ x8 = $ x6 ; $ y8 = $ y5 ; echo \" ( \" . $ x8 . \" , \u2581 \" . $ y8 . \" ) \u2581 \" ; }"}
{"text":"bottom - left and top - right corners of first rectangle","code":"$ x1 = 0 ; $ y1 = 0 ; $ x2 = 10 ; $ y2 = 8 ;"}
{"text":"bottom - left and top - right corners of first rectangle","code":"$ x3 = 2 ; $ y3 = 3 ; $ x4 = 7 ; $ y4 = 9 ;"}
{"text":"function call","code":"FindPoints ( $ x1 , $ y1 , $ x2 , $ y2 , $ x3 , $ y3 , $ x4 , $ y4 ) ; ? >"}
{"text":"Function to find area","code":"< ? php function area ( $ a , $ b , $ c ) { $ d = abs ( ( $ c * $ c ) \/ ( 2 * $ a * $ b ) ) ; return $ d ; }"}
{"text":"Driver code","code":"$ a = -2 ; $ b = 4 ; $ c = 3 ; echo area ( $ a , $ b , $ c ) ; ? >"}
{"text":"Function to return the vector containing the answer","code":"< ? php function addToArrayForm ( $ A , $ K ) {"}
{"text":"Vector v is to store each digits sum and vector ans is to store the answer","code":"$ v = array ( ) ; $ ans = array ( ) ;"}
{"text":"No carry in the beginning","code":"$ rem = 0 ; $ i = 0 ;"}
{"text":"Start loop from the end and take element one by one","code":"for ( $ i = count ( $ A ) - 1 ; $ i >= 0 ; $ i -- ) {"}
{"text":"Array index and last digit of number","code":"$ my = $ A [ $ i ] + $ K % 10 + $ rem ; if ( $ my > 9 ) {"}
{"text":"Maintain carry of summation","code":"$ rem = 1 ;"}
{"text":"Push the digit value into the array","code":"array_push ( $ v , $ my % 10 ) ; } else { array_push ( $ v , $ my ) ; $ rem = 0 ; } $ K = floor ( $ K \/ 10 ) ; }"}
{"text":"K value is greater then 0","code":"while ( $ K > 0 ) {"}
{"text":"Push digits of K one by one in the array","code":"$ my = $ K % 10 + $ rem ; array_push ( $ v , $ my % 10 ) ;"}
{"text":"Also maintain carry with summation","code":"if ( $ my \/ 10 > 0 ) $ rem = 1 ; else $ rem = 0 ; $ K = floor ( $ K \/ 10 ) ; } if ( $ rem > 0 ) array_push ( $ v , $ rem ) ;"}
{"text":"Reverse the elements of vector v and store it in vector ans","code":"for ( $ i = count ( $ v ) - 1 ; $ i >= 0 ; $ i -- ) array_push ( $ ans , $ v [ $ i ] ) ; return $ ans ; }"}
{"text":"Driver code","code":"$ A = array ( 2 , 7 , 4 ) ; $ K = 181 ; $ ans = addToArrayForm ( $ A , $ K ) ;"}
{"text":"Print the answer","code":"for ( $ i = 0 ; $ i < count ( $ ans ) ; $ i ++ ) echo $ ans [ $ i ] ; ? >"}
{"text":"Function to find the element","code":"< ? php function findThirdDigit ( $ n ) {"}
{"text":"if n < 3","code":"if ( $ n < 3 ) return 0 ;"}
{"text":"If n is even return 6 If n is odd return 1","code":"return $ n & 1 ? 1 : 6 ; }"}
{"text":"Driver code","code":"$ n = 7 ; echo findThirdDigit ( $ n ) ; ? >"}
{"text":"Function to return the probability of A winning","code":"< ? php function getProbability ( $ a , $ b , $ c , $ d ) {"}
{"text":"p and q store the values of fractions a \/ b and c \/ d","code":"$ p = $ a \/ $ b ; $ q = $ c \/ $ d ;"}
{"text":"To store the winning probability of A","code":"$ ans = $ p * ( 1 \/ ( 1 - ( 1 - $ q ) * ( 1 - $ p ) ) ) ; return round ( $ ans , 6 ) ; }"}
{"text":"Driver code","code":"$ a = 1 ; $ b = 2 ; $ c = 10 ; $ d = 11 ; echo getProbability ( $ a , $ b , $ c , $ d ) ; ? >"}
{"text":"Function to check if n is palindrome","code":"< ? php function isPalindrome ( $ n ) {"}
{"text":"Find the appropriate divisor to extract the leading digit","code":"$ divisor = 1 ; while ( ( int ) ( $ n \/ $ divisor ) >= 10 ) $ divisor *= 10 ; while ( $ n != 0 ) { $ leading = ( int ) ( $ n \/ $ divisor ) ; $ trailing = $ n % 10 ;"}
{"text":"If first and last digits are not same then return false","code":"if ( $ leading != $ trailing ) return false ;"}
{"text":"Removing the leading and trailing digits from the number","code":"$ n = ( $ n % $ divisor ) \/ 10 ;"}
{"text":"Reducing divisor by a factor of 2 as 2 digits are dropped","code":"$ divisor = $ divisor \/ 100 ; } return true ; }"}
{"text":"Function to find the largest palindromic number","code":"function largestPalindrome ( $ A , $ n ) { $ currentMax = -1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) {"}
{"text":"If a palindrome larger than the currentMax is found","code":"if ( $ A [ $ i ] > $ currentMax && isPalindrome ( $ A [ $ i ] ) ) $ currentMax = $ A [ $ i ] ; }"}
{"text":"Return the largest palindromic number from the array","code":"return $ currentMax ; }"}
{"text":"Driver Code","code":"$ A = array ( 1 , 232 , 54545 , 999991 ) ; $ n = sizeof ( $ A ) ;"}
{"text":"print required answer","code":"echo ( largestPalindrome ( $ A , $ n ) ) ; ? >"}
{"text":"Function to return the final element","code":"< ? php function getFinalElement ( $ n ) { $ finalNum = 0 ; for ( $ finalNum = 2 ; ( $ finalNum * 2 ) <= $ n ; $ finalNum *= 2 ) ; return $ finalNum ; }"}
{"text":"Driver code","code":"$ N = 12 ; echo getFinalElement ( $ N ) ; ? >"}
{"text":"Function that returns true if the given number is a palindrome","code":"< ? php function isPalindrome ( $ num ) { $ reverse_num = 0 ; $ remainder ; $ temp ;"}
{"text":"Here we are generating a new number ( reverse_num ) by reversing the digits of original input number","code":"$ temp = $ num ; while ( $ temp != 0 ) { $ remainder = $ temp % 10 ; $ reverse_num = $ reverse_num * 10 + $ remainder ; $ temp = ( int ) ( $ temp \/ 10 ) ; }"}
{"text":"If the original input number ( num ) is equal to its reverse ( reverse_num ) then its palindrome else it is not .","code":"if ( $ reverse_num == $ num ) { return true ; } return false ; }"}
{"text":"Function that returns true if the given number is of odd length","code":"function isOddLength ( $ num ) { $ count = 0 ; while ( $ num > 0 ) { $ num = ( int ) ( $ num \/ 10 ) ; $ count ++ ; } if ( $ count % 2 != 0 ) { return true ; } return false ; }"}
{"text":"Function to return the sum of all odd length palindromic numbers within the given range","code":"function sumOfAllPalindrome ( $ L , $ R ) { $ sum = 0 ; if ( $ L <= $ R ) for ( $ i = $ L ; $ i <= $ R ; $ i ++ ) {"}
{"text":"if number is palindrome and of odd length","code":"if ( isPalindrome ( $ i ) && isOddLength ( $ i ) ) { $ sum += $ i ; } } return $ sum ; }"}
{"text":"Driver code","code":"$ L = 110 ; $ R = 1130 ; echo sumOfAllPalindrome ( $ L , $ R ) ; ? >"}
{"text":"Computes value of first fibonacci numbers and stores their alternate sum","code":"< ? php function calculateAlternateSum ( $ n ) { if ( $ n <= 0 ) return 0 ; $ fibo = array ( ) ; $ fibo [ 0 ] = 0 ; $ fibo [ 1 ] = 1 ;"}
{"text":"Initialize result","code":"$ sum = pow ( $ fibo [ 0 ] , 2 ) + pow ( $ fibo [ 1 ] , 2 ) ;"}
{"text":"Add remaining terms","code":"for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { $ fibo [ $ i ] = $ fibo [ $ i - 1 ] + $ fibo [ $ i - 2 ] ;"}
{"text":"For even terms","code":"if ( $ i % 2 == 0 ) $ sum -= $ fibo [ $ i ] ;"}
{"text":"For odd terms","code":"else $ sum += $ fibo [ $ i ] ; }"}
{"text":"Return the alternating sum","code":"return $ sum ; }"}
{"text":"Get n","code":"$ n = 8 ;"}
{"text":"Find the alternating sum","code":"echo ( \" Alternating \u2581 Fibonacci \u2581 Sum \u2581 upto \u2581 \" ) ; echo $ n ; echo \" \u2581 terms : \u2581 \" ; echo ( calculateAlternateSum ( $ n ) ) ; ? >"}
{"text":"Function that will return nth term","code":"< ? php function getValue ( $ n ) { $ i = 0 ; $ k = 1 ; while ( $ i < $ n ) { $ i = $ i + $ k ; $ k = $ k * 2 ; } return ( int ) $ k \/ 2 ; }"}
{"text":"Get n","code":"$ n = 9 ;"}
{"text":"Get the value","code":"echo getValue ( $ n ) , \" STRNEWLINE \" ;"}
{"text":"Get n","code":"$ n = 1025 ;"}
{"text":"Get the value","code":"echo getValue ( $ n ) , \" STRNEWLINE \" ; ? >"}
{"text":"Function that traverses digits in a number and modifies frequency count array","code":"< ? php function countDigits ( $ val , & $ arr ) { while ( $ val > 0 ) { $ digit = $ val % 10 ; $ arr [ ( int ) ( $ digit ) ] += 1 ; $ val = ( int ) ( $ val \/ 10 ) ; } return ; } function countFrequency ( $ x , $ n ) {"}
{"text":"Array to keep count of digits","code":"$ freq_count = array_fill ( 0 , 10 , 0 ) ;"}
{"text":"Traversing through x ^ 1 to x ^ n","code":"for ( $ i = 1 ; $ i < $ n + 1 ; $ i ++ ) {"}
{"text":"For power function , both its parameters are to be in double","code":"$ val = pow ( $ x , $ i ) ;"}
{"text":"calling countDigits function on x ^ i","code":"countDigits ( $ val , $ freq_count ) ; }"}
{"text":"Printing count of digits 0 - 9","code":"for ( $ i = 0 ; $ i < 10 ; $ i ++ ) { echo $ freq_count [ $ i ] . \" \" ; } }"}
{"text":"Driver code","code":"$ x = 15 ; $ n = 3 ; countFrequency ( $ x , $ n ) ? >"}
{"text":"function to return the number of solutions","code":"< ? php function countSolutions ( $ a ) { $ count = 0 ;"}
{"text":"check for every possible value","code":"for ( $ i = 0 ; $ i <= $ a ; $ i ++ ) { if ( $ a == ( $ i + ( $ a ^ $ i ) ) ) $ count ++ ; } return $ count ; }"}
{"text":"Driver Code","code":"$ a = 3 ; echo countSolutions ( $ a ) ; ? >"}
{"text":"function to return the number of solutions","code":"< ? php function countSolutions ( $ a ) { $ count = bitCount ( $ a ) ; $ count = ( int ) pow ( 2 , $ count ) ; return $ count ; } function bitCount ( $ n ) { $ count = 0 ; while ( $ n != 0 ) { $ count ++ ; $ n &= ( $ n - 1 ) ; } return $ count ; }"}
{"text":"Driver Code","code":"$ a = 3 ; echo ( countSolutions ( $ a ) ) ; ? >"}
{"text":"Function to calculate the sum of area of all possible squares that comes inside the rectangle","code":"< ? php function calculateAreaSum ( $ l , $ b ) { $ size = 1 ;"}
{"text":"Square with max size possible","code":"$ maxSize = min ( $ l , $ b ) ; $ totalArea = 0 ; for ( $ i = 1 ; $ i <= $ maxSize ; $ i ++ ) {"}
{"text":"calculate total square of a given size","code":"$ totalSquares = ( $ l - $ size + 1 ) * ( $ b - $ size + 1 ) ;"}
{"text":"calculate area of squares of a particular size","code":"$ area = $ totalSquares * $ size * $ size ;"}
{"text":"total area","code":"$ totalArea += $ area ;"}
{"text":"increment size","code":"$ size ++ ; } return $ totalArea ; }"}
{"text":"Driver Code","code":"$ l = 4 ; $ b = 3 ; echo calculateAreaSum ( $ l , $ b ) ; ? >"}
{"text":"function to calculate the value of hyperfactorial","code":"< ? php function boost_hyperfactorial ( $ num ) {"}
{"text":"initialise the val to 1","code":"$ val = 1 ; for ( $ i = 1 ; $ i <= $ num ; $ i ++ ) { $ val = $ val * pow ( $ i , $ i ) ; }"}
{"text":"returns the hyperfactorial of a number","code":"return $ val ; }"}
{"text":"Driver code","code":"$ num = 5 ; echo boost_hyperfactorial ( $ num ) ; ? >"}
{"text":"function to calculate the value of hyperfactorial","code":"< ? php function boost_hyperfactorial ( $ num ) {"}
{"text":"initialise the val to 1","code":"$ val = 1 ; for ( $ i = 1 ; $ i <= $ num ; $ i ++ ) { for ( $ j = 1 ; $ j <= $ i ; $ j ++ ) {"}
{"text":"1 ^ 1 * 2 ^ 2 * 3 ^ 3. . . .","code":"$ val *= $ i ; } }"}
{"text":"returns the hyperfactorial of a number","code":"return $ val ; }"}
{"text":"Driver code","code":"$ num = 5 ; echo boost_hyperfactorial ( $ num ) ; ? >"}
{"text":"PHP code to subtract one from a given number","code":"< ? php function subtractOne ( $ x ) { $ m = 1 ;"}
{"text":"Flip all the set bits until we find a 1","code":"while ( ! ( $ x & $ m ) ) { $ x = $ x ^ $ m ; $ m <<= 1 ; }"}
{"text":"flip the rightmost 1 bit","code":"$ x = $ x ^ $ m ; return $ x ; }"}
{"text":"Driver Code","code":"echo subtractOne ( 13 ) ; ? >"}
{"text":"PHP program to find mean vector of given matrix","code":"< ? php $ rows = 3 ; $ cols = 3 ;"}
{"text":"Function to find mean vector","code":"function meanVector ( $ mat ) { global $ rows , $ cols ; echo \" [ \""}
{"text":"loop to traverse each column","code":"for ( $ i = 0 ; $ i < $ rows ; $ i ++ ) {"}
{"text":"to calculate mean of each row","code":"$ mean = 0.00 ;"}
{"text":"to store sum of elements of a column","code":"$ sum = 0 ; for ( $ j = 0 ; $ j < $ cols ; $ j ++ ) $ sum += $ mat [ $ j ] [ $ i ] ; $ mean = $ sum \/ $ rows ; echo $ mean , \" \" ; \u2581 } \u2581 echo \u2581 \" ] \" }"}
{"text":"Driver Code","code":"$ mat = array ( array ( 1 , 2 , 3 ) , array ( 4 , 5 , 6 ) , array ( 7 , 8 , 9 ) ) ; meanVector ( $ mat ) ; ? >"}
{"text":"Function to find distinct prime factors of given number n","code":"< ? php function primeFactors ( $ n ) { $ res = array ( ) ; if ( $ n % 2 == 0 ) { while ( $ n % 2 == 0 ) $ n = ( int ) $ n \/ 2 ; array_push ( $ res , 2 ) ; }"}
{"text":"n is odd at this point , since it is no longer divisible by 2. So we can test only for the odd numbers , whether they are factors of n","code":"for ( $ i = 3 ; $ i <= sqrt ( $ n ) ; $ i = $ i + 2 ) {"}
{"text":"Check if i is prime factor","code":"if ( $ n % $ i == 0 ) { while ( $ n % $ i == 0 ) $ n = ( int ) $ n \/ $ i ; array_push ( $ res , $ i ) ; } }"}
{"text":"This condition is to handle the case when n is a prime number greater than 2","code":"if ( $ n > 2 ) array_push ( $ res , $ n ) ; return $ res ; }"}
{"text":"Function to calculate sum of digits of distinct prime factors of given number n and sum of digits of number n and compare the sums obtained","code":"function isHoax ( $ n ) {"}
{"text":"Distinct prime factors of n are being stored in vector pf","code":"$ pf = primeFactors ( $ n ) ;"}
{"text":"If n is a prime number , it cannot be a hoax number","code":"if ( $ pf [ 0 ] == $ n ) return false ;"}
{"text":"Finding sum of digits of distinct prime factors of the number n","code":"$ all_pf_sum = 0 ; for ( $ i = 0 ; $ i < count ( $ pf ) ; $ i ++ ) {"}
{"text":"Finding sum of digits in current prime factor pf [ i ] .","code":"$ pf_sum ; for ( $ pf_sum = 0 ; $ pf [ $ i ] > 0 ; $ pf_sum += $ pf [ $ i ] % 10 , $ pf [ $ i ] \/= 10 ) ; $ all_pf_sum += $ pf_sum ; }"}
{"text":"Finding sum of digits of number n","code":"for ( $ sum_n = 0 ; $ n > 0 ; $ sum_n += $ n % 10 , $ n \/= 10 ) ;"}
{"text":"Comparing the two calculated sums","code":"return $ sum_n == $ all_pf_sum ; }"}
{"text":"Driver Code","code":"$ n = 84 ; if ( isHoax ( $ n ) ) echo ( \" A \u2581 Hoax \u2581 Number STRNEWLINE \" ) ; else echo ( \" Not \u2581 a \u2581 Hoax \u2581 Number STRNEWLINE \" ) ; ? >"}
{"text":"A naive method to find modular multiplicative inverse of ' a ' under modulo ' prime '","code":"< ? php function modInverse ( int $ a , int $ prime ) { $ a = $ a % $ prime ; for ( $ x = 1 ; $ x < $ prime ; $ x ++ ) if ( ( $ a * $ x ) % $ prime == 1 ) return $ x ; return -1 ; } function printModIverses ( $ n , $ prime ) { for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) echo modInverse ( $ i , $ prime ) , \" \u2581 \" ; }"}
{"text":"Driver Program","code":"$ n = 10 ; $ prime = 17 ; printModIverses ( $ n , $ prime ) ; ? >"}
{"text":"function for minimum operation","code":"< ? php function minOp ( $ num ) {"}
{"text":"remainder and operations count","code":"$ count = 0 ;"}
{"text":"count digits not equal to 3 or 8","code":"while ( $ num ) { $ rem = intval ( $ num % 10 ) ; if ( ! ( $ rem == 3 $ rem == 8 ) ) $ count ++ ; $ num = intval ( $ num \/ 10 ) ; } return $ count ; }"}
{"text":"Driver Code","code":"$ num = 234198 ; echo \" Minimum \u2581 Operations \u2581 = \u2581 \" . minOp ( $ num ) ; ? >"}
{"text":"function to calculate the sum of digits of a number .","code":"< ? php function sumOfDigits ( $ a ) { $ sum = 0 ; while ( $ a ) { $ sum += $ a % 10 ; $ a = ( int ) $ a \/ 10 ; } return $ sum ; }"}
{"text":"Returns the maximum number with maximum sum of digits .","code":"function findMax ( $ x ) {"}
{"text":"initializing b as 1 and initial max sum to be of n","code":"$ b = 1 ; $ ans = $ x ;"}
{"text":"iterates from right to left in a digit","code":"while ( $ x ) {"}
{"text":"while iterating this is the number from from right to left","code":"$ cur = ( $ x - 1 ) * $ b + ( $ b - 1 ) ;"}
{"text":"calls the function to check if sum of cur is more then of ans","code":"if ( sumOfDigits ( $ cur ) > sumOfDigits ( $ ans ) || ( sumOfDigits ( $ cur ) == sumOfDigits ( $ ans ) && $ cur > $ ans ) ) $ ans = $ cur ;"}
{"text":"reduces the number to one unit less","code":"$ x = ( int ) $ x \/ 10 ; $ b *= 10 ; } return $ ans ; }"}
{"text":"Driver Code","code":"$ n = 521 ; echo findMax ( $ n ) ; ? >"}
{"text":"Function to give index of the median","code":"< ? php function median ( $ a , $ l , $ r ) { $ n = $ r - $ l + 1 ; $ n = ( int ) ( ( $ n + 1 ) \/ 2 ) - 1 ; return $ n + $ l ; }"}
{"text":"Function to calculate IQR","code":"function IQR ( $ a , $ n ) { sort ( $ a ) ;"}
{"text":"Index of median of entire data","code":"$ mid_index = median ( $ a , 0 , $ n ) ;"}
{"text":"Median of first half","code":"$ Q1 = $ a [ median ( $ a , 0 , $ mid_index ) ] ;"}
{"text":"Median of second half","code":"$ Q3 = $ a [ $ mid_index + median ( $ a , $ mid_index + 1 , $ n ) ] ;"}
{"text":"IQR calculation","code":"return ( $ Q3 - $ Q1 ) ; }"}
{"text":"Driver Function","code":"$ a = array ( 1 , 19 , 7 , 6 , 5 , 9 , 12 , 27 , 18 , 2 , 15 ) ; $ n = count ( $ a ) ; echo IQR ( $ a , $ n ) ; ? >"}
{"text":"Function to check if n is palindrome","code":"< ? php function isPalindrome ( $ n ) {"}
{"text":"Find the appropriate divisor to extract the leading digit","code":"$ divisor = 1 ; while ( ( int ) ( $ n \/ $ divisor ) >= 10 ) $ divisor *= 10 ; while ( $ n != 0 ) { $ leading = ( int ) ( $ n \/ $ divisor ) ; $ trailing = $ n % 10 ;"}
{"text":"If first and last digits are not same then return false","code":"if ( $ leading != $ trailing ) return false ;"}
{"text":"Removing the leading and trailing digits from the number","code":"$ n = ( int ) ( ( $ n % $ divisor ) \/ 10 ) ;"}
{"text":"Reducing divisor by a factor of 2 as 2 digits are dropped","code":"$ divisor = ( int ) ( $ divisor \/ 100 ) ; } return true ; }"}
{"text":"Function to find the largest palindromic number","code":"function largestPalindrome ( $ A , $ n ) {"}
{"text":"Sort the array","code":"sort ( $ A ) ; for ( $ i = $ n - 1 ; $ i >= 0 ; -- $ i ) {"}
{"text":"If number is palindrome","code":"if ( isPalindrome ( $ A [ $ i ] ) ) return $ A [ $ i ] ; }"}
{"text":"If no palindromic number found","code":"return -1 ; }"}
{"text":"Driver Code","code":"$ A = array ( 1 , 232 , 54545 , 999991 ) ; $ n = sizeof ( $ A ) ;"}
{"text":"print required answer","code":"echo largestPalindrome ( $ A , $ n ) ; ? >"}
{"text":"Function to return the sum of all the integers below N which are multiples of either A or B","code":"< ? php function findSum ( $ n , $ a , $ b ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ )"}
{"text":"If i is a multiple of a or b","code":"if ( $ i % $ a == 0 $ i % $ b == 0 ) $ sum += $ i ; return $ sum ; }"}
{"text":"Driver code","code":"$ n = 10 ; $ a = 3 ; $ b = 5 ; echo findSum ( $ n , $ a , $ b ) ; ? >"}
{"text":"Driver code","code":"< ? php function subtractOne ( $ x ) { return ( ( $ x << 1 ) + ( ~ $ x ) ) ; } print ( subtractOne ( 13 ) ) ; ? >"}
{"text":"calculate nth pell number","code":"< ? php function pell ( $ n ) { if ( $ n <= 2 ) return $ n ; return 2 * pell ( $ n - 1 ) + pell ( $ n - 2 ) ; }"}
{"text":"Driver Code","code":"$ n = 4 ; echo ( pell ( $ n ) ) ; ? >"}
{"text":"Returns LCM of arr [ 0. . n - 1 ]","code":"< ? php function LCM ( $ arr , $ n ) {"}
{"text":"Find the maximum value in arr [ ]","code":"$ max_num = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ max_num < $ arr [ $ i ] ) $ max_num = $ arr [ $ i ] ;"}
{"text":"Initialize result","code":"$ res = 1 ;"}
{"text":"Find all factors that are present in two or more array elements . $x = 2 ; Current factor .","code":"while ( $ x <= $ max_num ) {"}
{"text":"To store indexes of all array elements that are divisible by x .","code":"$ indexes = array ( ) ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) if ( $ arr [ $ j ] % $ x == 0 ) array_push ( $ indexes , $ j ) ;"}
{"text":"If there are 2 or more array elements that are divisible by x .","code":"if ( count ( $ indexes ) >= 2 ) {"}
{"text":"Reduce all array elements divisible by x .","code":"for ( $ j = 0 ; $ j < count ( $ indexes ) ; $ j ++ ) $ arr [ $ indexes [ $ j ] ] = ( int ) ( $ arr [ $ indexes [ $ j ] ] \/ $ x ) ; $ res = $ res * $ x ; } else $ x ++ ; }"}
{"text":"Then multiply all reduced array elements","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ res = $ res * $ arr [ $ i ] ; return $ res ; }"}
{"text":"Driver code","code":"$ arr = array ( 1 , 2 , 3 , 4 , 5 , 10 , 20 , 35 ) ; $ n = count ( $ arr ) ; echo LCM ( $ arr , $ n ) . \" STRNEWLINE \" ; ? >"}
{"text":"PHP program to implement Goldbach 's conjecture","code":"< ? php $ MAX = 10000 ;"}
{"text":"Array to store all prime less than and equal to 10 ^ 6","code":"$ primes = array ( ) ;"}
{"text":"Utility function for Sieve of Sundaram","code":"function sieveSundaram ( ) { global $ MAX , $ primes ;"}
{"text":"In general Sieve of Sundaram , produces primes smaller than ( 2 * x + 2 ) for a number given number x . Since we want primes smaller than MAX , we reduce MAX to half . This array is used to separate numbers of the form i + j + 2 * i * j from others where 1 <= i <= j","code":"$ marked = array_fill ( 0 , ( int ) ( $ MAX \/ 2 ) + 100 , false ) ;"}
{"text":"Main logic of Sundaram . Mark all numbers which do not generate prime number by doing 2 * i + 1","code":"for ( $ i = 1 ; $ i <= ( sqrt ( $ MAX ) - 1 ) \/ 2 ; $ i ++ ) for ( $ j = ( $ i * ( $ i + 1 ) ) << 1 ; $ j <= $ MAX \/ 2 ; $ j = $ j + 2 * $ i + 1 ) $ marked [ $ j ] = true ;"}
{"text":"Since 2 is a prime number","code":"array_push ( $ primes , 2 ) ;"}
{"text":"Print other primes . Remaining primes are of the form 2 * i + 1 such that marked [ i ] is false .","code":"for ( $ i = 1 ; $ i <= $ MAX \/ 2 ; $ i ++ ) if ( $ marked [ $ i ] == false ) array_push ( $ primes , 2 * $ i + 1 ) ; }"}
{"text":"Function to perform Goldbach 's conjecture","code":"function findPrimes ( $ n ) { global $ MAX , $ primes ;"}
{"text":"Return if number is not even or less than 3","code":"if ( $ n <= 2 $ n % 2 != 0 ) { print ( \" Invalid \u2581 Input \u2581 STRNEWLINE \" ) ; return ; }"}
{"text":"Check only upto half of number","code":"for ( $ i = 0 ; $ primes [ $ i ] <= $ n \/ 2 ; $ i ++ ) {"}
{"text":"find difference by subtracting current prime from n","code":"$ diff = $ n - $ primes [ $ i ] ;"}
{"text":"Search if the difference is also a prime number","code":"if ( in_array ( $ diff , $ primes ) ) {"}
{"text":"Express as a sum of primes","code":"print ( $ primes [ $ i ] . \" + \" \u2581 . \u2581 $ diff \u2581 . \u2581 \" = \" \u2581 . \u2581 $ n \u2581 . \u2581 \" \" return ; } } }"}
{"text":"Finding all prime numbers before limit","code":"sieveSundaram ( ) ;"}
{"text":"Express number as a sum of two primes","code":"findPrimes ( 4 ) ; findPrimes ( 38 ) ; findPrimes ( 100 ) ; ? >"}
{"text":"A function to generate prime factors of a given number n and return k - th prime factor","code":"< ? php function kPrimeFactor ( $ n , $ k ) {"}
{"text":"Find the number of 2 's  that divide k","code":"while ( $ n % 2 == 0 ) { $ k -- ; $ n = $ n \/ 2 ; if ( $ k == 0 ) return 2 ; }"}
{"text":"n must be odd at this point . So we can skip one element ( Note i = i + 2 )","code":"for ( $ i = 3 ; $ i <= sqrt ( $ n ) ; $ i = $ i + 2 ) {"}
{"text":"While i divides n , store i and divide n","code":"while ( $ n % $ i == 0 ) { if ( $ k == 1 ) return $ i ; $ k -- ; $ n = $ n \/ $ i ; } }"}
{"text":"This condition is to handle the case where n is a prime number greater than 2","code":"if ( $ n > 2 && $ k == 1 ) return $ n ; return -1 ; }"}
{"text":"Driver Code","code":"{ $ n = 12 ; $ k = 3 ; echo kPrimeFactor ( $ n , $ k ) , \" STRNEWLINE \" ; $ n = 14 ; $ k = 3 ; echo kPrimeFactor ( $ n , $ k ) ; return 0 ; } ? >"}
{"text":"PHP program to find k - th prime factor using Sieve Of Eratosthenes . This program is efficient when we have a range of numbers .","code":"< ? php $ MAX = 10001 ;"}
{"text":"Using SieveOfEratosthenes to find smallest prime factor of all the numbers . For example , if MAX is 10 , s [ 2 ] = s [ 4 ] = s [ 6 ] = s [ 10 ] = 2 s [ 3 ] = s [ 9 ] = 3 s [ 5 ] = 5 s [ 7 ] = 7","code":"function sieveOfEratosthenes ( & $ s ) { global $ MAX ;"}
{"text":"Create a boolean array \" prime [ 0 . . MAX ] \" and initialize all entries in it as false .","code":"$ prime = array_fill ( 0 , $ MAX + 1 , false ) ;"}
{"text":"Initializing smallest factor equal to 2 for all the even numbers","code":"for ( $ i = 2 ; $ i <= $ MAX ; $ i += 2 ) $ s [ $ i ] = 2 ;"}
{"text":"For odd numbers less then equal to n","code":"for ( $ i = 3 ; $ i <= $ MAX ; $ i += 2 ) { if ( $ prime [ $ i ] == false ) {"}
{"text":"s ( i ) for a prime is the number itself","code":"$ s [ $ i ] = $ i ;"}
{"text":"For all multiples of current prime number","code":"for ( $ j = $ i ; $ j * $ i <= $ MAX ; $ j += 2 ) { if ( $ prime [ $ i * $ j ] == false ) { $ prime [ $ i * $ j ] = true ;"}
{"text":"i is the smallest prime factor for number \" i * j \" .","code":"$ s [ $ i * $ j ] = $ i ; } } } } }"}
{"text":"Function to generate prime factors and return its k - th prime factor . s [ i ] stores least prime factor of i .","code":"function kPrimeFactor ( $ n , $ k , $ s ) {"}
{"text":"Keep dividing n by least prime factor while either n is not 1 or count of prime factors is not k .","code":"while ( $ n > 1 ) { if ( $ k == 1 ) return $ s [ $ n ] ;"}
{"text":"To keep track of count of prime factors","code":"$ k -- ;"}
{"text":"Divide n to find next prime factor","code":"$ n = ( int ) ( $ n \/ $ s [ $ n ] ) ; } return -1 ; }"}
{"text":"s [ i ] is going to store prime factor of i .","code":"$ s = array_fill ( 0 , $ MAX + 1 , -1 ) ; sieveOfEratosthenes ( $ s ) ; $ n = 12 ; $ k = 3 ; print ( kPrimeFactor ( $ n , $ k , $ s ) . \" \" ) ; $ n = 14 ; $ k = 3 ; print ( kPrimeFactor ( $ n , $ k , $ s ) ) ; ? >"}
{"text":"Returns true if square root of n under modulo p exists","code":"< ? php function squareRootExists ( $ n , $ p ) { $ n = $ n % $ p ;"}
{"text":"One by one check all numbers from 2 to p - 1","code":"for ( $ x = 2 ; $ x < $ p ; $ x ++ ) if ( ( $ x * $ x ) % $ p == $ n ) return true ; return false ; }"}
{"text":"Driver Code","code":"$ p = 7 ; $ n = 2 ; if ( squareRootExists ( $ n , $ p ) == true ) echo \" Yes \" ; else echo \" No \" ; ? >"}
{"text":"Returns largest power of p that divides n !","code":"< ? php function largestPower ( $ n , $ p ) {"}
{"text":"Initialize result","code":"$ x = 0 ;"}
{"text":"Calculate x = n \/ p + n \/ ( p ^ 2 ) + n \/ ( p ^ 3 ) + ... .","code":"while ( $ n ) { $ n = ( int ) $ n \/ $ p ; $ x += $ n ; } return floor ( $ x ) ; }"}
{"text":"Driver Code","code":"$ n = 10 ; $ p = 3 ; echo \" The \u2581 largest \u2581 power \u2581 of \u2581 \" , $ p ; echo \" \u2581 that \u2581 divides \u2581 \" , $ n , \" ! \u2581 is \u2581 \" ; echo largestPower ( $ n , $ p ) ; ? >"}
{"text":"PHP program to find factorial of given number","code":"< ? php function factorial ( $ n ) {"}
{"text":"single line to find factorial","code":"return ( $ n == 1 $ n == 0 ) ? 1 : $ n * factorial ( $ n - 1 ) ; }"}
{"text":"Driver Code","code":"$ num = 5 ; echo \" Factorial \u2581 of \u2581 \" , $ num , \" \u2581 is \u2581 \" , factorial ( $ num ) ; ? >"}
{"text":"function to reverse bits of a number","code":"< ? php function reverseBits ( $ n ) { $ rev = 0 ;"}
{"text":"traversing bits of ' n ' from the right","code":"while ( $ n > 0 ) {"}
{"text":"bitwise left shift ' rev ' by 1","code":"$ rev <<= 1 ;"}
{"text":"if current bit is '1'","code":"if ( $ n & 1 == 1 ) $ rev ^= 1 ;"}
{"text":"bitwise right shift ' n ' by 1","code":"$ n >>= 1 ; }"}
{"text":"required number","code":"return $ rev ; }"}
{"text":"Driver code","code":"$ n = 11 ; echo reverseBits ( $ n ) ; ? >"}
{"text":"Return the count number of ways to split array into two groups such that each grouphas equal XOR value .","code":"< ? php function countgroup ( $ a , $ n ) { $ xs = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ xs = $ xs ^ $ a [ $ i ] ;"}
{"text":"We can split only if XOR is 0. Since XOR of all is 0 , we can consider all subsets as one group .","code":"if ( $ xs == 0 ) return ( 1 << ( $ n - 1 ) ) - 1 ; return 0 ; }"}
{"text":"Driver Code","code":"$ a = array ( 1 , 2 , 3 ) ; $ n = count ( $ a ) ; echo countgroup ( $ a , $ n ) ; ? >"}
{"text":"Function to extract k bits from p position and returns the extracted value as integer","code":"< ? php function bitExtracted ( $ number , $ k , $ p ) { return ( ( ( 1 << $ k ) - 1 ) & ( $ number >> ( $ p - 1 ) ) ) ; }"}
{"text":"Driver Code","code":"$ number = 171 ; $ k = 5 ; $ p = 2 ; echo \" The \u2581 extracted \u2581 number \u2581 is \u2581 \" , bitExtracted ( $ number , $ k , $ p ) ; ? >"}
{"text":"function to check whether ' n ' is a multiple of 4 or not","code":"< ? php function isAMultipleOf4 ( $ n ) {"}
{"text":"if true , then ' n ' is a multiple of 4","code":"if ( ( $ n & 3 ) == 0 ) return \" Yes \" ;"}
{"text":"else ' n ' is not a multiple of 4","code":"return \" No \" ; }"}
{"text":"Driver Code","code":"$ n = 16 ; echo isAMultipleOf4 ( $ n ) ; ? >"}
{"text":"PHP implementation to calculate square without using * and pow ( )","code":"< ? php function square ( $ n ) {"}
{"text":"handle negative input","code":"if ( $ n < 0 ) $ n = - $ n ;"}
{"text":"Initialize result","code":"$ res = $ n ;"}
{"text":"Add n to res n - 1 times","code":"for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ res += $ n ; return $ res ; }"}
{"text":"Driver Code","code":"for ( $ n = 1 ; $ n <= 5 ; $ n ++ ) echo \" n = \" , \u2581 $ n , \u2581 \" , \" , \u2581 \" n ^ 2 = \" , square ( $ n ) , \" STRNEWLINE \u2581 \" ; ? >"}
{"text":"PHP implementation of the approach","code":"< ? php function PointInKSquares ( $ n , $ a , $ k ) { sort ( $ a ) ; return $ a [ $ n - $ k ] ; }"}
{"text":"Driver Code","code":"$ k = 2 ; $ a = array ( 1 , 2 , 3 , 4 ) ; $ n = sizeof ( $ a ) ; $ x = PointInKSquares ( $ n , $ a , $ k ) ; echo \" ( \" . $ x . \" , \" \u2581 . \u2581 $ x \u2581 . \u2581 \" ) \" ; ? >"}
{"text":"function that calculates the answer","code":"< ? php function answer ( $ n ) {"}
{"text":"dp [ j ] stores count of i digit stepping numbers ending with digit j .","code":"$ dp = array_fill ( 0 , 10 , 0 ) ;"}
{"text":"To store result of length i - 1 before updating dp [ j ] for length i .","code":"$ prev = array_fill ( 0 , 10 , 0 ) ; ;"}
{"text":"if n is 1 then answer will be 10.","code":"if ( $ n == 1 ) return 10 ;"}
{"text":"Initialize values for count of digits equal to 1.","code":"for ( $ j = 0 ; $ j <= 9 ; $ j ++ ) $ dp [ $ j ] = 1 ;"}
{"text":"Compute values for count of digits more than 1.","code":"for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { for ( $ j = 0 ; $ j <= 9 ; $ j ++ ) { $ prev [ $ j ] = $ dp [ $ j ] ; } for ( $ j = 0 ; $ j <= 9 ; $ j ++ ) {"}
{"text":"If ending digit is 0","code":"if ( $ j == 0 ) $ dp [ $ j ] = $ prev [ $ j + 1 ] ;"}
{"text":"If ending digit is 9","code":"else if ( $ j == 9 ) $ dp [ $ j ] = $ prev [ $ j - 1 ] ;"}
{"text":"For other digits .","code":"else $ dp [ $ j ] = $ prev [ $ j - 1 ] + $ prev [ $ j + 1 ] ; } }"}
{"text":"stores the final answer","code":"$ sum = 0 ; for ( $ j = 1 ; $ j <= 9 ; $ j ++ ) $ sum += $ dp [ $ j ] ; return $ sum ; }"}
{"text":"Driver program to test the above function","code":"$ n = 2 ; echo answer ( $ n ) ; ? >"}
{"text":"PHP implementation of the approach","code":"< ? php $ MAX = 1000 ;"}
{"text":"To store first N Catalan numbers","code":"$ catalan = array_fill ( 0 , $ MAX , 0 ) ;"}
{"text":"Function to find first n Catalan numbers","code":"function catalanDP ( $ n ) { global $ catalan ;"}
{"text":"Initialize first two values in table","code":"$ catalan [ 0 ] = $ catalan [ 1 ] = 1 ;"}
{"text":"Filong entries in catalan [ ] using recursive formula","code":"for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { $ catalan [ $ i ] = 0 ; for ( $ j = 0 ; $ j < $ i ; $ j ++ ) { $ catalan [ $ i ] += $ catalan [ $ j ] * $ catalan [ $ i - $ j - 1 ] ; } } }"}
{"text":"Function to return the minimum changes required","code":"function CatalanSequence ( $ arr , $ n ) { global $ catalan ;"}
{"text":"Find first n Catalan Numbers","code":"catalanDP ( $ n ) ; $ s = array ( ) ;"}
{"text":"a and b are first two Catalan Sequence numbers","code":"$ a = $ b = 1 ;"}
{"text":"Insert first n catalan elements to set","code":"array_push ( $ s , $ a ) ; if ( $ n >= 2 ) { array_push ( $ s , $ b ) ; } for ( $ i = 2 ; $ i < $ n ; $ i ++ ) { array_push ( $ s , $ catalan [ $ i ] ) ; } $ s = array_unique ( $ s ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) {"}
{"text":"If catalan element is present in the array then remove it from set","code":"if ( in_array ( $ arr [ $ i ] , $ s ) ) { unset ( $ s [ array_search ( $ arr [ $ i ] , $ s ) ] ) ; } }"}
{"text":"Return the remaining number of elements in the set","code":"return count ( $ s ) ; }"}
{"text":"Driver code","code":"$ arr = array ( 1 , 1 , 2 , 5 , 41 ) ; $ n = count ( $ arr ) ; print ( CatalanSequence ( $ arr , $ n ) ) ; ? >"}
{"text":"function to solve the given problem","code":"< ? php function solve ( $ a , $ n ) { $ max1 = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( abs ( $ a [ $ i ] - $ a [ $ j ] ) > $ max1 ) { $ max1 = abs ( $ a [ $ i ] - $ a [ $ j ] ) ; } } } return $ max1 ; }"}
{"text":"Driver Code","code":"$ arr = array ( -1 , 2 , 3 , -4 , -10 , 22 ) ; $ size = count ( $ arr ) ; echo \" Largest \u2581 gap \u2581 is \u2581 : \u2581 \" , solve ( $ arr , $ size ) ; ? >"}
{"text":"function to solve the given problem","code":"< ? php function solve ( $ a , $ n ) { $ min1 = $ a [ 0 ] ; $ max1 = $ a [ 0 ] ;"}
{"text":"finding maximum and minimum of an array","code":"for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] > $ max1 ) $ max1 = $ a [ $ i ] ; if ( $ a [ $ i ] < $ min1 ) $ min1 = $ a [ $ i ] ; } return abs ( $ min1 - $ max1 ) ; }"}
{"text":"Driver Code","code":"$ arr = array ( -1 , 2 , 3 , 4 , -10 ) ; $ size = count ( $ arr ) ; echo \" Largest \u2581 gap \u2581 is \u2581 : \u2581 \" , solve ( $ arr , $ size ) ; ? >"}
{"text":"function to find minimum elements needed .","code":"< ? php function minElements ( $ arr , $ n ) {"}
{"text":"calculating HALF of array sum","code":"$ halfSum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ halfSum = $ halfSum + $ arr [ $ i ] ; $ halfSum = $ halfSum \/ 2 ;"}
{"text":"sort the array in descending order .","code":"rsort ( $ arr ) ; $ res = 0 ; $ curr_sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ curr_sum += $ arr [ $ i ] ; $ res ++ ;"}
{"text":"current sum greater than sum","code":"if ( $ curr_sum > $ halfSum ) return $ res ; } return $ res ; }"}
{"text":"Driver Code","code":"$ arr = array ( 3 , 1 , 7 , 1 ) ; $ n = sizeof ( $ arr ) ; echo minElements ( $ arr , $ n ) ; ? >"}
{"text":"Function to return minimum cost to reach destination","code":"< ? php function minCost ( $ N , $ P , $ Q ) {"}
{"text":"Initialize cost to 0","code":"$ cost = 0 ;"}
{"text":"going backwards until we reach initial position","code":"while ( $ N > 0 ) { if ( $ N & 1 ) { $ cost += $ P ; $ N -- ; } else { $ temp = $ N \/ 2 ;"}
{"text":"if 2 * X jump is better than X + 1","code":"if ( $ temp * $ P > $ Q ) $ cost += $ Q ;"}
{"text":"if X + 1 jump is better","code":"else $ cost += $ P * $ temp ; $ N \/= 2 ; } }"}
{"text":"return cost","code":"return $ cost ; }"}
{"text":"Driver Code","code":"$ N = 9 ; $ P = 5 ; $ Q = 1 ; echo minCost ( $ N , $ P , $ Q ) ; ? >"}
