"Find maximum sum taking every Kth element in the array | Function to return the maximum sum for every possible sequence such that a [ i ] + a [ i + k ] + a [ i + 2 k ] + ... + a [ i + qk ] is maximized ; Initialize the maximum with the smallest value ; Initialize the sum array with zero ; Iterate from the right ; Update the sum starting at the current element ; Update the maximum so far ; Driver code"
Solution in php:

< ? php function maxSum ( $ arr , $ n , $ K ) { $ maximum = PHP_INT_MIN ; $ sum = array ( $ n ) ; for ( $ i = $ n - 1 ; $ i >= 0 ; $ i -- ) { if ( $ i + $ K < $ n ) $ sum [ $ i ] = $ sum [ $ i + $ K ] + $ arr [ $ i ] ; else $ sum [ $ i ] = $ arr [ $ i ] ; $ maximum = max ( $ maximum , $ sum [ $ i ] ) ; } return $ maximum ; } { $ arr = array ( 3 , 6 , 4 , 7 , 2 ) ; $ n = sizeof ( $ arr ) ; $ K = 2 ; echo ( maxSum ( $ arr , $ n , $ K ) ) ; }


"Find maximum value of Sum ( i * arr [ i ] ) with only rotations on given array allowed | Returns max possible value of i * arr [ i ] ; Find array sum and i * arr [ i ] with no rotation Stores sum of arr [ i ] ; Stores sum of i * arr [ i ] ; Initialize result as 0 rotation sum ; Try all rotations one by one and find the maximum rotation sum . ; Return result ; Driver Code"
Solution in php:

< ? php function maxSum ( $ arr , $ n ) { $ arrSum = 0 ; $ currVal = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ arrSum = $ arrSum + $ arr [ $ i ] ; $ currVal = $ currVal + ( $ i * $ arr [ $ i ] ) ; } $ maxVal = $ currVal ; for ( $ j = 1 ; $ j < $ n ; $ j ++ ) { $ currVal = $ currVal + $ arrSum - $ n * $ arr [ $ n - $ j ] ; if ( $ currVal > $ maxVal ) $ maxVal = $ currVal ; } return $ maxVal ; } $ arr = array ( 10 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ) ; $ n = sizeof ( $ arr ) ; echo " Max ▁ sum ▁ is ▁ " , maxSum ( $ arr , $ n ) ; ? >


"Find maximum value of x such that n ! % ( k ^ x ) = 0 | Function to maximize the value of x such that n ! % ( k ^ x ) = 0 ; Find square root of k and add 1 to it ; Run the loop from 2 to m and k should be greater than 1 ; optimize the value of k ; Minimum store ; Driver Code"
Solution in php:


"Find maximum volume of a cuboid from the given perimeter and area | function to return maximum volume ; calculate length ; calculate volume ; return result ; Driver code ; Function call"
Solution in php:


"Find middle point segment from given segment lengths | Function that returns the segment for the middle point ; the middle point ; stores the segment index ; increment sum by length of the segment ; if the middle is in between two segments ; if sum is greater than middle point ; Driver code"
Solution in php:

< ? php function findSegment ( $ n , $ m , $ segment_length ) { $ meet_point = ( 1.0 * $ n ) / 2.0 ; $ sum = 0 ; $ segment_number = 0 ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) { $ sum += $ segment_length [ $ i ] ; if ( ( double ) $ sum == $ meet_point ) { $ segment_number = -1 ; break ; } if ( $ sum > $ meet_point ) { $ segment_number = $ i + 1 ; break ; } } return $ segment_number ; } $ n = 13 ; $ m = 3 ; $ segment_length = array ( 3 , 2 , 8 ) ; $ ans = findSegment ( $ n , $ m , $ segment_length ) ; echo ( $ ans ) ; ? >


"Find minimum difference between any two elements | Returns minimum difference between any pair ; Initialize difference as infinite ; Find the min diff by comparing difference of all possible pairs in given array ; Return min diff ; Driver code"
Solution in php:

< ? php function findMinDiff ( $ arr , $ n ) { $ diff = PHP_INT_MAX ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) if ( abs ( $ arr [ $ i ] - $ arr [ $ j ] ) < $ diff ) $ diff = abs ( $ arr [ $ i ] - $ arr [ $ j ] ) ; return $ diff ; } $ arr = array ( 1 , 5 , 3 , 19 , 18 , 25 ) ; $ n = sizeof ( $ arr ) ; echo " Minimum ▁ difference ▁ is ▁ " , findMinDiff ( $ arr , $ n ) ; ? >


"Find minimum difference between any two elements | Returns minimum difference between any pair ; Sort array in non - decreasing order ; Initialize difference as infinite ; Find the min diff by comparing adjacent pairs in sorted array ; Return min diff ; Driver code"
Solution in php:

< ? php function findMinDiff ( $ arr , $ n ) { sort ( $ arr ) ; $ diff = PHP_INT_MAX ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) if ( $ arr [ $ i + 1 ] - $ arr [ $ i ] < $ diff ) $ diff = $ arr [ $ i + 1 ] - $ arr [ $ i ] ; return $ diff ; } $ arr = array ( 1 , 5 , 3 , 19 , 18 , 25 ) ; $ n = sizeof ( $ arr ) ; echo " Minimum ▁ difference ▁ is ▁ " , findMinDiff ( $ arr , $ n ) ; ? >


"Find minimum moves to reach target on an infinite line | PHP program to find minimum moves to reach target if we can move i steps in i - th move . ; Handling negatives by symmetry ; Keep moving while sum is smaller or difference is odd . ; Driver code"
Solution in php:

< ? php function reachTarget ( $ target ) { $ target = abs ( $ target ) ; $ sum = 0 ; $ step = 0 ; while ( $ sum < $ target or ( $ sum - $ target ) % 2 != 0 ) { $ step ++ ; $ sum += $ step ; } return $ step ; } $ target = 5 ; echo reachTarget ( $ target ) ; ? >


"Find minimum number of coins that make a given value | m is size of coins array ( number of different coins ) ; base case ; Initialize result ; Try every coin that has smaller value than V ; Check for INT_MAX to avoid overflow and see if result can minimized ; Driver Code"
Solution in php:

< ? php function minCoins ( $ coins , $ m , $ V ) { if ( $ V == 0 ) return 0 ; $ res = PHP_INT_MAX ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) { if ( $ coins [ $ i ] <= $ V ) { $ sub_res = minCoins ( $ coins , $ m , $ V - $ coins [ $ i ] ) ; if ( $ sub_res != PHP_INT_MAX && $ sub_res + 1 < $ res ) $ res = $ sub_res + 1 ; } } return $ res ; } $ coins = array ( 9 , 6 , 5 , 1 ) ; $ m = sizeof ( $ coins ) ; $ V = 11 ; echo " Minimum ▁ coins ▁ required ▁ is ▁ " , minCoins ( $ coins , $ m , $ V ) ; ? >


"Find minimum number of coins that make a given value | m is size of coins array ( number of different coins ) ; table [ i ] will be storing the minimum number of coins required for i value . So table [ V ] will have result ; Base case ( If given value V is 0 ) ; Initialize all table values as Infinite ; Compute minimum coins required for all values from 1 to V ; Go through all coins smaller than i ; Driver Code"
Solution in php:

< ? php function minCoins ( $ coins , $ m , $ V ) { $ table [ $ V + 1 ] = array ( ) ; $ table [ 0 ] = 0 ; for ( $ i = 1 ; $ i <= $ V ; $ i ++ ) $ table [ $ i ] = PHP_INT_MAX ; for ( $ i = 1 ; $ i <= $ V ; $ i ++ ) { for ( $ j = 0 ; $ j < $ m ; $ j ++ ) if ( $ coins [ $ j ] <= $ i ) { $ sub_res = $ table [ $ i - $ coins [ $ j ] ] ; if ( $ sub_res != PHP_INT_MAX && $ sub_res + 1 < $ table [ $ i ] ) $ table [ $ i ] = $ sub_res + 1 ; } } if ( $ table [ $ V ] == PHP_INT_MAX ) return -1 ; return $ table [ $ V ] ; } $ coins = array ( 9 , 6 , 5 , 1 ) ; $ m = sizeof ( $ coins ) ; $ V = 11 ; echo " Minimum ▁ coins ▁ required ▁ is ▁ " , minCoins ( $ coins , $ m , $ V ) ; ? >


