"Find the good permutation of first N natural numbers | Function to print the good permutation of first N natural numbers ; If n is odd ; Otherwise ; Driver code"
Solution in php:

< ? php function printPermutation ( $ n ) { if ( $ n % 2 != 0 ) { echo ( " - 1" ) ; } else for ( $ i = 1 ; $ i <= $ n / 2 ; $ i ++ ) { echo ( 2 * $ i . " ▁ " . ( ( 2 * $ i ) - 1 ) . " ▁ " ) ; } return $ n ; } $ n = 4 ; printPermutation ( $ n ) ; ? >


"Find the highest occurring digit in prime numbers in a range | Sieve of Eratosthenes ; Returns maximum occurring digits in primes from l to r . ; Finding the prime number up to R . ; Initialise frequency of all digit to 0. ; For all number between L to R , check if prime or not . If prime , incrementing the frequency of digits present in the prime number . ; $p = $i ; If i is prime ; Finding digit with highest frequency . ; Driver Code"
Solution in php:

< ? php function sieve ( & $ prime , $ n ) { for ( $ p = 2 ; $ p * $ p <= $ n ; $ p ++ ) { if ( $ prime [ $ p ] == false ) for ( $ i = $ p * 2 ; $ i <= $ n ; $ i += $ p ) $ prime [ $ i ] = true ; } } function maxDigitInPrimes ( $ L , $ R ) { $ prime = array_fill ( 0 , $ R + 1 , false ) ; sieve ( $ prime , $ R ) ; $ freq = array_fill ( 0 , 10 , 0 ) ; for ( $ i = $ L ; $ i <= $ R ; $ i ++ ) { if ( ! $ prime [ $ i ] ) { while ( $ p ) { $ freq [ $ p % 10 ] ++ ; $ p = ( int ) ( $ p / 10 ) ; } } } $ max = $ freq [ 0 ] ; $ ans = 0 ; for ( $ j = 1 ; $ j < 10 ; $ j ++ ) { if ( $ max <= $ freq [ $ j ] ) { $ max = $ freq [ $ j ] ; $ ans = $ j ; } } return $ ans ; } $ L = 1 ; $ R = 20 ; echo maxDigitInPrimes ( $ L , $ R ) ; ? >


"Find the index of the left pointer after possible moves in the array | Function that returns the index of the left pointer ; there 's only one element  in the array ; initially both are at end ; Driver code"
Solution in php:

< ? php function getIndex ( $ a , $ n ) { if ( $ n == 1 ) return 0 ; $ ptrL = 0 ; $ ptrR = $ n - 1 ; $ sumL = $ a [ 0 ] ; $ sumR = $ a [ $ n - 1 ] ; while ( $ ptrR - $ ptrL > 1 ) { if ( $ sumL < $ sumR ) { $ ptrL ++ ; $ sumL += $ a [ $ ptrL ] ; } else if ( $ sumL > $ sumR ) { $ ptrR -- ; $ sumR += $ a [ $ ptrR ] ; } else { break ; } } return $ ptrL ; } $ a = array ( 2 , 7 , 9 , 8 , 7 ) ; $ n = count ( $ a ) ; echo getIndex ( $ a , $ n ) ; ? >


"Find the intersection of two Matrices | Function to print the resultant matrix ; print element value for equal elements else * ; Driver Code"
Solution in php:


"Find the kth element in the series generated by the given N ranges | Function to return the kth element of the required series ; To store the number of integers that lie upto the ith index ; Compute the number of integers ; Stores the index , lying from 1 to n , ; Using binary search , find the index in which the kth element will lie ; Find the position of the kth element in the interval in which it lies ; Driver code"
Solution in php:


"Find the largest good number in the divisors of given number N | function to return distinct prime factors ; to store distinct prime factors ; run a loop upto sqrt ( n ) ; place this prime factor in vector ; This condition is to handle the case when n is a prime number greater than 1 ; function that returns good number ; distinct prime factors ; to store answer ; product of all distinct prime factors is required answer ; Driver code ; function call"
Solution in php:

< ? php function PrimeFactors ( $ n ) { $ v = array ( ) ; $ x = $ n ; for ( $ i = 2 ; $ i * $ i <= $ n ; $ i ++ ) { if ( $ x % $ i == 0 ) { array_push ( $ v , $ i ) ; while ( $ x % $ i == 0 ) $ x /= $ i ; } } if ( $ x > 1 ) array_push ( $ v , $ x ) ; return $ v ; } function GoodNumber ( $ n ) { $ v = PrimeFactors ( $ n ) ; $ ans = 1 ; for ( $ i = 0 ; $ i < count ( $ v ) ; $ i ++ ) $ ans *= $ v [ $ i ] ; return $ ans ; } $ n = 12 ; echo GoodNumber ( $ n ) ; ? >


"Find the largest number that can be formed with the given digits | Function to generate largest possible number with given digits ; Declare a hash array of size 10 and initialize all the elements to zero ; store the number of occurrences of the digits in the given array into the hash table ; Traverse the hash in descending order to print the required number ; Print the number of times a digits occurs ; Driver code"
Solution in php:

< ? php function findMaxNum ( $ arr , $ n ) { $ hash = array_fill ( 0 , 10 , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ hash [ $ arr [ $ i ] ] += 1 ; for ( $ i = 9 ; $ i >= 0 ; $ i -- ) for ( $ j = 0 ; $ j < $ hash [ $ i ] ; $ j ++ ) echo $ i ; } $ arr = array ( 1 , 2 , 3 , 4 , 5 , 0 ) ; $ n = sizeof ( $ arr ) ; findMaxNum ( $ arr , $ n ) ; ? >


"Find the largest number that can be formed with the given digits | Function to generate largest possible number with given digits ; sort the given array in descending order ; generate the number ; Driver code"
Solution in php:

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


"Find the largest twins in given range | Function to find twins ; Create a boolean array " prime [ 0 . . high ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Look for the smallest twin ; If p is not marked , then it is a prime ; Update all multiples of p ; Now print the largest twin in range ; Driver program"
Solution in php:

< ? php function printTwins ( $ low , $ high ) { $ prime [ $ high + 1 ] = array ( ) ; $ twin = false ; $ prime = array_fill ( 0 , ( $ high + 1 ) , true ) ; $ prime [ 0 ] = $ prime [ 1 ] = false ; for ( $ p = 2 ; $ p <= floor ( sqrt ( $ high ) ) + 1 ; $ p ++ ) { if ( $ prime [ $ p ] ) { for ( $ i = $ p * 2 ; $ i <= $ high ; $ i += $ p ) $ prime [ $ i ] = false ; } } for ( $ i = $ high ; $ i >= $ low ; $ i -- ) { if ( $ prime [ $ i ] && ( $ i - 2 >= $ low && $ prime [ $ i - 2 ] == true ) ) { echo " Largest ▁ twins ▁ in ▁ given ▁ range : ▁ ( " , $ i - 2 , " , ▁ " , $ i , " ) " ; $ twin = true ; break ; } } if ( $ twin == false ) echo " No ▁ such ▁ pair ▁ exists " ; } printTwins ( 10 , 100 ) ; ? >


"Find the last digit of given series | Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; $x = $x % $p ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now $y = $y >> 1 ; y = y / 2 ; Returns modulo inverse of a with respect to m using extended Euclid Algorithm ; q is quotient ; m is remainder now , process same as Euclid 's algo  ; Make x1 positive ; Function to calculate the above expression ; Initialize the result ; Compute first part of expression ; Compute second part of expression i . e . , ( ( 4 ^ ( n + 1 ) - 1 ) / 3 ) mod 10 Since division of 3 in modulo can ' t ▁ ▁ ▁ be ▁ performed ▁ directly ▁ therefore ▁ we ▁ ▁ ▁ need ▁ to ▁ find ▁ it ' s modulo Inverse ; Driver code"
Solution in php:

 < ? php function powermod ( $ x , $ y , $ p ) { while ( $ y > 0 ) { if ( ( $ y & 1 ) > 0 ) $ res = ( $ res * $ x ) % $ p ; $ x = ( $ x * $ x ) % $ p ; } return $ res ; } function modInverse ( $ a , $ m ) { $ m0 = $ m ; $ x0 = 0 ; $ x1 = 1 ; if ( $ m == 1 ) return 0 ; while ( $ a > 1 ) { $ q = ( int ) ( $ a / $ m ) ; $ t = $ m ; $ m = $ a % $ m ; $ a = $ t ; $ t = $ x0 ; $ x0 = $ x1 - $ q * $ x0 ; $ x1 = $ t ; } if ( $ x1 < 0 ) $ x1 += $ m0 ; return $ x1 ; } function evaluteExpression ( $ n ) { $ firstsum = 0 ; $ mod = 10 ; for ( $ i = 2 , $ j = 0 ; ( 1 << $ j ) <= $ n ; $ i *= $ i , ++ $ j ) $ firstsum = ( $ firstsum + $ i ) % $ mod ; $ secondsum = ( powermod ( 4 , $ n + 1 , $ mod ) - 1 ) * modInverse ( 3 , $ mod ) ; return ( $ firstsum * $ secondsum ) % $ mod ; } $ n = 3 ; echo evaluteExpression ( $ n ) . "
 " ; $ n = 10 ; echo evaluteExpression ( $ n ) ; ? >


"Find the last digit when factorial of A divides factorial of B | Function which computes the last digit of resultant of B ! / A ! ; If A = B , B ! = A ! and B ! / A ! = 1 ; If difference ( B - A ) >= 5 , answer = 0 ; If non of the conditions are true , we iterate from A + 1 to B and multiply them . We are only concerned for the last digit , thus we take modulus of 10 ; Driver Code"
Solution in php:

< ? php function computeLastDigit ( $ A , $ B ) { $ variable = 1 ; if ( $ A == $ B ) return 1 ; else if ( ( $ B - $ A ) >= 5 ) return 0 ; else { for ( $ i = $ A + 1 ; $ i <= $ B ; $ i ++ ) $ variable = ( $ variable * ( $ i % 10 ) ) % 10 ; return $ variable % 10 ; } } echo computeLastDigit ( 2632 , 2634 ) ; ? >


"Find the lexicographically largest palindromic Subsequence of a String | Function to find the largest palindromic subsequence ; Find the largest character ; Append all occurrences of largest character to the resultant string ; Driver Code"
Solution in php:

< ? php function largestPalinSub ( $ s ) { $ res = " " ; $ mx = $ s [ 0 ] ; for ( $ i = 1 ; $ i < strlen ( $ s ) ; $ i ++ ) { $ mx = max ( $ mx , $ s [ $ i ] ) ; } for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) { if ( $ s [ $ i ] == $ mx ) { $ res . = $ s [ $ i ] ; } } return $ res ; } $ s = " geeksforgeeks " ; echo ( largestPalinSub ( $ s ) ) ; ? >


"Find the lexicographically smallest string which satisfies the given condition | Function to return the required string ; First character will always be ' a ' ; To store the resultant string ; Since length of the string should be greater than 0 and first element of array should be 1 ; Check one by one all element of given prefix array ; If the difference between any two consecutive elements of the prefix array is greater than 1 then there will be no such string possible that satisfies the given array Also , string cannot have more than 26 distinct characters ; If difference is 0 then the ( i + 1 ) th character will be same as the ith character ; If difference is 1 then the ( i + 1 ) th character will be different from the ith character ; Return the resultant string ; Driver code"
Solution in php:


"Find the longest common prefix between two strings after performing swaps on second string | PHP program to find the longest common prefix between two strings after performing swaps on the second string ; $a = strlen ( $x ) ; length of x $b = strlen ( $y ) ; length of y ; creating frequency array of characters of y ; storing the length of longest common prefix ; checking if the frequency of the character at position i in x in b is greater than zero or not if zero we increase the prefix count by 1 ; Driver Code"
Solution in php:


"Find the longest sub | Function to find longest prefix suffix ; To store longest prefix suffix ; Length of the previous longest prefix suffix ; lps [ 0 ] is always 0 ; Loop calculates lps [ i ] for i = 1 to n - 1 ; ( pat [ i ] != pat [ len ] ) ; If len = 0 ; Function to find the longest substring which is prefix as well as a sub - string of s [ 1. . . n - 2 ] ; Find longest prefix suffix ; If lps of n - 1 is zero ; At any position lps [ i ] equals to lps [ n - 1 ] ; If answer is not possible ; Driver code ; function call"
Solution in php:

< ? php function compute_lps ( $ s ) { $ n = strlen ( $ s ) ; $ lps = array ( ) ; $ len = 0 ; $ lps [ 0 ] = 0 ; $ i = 1 ; while ( $ i < $ n ) { if ( $ s [ $ i ] == $ s [ $ len ] ) { $ len ++ ; $ lps [ $ i ] = $ len ; $ i ++ ; } else { if ( $ len != 0 ) $ len = $ lps [ $ len - 1 ] ; else { $ lps [ $ i ] = 0 ; $ i ++ ; } } } return $ lps ; } function Longestsubstring ( $ s ) { $ lps = compute_lps ( $ s ) ; $ n = strlen ( $ s ) ; if ( $ lps [ $ n - 1 ] == 0 ) { echo - 1 ; return ; } for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { if ( $ lps [ $ i ] == $ lps [ $ n - 1 ] ) { echo substr ( $ s , 0 , $ lps [ $ i ] ) ; return ; } } if ( $ lps [ $ lps [ $ n - 1 ] - 1 ] == 0 ) echo - 1 ; else echo substr ( $ s , 0 , $ lps [ $ lps [ $ n - 1 ] - 1 ] ) ; } $ s = " fixprefixsuffix " ; Longestsubstring ( $ s ) ; ? >


"Find the longest subsequence of an array having LCM at most K | Function to find the longest subsequence having LCM less than or equal to K ; Map to store unique elements and their frequencies ; Update the frequencies ; Array to store the count of numbers whom 1 <= X <= K is a multiple of ; Check every unique element ; Find all its multiples <= K ; Store its frequency ; Obtain the number having maximum count ; Condition to check if answer doesn 't exist ; Print the answer ; Driver code"
Solution in php:

 < ? php function findSubsequence ( $ arr , $ n , $ k ) { $ M = array ( ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ M [ $ arr [ $ i ] ] = 0 ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) ++ $ M [ $ arr [ $ i ] ] ; $ numCount = array ( ) ; for ( $ i = 0 ; $ i <= $ k ; ++ $ i ) $ numCount [ $ i ] = 0 ; foreach ( $ M as $ key = > $ value ) { if ( $ key <= $ k ) { for ( $ i = 1 ; ; ++ $ i ) { if ( $ key * $ i > $ k ) break ; $ numCount [ $ key * $ i ] += $ value ; } } else break ; } $ lcm = 0 ; $ length = 0 ; for ( $ i = 1 ; $ i <= $ k ; ++ $ i ) { if ( $ numCount [ $ i ] > $ length ) { $ length = $ numCount [ $ i ] ; $ lcm = $ i ; } } if ( $ lcm == 0 ) echo - 1 << "
 " ; else { echo " LCM = " , ▁ $ lcm ,
 	 	 	 " , Length = " , ▁ $ length , ▁ " " ;
 	 	 echo ▁ " Indexes = " for ( $ i = 0 ; $ i < $ n ; ++ $ i ) if ( $ lcm % $ arr [ $ i ] == 0 ) echo $ i , " ▁ " ; } } $ k = 14 ; $ arr = array ( 2 , 3 , 4 , 5 ) ; $ n = count ( $ arr ) ; findSubsequence ( $ arr , $ n , $ k ) ; ? >