"Find minimum number of currency notes and values that sum to given amount | function to count and prcurrency notes ; count notes using Greedy approach ; Print notes ; Driver Code"
Solution in php:

 < ? php function countCurrency ( $ amount ) { $ notes = array ( 2000 , 500 , 200 , 100 , 50 , 20 , 10 , 5 , 1 ) ; $ noteCounter = array ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ) ; for ( $ i = 0 ; $ i < 9 ; $ i ++ ) { if ( $ amount >= $ notes [ $ i ] ) { $ noteCounter [ $ i ] = intval ( $ amount / $ notes [ $ i ] ) ; $ amount = $ amount - $ noteCounter [ $ i ] * $ notes [ $ i ] ; } } echo ( " Currency ▁ Count ▁ - > " . "
 " ) ; for ( $ i = 0 ; $ i < 9 ; $ i ++ ) { if ( $ noteCounter [ $ i ] != 0 ) { echo ( $ notes [ $ i ] . " : " ▁ . ▁ $ noteCounter [ $ i ] ▁ . ▁ " " } } } $ amount = 868 ; countCurrency ( $ amount ) ; ? >


"Find minimum number of merge operations to make an array palindrome | Returns minimum number of count operations required to make arr [ ] palindrome ; Initialize result ; Start from two corners ; If corner elements are same , problem reduces arr [ i + 1. . j - 1 ] ; If left element is greater , then we merge right two elements ; need to merge from tail . ; Else we merge left two elements ; Driver Code"
Solution in php:

< ? php function findMinOps ( $ arr , $ n ) { $ ans = 1 ; for ( $ i = 0 , $ j = $ n - 1 ; $ i <= $ j { if ( $ arr [ $ i ] == $ arr [ $ j ] ) { $ i ++ ; $ j -- ; } else if ( $ arr [ $ i ] > $ arr [ $ j ] ) { $ j -- ; $ arr [ $ j ] += $ arr [ $ j + 1 ] ; $ ans ++ ; } else { $ i ++ ; $ arr [ $ i ] += $ arr [ $ i - 1 ] ; $ ans ++ ; } } return $ ans ; } $ arr [ ] = array ( 1 , 4 , 5 , 9 , 1 ) ; $ n = sizeof ( $ arr ) ; echo " Count ▁ of ▁ minimum ▁ operations ▁ is ▁ " , findMinOps ( $ arr , $ n ) ; ? >


"Find minimum number to be divided to make a number a perfect square | Return the minimum number to be divided to make n a perfect square . ; Since 2 is only even prime , compute its power separately . ; If count is odd , it must be removed by dividing n by prime number . ; If count is odd , it must be removed by dividing n by prime number . ; Driver Code"
Solution in php:

 < ? php function findMinNumber ( $ n ) { $ count = 0 ; $ ans = 1 ; while ( $ n % 2 == 0 ) { $ count ++ ; $ n /= 2 ; } if ( $ count % 2 ) $ ans *= 2 ; for ( $ i = 3 ; $ i <= sqrt ( $ n ) ; $ i += 2 ) { $ count = 0 ; while ( $ n % $ i == 0 ) { $ count ++ ; $ n /= $ i ; } if ( $ count % 2 ) $ ans *= $ i ; } if ( $ n > 2 ) $ ans *= $ n ; return $ ans ; } $ n = 72 ; echo findMinNumber ( $ n ) , "
 " ; ? >


"Find minimum operations needed to make an Array beautiful | Function to find minimum operations required to make array beautiful ; counting consecutive zeros . ; check that start and end are same ; check is zero and one are equal ; Driver Code"
Solution in php:

< ? php function minOperations ( $ A , $ n ) { if ( $ n & 1 ) return -1 ; $ zeros = 0 ; $ consZeros = 0 ; $ ones = 0 ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { $ A [ $ i ] == 0 ? $ zeros ++ : $ ones ++ ; if ( ( $ i + 1 ) < $ n ) { if ( $ A [ $ i ] == 0 && $ A [ $ i + 1 ] == 0 ) $ consZeros ++ ; } } if ( $ A [ 0 ] == $ A [ $ n - 1 ] && $ A [ 0 ] == 0 ) $ consZeros ++ ; if ( $ zeros == $ ones ) return $ consZeros ; else return -1 ; } $ A = array ( 1 , 1 , 0 , 0 ) ; $ n = sizeof ( $ A ) ; echo minOperations ( $ A , $ n ) ; ? >


"Find minimum positive integer x such that a ( x ^ 2 ) + b ( x ) + c >= k | Function to return the minimum positive integer satisfying the given equation ; Binary search to find the value of x ; Return the answer ; Driver code"
Solution in php:


"Find minimum radius such that atleast k point lie inside the circle | Return minimum distance required so that aleast k point lie inside the circle . ; Finding distance between of each point from origin ; Sorting the distance ; Driver Code"
Solution in php:


"Find minimum sum of factors of number | To find minimum sum of product of number ; Find factors of number and add to the sum ; Return sum of numbers having minimum product ; Driver Code"
Solution in php:

< ? php function findMinSum ( $ num ) { $ sum = 0 ; for ( $ i = 2 ; $ i * $ i <= $ num ; $ i ++ ) { while ( $ num % $ i == 0 ) { $ sum += $ i ; $ num /= $ i ; } } $ sum += $ num ; return $ sum ; } $ num = 12 ; echo ( findMinSum ( $ num ) ) ; ? >


"Find minimum sum such that one of every three consecutive elements is taken | function to find minimum of 3 elements ; Returns minimum possible sum of elements such that an element out of every three consecutive elements is picked . ; Create a DP table to store results of subproblems . sum [ i ] is going to store minimum possible sum when arr [ i ] is part of the solution . ; When there are less than or equal to 3 elements ; Iterate through all other elements ; Driver code"
Solution in php:

< ? php function minimum ( $ a , $ b , $ c ) { return min ( min ( $ a , $ b ) , $ c ) ; } function findMinSum ( $ arr , $ n ) { $ sum [ $ n ] = 0 ; $ sum [ 0 ] = $ arr [ 0 ] ; $ sum [ 1 ] = $ arr [ 1 ] ; $ sum [ 2 ] = $ arr [ 2 ] ; for ( $ i = 3 ; $ i < $ n ; $ i ++ ) $ sum [ $ i ] = $ arr [ $ i ] + minimum ( $ sum [ $ i - 3 ] , $ sum [ $ i - 2 ] , $ sum [ $ i - 1 ] ) ; return minimum ( $ sum [ $ n - 1 ] , $ sum [ $ n - 2 ] , $ sum [ $ n - 3 ] ) ; } $ arr = array ( 1 , 2 , 3 , 20 , 2 , 10 , 1 ) ; $ n = sizeof ( $ arr ) ; echo " Min ▁ Sum ▁ is ▁ " , findMinSum ( $ arr , $ n ) ; ? >


"Find minimum value to assign all array elements so that array product becomes greater | PHP program to find minimum value that can be assigned to all elements so that product becomes greater than current product . ; sort the array to apply Binary search ; using log property add every logarithmic value of element to val $val = 0 ; where ld is long double ; set left and right extremities to find min value ; multiplying n to mid , to find the correct min value ; Driver code"
Solution in php:

 < ? php function findMinValue ( $ arr , $ n ) { sort ( $ arr ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ val += ( log ( $ arr [ $ i ] ) ) ; $ left = $ arr [ 0 ] ; $ right = $ arr [ $ n - 1 ] + 1 ; $ ans = 0 ; while ( $ left <= $ right ) { $ mid = ( int ) ( $ left + $ right ) / 2 ; $ temp = $ n * ( log ( $ mid ) ) ; if ( $ val < $ temp ) { $ ans = $ mid ; $ right = $ mid - 1 ; } else $ left = $ mid + 1 ; } return ( floor ( $ ans ) ) ; } $ arr = array ( 4 , 2 , 1 , 10 , 6 ) ; $ n = sizeof ( $ arr ) ; echo findMinValue ( $ arr , $ n ) , "
 " ; ? >


"Find minimum x such that ( x % k ) * ( x / k ) == n | Set | Function gives the required answer ; Iterate for all the factors ; Check if i is a factor ; Consider i to be A and n / i to be B ; Consider i to be B and n / i to be A ; Driver Code"
Solution in php:

 < ? php function minimumX ( $ n , $ k ) { $ mini = PHP_INT_MAX ; for ( $ i = 1 ; $ i * $ i <= $ n ; $ i ++ ) { if ( $ n % $ i == 0 ) { $ fir = $ i ; $ sec = ( int ) $ n / $ i ; $ num1 = $ fir * $ k + $ sec ; $ res = ( int ) ( $ num1 / $ k ) * ( $ num1 % $ k ) ; if ( $ res == $ n ) $ mini = min ( $ num1 , $ mini ) ; $ num2 = $ sec * $ k + $ fir ; $ res = ( int ) ( $ num2 / $ k ) * ( $ num2 % $ k ) ; if ( $ res == $ n ) $ mini = min ( $ num2 , $ mini ) ; } } return $ mini ; } $ n = 4 ; $ k = 6 ; echo minimumX ( $ n , $ k ) , "
 " ; $ n = 5 ; $ k = 5 ; echo minimumX ( $ n , $ k ) , "
 " ; ? >


"Find minimum x such that ( x % k ) * ( x / k ) == n | This function gives the required answer ; Iterate over all possible remainders ; it must divide n ; Driver Code"
Solution in php:

 < ? php function minimumX ( $ n , $ k ) { $ ans = PHP_INT_MAX ; for ( $ rem = $ k - 1 ; $ rem > 0 ; $ rem -- ) { if ( $ n % $ rem == 0 ) $ ans = min ( $ ans , $ rem + ( $ n / $ rem ) * $ k ) ; } return $ ans ; } $ n = 4 ; $ k = 6 ; echo minimumX ( $ n , $ k ) , "
 " ; $ n = 5 ; $ k = 5 ; echo minimumX ( $ n , $ k ) ; ? >


"Find missing element in a sorted array of consecutive numbers | Function to return the missing element ; Check if middle element is consistent ; No inconsistency till middle elements When missing element is just after the middle element ; Move right ; Inconsistency found When missing element is just before the middle element ; Move left ; No missing element found ; Driver code"
Solution in php:

< ? php function findMissing ( $ arr , $ n ) { $ l = 0 ; $ h = $ n - 1 ; while ( $ h > $ l ) { $ mid = floor ( $ l + ( $ h - $ l ) / 2 ) ; if ( $ arr [ $ mid ] - $ mid == $ arr [ 0 ] ) { if ( $ arr [ $ mid + 1 ] - $ arr [ $ mid ] > 1 ) return $ arr [ $ mid ] + 1 ; else { $ l = $ mid + 1 ; } } else { if ( $ arr [ $ mid ] - $ arr [ $ mid - 1 ] > 1 ) return $ arr [ $ mid ] - 1 ; else { $ h = $ mid - 1 ; } } } return -1 ; } $ arr = array ( -9 , -8 , -7 , -5 , - 4 , -3 , -2 , -1 , 0 ) ; $ n = count ( $ arr ) ; echo findMissing ( $ arr , $ n ) ; ? >


"Find missing number in another array which is shuffled copy | Returns the missing number Size of arr2 [ ] is n - 1 ; Missing number ' mnum ' ; 1 st array is of size ' n ' ; 2 nd array is of size ' n ▁ - ▁ 1' ; Required missing number ; Driver Code"
Solution in php:

< ? php function missingNumber ( $ arr1 , $ arr2 , $ n ) { $ mnum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ mnum = $ mnum ^ $ arr1 [ $ i ] ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) $ mnum = $ mnum ^ $ arr2 [ $ i ] ; return $ mnum ; } $ arr1 = array ( 4 , 8 , 1 , 3 , 7 ) ; $ arr2 = array ( 7 , 4 , 3 , 1 ) ; $ n = count ( $ arr1 ) ; echo " Missing ▁ number ▁ = ▁ " , missingNumber ( $ arr1 , $ arr2 , $ n ) ; ? >


"Find multiple of x closest to or a ^ b ( a raised to power b ) | function to find closest multiple of x to a ^ b ; calculate a ^ b / x ; Answer is either ( ans * x ) or ( ans + 1 ) * x ; Printing nearest answer ; Driver Code"
Solution in php:


"Find n positive integers that satisfy the given equations | Function to find n positive integers that satisfy the given conditions ; To store n positive integers ; Place N - 1 one 's ; If can not place ( y - ( n - 1 ) ) as the Nth integer ; Place Nth integer ; To store the sum of squares of N integers ; If it is less than x ; Print the required integers ; Driver code"
Solution in php:


"Find n | Definition of findNumber function ; Finding x from equation n = x ( x + 1 ) / 2 + 1 ; Base of current block ; Value of n - th element ; Driver code"
Solution in php:

< ? php function findNumber ( $ n ) { $ x = floor ( ( -1 + sqrt ( 1 + 8 * $ n - 8 ) ) / 2 ) ; $ base = ( $ x * ( $ x + 1 ) ) / 2 + 1 ; return $ n - $ base + 1 ; } $ n = 55 ; echo findNumber ( $ n ) ; ? >


"Find n | Function to find nth term ; Driver code"
Solution in php:

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


"Find n | Function to find the nth term of series ; Loop to add numbers ; Driver code"
Solution in php:

< ? php function term ( $ n ) { $ ans = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ ans += $ i ; return $ ans ; } $ n = 4 ; echo ( term ( $ n ) ) ; ? >


"Find n | PHP program to find n - th Fortunate number ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to Find primorial of order n ( product of first n prime numbers ) . ; Function to find next prime number greater than n ; Note that difference ( or m ) should be greater than 1. ; loop continuously until isPrime returns true for a number above n ; Ignoring the prime number that is 1 greater than n ; Returns n - th Fortunate number ; Driver Code"
Solution in php:

 < ? php function isPrime ( $ n ) { if ( $ n <= 1 ) return false ; if ( $ n <= 3 ) return true ; if ( $ n % 2 == 0 $ n % 3 == 0 ) return false ; for ( $ i = 5 ; $ i * $ i <= $ n ; $ i = $ i + 6 ) if ( $ n % $ i == 0 || $ n % ( $ i + 2 ) == 0 ) return false ; return true ; } function primorial ( $ n ) { $ p = 2 ; $ n -- ; for ( $ i = 3 ; $ n != 0 ; $ i ++ ) { if ( isPrime ( $ i ) ) { $ p = $ p * $ i ; $ n -- ; } $ i ++ ; } return $ p ; } function findNextPrime ( $ n ) { $ nextPrime = $ n + 2 ; while ( true ) { if ( isPrime ( $ nextPrime ) ) break ; $ nextPrime ++ ; } return $ nextPrime ; } function fortunateNumber ( $ n ) { $ p = primorial ( $ n ) ; return findNextPrime ( $ p ) - $ p ; } $ n = 5 ; echo fortunateNumber ( $ n ) , "
 " ; ? >


"Find n | PHP program to find n - th number containing only 4 and 7. ; If n is odd , append 4 and move to parent ; If n is even , append 7 and move to parent ; Reverse res and return . ; Driver code"
Solution in php:

< ? php function findNthNo ( $ n ) { $ res = " " ; while ( $ n >= 1 ) { if ( $ n & 1 ) { $ res = $ res . "4" ; $ n = ( int ) ( ( $ n - 1 ) / 2 ) ; } else { $ res = $ res . "7" ; $ n = ( int ) ( ( $ n - 2 ) / 2 ) ; } } return strrev ( $ res ) ; } $ n = 13 ; echo findNthNo ( $ n ) ; ? >


"Find n | Return n - th number in series made of 4 and 7 ; create an array of size ( n + 1 ) ; If i is odd ; Driver code"
Solution in php:


"Find n | Returns n - th element of the series ; Driver Code"
Solution in php:

< ? php function series ( $ n ) { return ( 8 * $ n * $ n ) + 1 ; } $ n = 5 ; echo ( series ( $ n ) ) ; ? >


"Find n | Returns n - th number in sequence 1 , 1 , 2 , 1 , 2 , 3 , 1 , 2 , 4 , ... ; One by one subtract counts elements in different blocks ; Driver code"
Solution in php:

< ? php function findNumber ( $ n ) { $ n -- ; $ i = 1 ; while ( $ n >= 0 ) { $ n -= $ i ; ++ $ i ; } return ( $ n + $ i ) ; } $ n = 3 ; echo findNumber ( $ n ) ; ? >


"Find n | func for calualtion ; for summation of square of first n - natural nos . ; summation of first n natural nos . ; return result ; Driver Code"
Solution in php:

< ? php function seriesFunc ( $ n ) { $ sumSquare = ( $ n * ( $ n + 1 ) * ( 2 * $ n + 1 ) ) / 6 ; $ sumNatural = ( $ n * ( $ n + 1 ) / 2 ) ; return ( $ sumSquare + $ sumNatural + 1 ) ; } $ n = 8 ; echo ( seriesFunc ( $ n ) . " " ) ; $ n = 13 ; echo ( seriesFunc ( $ n ) . " " ) ; ? >


"Find n | function to find nth stern ' diatomic series ; SET the Base case ; Traversing the array from 2 nd Element to nth Element ; Case 1 : for even n ; Case 2 : for odd n ; Driver Code"
Solution in php:


"Find n | function to solve the quadratic equation ; calculating the Nth term ; Driver Code"
Solution in php:

< ? php function term ( $ n ) { $ x = ( ( ( 1 ) + ( double ) sqrt ( 1 + ( 8 * $ n ) ) ) / 2 ) ; return $ x ; } $ n = 5 ; echo ( ( int ) term ( $ n ) ) ; ? >


"Find n | utility function ; since first element of the series is 7 , we initialise a variable with 7 ; Using iteration to find nth term ; Driver Code"
Solution in php:

< ? php function findTerm ( $ n ) { if ( $ n == 1 ) return $ n ; else { $ term = 7 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ term = $ term * 2 + ( $ i - 1 ) ; return $ term ; } } $ n = 5 ; echo ( findTerm ( $ n ) ) ; ? >


"Find next palindrome prime | PHP program to find next palindromic prime for a given number . ; if ( 8 <= N <= 11 ) return 11 ; generate odd length palindrome number which will cover given constraint . ; if y >= N and it is a prime number then return it . ; Driver code"
Solution in php:

< ? php function isPrime ( $ num ) { if ( $ num < 2 $ num % 2 == 0 ) return $ num == 2 ; for ( $ i = 3 ; $ i * $ i <= $ num ; $ i += 2 ) if ( $ num % $ i == 0 ) return false ; return true ; } function primePalindrome ( $ N ) { if ( 8 <= $ N && $ N <= 11 ) return 11 ; for ( $ x = 1 ; $ x < 100000 ; ++ $ x ) { $ s = strval ( $ x ) ; $ r = strrev ( $ s ) ; $ y = intval ( $ s . substr ( $ r , 1 ) ) ; if ( $ y >= $ N && isPrime ( $ y ) == true ) return $ y ; } return -1 ; } print ( primePalindrome ( 112 ) ) ; ? >


"Find nth Fibonacci number using Golden ratio | Approximate value of golden ratio ; Function to find nth Fibonacci number ; Fibonacci numbers for n < 6 ; Else start counting from 5 th term ; Driver Code"
Solution in php:

 < ? php $ PHI = 1.6180339 ; function fib ( $ n ) { global $ PHI ; $ f = array ( 0 , 1 , 1 , 2 , 3 , 5 ) ; if ( $ n < 6 ) return $ f [ $ n ] ; $ t = 5 ; $ fn = 5 ; while ( $ t < $ n ) { $ fn = round ( $ fn * $ PHI ) ; $ t ++ ; } return $ fn ; } $ n = 9 ; echo $ n , " th ▁ Fibonacci ▁ Number ▁ = ▁ " , fib ( $ n ) , "
 " ; ? >


"Find nth Hermite number | Function to return nth Hermite number ; Base conditions ; Driver Code ; Print nth Hermite number"
Solution in php:

< ? php function getHermiteNumber ( $ n ) { if ( $ n == 0 ) return 1 ; if ( $ n == 1 ) return 0 ; else return -2 * ( $ n - 1 ) * getHermiteNumber ( $ n - 2 ) ; } $ n = 6 ; echo getHermiteNumber ( $ n ) ; ? >


"Find nth Hermite number | Utility function to calculate double factorial of a number ; Function to return nth Hermite number ; If n is even then return 0 ; If n is odd ; Calculate double factorial of ( n - 1 ) and multiply it with 2 ^ ( n / 2 ) ; If n / 2 is odd then nth Hermite number will be negative ; Return nth Hermite number ; Driver Code ; Print nth Hermite number"
Solution in php:

< ? php function doubleFactorial ( $ n ) { $ fact = 1 ; for ( $ i = 1 ; $ i <= $ n ; $ i = $ i + 2 ) { $ fact = $ fact * $ i ; } return $ fact ; } function hermiteNumber ( $ n ) { if ( $ n % 2 == 1 ) return 0 ; else { $ number = ( pow ( 2 , $ n / 2 ) ) * doubleFactorial ( $ n - 1 ) ; if ( ( $ n / 2 ) % 2 == 1 ) $ number = $ number * -1 ; return $ number ; } } $ n = 6 ; echo hermiteNumber ( $ n ) ; ? >


"Find nth Magic Number | Function to find nth magic number ; Go through every bit of n ; If last bit of n is set ; proceed to next bit $n >>= 1 ; or $n = $n / 2 ; Driver Code"
Solution in php:

 < ? php function nthMagicNo ( $ n ) { $ pow = 1 ; $ answer = 0 ; while ( $ n ) { $ pow = $ pow * 5 ; if ( $ n & 1 ) $ answer += $ pow ; } return $ answer ; } $ n = 5 ; echo " nth ▁ magic ▁ number ▁ is ▁ " , nthMagicNo ( $ n ) , "
 " ; ? >


"Find nth term of a given recurrence relation | function to return required value ; Get the answer ; Return the answer ; Get the value of n ; function call to print result"
Solution in php:

< ? php function sum ( $ n ) { $ ans = ( $ n * ( $ n - 1 ) ) / 2 ; return $ ans ; } $ n = 5 ; echo sum ( $ n ) ; ? >


"Find nth term of the Dragon Curve Sequence | function to generate the nth term ; first term ; generating each term of the sequence ; loop to generate the ith term ; add character from the original string ; add alternate 0 and 1 in between ; if previous added term was '0' then add '1' ; now current term becomes previous term ; if previous added term was '1' , then add '0' ; now current term becomes previous term ; s becomes the ith term of the sequence ; Taking inputs ; generate nth term of dragon curve sequence ; Printing output"
Solution in php:

 < ? php function Dragon_Curve_Sequence ( $ n ) { $ s = "1" ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { $ temp = "1" ; $ prev = '1' ; $ zero = '0' ; $ one = '1' ; for ( $ j = 0 ; $ j < strlen ( $ s ) ; $ j ++ ) { $ temp . = $ s [ $ j ] ; if ( $ prev == '0' ) { $ temp . = $ one ; $ prev = $ one ; } else { $ temp . = $ zero ; $ prev = $ zero ; } } $ s = $ temp ; } return $ s ; } $ n = 4 ; $ s = Dragon_Curve_Sequence ( $ n ) ; echo $ s . "
 " ; ? >


"Find nth term of the series 5 2 13 41 | function to calculate nth term of the series ; if n is even number ; if n is odd number ; return nth term ; Driver code"
Solution in php:

 < ? php function nthTermOfTheSeries ( $ n ) { if ( $ n % 2 == 0 ) $ nthTerm = pow ( $ n - 1 , 2 ) + $ n ; else $ nthTerm = pow ( $ n + 1 , 2 ) + $ n ; return $ nthTerm ; } $ n = 8 ; echo nthTermOfTheSeries ( $ n ) . "
 " ; $ n = 12 ; echo nthTermOfTheSeries ( $ n ) . "
 " ; $ n = 102 ; echo nthTermOfTheSeries ( $ n ) . "
 " ; $ n = 999 ; echo nthTermOfTheSeries ( $ n ) . "
 " ; $ n = 9999 ; echo nthTermOfTheSeries ( $ n ) . "
 " ; ? >


"Find number from given list for which value of the function is closest to A | Function to find number from given list for which value of the function is closest to A ; Stores the final index ; Declaring a variable to store the minimum absolute difference ; Finding F ( n ) ; Updating the index of the answer if new absolute difference is less than tmp ; Driver Code"
Solution in php:


"Find number of endless points | Returns count of endless points ; Fills column matrix . For every column , start from every last row and fill every entry as blockage after a 0 is found . ; flag which will be zero once we get a '0' and it will be 1 otherwise ; encountered a '0' , set the isEndless variable to false ; Similarly , fill row matrix ; Calculate total count of endless points ; If there is NO blockage or column after this point , increment result . ; Driver code"
Solution in php:


"Find number of magical pairs of string of length L | Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; Update x if it is >= p ; If y is odd , multiply x with result ; Y must be even now $y = $y >> 1 ; y = y / 2 ; Driver Code"
Solution in php:


"Find number of solutions of a linear equation of n variables | Recursive function that returns count of solutions for given rhs value and coefficients coeff [ start . . end ] ; Base case ; Initialize count of solutions ; One by subtract all smaller or equal coefficiants and recur ; Driver Code"
Solution in php:

< ? php function countSol ( $ coeff , $ start , $ end , $ rhs ) { if ( $ rhs == 0 ) return 1 ; $ result = 0 ; for ( $ i = $ start ; $ i <= $ end ; $ i ++ ) if ( $ coeff [ $ i ] <= $ rhs ) $ result += countSol ( $ coeff , $ i , $ end , $ rhs - $ coeff [ $ i ] ) ; return $ result ; } $ coeff = array ( 2 , 2 , 5 ) ; $ rhs = 4 ; $ n = sizeof ( $ coeff ) ; echo countSol ( $ coeff , 0 , $ n - 1 , $ rhs ) ; ? >


"Find number of solutions of a linear equation of n variables | Returns count of solutions for given rhs and coefficients coeff [ 0. . n - 1 ] ; Create and initialize a table to store results of subproblems ; Fill table in bottom up manner ; Driver Code"
Solution in php:

< ? php function countSol ( $ coeff , $ n , $ rhs ) { $ dp = str_repeat ( " \0" , 256 ) ; $ dp [ 0 ] = 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = $ coeff [ $ i ] ; $ j <= $ rhs ; $ j ++ ) $ dp [ $ j ] = $ dp [ $ j ] + ( $ dp [ $ j - $ coeff [ $ i ] ] ) ; return $ dp [ $ rhs ] ; } $ coeff = array ( 2 , 2 , 5 ) ; $ rhs = 4 ; $ n = sizeof ( $ coeff ) / sizeof ( $ coeff [ 0 ] ) ; echo countSol ( $ coeff , $ n , $ rhs ) ; ? >


"Find number of subarrays with even sum | PHP program to count number of sub - arrays whose sum is even using brute force Time Complexity - O ( N ^ 2 ) Space Complexity - O ( 1 ) ; Find sum of all subarrays and increment result if sum is even ; Driver code"
Solution in php:

< ? php function countEvenSum ( $ arr , $ n ) { $ result = 0 ; for ( $ i = 0 ; $ i <= $ n - 1 ; $ i ++ ) { $ sum = 0 ; for ( $ j = $ i ; $ j <= $ n - 1 ; $ j ++ ) { $ sum = $ sum + $ arr [ $ j ] ; if ( $ sum % 2 == 0 ) $ result ++ ; } } return ( $ result ) ; } $ arr = array ( 1 , 2 , 2 , 3 , 4 , 1 ) ; $ n = sizeof ( $ arr ) ; echo " The ▁ Number ▁ of ▁ Subarrays ▁ " , " with ▁ even ▁ sum ▁ is ▁ " , countEvenSum ( $ arr , $ n ) ; ? >


"Find number of subarrays with even sum | PHP program to count number of sub - arrays with even sum using an efficient algorithm Time Complexity - O ( N ) Space Complexity - O ( 1 ) ; A temporary array of size 2. temp [ 0 ] is going to store count of even subarrays and temp [ 1 ] count of odd . temp [ 0 ] is initialized as 1 because there a single even element is also counted as a subarray ; Initialize count . sum is sum of elements under modulo 2 and ending with arr [ i ] . ; i ' th ▁ iteration ▁ computes ▁ ▁ sum ▁ of ▁ arr [ 0 . . i ] ▁ under ▁ ▁ modulo ▁ 2 ▁ and ▁ increments ▁ ▁ even / odd ▁ count ▁ according ▁ ▁ to ▁ sum ' s value ; 2 is added to handle negative numbers ; Increment even / odd count ; Use handshake lemma to count even subarrays ( Note that an even can be formed by two even or two odd ) ; Driver code"
Solution in php:

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


"Find number of times a string occurs as a subsequence in given string | Iterative DP function to find the number of times the second string occurs in the first string , whether continuous or discontinuous ; Create a table to store results of sub - problems ; If second string is empty ; Fill lookup [ ] [ ] in bottom up manner ; If last characters are same , we have two options - 1. consider last characters of both strings in solution 2. ignore last character of first string ; If last character are different , ignore last character of first string ; Driver code"
Solution in php:


"Find number of times a string occurs as a subsequence in given string | Recursive function to find the number of times the second string occurs in the first string , whether continuous or discontinuous ; If both first and second string is empty , or if second string is empty , return 1 ; If only first string is empty and second string is not empty , return 0 ; If last characters are same Recur for remaining strings by 1. considering last characters of both strings 2. ignoring last character of first string ; If last characters are different , ignore last char of first string and recur for remaining string ; Driver code"
Solution in php:

< ? php function count_1 ( $ a , $ b , $ m , $ n ) { if ( ( $ m == 0 && $ n == 0 ) $ n == 0 ) return 1 ; if ( $ m == 0 ) return 0 ; if ( $ a [ $ m - 1 ] == $ b [ $ n - 1 ] ) return count_1 ( $ a , $ b , $ m - 1 , $ n - 1 ) + count_1 ( $ a , $ b , $ m - 1 , $ n ) ; else return count_1 ( $ a , $ b , $ m - 1 , $ n ) ; } $ a = " GeeksforGeeks " ; $ b = " Gks " ; echo count_1 ( $ a , $ b , strlen ( $ a ) , strlen ( $ b ) ) . " " ; return 0 ; ? >


"Find number of transformation to make two Matrix Equal | PHP program to find number of countOpsation to make two matrix equals ; Update matrix A [ ] [ ] so that only A [ ] [ ] has to be countOpsed ; Check necessary condition for condition for existence of full countOpsation ; If countOpsation is possible calculate total countOpsation ; Driver code"
Solution in php:


"Find numbers a and b that satisfy the given conditions | Function to print the required numbers ; Suppose b = n and we want a % b = 0 and also ( a / b ) < n so a = b * ( n - 1 ) ; Special case if n = 1 we get a = 0 so ( a * b ) < n ; If no pair satisfies the conditions ; Driver code"
Solution in php:


"Find numbers of balancing positions in string | PHP program to find number of balancing points in string ; function to return number of balancing points ; hash array for storing hash of string initialized by 0 being global ; process string initially for rightVisited ; check for balancing points ; for every position inc left hash & dec rightVisited ; check whether both hash have same character or not ; Either both leftVisited [ j ] and rightVisited [ j ] should have none zero value or both should have zero value ; if both have same character increment count ; Driver Code"
Solution in php:

< ? php $ MAX_CHAR = 256 ; function countBalance ( $ str ) { global $ MAX_CHAR ; $ leftVisited = array_fill ( 0 , $ MAX_CHAR , NULL ) ; $ rightVisited = array_fill ( 0 , $ MAX_CHAR , NULL ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ rightVisited [ ord ( $ str [ $ i ] ) ] ++ ; $ res = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ leftVisited [ ord ( $ str [ $ i ] ) ] ++ ; $ rightVisited [ ord ( $ str [ $ i ] ) ] -- ; for ( $ j = 0 ; $ j < $ MAX_CHAR ; $ j ++ ) { if ( ( $ leftVisited [ $ j ] == 0 && $ rightVisited [ $ j ] != 0 ) || ( $ leftVisited [ $ j ] != 0 && $ rightVisited [ $ j ] == 0 ) ) break ; } if ( $ j == $ MAX_CHAR ) $ res ++ ; } return $ res ; } $ str = " abaababa " ; echo countBalance ( $ str ) ; ? >


"Find numbers with K odd divisors in a given range | function to check if number is perfect square or not ; Function to return count of divisors of a number ; Note that this loop runs till square root ; If divisors are equal , count it only once ; Otherwise print both ; Function to calculate all divisors having exactly k divisors between a and b ; Initialize result ; calculate only for perfect square numbers ; check if number is perfect square or not ; total divisors of number equals to k or not ; Driver Code"
Solution in php:

< ? php function isPerfect ( $ n ) { $ s = sqrt ( $ n ) ; return ( $ s * $ s == $ n ) ; } function divisorsCount ( $ n ) { $ count = 0 ; for ( $ i = 1 ; $ i <= sqrt ( $ n ) + 1 ; $ i ++ ) { if ( $ n % $ i == 0 ) { if ( $ n / $ i == $ i ) $ count += 1 ; else $ count += 2 ; } } return $ count ; } function kDivisors ( $ a , $ b , $ k ) { $ count = 0 ; for ( $ i = $ a ; $ i <= $ b ; $ i ++ ) { if ( isPerfect ( $ i ) ) if ( divisorsCount ( $ i ) == $ k ) $ count ++ ; } return $ count ; } $ a = 2 ; $ b = 49 ; $ k = 3 ; echo kDivisors ( $ a , $ b , $ k ) ; ? >


"Find one extra character in a string | PHP program to find extra character in one string ; result store the result ; traverse string A till end and xor with res ; xor with res ; traverse string B till end and xor with res ; xor with res ; print result at the end ; given string"
Solution in php:

< ? php function findExtraCharcter ( $ strA , $ strB ) { $ res = 0 ; for ( $ i = 0 ; $ i < strlen ( $ strA ) ; $ i ++ ) { $ res ^= ord ( $ strA [ $ i ] ) ; } for ( $ i = 0 ; $ i < strlen ( $ strB ) ; $ i ++ ) { $ res ^= ord ( $ strB [ $ i ] ) ; } return $ res ; } $ strA = " abcd " ; $ strB = " cbdad " ; echo chr ( findExtraCharcter ( $ strA , $ strB ) ) ; ? >


"Find original numbers from gcd ( ) every pair | PHP implementation of the approach ; Utility function to print the contents of an array ; Function to find the required numbers ; Sort array in decreasing order ; Count frequency of each element ; Size of the resultant array ; Store the highest element in the resultant array ; Decrement the frequency of that element ; Compute GCD ; Decrement GCD value by 2 ; Driver code"
Solution in php:


"Find other two sides and angles of a right angle triangle | PHP program to print all sides and angles of right angle triangle given one side ; Function to find angle A Angle in front of side a ; applied cosine rule ; convert into degrees ; Function to find angle B Angle in front of side b ; applied cosine rule ; convert into degrees and return ; Function to print all angles of the right angled triangle ; for calculate angle A ; for calculate angle B ; Function to find other two sides of the right angled triangle ; if n is odd ; case of n = 1 handled separately ; case of n = 2 handled separately ; Print angles of the triangle ; Driver Code"
Solution in php:

 < ? php $ PI = 3.1415926535 ; function findAnglesA ( $ a , $ b , $ c ) { global $ PI ; $ A = acos ( ( $ b * $ b + $ c * $ c - $ a * $ a ) / ( 2 * $ b * $ c ) ) ; return $ A * 180 / $ PI ; } function findAnglesB ( $ a , $ b , $ c ) { global $ PI ; $ B = acos ( ( $ a * $ a + $ c * $ c - $ b * $ b ) / ( 2 * $ a * $ c ) ) ; return $ B * 180 / $ PI ; } function printAngles ( $ a , $ b , $ c ) { $ x = ( double ) $ a ; $ y = ( double ) $ b ; $ z = ( double ) $ c ; $ A = findAnglesA ( $ x , $ y , $ z ) ; $ B = findAnglesB ( $ x , $ y , $ z ) ; echo " Angles ▁ are ▁ A ▁ = ▁ " . $ A . " , B = " ▁ . ▁ $ B ▁ . ▁ " , C = 90 " ; } function printOtherSides ( $ n ) { if ( $ n & 1 ) { if ( $ n == 1 ) echo " - 1
 " ; else { $ b = ( $ n * $ n - 1 ) / 2 ; $ c = ( $ n * $ n + 1 ) / 2 ; echo " Side ▁ b ▁ = ▁ " . $ b . " , ▁ Side ▁ c ▁ = ▁ " . $ c . "
 " ; } } else { if ( $ n == 2 ) echo " - 1
 " ; else { $ b = $ n * $ n / 4 - 1 ; $ c = $ n * $ n / 4 + 1 ; echo " Side ▁ b ▁ = ▁ " . $ b . " , ▁ Side ▁ c ▁ = ▁ " . $ c . "
 " ; } } printAngles ( $ n , $ b , $ c ) ; } $ a = 12 ; printOtherSides ( $ a ) ; ? >


"Find other two sides of a right angle triangle | Finds two sides of a right angle triangle if it they exist . ; if n is odd ; case of n = 1 handled separately ; case of n = 2 handled separately ; Driver Code"
Solution in php:


"Find pair with greatest product in array | Function to find greatest number ; Driver code"
Solution in php:

< ? php function findGreatest ( $ arr , $ n ) { $ result = -1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ n - 1 ; $ j ++ ) for ( $ k = $ j + 1 ; $ k < $ n ; $ k ++ ) if ( $ arr [ $ j ] * $ arr [ $ k ] == $ arr [ $ i ] ) $ result = max ( $ result , $ arr [ $ i ] ) ; return $ result ; } $ arr = array ( 30 , 10 , 9 , 3 , 35 ) ; $ n = count ( $ arr ) ; echo findGreatest ( $ arr , $ n ) ; ? >


"Find pair with maximum GCD in an array | Function to find GCD of pair with max GCD in the array ; Computing highest element ; Array to store the count of divisors i . e . Potential GCDs ; Iterating over every element ; Calculating all the divisors ; Divisor found ; Incrementing count for divisor ; Element / divisor is also a divisor Checking if both divisors are not same ; Checking the highest potential GCD ; If this divisor can divide at least 2 numbers , it is a GCD of at least 1 pair ; Array in which pair with max GCD is to be found ; Size of array"
Solution in php:

< ? php function findMaxGCD ( $ arr , $ n ) { $ high = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ high = max ( $ high , $ arr [ $ i ] ) ; $ divisors = array_fill ( 0 , $ high + 1 , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 1 ; $ j <= ( int ) ( sqrt ( $ arr [ $ i ] ) ) ; $ j ++ ) { if ( $ arr [ $ i ] % $ j == 0 ) { $ divisors [ $ j ] ++ ; if ( $ j != ( int ) ( $ arr [ $ i ] / $ j ) ) $ divisors [ ( int ) ( $ arr [ $ i ] / $ j ) ] ++ ; } } } for ( $ i = $ high ; $ i >= 1 ; $ i -- ) if ( $ divisors [ $ i ] > 1 ) return $ i ; } $ arr = array ( 1 , 2 , 4 , 8 , 8 , 12 ) ; $ n = sizeof ( $ arr ) ; echo findMaxGCD ( $ arr , $ n ) ; ? >


"Find pair with maximum GCD in an array | function to find GCD of pair with max GCD in the array ; Calculating MAX in array ; Maintaining count array ; Variable to store the multiples of a number ; Iterating from MAX to 1 GCD is always between MAX and 1. The first GCD found will be the highest as we are decrementing the potential GCD ; Iterating from current potential GCD till it is less than MAX ; A multiple found ; Incrementing potential GCD by itself To check i , 2 i , 3 i ... . ; 2 multiples found , max GCD found ; Array in which pair with max GCD is to be found ; Size of array"
Solution in php:

< ? php function findMaxGCD ( $ arr , $ n ) { $ high = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ high = max ( $ high , $ arr [ $ i ] ) ; $ count = array_fill ( 0 , $ high + 1 , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ count [ $ arr [ $ i ] ] ++ ; $ counter = 0 ; for ( $ i = $ high ; $ i >= 1 ; $ i -- ) { $ j = $ i ; $ counter = 0 ; while ( $ j <= $ high ) { if ( $ count [ $ j ] >= 2 ) return $ j ; else if ( $ count [ $ j ] == 1 ) $ counter ++ ; $ j += $ i ; if ( $ counter == 2 ) return $ i ; } } } $ arr = array ( 1 , 2 , 4 , 8 , 8 , 12 ) ; $ n = count ( $ arr ) ; print ( findMaxGCD ( $ arr , $ n ) ) ; ? >


"Find pair with maximum difference in any column of a Matrix | Function to find the column with max difference ; Traverse matrix column wise ; Insert elements of column to vector ; calculating difference between maximum and minimum ; driver code"
Solution in php:

 < ? php function colMaxDiff ( $ mat ) { $ N = 5 ; $ max_diff = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { $ max_val = $ mat [ 0 ] [ $ i ] ; $ min_val = $ mat [ 0 ] [ $ i ] ; for ( $ j = 1 ; $ j < $ N ; $ j ++ ) { $ max_val = max ( $ max_val , $ mat [ $ j ] [ $ i ] ) ; $ min_val = min ( $ min_val , $ mat [ $ j ] [ $ i ] ) ; } $ max_diff = max ( $ max_diff , $ max_val - $ min_val ) ; } return $ max_diff ; } $ mat = array ( array ( 1 , 2 , 3 , 4 , 5 ) , array ( 5 , 3 , 5 , 4 , 0 ) , array ( 5 , 6 , 7 , 8 , 9 ) , array ( 0 , 6 , 3 , 4 , 12 ) , array ( 9 , 7 , 12 , 4 , 3 ) ) ; echo " Max ▁ difference ▁ : ▁ " . colMaxDiff ( $ mat ) . "
 " ;


"Find pairs in array whose sums already exist in array | Function to find pair whose sum exists in arr [ ] ; Driver code"
Solution in php:

< ? php function findPair ( $ arr , $ n ) { $ found = false ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) { for ( $ k = 0 ; $ k < $ n ; $ k ++ ) { if ( $ arr [ $ i ] + $ arr [ $ j ] == $ arr [ $ k ] ) { echo $ arr [ $ i ] , " ▁ " , $ arr [ $ j ] ; $ found = true ; } } } } if ( $ found == false ) echo " Not ▁ exist " ; } $ arr = array ( 10 , 4 , 8 , 13 , 5 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; findPair ( $ arr , $ n ) ; ? >


"Find perimeter of shapes formed with 1 s in binary matrix | PHP program to find perimeter of area covered by 1 in 2D matrix consisits of 0 ' s ▁ and ▁ 1' s . ; Find the number of covered side for mat [ i ] [ j ] . ; UP ; LEFT ; DOWN ; RIGHT ; Returns sum of perimeter of shapes formed with 1 s ; Traversing the matrix and finding ones to calculate their contribution . ; Driver Code"
Solution in php:

 < ? php $ R = 3 ; $ C = 5 ; function numofneighbour ( $ mat , $ i , $ j ) { global $ R ; global $ C ; $ count = 0 ; if ( $ i > 0 && ( $ mat [ $ i - 1 ] [ $ j ] ) ) $ count ++ ; if ( $ j > 0 && ( $ mat [ $ i ] [ $ j - 1 ] ) ) $ count ++ ; if ( ( $ i < $ R - 1 ) && ( $ mat [ $ i + 1 ] [ $ j ] ) ) $ count ++ ; if ( ( $ j < $ C - 1 ) && ( $ mat [ $ i ] [ $ j + 1 ] ) ) $ count ++ ; return $ count ; } function findperimeter ( $ mat ) { global $ R ; global $ C ; $ perimeter = 0 ; for ( $ i = 0 ; $ i < $ R ; $ i ++ ) for ( $ j = 0 ; $ j < $ C ; $ j ++ ) if ( $ mat [ $ i ] [ $ j ] ) $ perimeter += ( 4 - numofneighbour ( $ mat , $ i , $ j ) ) ; return $ perimeter ; } $ mat = array ( array ( 0 , 1 , 0 , 0 , 0 ) , array ( 1 , 1 , 1 , 0 , 0 ) , array ( 1 , 0 , 0 , 0 , 0 ) ) ; echo findperimeter ( $ mat ) , "
 " ; ? >


"Find permutation of first N natural numbers that satisfies the given condition | Function to find permutation ( p ) of first N natural numbers such that there are exactly K elements of permutation such that GCD ( p [ i ] , i ) > 1 ; First place all the numbers in their respective places ; Modify for first n - k integers ; In first index place n - k ; Print the permutation ; Driver code"
Solution in php:


"Find permutation of n which is divisible by 3 but not divisible by 6 | Function to find the permutation ; length of integer ; if integer is even ; return odd integer ; rotate integer ; return - 1 in case no required permutation exists ; Driver Code"
Solution in php:

< ? php function findPermutation ( $ n ) { $ len = ceil ( log10 ( $ n ) ) ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { if ( $ n % 2 != 0 ) { return ( int ) $ n ; } else { $ n = ( $ n / 10 ) + ( $ n % 10 ) * pow ( 10 , $ len - $ i - 1 ) ; continue ; } } return -1 ; } $ n = 132 ; echo findPermutation ( $ n ) ; ? >


"Find position of an element in a sorted array of infinite numbers | Simple binary search algorithm ; function takes an infinite size array and a key to be searched and returns its position if found else - 1. We don 't know size of arr[] and we can assume size to be infinite in this function. NOTE THAT THIS FUNCTION ASSUMES arr[] TO BE OF INFINITE SIZE THEREFORE, THERE IS NO INDEX OUT OF BOUND CHECKING ; Find h to do binary search ; store previous high ; double high index ; update new val ; at this point we have updated low and high indices , Thus use binary search between them ; Driver Code"
Solution in php:

< ? php function binarySearch ( $ arr , $ l , $ r , $ x ) { if ( $ r >= $ l ) { $ mid = $ l + ( $ r - $ l ) / 2 ; if ( $ arr [ $ mid ] == $ x ) return $ mid ; if ( $ arr [ $ mid ] > $ x ) return binarySearch ( $ arr , $ l , $ mid - 1 , $ x ) ; return binarySearch ( $ arr , $ mid + 1 , $ r , $ x ) ; } return -1 ; } function findPos ( $ arr , $ key ) { $ l = 0 ; $ h = 1 ; $ val = $ arr [ 0 ] ; while ( $ val < $ key ) { $ l = $ h ; $ h = 2 * $ h ; $ val = $ arr [ $ h ] ; } return binarySearch ( $ arr , $ l , $ h , $ key ) ; } $ arr = array ( 3 , 5 , 7 , 9 , 10 , 90 , 100 , 130 , 140 , 160 , 170 ) ; $ ans = findPos ( $ arr , 10 ) ; if ( $ ans == -1 ) echo " Element ▁ not ▁ found " ; else echo " Element ▁ found ▁ at ▁ index ▁ " , $ ans ; ? >


"Find position of left most dis | Function to find first dis - similar bit ; return zero for equal number ; count bit length of n1 and n2 ; find bit difference and maxBit ; Driver Code"
Solution in php:

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


"Find position of the only set bit | A utility function to check whether n is power of 2 or goo . gl / 17 Arj not . See http : ; Returns position of the only set bit in ' n ' ; Iterate through bits of n till we find a set bit i & n will be non - zero only when ' i ' and ' n ' have a set bit at same position ; Unset current bit and set the next bit in ' i ' ; increment position ; Driver Code"
Solution in php:

 < ? php function isPowerOfTwo ( $ n ) { return $ n && ( ! ( $ n & ( $ n - 1 ) ) ) ; } function findPosition ( $ n ) { if ( ! isPowerOfTwo ( $ n ) ) return -1 ; $ i = 1 ; $ pos = 1 ; while ( ! ( $ i & $ n ) ) { $ i = $ i << 1 ; ++ $ pos ; } return $ pos ; } $ n = 16 ; $ pos = findPosition ( $ n ) ; if ( ( $ pos == -1 ) == true ) echo " n = " , ▁ $ n , ▁ " , " ,
 	 	 	 " Invalid number " , ▁ " " ;
 else
 	 	 echo ▁ " n = " , ▁ $ n , ▁ " , " ,
 	 	 	 " Position " , ▁ $ pos , ▁ " " $ n = 12 ; $ pos = findPosition ( $ n ) ; if ( ( $ pos == -1 ) == true ) echo " n = " , ▁ $ n , ▁ " , " ,
 	 	 	 " Invalid number " , ▁ " " ;
 else
 	 	 echo ▁ " n = " , ▁ $ n , ▁ " , " ,
 	 	 	 " Position " , ▁ $ pos , ▁ " " $ n = 128 ; $ pos = findPosition ( $ n ) ; if ( ( $ pos == -1 ) == true ) echo " n = " , ▁ $ n , ▁ " , " ,
 	 	 	 " Invalid number " , ▁ " " ;
 else
 	 	 echo ▁ " n = " , ▁ $ n , ▁ " , " ,
 	 	 	 " Position " , ▁ $ pos , ▁ " " ? >


"Find position of the only set bit | A utility function to check whether n is power of 2 or not ; Returns position of the only set bit in ' n ' ; One by one move the only set bit to right till it reaches end ; increment count of shifts ; Driver Code"
Solution in php:

 < ? php function isPowerOfTwo ( $ n ) { return $ n && ( ! ( $ n & ( $ n - 1 ) ) ) ; } function findPosition ( $ n ) { if ( ! isPowerOfTwo ( $ n ) ) return -1 ; $ count = 0 ; while ( $ n ) { $ n = $ n >> 1 ; ++ $ count ; } return $ count ; } $ n = 0 ; $ pos = findPosition ( $ n ) ; if ( ( $ pos == -1 ) == true ) echo " n = " , ▁ $ n , ▁ " , " ,
 	 	 " Invalid number " , ▁ " " ;
 else
 	 echo ▁ " n = " , ▁ $ n , ▁ " , " ,
 	 	 " Position " , ▁ $ pos , ▁ " " $ n = 12 ; $ pos = findPosition ( $ n ) ; if ( ( $ pos == -1 ) == true ) echo " n = " , ▁ $ n , ▁ " , " ,
 	 	 	 " Invalid number " , ▁ " " ;
 else
 	 	 echo ▁ " n = " , ▁ $ n ,
 	 	 	 " Position " , ▁ $ pos , ▁ " " $ n = 128 ; $ pos = findPosition ( $ n ) ; if ( ( $ pos == -1 ) == true ) echo " n = " , ▁ $ n , ▁ " , " ,
 	 	 	 " Invalid number " , ▁ " " ;
 else
 	 	 echo ▁ " n = " , ▁ $ n , ▁ " , " ,
 	 	 	 " Position " , ▁ $ pos , ▁ " " ? >


"Find prime number K in an array such that ( A [ i ] % K ) is maximum | Function to return the count of primes in the given array ; Find maximum value in the array ; USE SIEVE TO FIND ALL PRIME NUMBERS LESS THAN OR EQUAL TO max_val Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Remaining part of SIEVE ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; To store the maximum prime number ; If current element is prime then update the maximum prime ; Return the maximum prime number from the array ; Driver code"
Solution in php:

< ? php function getPrime ( $ arr , $ n ) { $ max_val = max ( $ arr ) ; $ prime = array_fill ( 0 , $ max_val + 1 , true ) ; $ prime [ 0 ] = false ; $ prime [ 1 ] = false ; for ( $ p = 2 ; $ p * $ p <= $ max_val ; $ p ++ ) { if ( $ prime [ $ p ] == true ) { for ( $ i = $ p * 2 ; $ i <= $ max_val ; $ i += $ p ) $ prime [ $ i ] = false ; } } $ maximum = -1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ prime [ $ arr [ $ i ] ] ) $ maximum = max ( $ maximum , $ arr [ $ i ] ) ; } return $ maximum ; } $ arr = array ( 2 , 10 , 15 , 7 , 6 , 8 , 13 ) ; $ n = count ( $ arr ) ; echo getPrime ( $ arr , $ n ) ; ? >


"Find probability of selecting element from kth column after N iterations | Function to calculate probability ; declare dp [ ] [ ] and sum [ ] ; precalculate the first row ; calculate the probability for each element and update dp table ; return result ; Driver code"
Solution in php:


"Find probability that a player wins when probabilities of hitting the target are given | Function to return the probability of the winner ; Driver Code ; Will print 9 digits after the decimal point"
Solution in php:

< ? php function find_probability ( $ p , $ q , $ r , $ s ) { $ t = ( 1 - $ p / $ q ) * ( 1 - $ r / $ s ) ; $ ans = ( $ p / $ q ) / ( 1 - $ t ) ; return $ ans ; } $ p = 1 ; $ q = 2 ; $ r = 1 ; $ s = 2 ; $ res = find_probability ( $ p , $ q , $ r , $ s ) ; $ update = number_format ( $ res , 7 ) ; echo $ update ; ? >


"Find profession in a special family | Function to get no of set bits in binary representation of passed binary no . ; Returns ' e ' if profession of node at given level and position is engineer . Else doctor . The function assumes that given position and level have valid values . ; Count set bits in ' pos - 1' ; If set bit count is odd , then doctor , else engineer ; Driver Code"
Solution in php:

 < ? php function countSetBits ( $ n ) { $ count = 0 ; while ( $ n ) { $ n &= ( $ n - 1 ) ; $ count ++ ; } return $ count ; } function findProffesion ( $ level , $ pos ) { $ c = countSetBits ( $ pos - 1 ) ; return ( $ c % 2 ) ? ' d ' : ' e ' ; } $ level = 3 ; $ pos = 4 ; if ( ( findProffesion ( $ level , $ pos ) == ' e ' ) == true ) echo " Engineer ▁
 " ; else echo " Doctor ▁
 " ; ? >