"Find the maximum element in an array which is first increasing and then decreasing | PHP program to Find the maximum element in an array which is first increasing and then decreasing ; Base Case : Only one element is present in arr [ low . . high ] ; If there are two elements and first is greater then the first element is maximum ; If there are two elements and second is greater then the second element is maximum ; If we reach a point where arr [ mid ] is greater than both of its adjacent elements arr [ mid - 1 ] and arr [ mid + 1 ] , then arr [ mid ] is the maximum element ; If arr [ mid ] is greater than the next element and smaller than the previous element then maximum lies on left side of mid ; when arr [ mid ] is greater than arr [ mid - 1 ] and smaller than arr [ mid + 1 ] ; Driver Code"
Solution in php:

< ? php function findMaximum ( $ arr , $ low , $ high ) { if ( $ low == $ high ) return $ arr [ $ low ] ; if ( ( $ high == $ low + 1 ) && $ arr [ $ low ] >= $ arr [ $ high ] ) return $ arr [ $ low ] ; if ( ( $ high == $ low + 1 ) && $ arr [ $ low ] < $ arr [ $ high ] ) return $ arr [ $ high ] ; $ mid = ( $ low + $ high ) / 2 ; if ( $ arr [ $ mid ] > $ arr [ $ mid + 1 ] && $ arr [ $ mid ] > $ arr [ $ mid - 1 ] ) return $ arr [ $ mid ] ; if ( $ arr [ $ mid ] > $ arr [ $ mid + 1 ] && $ arr [ $ mid ] < $ arr [ $ mid - 1 ] ) return findMaximum ( $ arr , $ low , $ mid - 1 ) ; else return findMaximum ( $ arr , $ mid + 1 , $ high ) ; } $ arr = array ( 1 , 3 , 50 , 10 , 9 , 7 , 6 ) ; $ n = sizeof ( $ arr ) ; echo ( " The ▁ maximum ▁ element ▁ is ▁ " ) ; echo ( findMaximum ( $ arr , 0 , $ n -1 ) ) ; ? >


"Find the maximum element in an array which is first increasing and then decreasing | PHP program to Find the maximum element in an array which is first increasing and then decreasing ; Driver Code"
Solution in php:

< ? php function findMaximum ( $ arr , $ low , $ high ) { $ max = $ arr [ $ low ] ; $ i ; for ( $ i = $ low ; $ i <= $ high ; $ i ++ ) { if ( $ arr [ $ i ] > $ max ) $ max = $ arr [ $ i ] ; } return $ max ; } $ arr = array ( 1 , 30 , 40 , 50 , 60 , 70 , 23 , 20 ) ; $ n = count ( $ arr ) ; echo " The ▁ maximum ▁ element ▁ is ▁ " , findMaximum ( $ arr , 0 , $ n - 1 ) ; ? >


"Find the maximum number of composite summands of a number | PHP implementation of the above approach ; Function to generate the dp array ; combination of three integers ; take the maximum number of summands ; Function to find the maximum number of summands ; If n is a smaller number , less than 16 return dp [ n ] ; Else , find a minimal number t as explained in solution ; Driver code ; Generate dp array"
Solution in php:

< ? php $ maxn = 16 ; function precompute ( ) { $ dp = array_fill ( 0 , $ GLOBALS [ ' axn ' , -1 ) ; $ dp [ 0 ] = 0 ; $ v = array ( 4 , 6 , 9 ) ; for ( $ i = 1 ; $ i < $ GLOBALS [ ' axn ' ; ++ $ i ) { for ( $ k = 0 ; $ k < 3 ; $ k ++ ) { $ j = $ v [ $ k ] ; if ( $ i >= $ j && $ dp [ $ i - $ j ] != -1 ) { $ dp [ $ i ] = max ( $ dp [ $ i ] , $ dp [ $ i - $ j ] + 1 ) ; } } } return $ dp ; } function Maximum_Summands ( $ dp , $ n ) { if ( $ n < $ GLOBALS [ ' axn ' ) return $ dp [ $ n ] ; else { $ t = ( $ n - $ GLOBALS [ ' axn ' ) / 4 + 1 ; return $ t + $ dp [ $ n - 4 * $ t ] ; } } $ n = 12 ; $ dp = precompute ( ) ; echo Maximum_Summands ( $ dp , $ n ) ; ? >


"Find the maximum number of handshakes | Calculating the maximum number of handshake using derived formula . ; Driver Code"
Solution in php:

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


"Find the maximum possible value of a [ i ] % a [ j ] over all pairs of i and j | Function that returns the second largest element in the array if exists , else 0 ; There must be at least two elements ; To store the maximum and the second maximum element from the array ; If current element is greater than first then update both first and second ; If arr [ i ] is in between first and second then update second ; No second maximum found ; Driver code"
Solution in php:

< ? php function getMaxValue ( $ arr , $ arr_size ) { if ( $ arr_size < 2 ) { return 0 ; } $ first = $ second = - ( PHP_INT_MAX - 1 ) ; for ( $ i = 0 ; $ i < $ arr_size ; $ i ++ ) { if ( $ arr [ $ i ] > $ first ) { $ second = $ first ; $ first = $ arr [ $ i ] ; } else if ( $ arr [ $ i ] > $ second && $ arr [ $ i ] != $ first ) $ second = $ arr [ $ i ] ; } if ( $ second == - ( PHP_INT_MAX - 1 ) ) return 0 ; else return $ second ; } $ arr = array ( 4 , 5 , 1 , 8 ) ; $ n = count ( $ arr ) ; echo getMaxValue ( $ arr , $ n ) ; ? >


"Find the maximum subarray XOR in a given array | A simple PHP program to find max subarray XOR ; Initialize result ; Pick starting points of subarrays ; to store xor of current subarray ; Pick ending points of subarrays starting with i ; Driver Code"
Solution in php:

< ? php function maxSubarrayXOR ( $ arr , $ n ) { $ ans = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ curr_xor = 0 ; for ( $ j = $ i ; $ j < $ n ; $ j ++ ) { $ curr_xor = $ curr_xor ^ $ arr [ $ j ] ; $ ans = max ( $ ans , $ curr_xor ) ; } } return $ ans ; } $ arr = array ( 8 , 1 , 2 , 12 ) ; $ n = count ( $ arr ) ; echo " Max ▁ subarray ▁ XOR ▁ is ▁ " , maxSubarrayXOR ( $ arr , $ n ) ; ? >


"Find the maximum subset XOR of a given set | Number of bits to represent int ; Function to return maximum XOR subset in set [ ] ; Initialize index of chosen elements ; Traverse through all bits of integer starting from the most significant bit ( MSB ) ; Initialize index of maximum element and the maximum element ; If i 'th bit of set[j]  is set and set[j] is  greater than max so far. ; If there was no element with i 'th  bit set, move to smaller i ; Put maximum element with i ' th ▁ bit ▁ ▁ set ▁ at ▁ index ▁ ' index ' ; Update maxInd and increment index ; Do XOR of set [ maxIndex ] with all numbers having i 'th bit as set. ; XOR set [ maxInd ] those numbers which have the i 'th bit set ; Increment index of chosen elements ; Final result is XOR of all elements ; Driver Code"
Solution in php:

< ? php $ INT_BITS = 32 ; function maxSubarrayXOR ( & $ set , $ n ) { global $ INT_BITS ; $ index = 0 ; for ( $ i = $ INT_BITS - 1 ; $ i >= 0 ; $ i -- ) { $ maxInd = $ index ; $ maxEle = 0 ; for ( $ j = $ index ; $ j < $ n ; $ j ++ ) { if ( ( $ set [ $ j ] & ( 1 << $ i ) ) != 0 && $ set [ $ j ] > $ maxEle ) { $ maxEle = $ set [ $ j ] ; $ maxInd = $ j ; } } if ( $ maxEle == 0 ) continue ; $ t = $ set [ $ index ] ; $ set [ $ index ] = $ set [ $ maxInd ] ; $ set [ $ maxInd ] = $ t ; $ maxInd = $ index ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ j != $ maxInd && ( $ set [ $ j ] & ( 1 << $ i ) ) != 0 ) $ set [ $ j ] = $ set [ $ j ] ^ $ set [ $ maxInd ] ; } $ index ++ ; } $ res = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ res ^= $ set [ $ i ] ; return $ res ; } $ set = array ( 9 , 8 , 5 ) ; $ n = sizeof ( $ set ) ; echo " Max ▁ subset ▁ XOR ▁ is ▁ " ; echo maxSubarrayXOR ( $ set , $ n ) ; ? >


"Find the minimum and maximum amount to buy all N candies | Function to find the minimum amount to buy all candies ; Buy current candy ; And take k candies for free from the last ; Function to find the maximum amount to buy all candies ; Buy candy with maximum amount ; And get k candies for free from the starting ; Driver Code ; Function call"
Solution in php:

< ? php function findMinimum ( $ arr , $ n , $ k ) { $ res = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ res += $ arr [ $ i ] ; $ n = $ n - $ k ; } return $ res ; } function findMaximum ( $ arr , $ n , $ k ) { $ res = 0 ; $ index = 0 ; for ( $ i = $ n - 1 ; $ i >= $ index ; $ i -- ) { $ res += $ arr [ $ i ] ; $ index += $ k ; } return $ res ; } $ arr = array ( 3 , 2 , 1 , 4 ) ; $ n = sizeof ( $ arr ) ; $ k = 2 ; sort ( $ arr ) ; sort ( $ arr , $ n ) ; echo findMinimum ( $ arr , $ n , $ k ) , " " , findMaximum ( $ arr , $ n , $ k ) ; return 0 ; ? >


"Find the minimum number of preprocess moves required to make two strings equal | Function to return the minimum number of pre - processing moves required on string A ; Length of the given strings ; To store the required answer ; To store frequency of 4 characters ; Run a loop upto n / 2 ; If size is 4 ; If size is 3 ; If size is 2 ; If n is odd ; Driver code"
Solution in php:


"Find the minimum number of steps to reach M from N | Function to find a minimum number of steps to reach M from N ; Continue till m is greater than n ; If m is odd ; add one ; divide m by 2 ; Return the required answer ; Driver code"
Solution in php:


"Find the minimum of maximum length of a jump required to reach the last island in exactly k jumps | Function that returns true if it is possible to reach end of the array in exactly k jumps ; Variable to store the number of steps required to reach the end ; If it is possible to reach the end in exactly k jumps ; Returns the minimum maximum distance required to reach the end of the array in exactly k jumps ; Stores the answer ; Binary search to calculate the result ; Driver code"
Solution in php:

< ? php function isPossible ( $ arr , $ n , $ dist , $ k ) { $ req = 0 ; $ curr = 0 ; $ prev = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { while ( $ curr != $ n && $ arr [ $ curr ] - $ arr [ $ prev ] <= $ dist ) $ curr ++ ; $ req ++ ; if ( $ curr == $ n ) break ; $ prev = $ curr - 1 ; } if ( $ curr != $ n ) return false ; if ( $ req <= $ k ) return true ; return false ; } function minDistance ( $ arr , $ n , $ k ) { $ l = 0 ; $ h = $ arr [ $ n - 1 ] ; $ ans = 0 ; while ( $ l <= $ h ) { $ m = floor ( ( $ l + $ h ) / 2 ) ; if ( isPossible ( $ arr , $ n , $ m , $ k ) ) { $ ans = $ m ; $ h = $ m - 1 ; } else $ l = $ m + 1 ; } return $ ans ; } $ arr = array ( 2 , 15 , 36 , 43 ) ; $ n = count ( $ arr ) ; $ k = 2 ; echo minDistance ( $ arr , $ n , $ k ) ; ? >


"Find the missing elements from 1 to M in given N ranges | Function to find missing elements from given Ranges ; First of all sort all the given ranges ; store ans in a different vector ; prev is use to store end of last range ; j is used as a counter for ranges ; for last segment ; finally print all answer ; Driver Code ; Store ranges in vector of pair"
Solution in php:

< ? php function findMissingNumber ( $ ranges , $ m ) { sort ( $ ranges ) ; $ ans = [ ] ; $ prev = 0 ; for ( $ j = 0 ; $ j < count ( $ ranges ) ; $ j ++ ) { $ start = $ ranges [ $ j ] [ 0 ] ; $ end = $ ranges [ $ j ] [ 1 ] ; for ( $ i = $ prev + 1 ; $ i < $ start ; $ i ++ ) array_push ( $ ans , $ i ) ; $ prev = $ end ; } for ( $ i = $ prev + 1 ; $ i < $ m + 1 ; $ i ++ ) array_push ( $ ans , $ i ) ; for ( $ i = 0 ; $ i < count ( $ ans ) ; $ i ++ ) { if ( $ ans [ $ i ] <= $ m ) echo " $ ans [ $ i ] ▁ " ; } } $ N = 2 ; $ M = 6 ; $ ranges = [ ] ; array_push ( $ ranges , [ 1 , 2 ] ) ; array_push ( $ ranges , [ 4 , 5 ] ) ; findMissingNumber ( $ ranges , $ M ) ? >


"Find the modified array after performing k operations of given type | Utility function to print the contents of an array ; Function to remove the minimum value of the array from every element of the array ; Get the minimum value from the array ; Remove the minimum value from every element of the array ; Function to remove every element of the array from the maximum value of the array ; Get the maximum value from the array ; Remove every element of the array from the maximum value of the array ; Function to print the modified array after k operations ; If k is odd then remove the minimum element of the array from every element of the array ; Else remove every element of the array from the maximum value from the array ; Print the modified array ; Driver code"
Solution in php:

< ? php function printArray ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; } function removeMin ( & $ arr , $ n ) { $ minVal = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ minVal = min ( $ minVal , $ arr [ $ i ] ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ arr [ $ i ] = $ arr [ $ i ] - $ minVal ; } function removeFromMax ( & $ arr , $ n ) { $ maxVal = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ maxVal = max ( $ maxVal , $ arr [ $ i ] ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ arr [ $ i ] = $ maxVal - $ arr [ $ i ] ; } function modifyArray ( $ arr , $ n , $ k ) { if ( $ k % 2 == 0 ) removeMin ( $ arr , $ n ) ; else removeFromMax ( $ arr , $ n ) ; printArray ( $ arr , $ n ) ; } $ arr = array ( 4 , 8 , 12 , 16 ) ; $ n = count ( $ arr ) ; $ k = 2 ; modifyArray ( $ arr , $ n , $ k ) ; ? >


"Find the most frequent digit without using array / string | Simple function to count occurrences of digit d in x ; Initialize count of digit d ; Increment count if current digit is same as d ; Returns the max occurring digit in x ; Handle negative number ; Traverse through all digits ; Count occurrences of current digit ; Update max_count and result if needed ; Driver Code"
Solution in php:

< ? php function countOccurrences ( $ x , $ d ) { $ count = 0 ; while ( $ x ) { if ( $ x % 10 == $ d ) $ count ++ ; $ x = ( int ) ( $ x / 10 ) ; } return $ count ; } function maxOccurring ( $ x ) { if ( $ x < 0 ) $ x = - $ x ; for ( $ d = 0 ; $ d <= 9 ; $ d ++ ) { $ count = countOccurrences ( $ x , $ d ) ; if ( $ count >= $ max_count ) { $ max_count = $ count ; $ result = $ d ; } } return $ result ; } $ x = 1223355 ; echo " Max ▁ occurring ▁ digit ▁ is ▁ " . maxOccurring ( $ x ) ; ? >


"Find the n | Efficient PHP program to find n - th palindrome ; Construct the nth binary palindrome with the given group number , aux_number and operation type ; No need to insert any bit in the middle ; Length of the final binary representation ; Fill first and last bit as 1 ; Start filling the a [ ] from middle , with the aux_num binary representation ; Get the auxiliary number 's ith bit and  fill around middle ; Insert bit 0 in the middle ; Length of the final binary representation ; Fill first and last bit as 1 ; Start filling the a [ ] from middle , with the aux_num binary representation ; Get the auxiliary number 's ith bit and fill  around middle ; else Insert bit 1 in the middle ; Length of the final binary representation ; Fill first and last bit as 1 ; Start filling the a [ ] from middle , with the aux_num binary representation ; Get the auxiliary number 's ith bit and fill  around middle ; Convert the number to decimal from binary ; Will return the nth binary palindrome number ; Add number of elements in all the groups , until the group of the nth number is found ; Total number of elements until this group ; Element 's offset position in the group ; Finding which bit to be placed in the middle and finding the number , which we will fill from the middle in both directions ; We need to fill this auxiliary number in binary form the middle in both directions ; $op = 0 ; Need to Insert 0 at middle ; $op = 1 ; Need to Insert 1 at middle ; Driver code ; Function Call"
Solution in php:

< ? php $ INT_SIZE = 32 ; function constructNthNumber ( $ group_no , $ aux_num , $ op ) { global $ INT_SIZE ; $ a = array_fill ( 0 , $ INT_SIZE , 0 ) ; $ num = 0 ; $ i = 0 ; $ len_f = 0 ; if ( $ op == 2 ) { $ len_f = 2 * $ group_no ; $ a [ $ len_f - 1 ] = $ a [ 0 ] = 1 ; while ( $ aux_num ) { $ a [ $ group_no + i ] = $ a [ $ group_no - 1 - $ i ] = $ aux_num & 1 ; $ aux_num = $ aux_num >> 1 ; $ i ++ ; } } else if ( $ op == 0 ) { $ len_f = 2 * $ group_no + 1 ; $ a [ $ len_f - 1 ] = $ a [ 0 ] = 1 ; $ a [ $ group_no ] = 0 ; while ( $ aux_num ) { $ a [ $ group_no + 1 + $ i ] = $ a [ $ group_no - 1 - $ i ] = $ aux_num & 1 ; $ aux_num = $ aux_num >> 1 ; $ i ++ ; } } { $ len_f = 2 * $ group_no + 1 ; $ a [ $ len_f - 1 ] = $ a [ 0 ] = 1 ; $ a [ $ group_no ] = 1 ; while ( $ aux_num ) { $ a [ $ group_no + 1 + $ i ] = $ a [ $ group_no - 1 - $ i ] = $ aux_num & 1 ; $ aux_num = $ aux_num >> 1 ; $ i ++ ; } } for ( $ i = 0 ; $ i < $ len_f ; $ i ++ ) $ num += ( 1 << $ i ) * $ a [ $ i ] ; return $ num ; } function getNthNumber ( $ n ) { $ group_no = 0 ; $ count_upto_group = 0 ; $ count_temp = 1 ; $ op = $ aux_num = 0 ; while ( $ count_temp < $ n ) { $ group_no ++ ; $ count_upto_group = $ count_temp ; $ count_temp += 3 * ( 1 << ( $ group_no - 1 ) ) ; } $ group_offset = $ n - $ count_upto_group - 1 ; if ( ( $ group_offset + 1 ) <= ( 1 << ( $ group_no - 1 ) ) ) { $ aux_num = $ group_offset ; } else { if ( ( ( $ group_offset + 1 ) - ( 1 << ( $ group_no - 1 ) ) ) % 2 ) else $ aux_num = ( int ) ( ( ( $ group_offset ) - ( 1 << ( $ group_no - 1 ) ) ) / 2 ) ; } return constructNthNumber ( $ group_no , $ aux_num , $ op ) ; } $ n = 9 ; print ( getNthNumber ( $ n ) ) ; ? >


"Find the n | Finds if the kth bit is set in the binary representation ; Returns the position of leftmost set bit in the binary representation ; Finds whether the integer in binary representation is palindrome or not ; One by one compare bits ; Compare left and right bits and converge ; Start from 1 , traverse through all the integers ; If we reach n , break the loop ; Driver code ; Function Call"
Solution in php:

< ? php function isKthBitSet ( $ x , $ k ) { return ( $ x & ( 1 << ( $ k - 1 ) ) ) ? 1 : 0 ; } function leftmostSetBit ( $ x ) { $ count = 0 ; while ( $ x ) { $ count ++ ; $ x = $ x >> 1 ; } return $ count ; } function isBinPalindrome ( $ x ) { $ l = leftmostSetBit ( $ x ) ; $ r = 1 ; while ( $ l > $ r ) { if ( isKthBitSet ( $ x , $ l ) != isKthBitSet ( $ x , $ r ) ) return 0 ; $ l -- ; $ r ++ ; } return 1 ; } function findNthPalindrome ( $ n ) { $ pal_count = 0 ; $ i = 0 ; for ( $ i = 1 ; $ i <= PHP_INT_MAX ; $ i ++ ) { if ( isBinPalindrome ( $ i ) ) { $ pal_count ++ ; } if ( $ pal_count == $ n ) break ; } return $ i ; } $ n = 9 ; echo ( findNthPalindrome ( $ n ) ) ; ? >


"Find the n | Function to return the nth string in the required sequence ; Length of the resultant string ; Relative index ; Initial string of length len consists of all a 's since the  list is sorted ; Convert relative index to Binary form and set 0 = a and 1 = b ; Reverse and return the string ; Driver Code"
Solution in php:

< ? php function obtain_str ( $ n ) { $ len = ( int ) ( log ( $ n + 1 ) / log ( 2 ) ) ; $ rel_ind = $ n + 1 - pow ( 2 , $ len ) ; $ i = 0 ; $ str = " " ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { $ str . = ' a ' ; } $ i = 0 ; while ( $ rel_ind > 0 ) { if ( $ rel_ind % 2 == 1 ) $ str [ $ i ] = ' b ' ; $ rel_ind = ( int ) ( $ rel_ind / 2 ) ; $ i ++ ; } return strrev ( $ str ) ; } $ n = 11 ; echo obtain_str ( $ n ) ; ? >


"Find the n | function to calculate nth number made of even digits only ; variable to note how many such numbers have been found till now ; bool variable to check if 1 , 3 , 5 , 7 , 9 is there or not ; checking each digit of the number ; If 1 , 3 , 5 , 7 , 9 is found temp is changed to false ; temp is true it means that it does not have 1 , 3 , 5 , 7 , 9 ; If nth such number is found return it ; Driver Code"
Solution in php:

 < ? php function findNthEvenDigitNumber ( $ n ) { $ count = 0 ; for ( $ i = 0 ; ; $ i ++ ) { $ curr = $ i ; $ isCurrEvenDigit = true ; while ( $ curr != 0 ) { if ( $ curr % 10 == 1 $ curr % 10 == 3 $ curr % 10 == 5 $ curr % 10 == 7 $ curr % 10 == 9 ) $ isCurrEvenDigit = false ; $ curr = $ curr / 10 ; } if ( $ isCurrEvenDigit == true ) $ count ++ ; if ( $ count == $ n ) return $ i ; } } echo findNthEvenDigitNumber ( 2 ) , "
 " ; echo findNthEvenDigitNumber ( 10 ) ; ? >


"Find the n | function to find nth number made of even digits only ; If n = 1 return 0 ; vector to store the digits when converted into base 5 ; Reduce n to n - 1 to exclude 0 ; Reduce n to base 5 number and store digits ; pushing the digits into vector ; variable to represent the number after converting it to base 5. Since the digits are be in reverse order , we traverse vector from back ; return 2 * result ( to convert digits 0 , 1 , 2 , 3 , 4 to 0 , 2 , 4 , 6 , 8. ; Driver Code"
Solution in php:

 < ? php function findNthEvenDigitNumber ( $ n ) { if ( $ n == 1 ) return 0 ; $ v = array ( ) ; $ n = $ n - 1 ; while ( $ n > 0 ) { array_push ( $ v , $ n % 5 ) ; $ n = ( int ) ( $ n / 5 ) ; } $ result = 0 ; for ( $ i = count ( $ v ) - 1 ; $ i >= 0 ; $ i -- ) { $ result = $ result * 10 ; $ result = $ result + $ v [ $ i ] ; } return 2 * $ result ; } echo findNthEvenDigitNumber ( 2 ) . "
 " ; echo findNthEvenDigitNumber ( 10 ) . "
 " ? >


"Find the non decreasing order array from given array | Utility function to print the contents of the array ; Function to build array B [ ] ; Lower and upper limits ; To store the required array ; Apply greedy approach ; Print the built array b [ ] ; Driver code"
Solution in php:


"Find the nth term of the given series | Function to return the nth term of the given series ; Driver code"
Solution in php:

< ? php function oddTriangularNumber ( $ N ) { return ( $ N * ( ( 2 * $ N ) - 1 ) ) ; } $ N = 3 ; echo oddTriangularNumber ( $ N ) ; ? >


"Find the nth term of the series 0 , 8 , 64 , 216 , 512 , . . . | Function to return the nth term of the given series ; Common difference ; First term ; nth term ; nth term of the given series ; Driver code"
Solution in php:


"Find the number after successive division | Function to find the number ; Driver Code"
Solution in php:

< ? php function findNum ( $ div , $ rem , $ N ) { $ num = $ rem [ $ N - 1 ] ; for ( $ i = $ N - 2 ; $ i >= 0 ; $ i -- ) { $ num = $ num * $ div [ $ i ] + $ rem [ $ i ] ; } return $ num ; } $ div = array ( 8 , 3 ) ; $ rem = array ( 2 , 2 ) ; $ N = sizeof ( $ div ) ; echo findNum ( $ div , $ rem , $ N ) ; ? >


"Find the number closest to n and divisible by m | function to find the number closest to n and divisible by m ; find the quotient ; 1 st possible closest number ; 2 nd possible closest number ; if true , then n1 is the required closest number ; else n2 is the required closest number ; Driver Code"
Solution in php:


"Find the number in a range having maximum product of the digits | Returns the product of digits of number x ; This function returns the number having maximum product of the digits ; Let the current answer be r ; Converting both integers to strings ; Stores the current number having current digit one less than current digit in b ; Replace all following digits with 9 to maximise the product ; Convert string to number ; Check if it lies in range and its product is greater than max product ; Driver Code"
Solution in php:

< ? php function product ( $ x ) { $ prod = 1 ; while ( $ x ) { $ prod *= ( $ x % 10 ) ; $ x = ( int ) ( $ x / 10 ) ; } return $ prod ; } function findNumber ( $ l , $ r ) { $ ans = $ r ; $ a = strval ( $ l ) ; $ b = strval ( $ r ) ; for ( $ i = 0 ; $ i < strlen ( $ b ) ; $ i ++ ) { if ( $ b [ $ i ] == '0' ) continue ; $ curr = $ b ; $ curr [ $ i ] = chr ( ( ( ord ( $ curr [ $ i ] ) - ord ( '0' ) ) - 1 ) + ord ( '0' ) ) ; for ( $ j = $ i + 1 ; $ j < strlen ( $ curr ) ; $ j ++ ) $ curr [ $ j ] = '9' ; $ num = 0 ; for ( $ c = 0 ; $ c < strlen ( $ curr ) ; $ c ++ ) $ num = $ num * 10 + ( ord ( $ curr [ $ c ] ) - ord ( '0' ) ) ; if ( $ num >= $ l and product ( $ ans ) < product ( $ num ) ) $ ans = $ num ; } return $ ans ; } $ l = 1 ; $ r = 10 ; print ( findNumber ( $ l , $ r ) . " " ) ; $ l = 51 ; $ r = 62 ; print ( findNumber ( $ l , $ r ) ) ; ? >


"Find the number of consecutive zero at the end after multiplying n numbers | Function to count two 's factor ; Count number of 2 s present in n ; Function to count five 's factor ; Function to count number of zeros ; Count the two 's  factor of n number ; Count the five 's  factor of n number ; Return the minimum ; Driver Code"
Solution in php:

< ? php function two_factor ( $ n ) { $ twocount = 0 ; while ( $ n % 2 == 0 ) { $ twocount ++ ; $ n = ( int ) ( $ n / 2 ) ; } return $ twocount ; } function five_factor ( $ n ) { $ fivecount = 0 ; while ( $ n % 5 == 0 ) { $ fivecount ++ ; $ n = ( int ) ( $ n / 5 ) ; } return $ fivecount ; } function find_con_zero ( $ arr , $ n ) { $ twocount = 0 ; $ fivecount = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ twocount += two_factor ( $ arr [ $ i ] ) ; $ fivecount += five_factor ( $ arr [ $ i ] ) ; } if ( $ twocount < $ fivecount ) return $ twocount ; else return $ fivecount ; } $ arr = array ( 100 , 10 , 5 , 25 , 35 , 14 ) ; $ n = 6 ; echo find_con_zero ( $ arr , $ n ) ; ? >


"Find the number of distinct pairs of vertices which have a distance of exactly k in a tree | PHP implementation of the approach ; To store vertices and value of k ; To store number vertices at a level i ; To store the final answer ; Function to add an edge between two nodes ; Function to find the number of distinct pairs of the vertices which have a distance of exactly k in a tree ; At level zero vertex itself is counted ; Count the pair of vertices at distance k ; For all levels count vertices ; Driver code ; Add edges ; Function call ; Required answer"
Solution in php:


"Find the number of divisors of all numbers in the range [ 1 , n ] | Function to find the number of divisors of all numbers in the range [ 1 , n ] ; Array to store the count of divisors ; For every number from 1 to n ; Increase divisors count for every number divisible by i ; Print the divisors ; Driver code"
Solution in php:


"Find the number of elements greater than k in a sorted array | Function to return the count of elements from the array which are greater than k ; Stores the index of the left most element from the array which is greater than k ; Finds number of elements greater than k ; If mid element is greater than k update leftGreater and r ; If mid element is less than or equal to k update l ; Return the count of elements greater than k ; Driver code"
Solution in php:

< ? php function countGreater ( $ arr , $ n , $ k ) { $ l = 0 ; $ r = $ n - 1 ; $ leftGreater = $ n ; while ( $ l <= $ r ) { $ m = $ l + ( int ) ( ( $ r - $ l ) / 2 ) ; if ( $ arr [ $ m ] > $ k ) { $ leftGreater = $ m ; $ r = $ m - 1 ; } else $ l = $ m + 1 ; } return ( $ n - $ leftGreater ) ; } $ arr = array ( 3 , 3 , 4 , 7 , 7 , 7 , 11 , 13 , 13 ) ; $ n = sizeof ( $ arr ) ; $ k = 7 ; echo countGreater ( $ arr , $ n , $ k ) ;


"Find the number of good permutations | Function to return the count of good permutations ; For m = 0 , ans is 1 ; If k is greater than 1 ; If k is greater than 2 ; If k is greater than 3 ; Driver code"
Solution in php:


"Find the number of integers from 1 to n which contains digits 0 ' s ▁ and ▁ 1' s only | Function to find the number of integers from 1 to n which contains 0 ' s ▁ and ▁ 1' s only ; If number is greater than n ; otherwise add count this number and call two functions ; Driver code"
Solution in php:

< ? php function countNumbers ( $ x , $ n ) { if ( $ x > $ n ) return 0 ; return 1 + countNumbers ( $ x * 10 , $ n ) + countNumbers ( $ x * 10 + 1 , $ n ) ; } $ n = 120 ; echo ( countNumbers ( 1 , $ n ) ) ; ? >


"Find the number of integers x in range ( 1 , N ) for which x and x + 1 have same number of divisors | PHP implementation of the approach ; To store number of divisors and Prefix sum of such numbers ; Function to find the number of integers 1 < x < N for which x and x + 1 have the same number of positive divisors ; Count the number of divisors ; Run a loop upto sqrt ( i ) ; If j is divisor of i ; If it is perfect square ; x and x + 1 have same number of positive divisors ; Function call ; Required answer"
Solution in php:


"Find the number of jumps to reach X in the number line from zero | Utility function to calculate sum of numbers from 1 to x ; Function to find the number of jumps to reach X in the number line from zero ; First make number positive Answer will be same either it is Positive or negative ; To store required answer ; Continue till number is lesser or not in same parity ; Return the required answer ; Driver code"
Solution in php:

< ? php function getsum ( $ x ) { return ( $ x * ( $ x + 1 ) ) / 2 ; } function countJumps ( $ n ) { $ n = abs ( $ n ) ; $ ans = 0 ; while ( getsum ( $ ans ) < $ n or ( getsum ( $ ans ) - $ n ) & 1 ) $ ans ++ ; return $ ans ; } $ n = 9 ; echo countJumps ( $ n ) ; ? >


"Find the number of players who roll the dice when the dice output sequence is given | Function to return the number of players ; Initialize cnt as 0 ; Iterate in the string ; Check for numbers other than x ; Driver code"
Solution in php:


"Find the number of rectangles of size 2 * 1 which can be placed inside a rectangle of size n * m | function to Find the number of rectangles of size 2 * 1 can be placed inside a rectangle of size n * m ; if n is even ; if m is even ; if both are odd ; Driver code ; function call"
Solution in php:

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


"Find the number of solutions to the given equation | Function to return the count of valid values of X ; Iterate through all possible sum of digits of X ; Get current value of X for sum of digits i ; Find sum of digits of cr ; If cr is a valid choice for X ; Return the count ; Driver code"
Solution in php:


"Find the number of spectators standing in the stadium at time t | PHP program to find number of spectators standing at a time ; If the time is less than k then we can print directly t time . ; If the time is n then k spectators are standing . ; Otherwise we calculate the spectators standing . ; Stores the value of n , k and t t is time n & k is the number of specators"
Solution in php:

< ? php function result ( $ n , $ k , $ t ) { if ( $ t <= $ k ) echo t ; else if ( $ t <= $ n ) echo k ; else { $ temp = $ t - $ n ; $ temp = $ k - $ temp ; echo $ temp ; } } $ n = 10 ; $ k = 5 ; $ t = 12 ; result ( $ n , $ k , $ t ) ; ? >


"Find the number of stair steps | Modified Binary search function to solve the equation ; if mid is solution to equation ; if our solution to equation lies between mid and mid - 1 ; if solution to equation is greater than mid ; if solution to equation is less than mid ; Driver Code ; call binary search method to solve for limits 1 to T ; Because our pattern starts from 2 , 3 , 4 , 5. . . so , we subtract 1 from ans"
Solution in php:

 < ? php function solve ( $ low , $ high , $ T ) { while ( $ low <= $ high ) { $ mid = ( $ low + $ high ) / 2 ; if ( ( $ mid * ( $ mid + 1 ) ) == $ T ) return $ mid ; if ( $ mid > 0 && ( $ mid * ( $ mid + 1 ) ) > $ T && ( $ mid * ( $ mid - 1 ) ) <= $ T ) return $ mid - 1 ; if ( ( $ mid * ( $ mid + 1 ) ) > $ T ) $ high = $ mid - 1 ; else $ low = $ mid + 1 ; } return -1 ; } $ T = 15 ; $ ans = solve ( 1 , $ T , 2 * $ T ) ; if ( $ ans != -1 ) $ ans -- ; echo " Number ▁ of ▁ stair ▁ steps ▁ = ▁ " , $ ans , "
 " ; ? >


"Find the number of sub arrays in the permutation of first N natural numbers such that their median is M | Function to return the count of sub - arrays in the given permutation of first n natural numbers such that their median is m ; If element is less than m ; If element greater than m ; If m is found ; Count the answer ; Increment sum ; Driver code"
Solution in php:


"Find the number of valid parentheses expressions of given length | Returns value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; A Binomial coefficient based function to find nth catalan number in O ( n ) time ; Calculate value of 2 nCn ; return 2 nCn / ( n + 1 ) ; Function to find possible ways to put balanced parenthesis in an expression of length n ; If n is odd , not possible to create any valid parentheses ; Otherwise return n / 2 'th  Catalan Number ; Driver Code"
Solution in php:

< ? php function binomialCoeff ( $ n , $ k ) { $ res = 1 ; if ( $ k > $ n - $ k ) $ k = $ n - $ k ; for ( $ i = 0 ; $ i < $ k ; ++ $ i ) { $ res *= ( $ n - $ i ) ; $ res /= ( $ i + 1 ) ; } return $ res ; } function catalan ( $ n ) { $ c = binomialCoeff ( 2 * $ n , $ n ) ; return $ c / ( $ n + 1 ) ; } function findWays ( $ n ) { if ( $ n & 1 ) return 0 ; return catalan ( $ n / 2 ) ; } $ n = 6 ; echo " Total ▁ possible ▁ expressions ▁ of ▁ length ▁ " , $ n , " ▁ is ▁ " , findWays ( 6 ) ; ? >


"Find the number of ways to divide number into four parts such that a = c and b = d | Function to find the number of ways to divide N into four parts such that a = c and b = d ; Driver code"
Solution in php:

< ? php function possibleways ( $ n ) { if ( $ n % 2 == 1 ) return 0 ; else if ( $ n % 4 == 0 ) return $ n / 4 - 1 ; else return $ n / 4 ; } $ n = 20 ; echo possibleways ( $ n ) ; ? >


"Find the occurrences of digit d in the range [ 0. . n ] | PHP program to count appearances of a digit ' d ' in range from [ 0. . n ] ; Initialize result ; Count appearances in numbers starting from d . ; When the last digit is equal to d ; When the first digit is equal to d then ; increment result as well as number ; In case of reverse of number such as 12 and 21 ; Driver code"
Solution in php:

< ? php function getOccurence ( $ n , $ d ) { $ result = 0 ; $ itr = $ d ; while ( $ itr <= $ n ) { if ( $ itr % 10 == $ d ) $ result ++ ; if ( $ itr != 0 && floor ( $ itr / 10 ) == $ d ) { $ result ++ ; $ itr ++ ; } else if ( floor ( $ itr / 10 ) == $ d - 1 ) $ itr = $ itr + ( 10 - $ d ) ; else $ itr = $ itr + 10 ; } return $ result ; } $ n = 11 ; $ d = 1 ; echo getOccurence ( $ n , $ d ) ; ? >


"Find the one missing number in range | Find the missing number in a range ; here we xor of all the number ; xor last number ; Driver code"
Solution in php:

< ? php function missingNum ( $ arr , $ n ) { $ minvalue = min ( $ arr ) ; $ xornum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ xornum ^= ( $ minvalue ) ^ $ arr [ $ i ] ; $ minvalue ++ ; } return $ xornum ^ $ minvalue ; } $ arr = array ( 13 , 12 , 11 , 15 ) ; $ n = sizeof ( $ arr ) ; echo missingNum ( $ arr , $ n ) ; ? >