"Find profession in a special family | Returns ' e ' if profession of node at given level and position is engineer . Else doctor . The function assumes that given position and level have valid values . ; Base case ; Recursively find parent 's  profession. If parent is  a Doctor, this node will  be a doctor if it is at  odd position and an engineer  if at even position ; If parent is an engineer , then current node will be an engineer if at odd position and doctor if even position . ; Driver code"
Solution in php:

< ? php function findProffesion ( $ level , $ pos ) { if ( $ level == 1 ) return ' e ' ; if ( findProffesion ( $ level - 1 , ( $ pos + 1 ) / 2 ) == ' d ' ) return ( $ pos % 2 ) ? ' d ' : ' e ' ; return ( $ pos % 2 ) ? ' e ' : ' d ' ; } $ level = 4 ; $ pos = 2 ; if ( ( findProffesion ( $ level , $ pos ) == ' e ' ) == true ) echo " Engineer " ; else echo " Doctor " ; ? >


"Find relative complement of two sorted arrays | PHP program to find all those elements of arr1 [ ] that are not present in arr2 [ ] ; If current element in arr2 [ ] is greater , then arr1 [ i ] can 't be  present in arr2[j..m-1] ; Skipping smaller elements of arr2 [ ] ; Equal elements found ( skipping in both arrays ) ; Printing remaining elements of arr1 [ ] ; Driver code"
Solution in php:


"Find row number of a binary matrix having maximum number of 1 s | PHP program to find row with maximum 1 in row sorted binary matrix ; function for finding row with maximum 1 ; find left most position of 1 in a row find 1 st zero in a row ; Driver Code"
Solution in php:


"Find size of the largest ' + ' formed by all ones in a binary matrix | size of binary square matrix ; Function to find the size of the largest ' + ' formed by all 1 's in given binary matrix ; left [ j ] [ j ] , right [ i ] [ j ] , top [ i ] [ j ] and bottom [ i ] [ j ] store maximum number of consecutive 1 's present to the left,  right, top and bottom of mat[i][j]  including cell(i, j) respectively ; initialize above four matrix ; initialize first row of top ; initialize last row of bottom ; initialize first column of left ; initialize last column of right ; fill all cells of above four matrix ; calculate left matrix ( filled left to right ) ; calculate top matrix ; calculate new value of j to calculate value of bottom ( i , j ) and right ( i , j ) ; calculate bottom matrix ; calculate right matrix ; revert back to old j ; n stores length of longest + found so far ; compute longest + ; find minimum of left ( i , j ) , right ( i , j ) , top ( i , j ) , bottom ( i , j ) ; largest + would be formed by a cell that has maximum value ; 4 directions of length n - 1 and 1 for middle cell ; matrix contains all 0 's ; Driver Code"
Solution in php:


"Find smallest and largest element from square matrix diagonals | Function to find smallest and largest element from principal and secondary diagonal ; take length of $matrix ; declare and initialize variables with appropriate value ; Condition for principal diagonal ; take new smallest value ; take new largest value ; Condition for secondary diagonal ; take new smallest value ; take new largest value ; Declare and initialize 5 X5 matrix"
Solution in php:

 < ? php function diagonalsMinMax ( $ mat ) { $ n = count ( $ mat ) ; if ( $ n == 0 ) return ; $ principalMin = $ mat [ 0 ] [ 0 ] ; $ principalMax = $ mat [ 0 ] [ 0 ] ; $ secondaryMin = $ mat [ $ n - 1 ] [ 0 ] ; $ secondaryMax = $ mat [ $ n - 1 ] [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { for ( $ j = 1 ; $ j < $ n ; $ j ++ ) { if ( $ i == $ j ) { if ( $ mat [ $ i ] [ $ j ] < $ principalMin ) { $ principalMin = $ mat [ $ i ] [ $ j ] ; } if ( $ mat [ $ i ] [ $ j ] > $ principalMax ) { $ principalMax = $ mat [ $ i ] [ $ j ] ; } } if ( ( $ i + $ j ) == ( $ n - 1 ) ) { if ( $ mat [ $ i ] [ $ j ] < $ secondaryMin ) { $ secondaryMin = $ mat [ $ i ] [ $ j ] ; } if ( $ mat [ $ i ] [ $ j ] > $ secondaryMax ) { $ secondaryMax = $ mat [ $ i ] [ $ j ] ; } } } } echo " Principal ▁ Diagonal ▁ Smallest ▁ Element : ▁ " , $ principalMin , "
 " ; echo " Principal ▁ Diagonal ▁ Greatest ▁ Element ▁ : ▁ " , $ principalMax , "
 " ; echo " Secondary ▁ Diagonal ▁ Smallest ▁ Element : ▁ " , $ secondaryMin , "
 " ; echo " Secondary ▁ Diagonal ▁ Greatest ▁ Element : ▁ " , $ secondaryMax , "
 " ; } $ matrix = array ( array ( 1 , 2 , 3 , 4 , -10 ) , array ( 5 , 6 , 7 , 8 , 6 ) , array ( 1 , 2 , 11 , 3 , 4 ) , array ( 5 , 6 , 70 , 5 , 8 ) , array ( 4 , 9 , 7 , 1 , -5 ) ) ; diagonalsMinMax ( $ matrix ) ; ? >


"Find smallest number K such that K % p = 0 and q % K = 0 | Function to return the minimum value K such that K % p = 0 and q % k = 0 ; If K is possible ; No such K is possible ; Driver code"
Solution in php:

< ? php function getMinVal ( $ p , $ q ) { if ( $ q % $ p == 0 ) return $ p ; return -1 ; } $ p = 24 ; $ q = 48 ; echo getMinVal ( $ p , $ q ) ; ? >


"Find smallest number n such that n XOR n + 1 equals to given k . | function to return the required n ; if k is of form 2 ^ i - 1 ; Driver Code"
Solution in php:

< ? php function xorCalc ( $ k ) { if ( $ k == 1 ) return 2 ; if ( ( ( $ k + 1 ) & $ k ) == 0 ) return floor ( $ k / 2 ) ; return 1 ; } $ k = 31 ; echo xorCalc ( $ k ) ; ? >


"Find smallest values of x and y such that ax | To find GCD using Eculcid 's algorithm ; Prints smallest values of x and y that satisfy " ax ▁ - ▁ by ▁ = ▁ 0" ; Find LCM ; Driver Code"
Solution in php:

< ? php function gcd ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; return ( gcd ( $ b , $ a % $ b ) ) ; } function findSmallest ( $ a , $ b ) { $ lcm = ( $ a * $ b ) / gcd ( $ a , $ b ) ; echo " x ▁ = ▁ " , $ lcm / $ a , " y = " } $ a = 25 ; $ b = 35 ; findSmallest ( $ a , $ b ) ; ? >


"Find sub | PHP implementation of the approach ; Function to return the sum of the sub - matrix ; Function that returns true if it is possible to find the sub - matrix with required sum ; 2 - D array to store the sum of all the sub - matrices ; Filling of dp [ ] [ ] array ; Checking for each possible sub - matrix of size k X k ; Sub - matrix with the given sum not found ; Driver code ; Function call"
Solution in php:


"Find subarray with given sum | Set 1 ( Nonnegative Numbers ) | Returns true if the there is a subarray of arr [ ] with sum equal to ' sum ' otherwise returns false . Also , prints the result ; Initialize curr_sum as value of first element and starting point as 0 ; Add elements one by one to curr_sum and if the curr_sum exceeds the sum , then remove starting element ; If curr_sum exceeds the sum , then remove the starting elements ; If curr_sum becomes equal to sum , then return true ; Add this element to curr_sum ; If we reach here , then no subarray ; Driver Code"
Solution in php:

< ? php function subArraySum ( $ arr , $ n , $ sum ) { $ curr_sum = $ arr [ 0 ] ; $ start = 0 ; $ i ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { while ( $ curr_sum > $ sum and $ start < $ i - 1 ) { $ curr_sum = $ curr_sum - $ arr [ $ start ] ; $ start ++ ; } if ( $ curr_sum == $ sum ) { echo " Sum ▁ found ▁ between ▁ indexes " , " ▁ " , $ start , " ▁ " , " and ▁ " , " ▁ " , $ i - 1 ; return 1 ; } if ( $ i < $ n ) $ curr_sum = $ curr_sum + $ arr [ $ i ] ; } echo " No ▁ subarray ▁ found " ; return 0 ; } $ arr = array ( 15 , 2 , 4 , 8 , 9 , 5 , 10 , 23 ) ; $ n = count ( $ arr ) ; $ sum = 23 ; subArraySum ( $ arr , $ n , $ sum ) ;


"Find subarray with given sum | Set 1 ( Nonnegative Numbers ) | Returns true if the there is a subarray of arr [ ] with sum equal to ' sum ' otherwise returns false . Also , prints the result ; Pick a starting point ; try all subarrays starting with ' i ' ; Driver Code"
Solution in php:

< ? php function subArraySum ( $ arr , $ n , $ sum ) { $ curr_sum ; $ i ; $ j ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ curr_sum = $ arr [ $ i ] ; for ( $ j = $ i + 1 ; $ j <= $ n ; $ j ++ ) { if ( $ curr_sum == $ sum ) { echo " Sum ▁ found ▁ between ▁ indexes ▁ " , $ i , " ▁ and ▁ " , $ j - 1 ; return 1 ; } if ( $ curr_sum > $ sum $ j == $ n ) break ; $ curr_sum = $ curr_sum + $ arr [ $ j ] ; } } echo " No ▁ subarray ▁ found " ; return 0 ; } $ arr = array ( 15 , 2 , 4 , 8 , 9 , 5 , 10 , 23 ) ; $ n = sizeof ( $ arr ) ; $ sum = 23 ; subArraySum ( $ arr , $ n , $ sum ) ; return 0 ; ? >


"Find subsequences with maximum Bitwise AND and Bitwise OR | Function to find the maximum sum ; Maximum AND is maximum element ; Maximum OR is bitwise OR of all . ; Driver code"
Solution in php:

< ? php function maxSum ( $ a , $ n ) { $ maxAnd = max ( $ a ) ; $ maxOR = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ maxOR |= $ a [ $ i ] ; print ( $ maxAnd + $ maxOR ) ; } $ n = 4 ; $ a = array ( 3 , 5 , 6 , 1 ) ; maxSum ( $ a , $ n ) ; ? >


"Find sum of N | calculate sum of Nth group ; Driver code"
Solution in php:

< ? php function nth_group ( $ n ) { return $ n * ( 2 * pow ( $ n , 2 ) + 1 ) ; } $ N = 5 ; echo nth_group ( $ N ) ; ? >


"Find sum of a number and its maximum prime factor | Function to return the sum of n and it 's largest prime factor ; Initialise maxPrime to - 1. ; n must be odd at this point , thus skip the even numbers and iterate only odd numbers ; This condition is to handle the case when n is a prime number greater than 2 ; finally return the sum . ; Driver Code"
Solution in php:

< ? php function maxPrimeFactors ( $ n ) { $ num = $ n ; $ maxPrime = -1 ; while ( $ n % 2 == 0 ) { $ maxPrime = 2 ; $ n /= 2 ; } for ( $ i = 3 ; $ i <= sqrt ( $ n ) ; $ i += 2 ) { while ( $ n % $ i == 0 ) { $ maxPrime = $ i ; $ n = $ n / $ i ; } } if ( $ n > 2 ) $ maxPrime = $ n ; $ sum = $ maxPrime + $ num ; return $ sum ; } $ n = 19 ; echo maxPrimeFactors ( $ n ) ; ? >


"Find sum of all nodes of the given perfect binary tree | function to find sum of all of the nodes of given perfect binary tree ; no of leaf nodes ; sum of nodes at last level ; sum of all nodes ; Driver Code"
Solution in php:

< ? php function sumNodes ( $ l ) { $ leafNodeCount = ( $ l - 1 ) * ( $ l - 1 ) ; $ sumLastLevel = 0 ; $ sumLastLevel = ( $ leafNodeCount * ( $ leafNodeCount + 1 ) ) / 2 ; $ sum = $ sumLastLevel * $ l ; return $ sum ; } $ l = 3 ; echo ( sumNodes ( $ l ) ) ; ? >


"Find sum of digits in factorial of a number | Function to multiply x with large number stored in vector v . Result is stored in v . ; Calculate res + prev carry ; updation at ith position ; Returns sum of digits in n ! ; One by one multiply i to current vector and update the vector . ; Find sum of digits in vector v [ ] ; Driver code"
Solution in php:

< ? php function multiply ( & $ v , $ x ) { $ carry = 0 ; $ size = count ( $ v ) ; for ( $ i = 0 ; $ i < $ size ; $ i ++ ) { $ res = $ carry + $ v [ $ i ] * $ x ; $ v [ $ i ] = $ res % 10 ; $ carry = ( int ) ( $ res / 10 ) ; } while ( $ carry != 0 ) { array_push ( $ v , $ carry % 10 ) ; $ carry = ( int ) ( $ carry / 10 ) ; } } function findSumOfDigits ( $ n ) { for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) multiply ( $ v , $ i ) ; $ sum = 0 ; $ size = count ( $ v ) ; for ( $ i = 0 ; $ i < $ size ; $ i ++ ) $ sum += $ v [ $ i ] ; return $ sum ; } $ n = 1000 ; print ( findSumOfDigits ( $ n ) ) ; ? >


"Find sum of even factors of a number | Returns sum of all factors of n . ; If n is odd , then there are no even factors . ; Traversing through all prime factors . ; While i divides n , print i and divide n ; here we remove the 2 ^ 0 that is 1. All other factors ; This condition is to handle the case when n is a prime number . ; Driver code"
Solution in php:

< ? php function sumofFactors ( $ n ) { if ( $ n % 2 != 0 ) return 0 ; $ res = 1 ; for ( $ i = 2 ; $ i <= sqrt ( $ n ) ; $ i ++ ) { $ count = 0 ; $ curr_sum = 1 ; $ curr_term = 1 ; while ( $ n % $ i == 0 ) { $ count ++ ; $ n = floor ( $ n / $ i ) ; if ( $ i == 2 && $ count == 1 ) $ curr_sum = 0 ; $ curr_term *= $ i ; $ curr_sum += $ curr_term ; } $ res *= $ curr_sum ; } if ( $ n >= 2 ) $ res *= ( 1 + $ n ) ; return $ res ; } $ n = 18 ; echo sumofFactors ( $ n ) ; ? >


"Find sum of even index binomial coefficients | Return the sum of even index term ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; finding sum of even index term . ; Driver Code"
Solution in php:


"Find sum of even index binomial coefficients | Returns value of even indexed Binomial Coefficient Sum which is 2 raised to power n - 1. ; Driver Code"
Solution in php:

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


"Find sum of factorials in an array | Function to return the factorial of n ; Function to return the sum of factorials of the array elements ; To store the required sum ; Add factorial of all the elements ; Driver code"
Solution in php:

< ? php function factorial ( $ n ) { $ f = 1 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ f *= $ i ; } return $ f ; } function sumFactorial ( $ arr , $ n ) { $ s = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ s += factorial ( $ arr [ $ i ] ) ; } return $ s ; } $ arr = array ( 7 , 3 , 5 , 4 , 8 ) ; $ n = sizeof ( $ arr ) ; echo sumFactorial ( $ arr , $ n ) ; ? >


"Find sum of modulo K of first N natural number | Return sum of modulo K of first N natural numbers . ; Counting the number of times 1 , 2 , . . , K - 1 , 0 sequence occurs . ; Finding the number of elements left which are incomplete of sequence Leads to Case 1 type . ; adding multiplication of number of times 1 , 2 , . . , K - 1 , 0 sequence occurs and sum of first k natural number and sequence from case 1. ; Driver program"
Solution in php:


"Find sum of modulo K of first N natural number | Return sum of modulo K of first N natural numbers . ; Iterate from 1 to N && evaluating and adding i % K . ; Driver Code"
Solution in php:

 < ? php function findSum ( $ N , $ K ) { $ ans = 0 ; for ( $ i = 1 ; $ i <= $ N ; $ i ++ ) $ ans += ( $ i % $ K ) ; return $ ans ; } $ N = 10 ; $ K = 2 ; echo findSum ( $ N , $ K ) , "
 " ; ? >


"Find sum of odd factors of a number | Returns sum of all factors of n . ; Traversing through all prime factors . ; ignore even factors by removing all powers of 2 ; While i divides n , print i and divide n ; This condition is to handle the case when n is a prime number . ; Driver code"
Solution in php:

< ? php function sumofoddFactors ( $ n ) { $ res = 1 ; while ( $ n % 2 == 0 ) $ n = $ n / 2 ; for ( $ i = 3 ; $ i <= sqrt ( $ n ) ; $ i ++ ) { $ count = 0 ; $ curr_sum = 1 ; $ curr_term = 1 ; while ( $ n % $ i == 0 ) { $ count ++ ; $ n = $ n / $ i ; $ curr_term *= $ i ; $ curr_sum += $ curr_term ; } $ res *= $ curr_sum ; } if ( $ n >= 2 ) $ res *= ( 1 + $ n ) ; return $ res ; } $ n = 30 ; echo sumofoddFactors ( $ n ) ; ? >


"Find sum of product of number in given series | function to calculate ( a ^ b ) % p ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now $y = $y >> 1 ; y = y / 2 ; function to return required answer ; modulo inverse of denominator ; calculating commentator part ; calculating t ! ; accumulating the final answer ; Driver code ; function call to print required sum"
Solution in php:

< ? php function power ( $ x , $ y , $ p ) { $ res = 1 ; $ x = $ x % $ p ; while ( $ y > 0 ) { if ( $ y & 1 ) $ res = ( $ res * $ x ) % $ p ; $ x = ( $ x * $ x ) % $ p ; } return $ res ; } function sumProd ( $ n , $ t ) { $ MOD = 1000000007 ; $ dino = power ( $ t + 1 , $ MOD - 2 , $ MOD ) ; $ ans = 1 ; for ( $ i = $ n + $ t + 1 ; $ i > $ n ; -- $ i ) $ ans = ( $ ans % $ MOD * $ i % $ MOD ) % $ MOD ; $ tfact = 1 ; for ( $ i = 1 ; $ i <= $ t ; ++ $ i ) $ tfact = ( $ tfact * $ i ) % $ MOD ; $ ans = $ ans * $ dino - $ tfact + $ MOD ; return $ ans % $ MOD ; } $ n = 3 ; $ t = 2 ; echo sumProd ( $ n , $ t ) ; ? >


"Find sum of the series 1 + 22 + 333 + 4444 + ... ... upto n terms | Function to calculate sum ; Return sum ; Driver code"
Solution in php:


"Find sum of the series 1 | Function to calculate sum ; when n is odd ; when n is not odd ; Driver code"
Solution in php:

< ? php function solve_sum ( $ n ) { if ( $ n % 2 == 1 ) return ( $ n + 1 ) / 2 ; return - $ n / 2 ; } $ n = 8 ; echo solve_sum ( $ n ) ; ? >


"Find sum of the series ? 3 + ? 12 + ... ... ... upto N terms | Function to find the sum ; Apply AP formula ; number of terms"
Solution in php:

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


"Find the Diameter or Longest chord of a Circle | Function to find the longest chord ; Get the radius ; Find the diameter"
Solution in php:

 < ? php function diameter ( $ r ) { echo " The ▁ length ▁ of ▁ the ▁ longest ▁ chord " , " ▁ or ▁ diameter ▁ of ▁ the ▁ circle ▁ is ▁ " , 2 * $ r << "
 " ; } $ r = 4 ; diameter ( $ r ) ; ? >


"Find the GCD that lies in given range | Return the greatest common divisor of two numbers ; Return the gretest common divisor of a and b which lie in the given range . ; Loop from 1 to sqrt ( GCD ( a , b ) . ; if i divides the GCD ( a , b ) , then find maximum of three numbers res , i and g / i ; Driver Code"
Solution in php:

< ? php function gcd ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; return gcd ( $ b , $ a % $ b ) ; } function maxDivisorRange ( $ a , $ b , $ l , $ h ) { $ g = gcd ( $ a , $ b ) ; $ res = -1 ; for ( $ i = $ l ; $ i * $ i <= $ g and $ i <= $ h ; $ i ++ ) if ( $ g % $ i == 0 ) $ res = max ( $ res , max ( $ i , $ g / $ i ) ) ; return $ res ; } $ a = 3 ; $ b = 27 ; $ l = 1 ; $ h = 5 ; echo maxDivisorRange ( $ a , $ b , $ l , $ h ) ; ? >


"Find the K | Function to find the K - th minimum element from an array concatenated M times ; Sort the elements in ascending order ; Return the K 'th Min element  present at ( (K-1) / M ) index ; Driver Code"
Solution in php:

< ? php function KthMinValAfterMconcatenate ( $ A , $ N , $ M , $ K ) { sort ( $ A ) ; return ( $ A [ ( ( $ K - 1 ) / $ M ) ] ) ; } $ A = array ( 3 , 1 , 2 ) ; $ M = 3 ; $ K = 4 ; $ N = sizeof ( $ A ) ; echo ( KthMinValAfterMconcatenate ( $ A , $ N , $ M , $ K ) ) ; ? >