"Find the only missing number in a sorted array | PHP program to find the only missing element . ; If this is the first element which is not index + 1 , then missing element is mid + 1 ; if this is not the first missing element search in left side ; if it follows index + 1 property then search in right side ; if no element is missing ; Driver Code"
Solution in php:

< ? php function findmissing ( & $ ar , $ N ) { $ r = $ N - 1 ; $ l = 0 ; while ( $ l <= $ r ) { $ mid = ( $ l + $ r ) / 2 ; if ( $ ar [ $ mid ] != $ mid + 1 && $ ar [ $ mid - 1 ] == $ mid ) return ( $ mid + 1 ) ; if ( $ ar [ $ mid ] != $ mid + 1 ) $ r = $ mid - 1 ; else $ l = $ mid + 1 ; } return ( -1 ) ; } $ ar = array ( 1 , 2 , 3 , 4 , 5 , 7 , 8 ) ; $ N = sizeof ( $ ar ) ; echo ( findmissing ( $ ar , $ N ) ) ; ? >


"Find the only repeating element in a sorted array of size n | Returns index of second appearance of a repeating element . The function assumes that array elements are in range from 1 to n - 1. ; low = 0 , high = n - 1 ; ; Check if the mid element is the repeating one ; If mid element is not at its position that means the repeated element is in left ; If mid is at proper position then repeated one is in right . ; Driver code"
Solution in php:

< ? php function findRepeatingElement ( $ arr , $ low , $ high ) { if ( $ low > $ high ) return -1 ; $ mid = floor ( ( $ low + $ high ) / 2 ) ; if ( $ arr [ $ mid ] != $ mid + 1 ) { if ( $ mid > 0 && $ arr [ $ mid ] == $ arr [ $ mid - 1 ] ) return $ mid ; return findRepeatingElement ( $ arr , $ low , $ mid - 1 ) ; } return findRepeatingElement ( $ arr , $ mid + 1 , $ high ) ; } $ arr = array ( 1 , 2 , 3 , 3 , 4 , 5 ) ; $ n = sizeof ( $ arr ) ; $ index = findRepeatingElement ( $ arr , 0 , $ n - 1 ) ; if ( $ index != -1 ) echo $ arr [ $ index ] ; ? >


"Find the original matrix when largest element in a row and a column are given | PHP implementation of the approach ; Function that prints the original matrix ; Iterate in the row ; Iterate in the column ; If previously existed an element ; Driver code"
Solution in php:


"Find the other end point of a line with given one end and mid | PHP function to find the end point of a line ; find end point for x coordinates ; find end point for y coordinates ; Driven Program"
Solution in php:


"Find the other number when LCM and HCF given | Function that will calculates the zeroes at the end ; Driver code ; Calling function ."
Solution in php:

< ? php function otherNumber ( $ A , $ Lcm , $ Hcf ) { return ( $ Lcm * $ Hcf ) / $ A ; } $ A = 8 ; $ Lcm = 8 ; $ Hcf = 1 ; $ result = otherNumber ( $ A , $ Lcm , $ Hcf ) ; echo " B = "


"Find the other | function to find the other - end point of diameter ; find end point for x coordinates ; find end point for y coordinates ; Driven Program"
Solution in php:

 < ? php function endPointOfDiameterofCircle ( $ x1 , $ y1 , $ c1 , $ c2 ) { echo " x2 = " , (2 ▁ * ▁ $ c1 ▁ - ▁ $ x1 ) , " " ;
 	 echo ▁ " y2 = " } $ x1 = -4 ; $ y1 = -1 ; $ c1 = 3 ; $ c2 = 5 ; endPointOfDiameterofCircle ( $ x1 , $ y1 , $ c1 , $ c2 ) ; ? >


"Find the perimeter of a cylinder | Function to calculate perimeter ; Driver Code"
Solution in php:

< ? php function perimeter ( $ diameter , $ height ) { return 2 * ( $ diameter + $ height ) ; } $ diameter = 5 ; $ height = 10 ; echo ( " Perimeter ▁ = ▁ " ) ; echo ( perimeter ( $ diameter , $ height ) ) ; echo ( " ▁ units " ) ; ? >


"Find the player who rearranges the characters to get a palindrome string first | Function that returns the winner of the game ; Initialize the freq array to 0 int freq [ 26 ] ; memset ( freq , 0 , sizeof freq ) ; $freg = array_fill ( ) ; Iterate and count the frequencies of each character in the string ; Count the odd occurring character ; If odd occurrence ; Check condition for Player - 1 winning the game ; Driver code ; Function call that returns the winner"
Solution in php:

< ? php function returnWinner ( $ s , $ l ) { $ freq = array_fill ( 0 , 26 , 0 ) ; for ( $ i = 0 ; $ i < $ l ; $ i ++ ) { $ freq [ $ s [ $ i ] - ' a ' ] ++ ; } $ cnt = 0 ; for ( $ i = 0 ; $ i < 26 ; $ i ++ ) { if ( $ freq [ $ i ] & 1 ) $ cnt ++ ; } if ( $ cnt == 0 $ cnt & 1 ) return 1 ; else return 2 ; } $ s = " abaaab " ; $ l = strlen ( $ s ) ; $ winner = returnWinner ( $ s , $ l ) ; echo " Player - " , $ winner ; ? >


"Find the point where maximum intervals overlap |  ; Finding maximum starting time O ( n ) ; Finding maximum ending time O ( n ) ; Creating and auxiliary array O ( n ) ; Lazy addition ; Lazily Calculating value at index i O ( n ) ; Driver code"
Solution in php:

 < ? php function maxOverlap ( $ start , $ end ) { $ n = count ( $ start ) ; $ maxa = max ( $ start ) ; $ maxb = max ( $ end ) ; $ maxc = max ( $ maxa , $ maxb ) ; $ x = array_fill ( 0 , $ maxc + 2 , 0 ) ; $ cur = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { ++ $ x [ $ start [ $ i ] ] ; -- $ x [ $ end [ $ i ] + 1 ] ; } $ maxy = - PHP_INT_MAX ; for ( $ i = 0 ; $ i <= $ maxc ; $ i ++ ) { $ cur += $ x [ $ i ] ; if ( $ maxy < $ cur ) { $ maxy = $ cur ; $ idx = $ i ; } } echo " Maximum ▁ value ▁ is ▁ " . $ maxy . " ▁ at ▁ position ▁ " . $ idx . "
 " ; } $ start = array ( 13 , 28 , 29 , 14 , 40 , 17 , 3 ) ; $ end = array ( 107 , 95 , 111 , 105 , 70 , 127 , 74 ) ; maxOverlap ( $ start , $ end ) ; ? >


"Find the point where maximum intervals overlap | PHP Program to find maximum guest at any time in a party ; Sort arrival and exit arrays ; guests_in indicates number of guests at a time ; Similar to merge in merge sort to process all events in sorted order ; If next event in sorted order is arrival , increment count of guests ; Update max_guests if needed ; increment index of arrival array ; If event is exit , decrement count of guests . ; of guests . ; Driver Code"
Solution in php:

< ? php function findMaxGuests ( $ arrl , $ exit , $ n ) { sort ( $ arrl ) ; sort ( $ exit ) ; $ guests_in = 1 ; $ max_guests = 1 ; $ time = $ arrl [ 0 ] ; $ i = 1 ; $ j = 0 ; while ( $ i < $ n and $ j < $ n ) { if ( $ arrl [ $ i ] <= $ exit [ $ j ] ) { $ guests_in ++ ; if ( $ guests_in > $ max_guests ) { $ max_guests = $ guests_in ; $ time = $ arrl [ $ i ] ; } $ i ++ ; } else { $ guests_in -- ; $ j ++ ; } } echo " Maximum ▁ Number ▁ of ▁ Guests ▁ = ▁ " , $ max_guests , " ▁ at ▁ time ▁ " , $ time ; } $ arr1 = array ( 1 , 2 , 10 , 5 , 5 ) ; $ exit = array ( 4 , 5 , 12 , 9 , 120 ) ; $ n = count ( $ arr1 ) ; findMaxGuests ( $ arr1 , $ exit , $ n ) ; ? >


"Find the position of box which occupies the given ball | function to find the lower bound ; Function to print the position of each boxes where a ball has to be placed ; Find the cumulative sum of array A [ ] ; Find the position of box for each ball ; Row number ; Column ( position of box in particular row ) ; Row + 1 denotes row if indexing of array start from 1 ; Driver code"
Solution in php:

 < ? php function lower_bound ( $ A , $ valueTosearch ) { $ row = 0 ; foreach ( $ A as $ key = > $ value ) { if ( $ valueTosearch <= $ value ) return $ row ; $ row ++ ; } return $ row + 1 ; } function printPosition ( $ A , $ B , $ sizeOfA , $ sizeOfB ) { for ( $ i = 1 ; $ i < $ sizeOfA ; $ i ++ ) $ A [ $ i ] += $ A [ $ i - 1 ] ; for ( $ i = 0 ; $ i < $ sizeOfB ; $ i ++ ) { $ row = lower_bound ( $ A , $ B [ $ i ] ) ; $ boxNumber = ( $ row >= 1 ) ? $ B [ $ i ] - $ A [ $ row - 1 ] : $ B [ $ i ] ; print_r ( $ row +1 . " , ▁ " . $ boxNumber ) ; echo "
 " ; } } $ A = array ( 2 , 2 , 2 , 2 ) ; $ B = array ( 1 , 2 , 3 , 4 ) ; $ sizeOfA = count ( $ A ) ; $ sizeOfB = count ( $ B ) ; printPosition ( $ A , $ B , $ sizeOfA , $ sizeOfB ) ; ? >


"Find the position of the last removed element from the array | Function to find the original position of the element which will be removed last ; take ceil of every number ; Since position is index + 1 ; Driver code"
Solution in php:


"Find the repeating and the missing | Added 3 new methods | PHP program to Find the repeating and missing elements ; Driver Code"
Solution in php:

< ? php function printTwoElements ( $ arr , $ size ) { $ i ; echo " The ▁ repeating ▁ element ▁ is " , " ▁ " ; for ( $ i = 0 ; $ i < $ size ; $ i ++ ) { if ( $ arr [ abs ( $ arr [ $ i ] ) - 1 ] > 0 ) $ arr [ abs ( $ arr [ $ i ] ) - 1 ] = - $ arr [ abs ( $ arr [ $ i ] ) - 1 ] ; else echo ( abs ( $ arr [ $ i ] ) ) ; } echo " and the missing element is " ; for ( $ i = 0 ; $ i < $ size ; $ i ++ ) { if ( $ arr [ $ i ] > 0 ) echo ( $ i + 1 ) ; } } $ arr = array ( 7 , 3 , 4 , 5 , 5 , 6 , 2 ) ; $ n = count ( $ arr ) ; printTwoElements ( $ arr , $ n ) ; ? >


"Find the repeating and the missing | Added 3 new methods | PHP program to Find the repeating and missing elements ; Will hold xor of all elements and numbers from 1 to n ; Will have only single set bit of xor1 ; Get the xor of all array elements ; XOR the previous result with numbers from 1 to n ; Get the rightmost set bit in set_bit_no ; Now divide elements in two sets by comparing rightmost set bit of xor1 with bit at same position in each element . Also , get XORs of two sets . The two XORs are the output elements . The following two for loops serve the purpose ; arr [ i ] belongs to first set ; arr [ i ] belongs to second set ; i belongs to first set ; i belongs to second set ; * x and * y hold the desired output elements ; Driver Code"
Solution in php:


"Find the resulting Colour Combination | Function to return Colour Combination ; Check for B * G = Y ; Check for B * Y = G ; Check for Y * G = B ; Driver Code"
Solution in php:

< ? php function Colour_Combination ( $ s ) { $ temp = $ s [ 0 ] ; for ( $ i = 1 ; $ i < strlen ( $ s ) ; $ i ++ ) { if ( $ temp != $ s [ $ i ] ) { if ( ( $ temp == ' B ' $ temp == ' G ' ) && ( $ s [ $ i ] == ' G ' $ s [ $ i ] == ' B ' ) ) $ temp = ' Y ' ; else if ( ( $ temp == ' B ' $ temp == ' Y ' ) && ( $ s [ $ i ] == ' Y ' $ s [ $ i ] == ' B ' ) ) $ temp = ' G ' ; else $ temp = ' B ' ; } } return $ temp ; } $ s = " GBYGB " ; echo Colour_Combination ( $ s ) ; ? >


"Find the slope of the given number | function to find slope of a number ; to store slope of the given number ' num ' ; loop from the 2 nd digit up to the 2 nd last digit of the given number ' num ' ; if the digit is a maxima ; if the digit is a minima ; required slope ; Driver Code"
Solution in php:

< ? php function slopeOfNum ( $ num , $ n ) { $ slope = 0 ; for ( $ i = 1 ; $ i < $ n - 1 ; $ i ++ ) { if ( $ num [ $ i ] > $ num [ $ i - 1 ] && $ num [ $ i ] > $ num [ $ i + 1 ] ) $ slope ++ ; else if ( $ num [ $ i ] < $ num [ $ i - 1 ] && $ num [ $ i ] < $ num [ $ i + 1 ] ) $ slope ++ ; } return $ slope ; } $ num = "1213321" ; $ n = strlen ( $ num ) ; echo " Slope = " ? >


"Find the smallest number X such that X ! contains at least Y trailing zeros . | Function to count the number of factors P in X ! ; Function to find the smallest X such that X ! contains Y trailing zeros ; Driver code"
Solution in php:

< ? php function countFactor ( $ P , $ X ) { if ( $ X < $ P ) return 0 ; return ( ( int ) ( $ X / $ P ) + countFactor ( $ P , ( $ X / $ P ) ) ) ; } function findSmallestX ( $ Y ) { $ low = 0 ; $ high = 5 * $ Y ; $ N = 0 ; while ( $ low <= $ high ) { $ mid = ( int ) ( ( $ high + $ low ) / 2 ) ; if ( countFactor ( 5 , $ mid ) < $ Y ) { $ low = $ mid + 1 ; } else { $ N = $ mid ; $ high = $ mid - 1 ; } } return $ N ; } $ Y = 10 ; echo ( findSmallestX ( $ Y ) ) ; ? >


"Find the smallest positive integer value that cannot be represented as sum of any subset of a given array | Returns the smallest number that cannot be represented as sum of subset of elements from set represented by sorted array arr [ 0. . n - 1 ] ; Initialize result ; Traverse the array and increment ' res ' if arr [ i ] is smaller than or equal to ' res ' . ; Driver Code"
Solution in php:

 < ? php function findSmallest ( $ arr , $ n ) { $ res = 1 ; for ( $ i = 0 ; $ i < $ n and $ arr [ $ i ] <= $ res ; $ i ++ ) $ res = $ res + $ arr [ $ i ] ; return $ res ; } $ arr1 = array ( 1 , 3 , 4 , 5 ) ; $ n1 = count ( $ arr1 ) ; echo findSmallest ( $ arr1 , $ n1 ) , "
 " ; $ arr2 = array ( 1 , 2 , 6 , 10 , 11 , 15 ) ; $ n2 = count ( $ arr2 ) ; echo findSmallest ( $ arr2 , $ n2 ) , "
 " ; $ arr3 = array ( 1 , 1 , 1 , 1 ) ; $ n3 = count ( $ arr3 ) ; echo findSmallest ( $ arr3 , $ n3 ) , "
 " ; $ arr4 = array ( 1 , 1 , 3 , 4 ) ; $ n4 = count ( $ arr4 ) ; echo findSmallest ( $ arr4 , $ n4 ) ; ? >


"Find the smallest positive number missing from an unsorted array | Set 2 | Function to find smallest positive missing number . ; to store current array element ; to store next array element in current traversal ; if value is negative or greater than array size , then it cannot be marked in array . So move to next element . ; traverse the array until we reach at an element which is already marked or which could not be marked . ; find first array index which is not marked which is also the smallest positive missing number . ; if all indices are marked , then smallest missing positive number is array_size + 1. ; Driver code"
Solution in php:

< ? php function findMissingNo ( $ arr , $ n ) { $ val ; $ nextval ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] <= 0 $ arr [ $ i ] > $ n ) continue ; $ val = $ arr [ $ i ] ; while ( $ arr [ $ val - 1 ] != $ val ) { $ nextval = $ arr [ $ val - 1 ] ; $ arr [ $ val - 1 ] = $ val ; $ val = $ nextval ; if ( $ val <= 0 $ val > $ n ) break ; } } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] != $ i + 1 ) { return $ i + 1 ; } } return $ n + 1 ; } $ arr = array ( 2 , 3 , 7 , 6 , 8 , -1 , -10 , 15 ) ; $ arr_size = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; $ missing = findMissingNo ( $ arr , $ arr_size ) ; echo " The ▁ smallest ▁ positive ▁ " . " missing ▁ number ▁ is ▁ " , $ missing ; ? >