"Find the Largest Cube formed by Deleting minimum Digits from a number | Returns vector of Pre Processed perfect cubes ; convert the cube to string and push into preProcessedCubes vector ; Utility function for findLargestCube ( ) . Returns the Largest cube number that can be formed ; reverse the preProcessed cubes so that we have the largest cube in the beginning of the vector ; iterate over all cubes ; check if the current digit of the cube matches with that of the number num ; if control reaches here , the its not possible to form a perfect cube ; wrapper for findLargestCubeUtil ( ) ; pre process perfect cubes ; convert number n to String ; Driver Code"
Solution in php:

 < ? php function preProcess ( $ n ) { $ preProcessedCubes = array ( ) ; for ( $ i = 1 ; $ i * $ i * $ i < $ n ; $ i ++ ) { $ iThCube = $ i * $ i * $ i ; $ cubeString = strval ( $ iThCube ) ; array_push ( $ preProcessedCubes , $ cubeString ) ; } return $ preProcessedCubes ; } function findLargestCubeUtil ( $ num , $ preProcessedCubes ) { $ preProcessedCubes = array_reverse ( $ preProcessedCubes ) ; $ totalCubes = count ( $ preProcessedCubes ) ; for ( $ i = 0 ; $ i < $ totalCubes ; $ i ++ ) { $ currCube = $ preProcessedCubes [ $ i ] ; $ digitsInCube = strlen ( $ currCube ) ; $ index = 0 ; $ digitsInNumber = strlen ( $ num ) ; for ( $ j = 0 ; $ j < $ digitsInNumber ; $ j ++ ) { if ( $ num [ $ j ] == $ currCube [ $ index ] ) $ index += 1 ; if ( $ digitsInCube == $ index ) return $ currCube ; } } return " Not ▁ Possible " ; } function findLargestCube ( $ n ) { $ preProcessedCubes = preProcess ( $ n ) ; $ num = strval ( $ n ) ; $ ans = findLargestCubeUtil ( $ num , $ preProcessedCubes ) ; print ( " Largest ▁ Cube ▁ that ▁ can ▁ be ▁ formed ▁ from ▁ " . $ n . " ▁ is ▁ " . $ ans . "
 " ) ; } $ n = 4125 ; findLargestCube ( $ n ) ; $ n = 876 ; findLargestCube ( $ n ) ? >


"Find the Largest number with given number of digits and sum of digits | Prints the smalles possible number with digit sum ' s ' and ' m ' number of digits . ; If sum of digits is 0 , then a number is possible only if number of digits is 1. ; Sum greater than the maximum possible sum . ; Fill from most significant digit to least significant digit . ; Fill 9 first to make the number largest ; If remaining sum becomes less than 9 , then fill the remaining sum ; Driver code"
Solution in php:

< ? php function findLargest ( $ m , $ s ) { if ( $ s == 0 ) { if ( ( $ m == 1 ) == true ) echo " Largest ▁ number ▁ is ▁ " , 0 ; else echo " Not ▁ possible " ; return ; } if ( $ s > 9 * $ m ) { echo " Not ▁ possible " ; return ; } for ( $ i = 0 ; $ i < $ m ; $ i ++ ) { if ( $ s >= 9 ) { $ res [ $ i ] = 9 ; $ s -= 9 ; } else { $ res [ $ i ] = $ s ; $ s = 0 ; } } echo " Largest ▁ number ▁ is ▁ " ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) echo $ res [ $ i ] ; } $ s = 9 ; $ m = 2 ; findLargest ( $ m , $ s ) ; ? >


"Find the Minimum length Unsorted Subarray , sorting which makes the complete array sorted | PHP program to find the Minimum length Unsorted Subarray , sorting which makes the complete array sorted ; step 1 ( a ) of above algo ; step 1 ( b ) of above algo ; step 2 ( a ) of above algo ; step 2 ( b ) of above algo ; step 2 ( c ) of above algo ; step 3 of above algo"
Solution in php:

 < ? php function printUnsorted ( & $ arr , $ n ) { $ s = 0 ; $ e = $ n - 1 ; for ( $ s = 0 ; $ s < $ n - 1 ; $ s ++ ) { if ( $ arr [ $ s ] > $ arr [ $ s + 1 ] ) break ; } if ( $ s == $ n - 1 ) { echo " The ▁ complete ▁ array ▁ is ▁ sorted " ; return ; } for ( $ e = $ n - 1 ; $ e > 0 ; $ e -- ) { if ( $ arr [ $ e ] < $ arr [ $ e - 1 ] ) break ; } $ max = $ arr [ $ s ] ; $ min = $ arr [ $ s ] ; for ( $ i = $ s + 1 ; $ i <= $ e ; $ i ++ ) { if ( $ arr [ $ i ] > $ max ) $ max = $ arr [ $ i ] ; if ( $ arr [ $ i ] < $ min ) $ min = $ arr [ $ i ] ; } for ( $ i = 0 ; $ i < $ s ; $ i ++ ) { if ( $ arr [ $ i ] > $ min ) { $ s = $ i ; break ; } } for ( $ i = $ n - 1 ; $ i >= $ e + 1 ; $ i -- ) { if ( $ arr [ $ i ] < $ max ) { $ e = $ i ; break ; } } echo " ▁ The ▁ unsorted ▁ subarray ▁ which ▁ makes ▁ " . " the ▁ given ▁ array ▁ " . "
 " . " ▁ sorted ▁ lies ▁ between ▁ the ▁ indees ▁ " . $ s . " ▁ and ▁ " . $ e ; return ; } $ arr = array ( 10 , 12 , 20 , 30 , 25 , 40 , 32 , 31 , 35 , 50 , 60 ) ; $ arr_size = sizeof ( $ arr ) ; printUnsorted ( $ arr , $ arr_size ) ; ? >


"Find the Missing Number in a sorted array | A binary search based program to find the only missing number in a sorted array of distinct elements within limited range . ; Driver Code"
Solution in php:

< ? php function search ( $ ar , $ size ) { $ a = 0 ; $ b = $ size - 1 ; $ mid ; while ( ( $ b - $ a ) > 1 ) { $ mid = ( int ) ( ( $ a + $ b ) / 2 ) ; if ( ( $ ar [ $ a ] - $ a ) != ( $ ar [ $ mid ] - $ mid ) ) $ b = $ mid ; else if ( ( $ ar [ $ b ] - $ b ) != ( $ ar [ $ mid ] - $ mid ) ) $ a = $ mid ; } return ( $ ar [ $ a ] + 1 ) ; } $ ar = array ( 1 , 2 , 3 , 4 , 5 , 6 , 8 ) ; $ size = sizeof ( $ ar ) ; echo " Missing ▁ number : ▁ " , search ( $ ar , $ size ) ; ? >


"Find the Missing Number | getMissingNo takes array and size of array as arguments ; Driver Code"
Solution in php:

< ? php function getMissingNo ( $ a , $ n ) { $ total = ( $ n + 1 ) * ( $ n + 2 ) / 2 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ total -= $ a [ $ i ] ; return $ total ; } $ a = array ( 1 , 2 , 4 , 5 , 6 ) ; $ miss = getMissingNo ( $ a , 5 ) ; echo ( $ miss ) ; ? >


"Find the Missing Point of Parallelogram | Driver Code ; coordinates of A ; coordinates of B ; coordinates of C"
Solution in php:


"Find the Next perfect square greater than a given number | Function to find the next perfect square ; Driver Code"
Solution in php:

< ? php function nextPerfectSquare ( $ N ) { $ nextN = floor ( sqrt ( $ N ) ) + 1 ; return $ nextN * $ nextN ; } $ n = 35 ; echo nextPerfectSquare ( $ n ) ; ? >


"Find the Nth term of the series 14 , 28 , 20 , 40 , ... . . | Function to find the N - th term ; initializing the 1 st number ; loop from 2 nd term to nth term ; if i is even , double the previous number ; if i is odd , subtract 8 from previous number ; Driver Code"
Solution in php:


"Find the Nth term of the series 2 + 6 + 13 + 23 + . . . | calculate Nth term of given series ; Driver code"
Solution in php:

< ? php function Nth_Term ( $ n ) { return ( 3 * pow ( $ n , 2 ) - $ n + 2 ) / ( 2 ) ; } $ N = 5 ; echo ( Nth_Term ( $ N ) ) ; ? >


"Find the Nth term of the series 9 , 45 , 243 , 1377 | Function to return the nth term of the given series ; nth term of the given series ; Driver code"
Solution in php:

< ? php function nthterm ( $ n ) { $ An = ( pow ( 1 , $ n ) + pow ( 2 , $ n ) ) * pow ( 3 , $ n ) ; return $ An ; } $ n = 3 ; echo nthterm ( $ n ) ; ? >


"Find the Number Occurring Odd Number of Times | Function to find the element occurring odd number of times ; Driver code ; Function calling"
Solution in php:

< ? php function getOddOccurrence ( & $ arr , $ arr_size ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ arr_size ; $ i ++ ) { for ( $ j = 0 ; $ j < $ arr_size ; $ j ++ ) { if ( $ arr [ $ i ] == $ arr [ $ j ] ) $ count ++ ; } if ( $ count % 2 != 0 ) return $ arr [ $ i ] ; } return -1 ; } $ arr = array ( 2 , 3 , 5 , 4 , 5 , 2 , 4 , 3 , 5 , 2 , 4 , 4 , 2 ) ; $ n = sizeof ( $ arr ) ; echo ( getOddOccurrence ( $ arr , $ n ) ) ; ? >


"Find the Number which contain the digit d | Returns true if d is present as digit in number x . ; Breal loop if d is present as digit ; If loop broke ; function to display the values ; Check all numbers one by one ; checking for digit ; Driver Code"
Solution in php:

< ? php function isDigitPresent ( $ x , $ d ) { while ( $ x > 0 ) { if ( $ x % 10 == $ d ) break ; $ x = $ x / 10 ; } return ( $ x > 0 ) ; } function printNumbers ( $ n , $ d ) { for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) if ( $ i == $ d || isDigitPresent ( $ i , $ d ) ) echo $ i , " ▁ " ; } $ n = 47 ; $ d = 7 ; printNumbers ( $ n , $ d ) ; ? >


"Find the Product of first N Prime Numbers | PHP implementation of above solution ; Create a boolean array " $ prime [ 0 . . $ n ] " and initialize all entries it as true . A value in $prime [ i ] will finally be false if i is Not a $prime , else true . ; If $prime [ $p ] is not changed , then it is a $prime ; Set all multiples of $$p to non - $prime ; find the product of 1 st N $prime numbers ; $count of $prime numbers ; product of $prime numbers ; if the number is $prime add it ; increase the $count ; get to next number ; create the sieve ; find the value of 1 st $n $prime numbers"
Solution in php:

< ? php $ MAX = 10000 ; $ prime = array_fill ( 0 , $ MAX + 1 , true ) ; function SieveOfEratosthenes ( ) { global $ MAX ; global $ prime ; $ prime = array_fill ( 0 , $ MAX + 1 , true ) ; $ prime [ 1 ] = false ; for ( $ p = 2 ; $ p * $ p <= $ MAX ; $ p ++ ) { if ( $ prime [ $ p ] == true ) { for ( $ i = $ p * 2 ; $ i <= $ MAX ; $ i += $ p ) $ prime [ $ i ] = false ; } } } function solve ( $ n ) { global $ prime ; $ count = 0 ; $ num = 1 ; $ prod = 1 ; while ( $ count < $ n ) { if ( $ prime [ $ num ] == true ) { $ prod *= $ num ; $ count ++ ; } $ num ++ ; } return $ prod ; } SieveOfEratosthenes ( ) ; $ n = 5 ; echo solve ( $ n ) ; ? >


"Find the Rotation Count in Rotated Sorted array | Returns count of rotations for an array which is first sorted in ascending order , then rotated ; This condition is needed to handle the case when array is not rotated at all ; If there is only one element left ; Find mid ; Check if element ( mid + 1 ) is minimum element . Consider the cases like { 3 , 4 , 5 , 1 , 2 } ; Check if mid itself is minimum element ; Decide whether we need to go to left half or right half ; Driver code"
Solution in php:

< ? php function countRotations ( $ arr , $ low , $ high ) { if ( $ high < $ low ) return 0 ; if ( $ high == $ low ) return $ low ; $ mid = $ low + ( $ high - $ low ) / 2 ; if ( $ mid < $ high && $ arr [ $ mid + 1 ] < $ arr [ $ mid ] ) return ( int ) ( $ mid + 1 ) ; if ( $ mid > $ low && $ arr [ $ mid ] < $ arr [ $ mid - 1 ] ) return ( int ) ( $ mid ) ; if ( $ arr [ $ high ] > $ arr [ $ mid ] ) return countRotations ( $ arr , $ low , $ mid - 1 ) ; return countRotations ( $ arr , $ mid + 1 , $ high ) ; } $ arr = array ( 15 , 18 , 2 , 3 , 6 , 12 ) ; $ n = sizeof ( $ arr ) ; echo countRotations ( $ arr , 0 , $ n - 1 ) ; ? >


"Find the Rotation Count in Rotated Sorted array | Returns count of rotations for an array which is first sorted in ascending order , then rotated ; We basically find index of minimum element ; Driver code"
Solution in php:

< ? php function countRotations ( $ arr , $ n ) { $ min = $ arr [ 0 ] ; $ min_index ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ min > $ arr [ $ i ] ) { $ min = $ arr [ $ i ] ; $ min_index = $ i ; } } return $ min_index ; } $ arr = array ( 15 , 18 , 2 , 3 , 6 , 12 ) ; $ n = sizeof ( $ arr ) ; echo countRotations ( $ arr , $ n ) ; ? >


"Find the Side of the smallest Square that can contain given 4 Big Squares | Function to find the maximum of two values ; Function to find the smallest side of the suitable suitcase ; sort array to find the smallest and largest side of suitcases ; side of the suitcase will be smallest if they arranged in 2 x 2 way so find all possible sides of that arrangement ; since suitcase should be square so find maximum of all four side ; now find greatest side and that will be the smallest square ; return the result ; Driver program ; Get the side of the 4 small squares ; Find the smallest side ; Get the side of the 4 small squares ; Find the smallest side"
Solution in php:

 < ? php function max1 ( $ a , $ b ) { if ( $ a > $ b ) return $ a ; else return $ b ; } function smallestSide ( $ a ) { sort ( $ a , 0 ) ; $ side1 = $ a [ 0 ] + $ a [ 3 ] ; $ side2 = $ a [ 1 ] + $ a [ 2 ] ; $ side3 = $ a [ 0 ] + $ a [ 1 ] ; $ side4 = $ a [ 2 ] + $ a [ 3 ] ; $ side11 = max1 ( $ side1 , $ side2 ) ; $ side12 = max1 ( $ side3 , $ side4 ) ; $ sideOfSquare = max1 ( $ side11 , $ side12 ) ; return $ sideOfSquare ; } $ side = array ( ) ; echo " Test ▁ Case ▁ 1
 " ; $ side [ 0 ] = 2 ; $ side [ 1 ] = 2 ; $ side [ 2 ] = 2 ; $ side [ 3 ] = 2 ; echo smallestSide ( $ side ) . "
 " ; echo " Test Case 2 " ; $ side [ 0 ] = 100000000000000 ; $ side [ 1 ] = 123450000000000 ; $ side [ 2 ] = 987650000000000 ; $ side [ 3 ] = 987654321000000 ; echo smallestSide ( $ side ) . "
 " ; ? >


"Find the Surface area of a 3D figure | Declaring the size of the matrix ; Absolute Difference between the height of two consecutive blocks ; Function To calculate the Total surfaceArea . ; Traversing the matrix . ; If we are traveling the topmost row in the matrix , we declare the wall above it as 0 as there is no wall above it . ; If we are traveling the leftmost column in the matrix , we declare the wall left to it as 0 as there is no wall left it . ; If its not the topmost row ; If its not the leftmost column ; Summing up the contribution of by the current block ; If its the rightmost block of the matrix it will contribute area equal to its height as a wall on the right of the figure ; If its the lowest block of the matrix it will contribute area equal to its height as a wall on the bottom of the figure ; Adding the contribution by the base and top of the figure ; Driver Code"
Solution in php:

< ? php $ M = 3 ; $ N = 3 ; function contribution_height ( $ current , $ previous ) { return abs ( $ current - $ previous ) ; } function surfaceArea ( $ A ) { global $ M ; global $ N ; $ ans = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { for ( $ j = 0 ; $ j < $ M ; $ j ++ ) { $ up = 0 ; $ left = 0 ; if ( $ i > 0 ) $ up = $ A [ $ i - 1 ] [ $ j ] ; if ( $ j > 0 ) $ left = $ A [ $ i ] [ $ j - 1 ] ; $ ans += contribution_height ( $ A [ $ i ] [ $ j ] , $ up ) + contribution_height ( $ A [ $ i ] [ $ j ] , $ left ) ; if ( $ i == $ N - 1 ) $ ans += $ A [ $ i ] [ $ j ] ; if ( $ j == $ M - 1 ) $ ans += $ A [ $ i ] [ $ j ] ; } } $ ans += $ N * $ M * 2 ; return $ ans ; } $ A = array ( array ( 1 , 3 , 4 ) , array ( 2 , 2 , 3 ) , array ( 1 , 2 , 4 ) ) ; echo surfaceArea ( $ A ) ;


"Find the altitude and area of an isosceles triangle | function to find the altitude ; return altitude ; function to find the area ; return area ; Driver Code"
Solution in php:

< ? php function altitude ( $ a , $ b ) { return sqrt ( pow ( $ a , 2 ) - ( pow ( $ b , 2 ) / 4 ) ) ; } function area ( $ b , $ h ) { return ( 1 * $ b * $ h ) / 2 ; } $ a = 2 ; $ b = 3 ; $ h = altitude ( $ a , $ b ) ; echo " Altitude = " ▁ , ▁ $ h ▁ , ▁ " , " echo " Area ▁ = ▁ " , area ( $ b , $ h ) ; ? >