"Find the smallest twins in given range | PHP program to find the smallest twin in given range ; Create a boolean array " prime [ 0 . . high ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Look for the smallest twin ; If p is not marked , then it is a prime ; Update all multiples of p ; Now print the smallest twin in range ; Driver Code"
Solution in php:

 < ? php function printTwins ( $ low , $ high ) { $ prime = array_fill ( 0 , $ high + 1 , true ) ; $ twin = false ; $ prime [ 0 ] = $ prime [ 1 ] = false ; for ( $ p = 2 ; $ p <= floor ( sqrt ( $ high ) ) + 1 ; $ p ++ ) { if ( $ prime [ $ p ] ) { for ( $ i = $ p * 2 ; $ i <= $ high ; $ i += $ p ) $ prime [ $ i ] = false ; } } for ( $ i = $ low ; $ i <= $ high ; $ i ++ ) { if ( $ prime [ $ i ] && $ prime [ $ i + 2 ] ) { print ( " Smallest ▁ twins ▁ in ▁ given ▁ range : ▁ ( $ i , ▁ " . ( $ i + 2 ) . " ) " ) ; $ twin = true ; break ; } } if ( $ twin == false ) print ( " No ▁ such ▁ pair ▁ exists
 " ) ; } printTwins ( 10 , 100 ) ; ? >


"Find the smallest window in a string containing all characters of another string | PHP program to find smallest window containing all characters of a pattern . ; Function to find smallest window containing all characters of ' pat ' ; check if string ' s ▁ length ▁ is ▁ less ▁ ▁ than ▁ pattern ' s length . If yes then no such window can exist ; store occurrence ofs characters of pattern ; start traversing the string $count = 0 ; count of characters ; count occurrence of characters of string ; If string ' s ▁ char ▁ matches ▁ with ▁ ▁ pattern ' s char then increment count ; if all the characters are matched ; Try to minimize the window i . e . , check if any character is occurring more no . of times than its occurrence in pattern , if yes then remove it from starting and also remove the useless characters . ; update window size ; If no window found ; Return substring starting from start_index and length min_len ; Driver code"
Solution in php:

 < ? php define ( " no _ of _ chars " , 256 ) ; function findSubString ( & $ str , & $ pat ) { $ len1 = strlen ( $ str ) ; $ len2 = strlen ( $ pat ) ; if ( $ len1 < $ len2 ) { echo " No ▁ such ▁ window ▁ exists " ; return " " ; } $ hash_pat = array_fill ( 0 , no_of_chars , 0 ) ; $ hash_str = array_fill ( 0 , no_of_chars , 0 ) ; for ( $ i = 0 ; $ i < $ len2 ; $ i ++ ) $ hash_pat [ ord ( $ pat [ $ i ] ) ] ++ ; $ start = 0 ; $ start_index = -1 ; $ min_len = PHP_INT_MAX ; for ( $ j = 0 ; $ j < $ len1 ; $ j ++ ) { $ hash_str [ ord ( $ str [ $ j ] ) ] ++ ; if ( $ hash_str [ ord ( $ str [ $ j ] ) ] <= $ hash_pat [ ord ( $ str [ $ j ] ) ] ) $ count ++ ; if ( $ count == $ len2 ) { while ( $ hash_str [ ord ( $ str [ $ start ] ) ] > $ hash_pat [ ord ( $ str [ $ start ] ) ] || $ hash_pat [ ord ( $ str [ $ start ] ) ] == 0 ) { if ( $ hash_str [ ord ( $ str [ $ start ] ) ] > $ hash_pat [ ord ( $ str [ $ start ] ) ] ) $ hash_str [ ord ( $ str [ $ start ] ) ] -- ; $ start ++ ; } $ len_window = $ j - $ start + 1 ; if ( $ min_len > $ len_window ) { $ min_len = $ len_window ; $ start_index = $ start ; } } } if ( $ start_index == -1 ) { echo " No ▁ such ▁ window ▁ exists " ; return " " ; } return substr ( $ str , $ start_index , $ min_len ) ; } $ str = " this ▁ is ▁ a ▁ test ▁ string " ; $ pat = " tist " ; echo " Smallest ▁ window ▁ is ▁ : ▁
 " . findSubString ( $ str , $ pat ) ; ? >


"Find the subarray with least average | Prints beginning and ending indexes of subarray of size k with minimum average ; k must be smaller than or equal to n ; Initialize beginning index of result ; Compute sum of first subarray of size k ; Initialize minimum sum as current sum ; Traverse from ( k + 1 ) ' th ▁ element ▁ ▁ to ▁ n ' th element ; Add current item and remove first item of previous subarray ; Update result if needed ; Driver Code ; Subarray size"
Solution in php:

< ? php function findMinAvgSubarray ( $ arr , $ n , $ k ) { if ( $ n < $ k ) return ; $ res_index = 0 ; $ curr_sum = 0 ; for ( $ i = 0 ; $ i < $ k ; $ i ++ ) $ curr_sum += $ arr [ $ i ] ; $ min_sum = $ curr_sum ; for ( $ i = $ k ; $ i < $ n ; $ i ++ ) { $ curr_sum += $ arr [ $ i ] - $ arr [ $ i - $ k ] ; if ( $ curr_sum < $ min_sum ) { $ min_sum = $ curr_sum ; $ res_index = ( $ i - $ k + 1 ) ; } } echo " Subarray between [ " ▁ , $ res _ index ▁ , ▁ " , " ▁ , $ res _ index ▁ + ▁ $ k ▁ - ▁ 1 , ▁ " ] has minimum average " ; } $ arr = array ( 3 , 7 , 90 , 20 , 10 , 50 , 40 ) ; $ k = 3 ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; findMinAvgSubarray ( $ arr , $ n , $ k ) ; return 0 ; ? >


"Find the sum of all Truncatable primes below N | PHP implementation of the approach ; To check if a number is prime or not ; Sieve of Eratosthenes function to find all prime numbers ; Function to return the sum of all truncatable primes below n ; To store the required sum ; Check every number below n ; Check from right to left ; If number is not prime at any stage ; Check from left to right ; If number is not prime at any stage ; If flag is still true ; Return the required sum ; Driver code"
Solution in php:

< ? php $ N = 10005 ; $ prime = array_fill ( 0 , $ N , true ) ; function sieve ( ) { global $ prime , $ N ; $ prime [ 1 ] = false ; $ prime [ 0 ] = false ; for ( $ i = 2 ; $ i < $ N ; $ i ++ ) if ( $ prime [ $ i ] ) for ( $ j = $ i * 2 ; $ j < $ N ; $ j += $ i ) $ prime [ $ j ] = false ; } function sumTruncatablePrimes ( $ n ) { global $ prime , $ N ; $ sum = 0 ; for ( $ i = 2 ; $ i < $ n ; $ i ++ ) { $ num = $ i ; $ flag = true ; while ( $ num ) { if ( ! $ prime [ $ num ] ) { $ flag = false ; break ; } $ num = ( int ) ( $ num / 10 ) ; } $ num = $ i ; $ power = 10 ; while ( ( int ) ( $ num / $ power ) ) { if ( ! $ prime [ $ num % $ power ] ) { $ flag = false ; break ; } $ power *= 10 ; } if ( $ flag ) $ sum += $ i ; } return $ sum ; } $ n = 25 ; sieve ( ) ; echo sumTruncatablePrimes ( $ n ) ; ? >


"Find the sum of all multiples of 2 and 5 below N | Function to find sum of AP series ; Number of terms ; Function to find the sum of all multiples of 2 and 5 below N ; Since , we need the sum of multiples less than N ; Driver code"
Solution in php:

< ? php function sumAP ( $ n , $ d ) { $ n = ( int ) ( $ n / $ d ) ; return ( $ n ) * ( ( 1 + $ n ) * ( int ) $ d / 2 ) ; } function sumMultiples ( $ n ) { $ n -- ; return sumAP ( $ n , 2 ) + sumAP ( $ n , 5 ) - sumAP ( $ n , 10 ) ; } $ n = 20 ; echo sumMultiples ( $ n ) ; ? >


"Find the sum of all the terms in the n | function to find the required sum ; sum = n * ( 2 * n ^ 2 + 1 ) ; Driver Code"
Solution in php:

< ? php function sumOfTermsInNthRow ( $ n ) { $ sum = $ n * ( 2 * pow ( $ n , 2 ) + 1 ) ; return $ sum ; } $ n = 4 ; echo " Sum ▁ of ▁ all ▁ the ▁ terms ▁ in ▁ nth ▁ row ▁ = ▁ " , sumOfTermsInNthRow ( $ n ) ; ? >


"Find the sum of digits of a number at even and odd places | Function to return the reverse of a number ; Function to find the sum of the odd and even positioned digits in a number ; If c is even number then it means digit extracted is at even place ; Driver code"
Solution in php:

 < ? php function reverse ( $ n ) { $ rev = 0 ; while ( $ n != 0 ) { $ rev = ( $ rev * 10 ) + ( $ n % 10 ) ; $ n = floor ( $ n / 10 ) ; } return $ rev ; } function getSum ( $ n ) { $ n = reverse ( $ n ) ; $ sumOdd = 0 ; $ sumEven = 0 ; $ c = 1 ; while ( $ n != 0 ) { if ( $ c % 2 == 0 ) $ sumEven += $ n % 10 ; else $ sumOdd += $ n % 10 ; $ n = floor ( $ n / 10 ) ; $ c ++ ; } echo " Sum odd = " , ▁ $ sumOdd , ▁ " " ;
 	 echo ▁ " Sum even = " } $ n = 457892 ; getSum ( $ n ) ; ? >


"Find the sum of first N terms of the series 2 * 3 * 5 , 3 * 5 * 7 , 4 * 7 * 9 , ... | Function to return the sum of the first n terms of the given series ; As described in the approach ; Driver code"
Solution in php:

< ? php function calculateSum ( $ n ) { return ( $ n * ( 2 * $ n * $ n * $ n + 12 * $ n * $ n + 25 * $ n + 21 ) ) / 2 ; } $ n = 3 ; echo calculateSum ( $ n ) ; ? >


"Find the sum of first N terms of the series 2 Ã — 3 + 4 Ã — 4 + 6 Ã — 5 + 8 Ã — 6 + ... | calculate sum upto N term of series ; Driver code"
Solution in php:

< ? php function Sum_upto_nth_Term ( $ n ) { $ r = $ n * ( $ n + 1 ) * ( 2 * $ n + 7 ) / 3 ; echo $ r ; } $ N = 5 ; Sum_upto_nth_Term ( $ N ) ; ? >


"Find the sum of n terms of the series 1 , 8 , 27 , 64 ... . | Function to calculate the sum ; Return total sum ; Driver code"
Solution in php:

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


"Find the sum of series 0. X + 0. XX + 0. XXX + ... upto k terms | function which return the sum of series ; Driver code"
Solution in php:

< ? php function sumOfSeries ( $ x , $ k ) { return ( ( $ x ) / 81 ) * ( 9 * $ k - 1 + pow ( 10 , ( -1 ) * $ k ) ) ; } $ x = 9 ; $ k = 20 ; echo sumOfSeries ( $ x , $ k ) ; ? >


"Find the sum of series 3 , 7 , 13 , 21 , 31. ... | Function to calculate sum ; Return sum ; Driver code"
Solution in php:

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


"Find the sum of series 3 , | calculate sum upto N term of series ; Driver code"
Solution in php:

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


"Find the sum of the diagonal elements of the given N X N spiral matrix | Function to return the sum of both the diagonal elements of the required matrix ; Array to store sum of diagonal elements ; Base cases ; Computing the value of dp ; Driver code"
Solution in php:


"Find the sum of the series 1 + 11 + 111 + 1111 + ... . . upto n terms | Function for finding summation ; Driver Code"
Solution in php:

< ? php function summation ( $ n ) { $ sum ; $ sum = ( pow ( 10 , $ n + 1 ) - 10 - ( 9 * $ n ) ) / 81 ; return $ sum ; } $ n = 5 ; echo summation ( $ n ) ; ? >


"Find the sum of the series x ( x + y ) + x ^ 2 ( x ^ 2 + y ^ 2 ) + x ^ 3 ( x ^ 3 + y ^ 3 ) + ... + x ^ n ( x ^ n + y ^ n ) | Function to return required sum ; sum of first series ; sum of second series ; Driver code ; function call to print sum"
Solution in php:


"Find the super power of a given Number | PHP for finding super power of n ; global hash for prime ; sieve method for storing a list of prime ; function to return super power ; find the super power ; Driver Code"
Solution in php:

< ? php $ MAX = 100000 ; $ prime = array_fill ( 0 , 100002 , true ) ; function SieveOfEratosthenes ( ) { global $ MAX , $ prime ; for ( $ p = 2 ; $ p * $ p <= $ MAX ; $ p ++ ) if ( $ prime [ $ p ] == true ) for ( $ i = $ p * 2 ; $ i <= $ MAX ; $ i += $ p ) $ prime [ $ i ] = false ; } function superpower ( $ n ) { SieveOfEratosthenes ( ) ; global $ MAX , $ prime ; $ superPower = 0 ; $ factor = 0 ; $ i = 2 ; while ( $ n > 1 && $ i <= $ MAX ) { if ( $ prime [ $ i ] ) { $ factor = 0 ; while ( $ n % $ i == 0 && $ n > 1 ) { $ factor ++ ; $ n = $ n / $ i ; } if ( $ superPower < $ factor ) $ superPower = $ factor ; } $ i ++ ; } return $ superPower ; } $ n = 256 ; echo superpower ( $ n ) ; ? >


"Find the time which is palindromic and comes after the given time | Function to return the required time ; To store the resultant time ; Hours are stored in h as integer ; Minutes are stored in m as integer ; Reverse of h ; Reverse of h as a string ; If MM < reverse of ( HH ) ; 0 is added if HH < 10 ; 0 is added if rev_h < 10 ; Increment hours ; Reverse of the hour after incrementing 1 ; 0 is added if HH < 10 ; 0 is added if rev_h < 10 ; Driver code"
Solution in php:


"Find the total Number of Digits in ( N ! ) N | Function to find the total Number of Digits in ( N ! ) ^ N ; Finding X ; Calculating N * X ; Floor ( N * X ) + 1 return ceil ( $sum ) ; equivalent to floor ( sum ) + 1 ; Driver code"
Solution in php:

< ? php function CountDigits ( $ n ) { if ( $ n == 1 ) return 1 ; $ sum = 0 ; for ( $ i = 2 ; $ i <= $ n ; ++ $ i ) { $ sum += log ( $ i ) / log ( 10 ) ; } $ sum *= $ n ; } $ N = 5 ; echo CountDigits ( $ N ) ; ? >


"Find the total marks obtained according to given marking scheme | Function that calculates marks . ; for not attempt score + 0 ; for each correct answer score + 3 ; for each wrong answer score - 1 ; calculate total marks ; Driver code"
Solution in php:

< ? php function markingScheme ( $ N , $ answerKey , $ studentAnswer ) { $ positive = 0 ; $ negative = 0 ; $ notattempt = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { if ( $ studentAnswer [ $ i ] == 0 ) $ notattempt ++ ; else if ( $ answerKey [ $ i ] == $ studentAnswer [ $ i ] ) $ positive ++ ; else if ( $ answerKey [ $ i ] != $ studentAnswer [ $ i ] ) $ negative ++ ; } return ( $ positive * 3 ) + ( $ negative * -1 ) ; } $ answerKey = array ( 1 , 2 , 3 , 4 , 1 ) ; $ studentAnswer = array ( 1 , 2 , 3 , 4 , 0 ) ; $ N = sizeof ( $ answerKey ) ; echo markingScheme ( $ N , $ answerKey , $ studentAnswer ) ; ? >


"Find the two repeating elements in a given array | Function ; Driver code"
Solution in php:

< ? php function printRepeating ( $ arr , $ size ) { $ count = array_fill ( 0 , $ size , 0 ) ; echo " Repeated ▁ elements ▁ are ▁ " ; for ( $ i = 0 ; $ i < $ size ; $ i ++ ) { if ( $ count [ $ arr [ $ i ] ] == 1 ) echo $ arr [ $ i ] . " ▁ " ; else $ count [ $ arr [ $ i ] ] ++ ; } } $ arr = array ( 4 , 2 , 4 , 5 , 2 , 3 , 1 ) ; $ arr_size = count ( $ arr ) ; printRepeating ( $ arr , $ arr_size ) ; ? >


"Find the two repeating elements in a given array | Function to print repeating ; Driver code"
Solution in php:

 < ? php function printRepeating ( $ arr , $ size ) { $ i ; echo " The ▁ repeating ▁ elements ▁ are " , " ▁ " ; for ( $ i = 0 ; $ i < $ size ; $ i ++ ) { if ( $ arr [ abs ( $ arr [ $ i ] ) ] > 0 ) $ arr [ abs ( $ arr [ $ i ] ) ] = - $ arr [ abs ( $ arr [ $ i ] ) ] ; else echo abs ( $ arr [ $ i ] ) , " ▁ " ; } } $ arr = array ( 4 , 2 , 4 , 5 , 2 , 3 , 1 ) ; $ arr_size = sizeof ( $ arr ) ; printRepeating ( $ arr , $ arr_size ) ; #This  code is contributed by aj_36
? >


"Find the two repeating elements in a given array | PHP code to Find the two repeating elements in a given array ; Will hold xor of all elements ; Will have only single set bit of xor ; Get the xor of all elements in arr [ ] and { 1 , 2 . . n } ; Get the rightmost set bit in set_bit_no ; Now divide elements in two sets by comparing rightmost set bit of xor with bit at same position in each element . ; XOR of first set in arr [ ] ; XOR of second set in arr [ ] ; XOR of first set in arr [ ] and { 1 , 2 , ... n } ; XOR of second set in arr [ ] and { 1 , 2 , ... n } ; driver code"
Solution in php:

< ? php function printRepeating ( $ arr , $ size ) { $ xor = $ arr [ 0 ] ; $ set_bit_no ; $ i ; $ n = $ size - 2 ; $ x = 0 ; $ y = 0 ; for ( $ i = 1 ; $ i < $ size ; $ i ++ ) $ xor ^= $ arr [ $ i ] ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ xor ^= $ i ; $ set_bit_no = $ xor & ~ ( $ xor - 1 ) ; for ( $ i = 0 ; $ i < $ size ; $ i ++ ) { if ( $ arr [ $ i ] & $ set_bit_no ) $ x = $ x ^ $ arr [ $ i ] ; else $ y = $ y ^ $ arr [ $ i ] ; } for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { if ( $ i & $ set_bit_no ) $ x = $ x ^ $ i ; else $ y = $ y ^ $ i ; } echo " n ▁ The ▁ two ▁ repeating ▁ elements ▁ are ▁ " ; echo $ y . " ▁ " . $ x ; } ? > $ arr = array ( 4 , 2 , 4 , 5 , 2 , 3 , 1 ) ; $ arr_size = count ( $ arr ) ; printRepeating ( $ arr , $ arr_size ) ;


"Find the two repeating elements in a given array | Print Repeating function ; Driver Code"
Solution in php:

< ? php function printRepeating ( $ arr , $ size ) { $ i ; $ j ; echo " ▁ Repeating ▁ elements ▁ are ▁ " ; for ( $ i = 0 ; $ i < $ size ; $ i ++ ) for ( $ j = $ i + 1 ; $ j < $ size ; $ j ++ ) if ( $ arr [ $ i ] == $ arr [ $ j ] ) echo $ arr [ $ i ] , " ▁ " ; } $ arr = array ( 4 , 2 , 4 , 5 , 2 , 3 , 1 ) ; $ arr_size = sizeof ( $ arr , 0 ) ; printRepeating ( $ arr , $ arr_size ) ; ? >


"Find the two repeating elements in a given array | printRepeating function ; S is for sum of elements in arr [ ] ; P is for product of elements in arr [ ] ; x and y are two repeating elements ; D is for difference of x and y , i . e . , x - y ; Calculate Sum and Product of all elements in arr [ ] ; S is x + y now ; P is x * y now ; D is x - y now ; factorial of n ; driver code"
Solution in php:

< ? php function printRepeating ( $ arr , $ size ) { $ S = 0 ; $ P = 1 ; $ x ; $ y ; $ D ; $ n = $ size - 2 ; for ( $ i = 0 ; $ i < $ size ; $ i ++ ) { $ S = $ S + $ arr [ $ i ] ; $ P = $ P * $ arr [ $ i ] ; } $ S = $ S - $ n * ( $ n + 1 ) / 2 ; $ P = $ P / fact ( $ n ) ; $ D = sqrt ( $ S * $ S - 4 * $ P ) ; $ x = ( $ D + $ S ) / 2 ; $ y = ( $ S - $ D ) / 2 ; echo " The ▁ two ▁ Repeating ▁ elements ▁ are ▁ " . $ x . " & " } function fact ( $ n ) { return ( $ n == 0 ) ? 1 : $ n * fact ( $ n - 1 ) ; } $ arr = array ( 4 , 2 , 4 , 5 , 2 , 3 , 1 ) ; $ arr_size = count ( $ arr ) ; printRepeating ( $ arr , $ arr_size ) ; ? >


"Find the unit place digit of sum of N factorials | Function to find the unit 's place digit ; Let us write for cases when N is smaller than or equal to 4. ; We know following ( 1 ! + 2 ! + 3 ! + 4 ! ) % 10 = 3 else ( N >= 4 ) ; Driver code"
Solution in php:

 < ? php function get_unit_digit ( $ N ) { if ( $ N == 0 $ N == 1 ) return 1 ; else if ( $ N == 2 ) return 3 ; else if ( $ N == 3 ) return 9 ; return 3 ; } $ N = 1 ; for ( $ N = 0 ; $ N <= 10 ; $ N ++ ) echo " For ▁ N ▁ = ▁ " . $ N . " ▁ : ▁ " . get_unit_digit ( $ N ) . "
 " ; ? >


"Find the value of N when F ( N ) = f ( a ) + f ( b ) where a + b is the minimum possible and a * b = N | Function to return the value of F ( N ) ; Base cases ; Count the number of times a number if divisible by 2 ; Return the summation ; Driver code"
Solution in php:

< ? php function getValueOfF ( $ n ) { if ( $ n == 1 ) return 0 ; if ( $ n == 2 ) return 1 ; $ cnt = 0 ; while ( $ n % 2 == 0 ) { $ cnt += 1 ; $ n /= 2 ; } return 2 * $ cnt ; } $ n = 20 ; echo getValueOfF ( $ n ) ; ? >


"Find the value of f ( n ) / f ( r ) * f ( n | Function to find value of given F ( n ) ; iterate over n ; calculate result ; return the result ; Driver code"
Solution in php:

< ? php function calcFunction ( $ n , $ r ) { $ finalDenominator = 1 ; $ mx = max ( $ r , $ n - $ r ) ; for ( $ i = $ mx + 1 ; $ i <= $ n ; $ i ++ ) { $ denominator = pow ( $ i , $ i ) ; $ numerator = pow ( $ i - $ mx , $ i - $ mx ) ; $ finalDenominator = ( $ finalDenominator * $ denominator ) / $ numerator ; } return $ finalDenominator ; } $ n = 6 ; $ r = 2 ; echo "1 / " , calcFunction ( $ n , $ r ) ; ? >


"Find the value of max ( f ( x ) ) | PHP implementation of above approach ; Function to calculate the value ; forming the prefix sum arrays ; Taking the query ; finding the sum in the range l to r in array a ; finding the sum in the range l to r in array b ; Finding the max value of the function ; Finding the min value of the function ; Driver code"
Solution in php:


"Find the value of the function Y = ( X ^ 6 + X ^ 2 + 9894845 ) % 971 | computing ( a ^ b ) % c ; Driver code"
Solution in php:

< ? php function modpow ( $ base , $ exp , $ modulus ) { $ base %= $ modulus ; $ result = 1 ; while ( $ exp > 0 ) { if ( $ exp & 1 ) $ result = ( $ result * $ base ) % $ modulus ; $ base = ( $ base * $ base ) % $ modulus ; $ exp >>= 1 ; } return $ result ; } $ n = 654654 ; $ mod = 971 ; echo ( ( ( modpow ( $ n , 6 , $ mod ) + modpow ( $ n , 2 , $ mod ) ) % $ mod + 355 ) % $ mod ) ; ? >


"Find the values of X and Y in the Given Equations | Function to find the values of X and Y ; base condition ; required answer ; Driver Code"
Solution in php:


"Find the winner by adding Pairwise difference of elements in the array until Possible | Function to calculate gcd ; Function to return the winner of the game ; To store the gcd of the original array ; To store the maximum element from the original array ; If number of moves are odd ; Driver Code"
Solution in php:

< ? php function __gcd ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; return __gcd ( $ b , $ a % $ b ) ; } function getWinner ( $ arr , $ n ) { $ gcd = $ arr [ 0 ] ; $ maxEle = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ gcd = __gcd ( $ gcd , $ arr [ $ i ] ) ; $ maxEle = max ( $ maxEle , $ arr [ $ i ] ) ; } $ totalMoves = ( $ maxEle / $ gcd ) - $ n ; if ( $ totalMoves % 2 == 1 ) return ' A ' ; return ' B ' ; } $ arr = array ( 5 , 6 , 7 ) ; $ n = sizeof ( $ arr ) ; echo getWinner ( $ arr , $ n ) ; ? >


"Find the winner in nim | function to find winner of NIM - game ; case when Alice is winner ; when Bob is winner ; Driver Code"
Solution in php:

< ? php function findWinner ( $ A , $ n ) { $ res = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ res ^= $ A [ $ i ] ; if ( $ res == 0 or $ n % 2 == 0 ) return " Alice " ; else return " Bob " ; } $ A = array ( 1 , 4 , 3 , 5 ) ; $ n = count ( $ A ) ; echo " Winner = " ? >


"Find three element from different three arrays such that a + b + c = sum | Function to check if there is an element from each array such that sum of the three elements is equal to given sum . ; Driver Code"
Solution in php:


"Find time taken for signal to reach all positions in a string | Returns time needed for signal to traverse through complete string . ; for the calculation of last index ; for strings like oxoooo , xoxxoooo . . ; if coun is greater than max_length ; if ' x ' is present at the right side of max_length ; if ' x ' is present at left side of max_length ; We use ceiling function to handle odd number ' o ' s ; Driver code"
Solution in php:

< ? php function maxLength ( $ s , $ n ) { $ right = 0 ; $ left = 0 ; $ coun = 0 ; $ max_length = PHP_INT_MIN ; $ s = $ s . '1' ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) { if ( $ s [ $ i ] == ' o ' ) $ coun ++ ; else { if ( $ coun > $ max_length ) { $ right = 0 ; $ left = 0 ; if ( $ s [ $ i ] == ' x ' ) $ right = 1 ; if ( ( ( $ i - $ coun ) > 0 ) && ( $ s [ $ i - $ coun - 1 ] == ' x ' ) ) $ left = 1 ; $ coun = ( int ) ceil ( ( double ) $ coun / ( $ right + $ left ) ) ; $ max_length = max ( $ max_length , $ coun ) ; } $ coun = 0 ; } } return $ max_length ; } $ s = " oooxoooooooooxooo " ; $ n = strlen ( $ s ) ; echo ( maxLength ( $ s , $ n ) ) ;


"Find two distinct prime numbers with given product | Function to generate all prime numbers less than n ; Initialize all entries of boolean array as true . A value in isPrime [ i ] will finally be false if i is Not a prime , else true bool isPrime [ n + 1 ] ; ; If isPrime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Function to print a prime pair with given product ; Generating primes using Sieve ; Traversing all numbers to find first pair ; Driver Code"
Solution in php:

< ? php function SieveOfEratosthenes ( $ n , & $ isPrime ) { $ isPrime [ 0 ] = false ; $ isPrime [ 1 ] = false ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ isPrime [ $ i ] = true ; for ( $ p = 2 ; $ p * $ p <= $ n ; $ p ++ ) { if ( $ isPrime [ $ p ] ) { for ( $ i = $ p * 2 ; $ i <= $ n ; $ i += $ p ) $ isPrime [ $ i ] = false ; } } } function findPrimePair ( $ n ) { $ flag = 0 ; $ isPrime = array_fill ( 0 , ( $ n + 1 ) , false ) ; SieveOfEratosthenes ( $ n , $ isPrime ) ; for ( $ i = 2 ; $ i < $ n ; $ i ++ ) { $ x = ( int ) ( $ n / $ i ) ; if ( $ isPrime [ $ i ] && $ isPrime [ $ x ] and $ x != $ i and $ x * $ i == $ n ) { echo $ i . " ▁ " . $ x ; $ flag = 1 ; return ; } } if ( ! $ flag ) echo " No ▁ such ▁ pair ▁ found " ; } $ n = 39 ; findPrimePair ( $ n ) ; ? >


"Find two numbers whose sum and GCD are given | Function to find gcd of two numbers ; Function to find two numbers whose sum and gcd is given ; sum != gcd checks that both the numbers are positive or not ; Driver code"
Solution in php:

 < ? php function __gcd ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; return __gcd ( $ b , $ a % $ b ) ; } function findTwoNumbers ( $ sum , $ gcd ) { if ( __gcd ( $ gcd , $ sum - $ gcd ) == $ gcd && $ sum != $ gcd ) echo " a = " ▁ , ▁ min ( $ gcd , ▁ $ sum ▁ - ▁ $ gcd ) ,
 	 	 	 " b = " else echo ( -1 ) ; } $ sum = 8 ; $ gcd = 2 ; findTwoNumbers ( $ sum , $ gcd ) ; ? >


"Find two numbers with sum and product both same as N | Function to return the smallest string ; Not possible ; find a and b ; Driver Code"
Solution in php:

 < ? php function findAandB ( $ N ) { $ val = $ N * $ N - 4.0 * $ N ; if ( $ val < 0 ) { echo " NO " ; return ; } $ a = ( $ N + sqrt ( $ val ) ) / 2.0 ; $ b = ( $ N - sqrt ( $ val ) ) / 2.0 ; echo " a = " ▁ , ▁ $ a , ▁ " " ;
 	 echo ▁ " b = " ▁ , ▁ $ b , ▁ " " } $ N = 69.0 ; findAandB ( $ N ) ; ? >


"Find two prime numbers with given sum | Generate all prime numbers less than n . ; Initialize all entries of boolean array as true . A value in isPrime [ i ] will finally be false if i is Not a prime , else true bool isPrime [ n + 1 ] ; ; If isPrime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Prints a prime pair with given sum ; Generating primes using Sieve ; Traversing all numbers to find first pair ; Driver Code"
Solution in php:

< ? php function SieveOfEratosthenes ( $ n , & $ isPrime ) { $ isPrime [ 0 ] = $ isPrime [ 1 ] = false ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ isPrime [ $ i ] = true ; for ( $ p = 2 ; $ p * $ p <= $ n ; $ p ++ ) { if ( $ isPrime [ $ p ] == true ) { for ( $ i = $ p * $ p ; $ i <= $ n ; $ i += $ p ) $ isPrime [ $ i ] = false ; } } } function findPrimePair ( $ n ) { $ isPrime = array_fill ( 0 , $ n + 1 , NULL ) ; SieveOfEratosthenes ( $ n , $ isPrime ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ isPrime [ $ i ] && $ isPrime [ $ n - $ i ] ) { echo $ i . " ▁ " . ( $ n - $ i ) ; return ; } } } $ n = 74 ; findPrimePair ( $ n ) ; ? >


"Find unique pairs such that each element is less than or equal to N | Finding the number of unique pairs ; Using the derived formula ; Printing the unique pairs ; Driver Code"
Solution in php:

 < ? php function No_Of_Pairs ( $ N ) { $ i = 1 ; while ( ( $ i * $ i * $ i ) + ( 2 * $ i * $ i ) + $ i <= $ N ) $ i ++ ; return ( $ i - 1 ) ; } function print_pairs ( $ pairs ) { $ i = 1 ; $ mul ; for ( $ i = 1 ; $ i <= $ pairs ; $ i ++ ) { $ mul = $ i * ( $ i + 1 ) ; echo " Pair ▁ no . " , $ i , " ▁ - - > ▁ ( " , ( $ mul * $ i ) , " , ▁ " , $ mul * ( $ i + 1 ) , " ) ▁
 " ; } } $ N = 500 ; $ pairs ; $ mul ; $ i = 1 ; $ pairs = No_Of_Pairs ( $ N ) ; echo " No . ▁ of ▁ pairs ▁ = ▁ " , $ pairs , " ▁
 " ; print_pairs ( $ pairs ) ; ? >


"Find unit digit of x raised to power y | Returns unit digit of x raised to power y ; Initialize result as 1 to handle case when y is 0. ; One by one multiply with x mod 10 to avoid overflow . ; Driver Code"
Solution in php:

< ? php function unitDigitXRaisedY ( $ x , $ y ) { $ res = 1 ; for ( $ i = 0 ; $ i < $ y ; $ i ++ ) $ res = ( $ res * $ x ) % 10 ; return $ res ; } echo ( unitDigitXRaisedY ( 4 , 2 ) ) ; ? >


"Find unit digit of x raised to power y | find unit digit ; Get last digit of x ; Last cyclic modular value ; here we simply return the unit digit or the power of a number ; Driver code ; get unit digit number here we pass the unit digit of x and the last cyclicity number that is y % 4"
Solution in php:


"Find value of ( n ^ 1 + n ^ 2 + n ^ 3 + n ^ 4 ) mod 5 for given n | function for f ( n ) mod 5 ; if n % 5 == 1 return 4 ; else return 0 ; Driver Code"
Solution in php:

 < ? php function fnMod ( $ n ) { if ( $ n % 5 == 1 ) return 4 ; else return 0 ; } $ n = 10 ; echo fnMod ( $ n ) , "
 " ; $ n = 11 ; echo fnMod ( $ n ) ; ? >


"Find value of k | PHP program to find k - th bit from right ; Driver Code ; Function Call"
Solution in php:

< ? php function printKthBit ( $ n , $ k ) { echo ( $ n & ( 1 << ( $ k - 1 ) ) ) ; } $ n = 13 ; $ k = 2 ; printKthBit ( $ n , $ k ) ; ? >


"Find ways an Integer can be expressed as sum of n | Function to calculate and return the power of any given number ; Function to check power representations recursively ; Initialize number of ways to express x as n - th powers of different natural numbers ; Calling power of ' i ' raised to ' n ' ; Recursively check all greater values of i ; If sum of powers is equal to x then increase the value of result . ; Return the final result ; Driver Code ."
Solution in php:

< ? php function power ( $ num , $ n ) { if ( $ n == 0 ) return 1 ; else if ( $ n % 2 == 0 ) return power ( $ num , ( int ) ( $ n / 2 ) ) * power ( $ num , ( int ) ( $ n / 2 ) ) ; else return $ num * power ( $ num , ( int ) ( $ n / 2 ) ) * power ( $ num , ( int ) ( $ n / 2 ) ) ; } function checkRecursive ( $ x , $ n , $ curr_num = 1 , $ curr_sum = 0 ) { $ results = 0 ; $ p = power ( $ curr_num , $ n ) ; while ( $ p + $ curr_sum < $ x ) { $ results += checkRecursive ( $ x , $ n , $ curr_num + 1 , $ p + $ curr_sum ) ; $ curr_num ++ ; $ p = power ( $ curr_num , $ n ) ; } if ( $ p + $ curr_sum == $ x ) $ results ++ ; return $ results ; } $ x = 10 ; $ n = 2 ; echo ( checkRecursive ( $ x , $ n ) ) ; ? >


"Find ways an Integer can be expressed as sum of n | PHP program to find number of ways to express a number as sum of n - th powers of numbers . ; Wrapper over checkRecursive ( ) ; Driver Code"
Solution in php:

< ? php $ res = 0 ; function checkRecursive ( $ num , $ x , $ k , $ n ) { global $ res ; if ( $ x == 0 ) $ res ++ ; $ r = ( int ) floor ( pow ( $ num , 1.0 / $ n ) ) ; for ( $ i = $ k + 1 ; $ i <= $ r ; $ i ++ ) { $ a = $ x - ( int ) pow ( $ i , $ n ) ; if ( $ a >= 0 ) checkRecursive ( $ num , $ x - ( int ) pow ( $ i , $ n ) , $ i , $ n ) ; } return $ res ; } function check ( $ x , $ n ) { return checkRecursive ( $ x , $ x , 0 , $ n ) ; } echo ( check ( 10 , 2 ) ) ; ? >


"Find whether a given integer is a power of 3 or not | Returns true if n is power of 3 , else false ; The maximum power of 3 value that integer can hold is 1162261467 ( 3 ^ 19 ) . ; Driver code"
Solution in php:

< ? php function check ( $ n ) { return 1162261467 % $ n == 0 ; } $ n = 9 ; if ( check ( $ n ) ) echo ( " Yes " ) ; else echo ( " No " ) ; ? >


"Find whether a given number is a power of 4 or not | Function to check if x is power of 4 ; Check if there is only one bit set in n ; count 0 bits before set bit ; If count is even then return true else false ; If there are more than 1 bit set then n is not a power of 4 ; Driver program to test above function"
Solution in php:

< ? php function isPowerOfFour ( $ n ) { $ count = 0 ; if ( $ n && ! ( $ n & ( $ n - 1 ) ) ) { while ( $ n > 1 ) { $ n >>= 1 ; $ count += 1 ; } return ( $ count % 2 == 0 ) ? 1 : 0 ; } return 0 ; } $ test_no = 64 ; if ( isPowerOfFour ( $ test_no ) ) echo $ test_no , " ▁ is ▁ a ▁ power ▁ of ▁ 4" ; else echo $ test_no , " ▁ not ▁ is ▁ a ▁ power ▁ of ▁ 4" ; ? >


"Find whether a given number is a power of 4 or not | Function to check if x is power of 4 ; Driver Code"
Solution in php:

< ? php function isPowerOfFour ( $ n ) { if ( $ n == 0 ) return 0 ; while ( $ n != 1 ) { if ( $ n % 4 != 0 ) return 0 ; $ n = $ n / 4 ; } return 1 ; } $ test_no = 64 ; if ( isPowerOfFour ( $ test_no ) ) echo $ test_no , " ▁ is ▁ a ▁ power ▁ of ▁ 4" ; else echo $ test_no , " ▁ is ▁ not ▁ a ▁ power ▁ of ▁ 4" ; ? >


"Find whether only two parallel lines contain all coordinates points or not | Find if slope is good with only two intercept ; if set of lines have only two distinct intercept ; Function to check if required solution exist ; check the result by processing the slope by starting three points ; Driver code"
Solution in php:

< ? php function isSlopeGood ( $ slope , $ arr , $ n ) { $ setOfLines = array_fill ( 0 , max ( $ arr ) * $ n , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ setOfLines [ $ arr [ $ i ] - $ slope * $ i ] = 1 ; $ setOfLines = array_unique ( $ setOfLines ) ; return ( count ( $ setOfLines ) == 2 ) ; } function checkForParallel ( $ arr , $ n ) { $ slope1 = isSlopeGood ( $ arr [ 1 ] - $ arr [ 0 ] , $ arr , $ n ) ; $ slope2 = isSlopeGood ( $ arr [ 2 ] - $ arr [ 1 ] , $ arr , $ n ) ; $ slope3 = isSlopeGood ( ( int ) ( ( $ arr [ 2 ] - $ arr [ 0 ] ) / 2 ) , $ arr , $ n ) ; return ( $ slope1 $ slope2 $ slope3 ) ; } $ arr = array ( 1 , 6 , 3 , 8 , 5 ) ; $ n = count ( $ arr ) ; echo ( int ) checkForParallel ( $ arr , $ n ) . " " ; ? >


"Find x , y , z that satisfy 2 / n = 1 / x + 1 / y + 1 / z | function to find x y and z that satisfy given equation . ; Driver Code"
Solution in php:

 < ? php function printXYZ ( $ n ) { if ( $ n == 1 ) echo - 1 ; else echo " x ▁ is ▁ " , $ n , "
 y ▁ is ▁ " , $ n + 1 , "
 z ▁ is ▁ " , $ n * ( $ n + 1 ) ; } $ n = 7 ; printXYZ ( $ n ) ; ? >


"Find x and y satisfying ax + by = n | function to find the solution ; traverse for all possible values ; check if it is satisfying the equation ; Driver Code"
Solution in php:


"Find zeroes to be flipped so that number of consecutive 1 's is maximized | m is maximum of number zeroes allowed to flip n is size of array ; Left and right indexes of current window ; Left index and size of the widest window ; Count of zeroes in current window ; While right boundary of current window doesn 't cross  right end ; If zero count of current window is less than m , widen the window toward right ; If zero count of current window is more than m , reduce the window from left ; Updqate widest window if this window size is more ; Print positions of zeroes in the widest window ; Driver Code"
Solution in php:

< ? php function findZeroes ( $ arr , $ n , $ m ) { $ wL = 0 ; $ wR = 0 ; $ bestL = 0 ; $ bestWindow = 0 ; $ zeroCount = 0 ; while ( $ wR < $ n ) { if ( $ zeroCount <= $ m ) { if ( $ arr [ $ wR ] == 0 ) $ zeroCount ++ ; $ wR ++ ; } if ( $ zeroCount > $ m ) { if ( $ arr [ $ wL ] == 0 ) $ zeroCount -- ; $ wL ++ ; } if ( ( $ wR - $ wL > $ bestWindow ) && ( $ zeroCount <= $ m ) ) { $ bestWindow = $ wR - $ wL ; $ bestL = $ wL ; } } for ( $ i = 0 ; $ i < $ bestWindow ; $ i ++ ) { if ( $ arr [ $ bestL + $ i ] == 0 ) echo $ bestL + $ i . " ▁ " ; } } $ arr = array ( 1 , 0 , 0 , 1 , 1 , 0 , 1 , 0 , 1 , 1 ) ; $ m = 2 ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; echo " Indexes ▁ of ▁ zeroes ▁ to ▁ be ▁ flipped ▁ are ▁ " ; findZeroes ( $ arr , $ n , $ m ) ; return 0 ; ? >


"Finding ' k ' such that its modulus with each array element is same | Prints all k such that arr [ i ] % k is same for all i ; sort the numbers ; max difference will be the difference between first and last element of sorted array ; Case when all the array elements are same ; Find all divisors of d and store in a vector v [ ] ; check for each v [ i ] if its modulus with each array element is same or not ; checking for each array element if its modulus with k is equal to k or not ; if check is true print v [ i ] ; Driver method"
Solution in php:

< ? php function printEqualModNumbers ( $ arr , $ n ) { sort ( $ arr ) ; $ d = $ arr [ $ n - 1 ] - $ arr [ 0 ] ; if ( d == 0 ) { print ( " Infinite ▁ solution " ) ; return ; } $ v = array ( ) ; for ( $ i = 1 ; $ i * $ i <= $ d ; $ i ++ ) { if ( $ d % $ i == 0 ) { array_push ( $ v , $ i ) ; if ( $ i != $ d / $ i ) array_push ( $ v , $ d / $ i ) ; } } for ( $ i = 0 ; $ i < count ( $ v ) ; $ i ++ ) { $ temp = $ arr [ 0 ] % $ v [ $ i ] ; $ j = 1 ; for ( ; $ j < $ n ; $ j ++ ) if ( $ arr [ $ j ] % $ v [ $ i ] != $ temp ) break ; if ( $ j == $ n ) print ( $ v [ $ i ] . " ▁ " ) ; } } $ arr = array ( 38 , 6 , 34 ) ; printEqualModNumbers ( $ arr , count ( $ arr ) ) ; ? >


"Finding Quadrant of a Coordinate with respect to a Circle | Thus function returns the quadrant number ; Coincides with center ; Outside circle ; 1 st quadrant ; 2 nd quadrant ; 3 rd quadrant ; 4 th quadrant ; Coordinates of centre ; Radius of circle ; Coordinates of the given po$"
Solution in php:

< ? php function getQuadrant ( $ X , $ Y , $ R , $ PX , $ PY ) { if ( $ PX == $ X and $ PY == $ Y ) return 0 ; $ val = pow ( ( $ PX - $ X ) , 2 ) + pow ( ( $ PY - $ Y ) , 2 ) ; if ( $ val > pow ( $ R , 2 ) ) return -1 ; if ( $ PX > $ X and $ PY >= $ Y ) return 1 ; if ( $ PX <= $ X and $ PY > $ Y ) return 2 ; if ( $ PX < $ X and $ PY <= $ Y ) return 3 ; if ( $ PX >= $ X and $ PY < $ Y ) return 4 ; } $ X = 0 ; $ Y = 3 ; $ R = 2 ; $ PX = 1 ; $ PY = 4 ; $ ans = getQuadrant ( $ X , $ Y , $ R , $ PX , $ PY ) ; if ( $ ans == -1 ) echo " Lies ▁ Outside ▁ the ▁ circle " ; else if ( $ ans == 0 ) echo " Coincides ▁ with ▁ centre " ; else echo $ ans , " ▁ Quadrant " ; ? >


"Finding n | A formula based PHP program to find sum of series with cubes of first n natural numbers ; Driver Code"
Solution in php:

 < ? php function magicOfSequence ( $ N ) { return ( $ N * ( $ N + 1 ) / 2 ) + 2 * $ N ; } $ N = 6 ; echo magicOfSequence ( $ N ) . "
 " ; ? >


"Finding n | Function to generate a fixed number ; Driver Method"
Solution in php:

< ? php function magicOfSequence ( $ N ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ N ; $ i ++ ) $ sum += ( $ i * $ i * $ i + $ i * 2 ) ; return $ sum ; } $ N = 4 ; echo magicOfSequence ( $ N ) ; ? >


"Finding n | PHP program to find n - th number with prime digits 2 , 3 and 7 ; remainder for check element position ; if number is 1 st position in tree ; if number is 2 nd position in tree ; if number is 3 rd position in tree ; if number is 4 th position in tree ; Driver code"
Solution in php:

 < ? php function nthprimedigitsnumber ( $ number ) { $ num = " " ; while ( $ number > 0 ) { $ rem = $ number % 4 ; switch ( $ rem ) { case 1 : $ num . = '2' ; break ; case 2 : $ num . = '3' ; break ; case 3 : $ num . = '5' ; break ; case 0 : $ num . = '7' ; break ; } if ( $ number % 4 == 0 ) $ number -- ; $ number = ( int ) ( $ number / 4 ) ; } return strrev ( $ num ) ; } $ number = 21 ; print ( nthprimedigitsnumber ( 10 ) . "
 " ) ; print ( nthprimedigitsnumber ( $ number ) ) ;


"Finding n | Prints n - th number where each digit is a prime number ; Finding the length of n - th number ; Count of numbers with len - 1 digits ; Count of numbers with i digits ; if i is the length of such number then n < 4 * ( 4 ^ ( i - 1 ) - 1 ) / 3 and n >= 4 * ( 4 ^ i - 1 ) / 3 if a valid i is found break the loop ; check for i + 1 ; Finding ith digit at ith place ; j = 1 means 2 j = 2 means ... j = 4 means 7 ; if prev_count + 4 ^ ( len - i ) is less than n , increase prev_count by 4 ^ ( x - i ) ; else print the ith digit and break ; Driver Code"
Solution in php:

 < ? php function nthprimedigitsnumber ( $ n ) { $ len = 1 ; $ prev_count = 0 ; while ( true ) { $ curr_count = $ prev_count + pow ( 4 , $ len ) ; if ( $ prev_count < $ n && $ curr_count >= $ n ) break ; $ len ++ ; $ prev_count = $ curr_count ; } for ( $ i = 1 ; $ i <= $ len ; $ i ++ ) { for ( $ j = 1 ; $ j <= 4 ; $ j ++ ) { if ( $ prev_count + pow ( 4 , $ len - $ i ) < $ n ) $ prev_count += pow ( 4 , $ len - $ i ) ; else { if ( $ j == 1 ) echo "2" ; else if ( $ j == 2 ) echo "3" ; else if ( $ j == 3 ) echo "5" ; else if ( $ j == 4 ) echo "7" ; break ; } } } echo "
 " ; } nthprimedigitsnumber ( 10 ) ; nthprimedigitsnumber ( 21 ) ; ? >