"Find the area of largest circle inscribed in ellipse | PHP program to find the area of the circle ; Area of the Reuleaux triangle ; Driver Code"
Solution in php:

< ? php $ GLOBALS [ ' pi ' ] = 3.1415926 ; function areaCircle ( $ b ) { $ area = $ GLOBALS [ ' pi ' ] * $ b * $ b ; return $ area ; } $ a = 10 ; $ b = 8 ; echo round ( areaCircle ( $ b ) , 3 ) ; ? >


"Find the area of the shaded region formed by the intersection of four semicircles in a square | Function to return the area of the shaded region ; Area of the square ; Area of the semicircle ; There are 4 semicircles shadedArea = Area of 4 semicircles - Area of square ; Driver code"
Solution in php:

< ? php function findAreaShaded ( $ a ) { $ sqArea = $ a * $ a ; $ semiCircleArea = ( 3.14 * ( $ a * $ a ) / 8 ) ; $ ShadedArea = 4 * $ semiCircleArea - $ sqArea ; return $ ShadedArea ; } $ a = 10 ; echo findAreaShaded ( $ a ) ; ? >


"Find the arrangement of queue at given time | prints the arrangement at time = t ; Checking the entire queue for every moment from time = 1 to time = t . ; If current index contains ' B ' and next index contains ' G ' then swap ; Driver code"
Solution in php:


"Find the average of first N natural numbers | Return the average of first n natural numbers ; Driver Code"
Solution in php:

< ? php function avgOfFirstN ( $ n ) { return ( float ) ( 1 + $ n ) / 2 ; } $ n = 20 ; echo ( avgOfFirstN ( $ n ) ) ; ? >


"Find the center of the circle using endpoints of diameter | function to find the center of the circle ; Driven Code"
Solution in php:


"Find the closest and smaller tidy number | PHP program to find closest tidy number smaller than the given number ; check whether string violates tidy property ; if string violates tidy property , then decrease the value stored at that index by 1 and replace all the value stored right to that index by 9 ; Driver code ; num will store closest tidy number"
Solution in php:


"Find the closest pair from two sorted arrays | ar1 [ 0. . m - 1 ] and ar2 [ 0. . n - 1 ] are two given sorted arrays and x is given number . This function prints the pair from both arrays such that the sum of the pair is closest to x . ; Initialize the diff between pair sum and x . ; res_l and res_r are result indexes from ar1 [ ] and ar2 [ ] respectively ; Start from left side of ar1 [ ] and right side of ar2 [ ] ; If this pair is closer to x than the previously found closest , then update res_l , res_r and diff ; If sum of this pair is more than x , move to smaller side ; move to the greater side ; Print the result ; Driver Code"
Solution in php:


"Find the count of Strictly decreasing Subarrays | Function to count the number of strictly decreasing subarrays ; Initialize length of current decreasing subarray ; Traverse through the array ; If arr [ i + 1 ] is less than arr [ i ] , then increment length ; Else Update count and reset length ; If last length is more than 1 ; Driver Code"
Solution in php:


"Find the count of numbers that can be formed using digits 3 , 4 only and having length at max N . | Function to find the count of numbers that can be formed using digits 3 , 4 only and having length at max N . ; Driver code"
Solution in php:

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


"Find the count of palindromic sub | PHP program to find the count of palindromic sub - string of a string in it 's ascending form ; function to return count of palindromic sub - string ; calculate frequency ; calculate count of palindromic sub - string ; return result ; Driver Code"
Solution in php:

< ? php $ MAX_CHAR = 26 ; function countPalindrome ( $ str ) { global $ MAX_CHAR ; $ n = strlen ( $ str ) ; $ sum = 0 ; $ hashTable = array_fill ( 0 , $ MAX_CHAR , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ hashTable [ ord ( $ str [ $ i ] ) - ord ( ' a ' ) ] ++ ; for ( $ i = 0 ; $ i < 26 ; $ i ++ ) { if ( $ hashTable [ $ i ] ) $ sum += ( int ) ( $ hashTable [ $ i ] * ( $ hashTable [ $ i ] + 1 ) / 2 ) ; } return $ sum ; } $ str = " ananananddd " ; echo countPalindrome ( $ str ) ; ? >


"Find the count of sub | Function to return the count of required occurrence ; To store the count of occurrences ; Check first four characters from ith position ; Variables for counting the required characters ; Check the four contiguous characters which can be reordered to form ' clap ' ; If all four contiguous characters are present then increment cnt variable ; Driver code"
Solution in php:

< ? php function countOcc ( $ s ) { $ cnt = 0 ; for ( $ i = 0 ; $ i < strlen ( $ s ) - 3 ; $ i ++ ) { $ c = 0 ; $ l = 0 ; $ a = 0 ; $ p = 0 ; for ( $ j = $ i ; $ j < $ i + 4 ; $ j ++ ) { switch ( $ s [ $ j ] ) { case ' c ' : $ c ++ ; break ; case ' l ' : $ l ++ ; break ; case ' a ' : $ a ++ ; break ; case ' p ' : $ p ++ ; break ; } } if ( $ c == 1 && $ l == 1 && $ a == 1 && $ p == 1 ) $ cnt ++ ; } return $ cnt ; } $ s = " clapc " ; echo countOcc ( strtolower ( $ s ) ) ; ? >


"Find the diagonal of the Cube | Function to find length of diagonal of cube ; Formula to Find length of diagonal of cube ; Driver code ; Function call"
Solution in php:

< ? php function diagonal_length ( $ a ) { $ L ; $ L = $ a * sqrt ( 3 ) ; return $ L ; } $ a = 5 ; echo diagonal_length ( $ a ) ; ? >


"Find the distance covered to collect items at equal distances | function to calculate the distance ; Driver program"
Solution in php:

< ? php function find_distance ( $ n ) { return $ n * ( ( 3 * $ n ) + 7 ) ; } $ n = 5 ; echo " Distance = " ? >


"Find the element that appears once in an array where every other element appears twice | Return the maximum Sum of difference between consecutive elements . ; Do XOR of all elements and return ; Driver code"
Solution in php:

< ? php function findSingle ( $ ar , $ ar_size ) { $ res = $ ar [ 0 ] ; for ( $ i = 1 ; $ i < $ ar_size ; $ i ++ ) $ res = $ res ^ $ ar [ $ i ] ; return $ res ; } $ ar = array ( 2 , 3 , 5 , 4 , 5 , 3 , 4 ) ; $ n = count ( $ ar ) ; echo " Element ▁ occurring ▁ once ▁ is ▁ " , findSingle ( $ ar , $ n ) ; ? >


"Find the element that appears once | Method to find the element that occur only once ; The expression " one ▁ & ▁ arr [ i ] " gives the bits that are there in both ' ones ' and new element from arr [ ] . We add these bits to ' twos ' using bitwise OR Value of ' twos ' will be set as 0 , 3 , 3 and 1 after 1 st , 2 nd , 3 rd and 4 th iterations respectively ; XOR the new bits with previous ' ones ' to get all bits appearing odd number of times Value of ' ones ' will be set as 3 , 0 , 2 and 3 after 1 st , 2 nd , 3 rd and 4 th iterations respectively ; The common bits are those bits which appear third time . So these bits should not be there in both ' ones ' and ' twos ' . common_bit_mask contains all these bits as 0 , so that the bits can be removed from ' ones ' and ' twos ' Value of ' common _ bit _ mask ' will be set as 00 , 00 , 01 and 10 after 1 st , 2 nd , 3 rd and 4 th iterations respectively ; Remove common bits ( the bits that appear third time ) from ' ones ' Value of ' ones ' will be set as 3 , 0 , 0 and 2 after 1 st , 2 nd , 3 rd and 4 th iterations respectively ; Remove common bits ( the bits that appear third time ) from ' twos ' Value of ' twos ' will be set as 0 , 3 , 1 and 0 after 1 st , 2 nd , 3 rd and 4 th itearations respectively ; Driver Code"
Solution in php:

< ? php function getSingle ( $ arr , $ n ) { $ ones = 0 ; $ twos = 0 ; $ common_bit_mask ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ twos = $ twos | ( $ ones & $ arr [ $ i ] ) ; $ ones = $ ones ^ $ arr [ $ i ] ; $ common_bit_mask = ~ ( $ ones & $ twos ) ; $ ones &= $ common_bit_mask ; $ twos &= $ common_bit_mask ; } return $ ones ; } $ arr = array ( 3 , 3 , 2 , 3 ) ; $ n = sizeof ( $ arr ) ; echo " The ▁ element ▁ with ▁ single ▁ " . " occurrence ▁ is ▁ " , getSingle ( $ arr , $ n ) ; ? >


"Find the element that appears once | PHP code to find the element that occur only once ; Initialize result ; Iterate through every bit ; Find sum of set bits at ith position in all array elements ; The bits with sum not multiple of 3 , are the bits of element with single occurrence . ; Driver Code"
Solution in php:

< ? php $ INT_SIZE = 32 ; function getSingle ( $ arr , $ n ) { global $ INT_SIZE ; $ result = 0 ; $ x ; $ sum ; for ( $ i = 0 ; $ i < $ INT_SIZE ; $ i ++ ) { $ sum = 0 ; $ x = ( 1 << $ i ) ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ arr [ $ j ] & $ x ) $ sum ++ ; } if ( ( $ sum % 3 ) != 0 ) $ result |= $ x ; } return $ result ; } $ arr = array ( 12 , 1 , 12 , 3 , 12 , 1 , 1 , 2 , 3 , 2 , 2 , 3 , 7 ) ; $ n = sizeof ( $ arr ) ; echo " The ▁ element ▁ with ▁ single ▁ occurrence ▁ is ▁ " , getSingle ( $ arr , $ n ) ; ? >


"Find the element whose multiplication with | Function to find minimum index such that sum becomes 0 when the element is multiplied by - 1 ; Find array sum ; Find element with value equal to sum / 2 ; when sum is equal to 2 * element then this is our required element ; Driver code"
Solution in php:

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


"Find the final X and Y when they are Altering under given condition | Function to get final value of X and Y ; Following the sequence but by replacing minus with modulo ; Step 1 ; Step 2 ; Step 3 ; Otherwise terminate ; Get the initial X and Y values ; Find the result"
Solution in php:


"Find the final radiations of each Radiated Stations | Function to print the final radiations ; Function to create the array of the resultant radiations ; Resultant radiations ; Declaring index counter for left and right radiation ; Effective radiation for left and right case ; Radiation for i - th station ; Radiation increment for left stations ; Radiation increment for right stations ; Print the resultant radiation for each of the stations ; 1 - based indexing"
Solution in php:

 < ? php function print_radiation ( $ rStation , $ n ) { for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { echo $ rStation [ $ i ] . " " ; } echo "
 " ; } function radiated_Station ( $ station , $ n ) { $ rStation = array ( ) ; $ rStation = array_fill ( 0 , $ n + 1 , 0 ) ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ li = $ i - 1 ; $ ri = $ i + 1 ; $ lRad = $ station [ $ i ] - 1 ; $ rRad = $ station [ $ i ] - 1 ; $ rStation [ $ i ] += $ station [ $ i ] ; while ( $ li >= 1 && $ lRad >= 1 ) { $ rStation [ $ li -- ] += $ lRad -- ; } while ( $ ri <= $ n && $ rRad >= 1 ) { $ rStation [ $ ri ++ ] += $ rRad -- ; } } print_radiation ( $ rStation , $ n ) ; } $ station = array ( 0 , 7 , 9 , 12 , 2 , 5 ) ; $ n = ( sizeof ( $ station ) / sizeof ( $ station [ 0 ] ) ) - 1 ; radiated_Station ( $ station , $ n ) ; ? >


"Find the first , second and third minimum elements in an array | php program to find the first , second and third minimum element in an array ; Check if current element is less than firstmin , then update first , second and third ; Check if current element is less than secmin then update second and third ; Check if current element is less than then update third ; Driver code"
Solution in php:

 < ? php function Print3Smallest ( $ array , $ n ) { $ MAX = 100000 ; $ firstmin = $ MAX ; $ secmin = $ MAX ; $ thirdmin = $ MAX ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ array [ $ i ] < $ firstmin ) { $ thirdmin = $ secmin ; $ secmin = $ firstmin ; $ firstmin = $ array [ $ i ] ; } else if ( $ array [ $ i ] < $ secmin ) { $ thirdmin = $ secmin ; $ secmin = $ array [ $ i ] ; } else if ( $ array [ $ i ] < $ thirdmin ) $ thirdmin = $ array [ $ i ] ; } echo " First min = " . $ firstmin . " " ;
 	 echo ▁ " Second min = " . $ secmin . " " ;
 	 echo ▁ " Third min = " . $ thirdmin . " " } $ array = array ( 4 , 9 , 1 , 32 , 12 ) ; $ n = sizeof ( $ array ) / sizeof ( $ array [ 0 ] ) ; Print3Smallest ( $ array , $ n ) ; ? >


"Find the first natural number whose factorial is divisible by x | GCD function to compute the greatest divisor among a and b ; Returns first number whose factorial divides x . ; Result ; Remove common factors ; We found first i . ; Driver code"
Solution in php:

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


"Find the first natural number whose factorial is divisible by x | Returns first number whose factorial divides x . ; Result ; Driver code"
Solution in php:

< ? php function firstFactorialDivisibleNumber ( $ x ) { $ i = 1 ; $ fact = 1 ; for ( $ i = 1 ; $ i < $ x ; $ i ++ ) { $ fact = $ fact * $ i ; if ( $ fact % $ x == 0 ) break ; } return $ i ; } $ x = 16 ; echo ( firstFactorialDivisibleNumber ( $ x ) ) ; ? >


"Find the first natural number whose factorial is divisible by x | function for calculating factorial ; function for check Special_Factorial_Number ; call fact function and the Modulo with k and check if condition is TRUE then return i ; taking input"
Solution in php:

< ? php function fact ( $ n ) { $ num = 1 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ num = $ num * $ i ; return $ num ; } function Special_Factorial_Number ( $ k ) { for ( $ i = 1 ; $ i <= $ k ; $ i ++ ) { if ( ( fact ( $ i ) % $ k ) == 0 ) { return $ i ; } } } $ k = 16 ; echo Special_Factorial_Number ( $ k ) ; ? >


"Find the foot of perpendicular of a point in a 3 D plane | Function to find foot of perpendicular ; Driver Code ; function call"
Solution in php: