codeparrot/xlcost-text-to-code huggingface dataset:
Maximum Bitwise AND pair from given range | Function to return the maximum bitwise AND possible among all the possible pairs ; If there is only a single value in the range [ L , R ] ; If there are only two values in the range [ L , R ] ; Driver code
< ? php function maxAND ( $ L , $ R ) { if ( $ L == $ R ) return $ L ; else if ( ( $ R - $ L ) == 1 ) return ( $ R & $ L ) ; else { if ( ( ( $ R - 1 ) & $ R ) > ( ( $ R - 2 ) & ( $ R - 1 ) ) ) return ( ( $ R - 1 ) & $ R ) ; else return ( ( $ R - 2 ) & ( $ R - 1 ) ) ; } } $ L = 1 ; $ R = 632 ; echo maxAND ( $ L , $ R ) ; ? >
Smallest Special Prime which is greater than or equal to a given number | Function to check whether the number is a special prime or not ; While number is not equal to zero ; If the number is not prime return false . ; Else remove the last digit by dividing the number by 10. ; If the number has become zero then the number is special prime , hence return true ; Function to find the Smallest Special Prime which is greater than or equal to a given number ; Initially all numbers are considered Primes . ; There is always an answer possible ; Checking if the number is a special prime or not ; If yes print the number and break the loop . ; Else increment the number . ; Driver code
< ? php function checkSpecialPrime ( $ sieve , $ num ) { while ( $ num ) { if ( ! $ sieve [ $ num ] ) { return false ; } $ num = floor ( $ num / 10 ) ; } return true ; } function findSpecialPrime ( $ N ) { $ sieve = array_fill ( 0 , $ N * 10 , true ) ; $ sieve [ 0 ] = $ sieve [ 1 ] = false ; for ( $ i = 2 ; $ i <= $ N * 10 ; $ i ++ ) { if ( $ sieve [ $ i ] ) { for ( $ j = $ i * $ i ; $ j <= $ N * 10 ; $ j += $ i ) { $ sieve [ $ j ] = false ; } } } while ( true ) { if ( checkSpecialPrime ( $ sieve , $ N ) ) { echo $ N , " STRNEWLINE " ; break ; } else $ N ++ ; } } $ N = 379 ; findSpecialPrime ( $ N ) ; $ N = 100 ; findSpecialPrime ( $ N ) ; ? >
Minimum number of given moves required to make N divisible by 25 | Function to return the minimum number of moves required to make n divisible by 25 ; Convert number into string ; To store required answer ; Length of the string ; To check all possible pairs ; Make a duplicate string ; Number of swaps required to place ith digit in last position ; Number of swaps required to place jth digit in 2 nd last position ; Find first non zero digit ; Place first non zero digit in the first position ; Convert string to number ; If this number is divisible by 25 then cur is one of the possible answer ; If not possible ; Driver code
< ? php function minMoves ( $ n ) { $ s = strval ( $ n ) ; $ ans = PHP_INT_MAX ; $ len = strlen ( $ s ) ; for ( $ i = 0 ; $ i < $ len ; ++ $ i ) { for ( $ j = 0 ; $ j < $ len ; ++ $ j ) { if ( $ i == $ j ) continue ; $ t = $ s ; $ cur = 0 ; for ( $ k = $ i ; $ k < $ len - 1 ; ++ $ k ) { $ e = $ t [ $ k ] ; $ t [ $ k ] = $ t [ $ k + 1 ] ; $ t [ $ k + 1 ] = $ e ; ++ $ cur ; } for ( $ k = $ j - ( $ j > $ i ) ; $ k < $ len - 2 ; ++ $ k ) { $ e = $ t [ $ k ] ; $ t [ $ k ] = $ t [ $ k + 1 ] ; $ t [ $ k + 1 ] = $ e ; ++ $ cur ; } $ pos = -1 ; for ( $ k = 0 ; $ k < $ len ; ++ $ k ) { if ( $ t [ $ k ] != '0' ) { $ pos = $ k ; break ; } } for ( $ k = $ pos ; $ k > 0 ; -- $ k ) { $ e = $ t [ $ k ] ; $ t [ $ k ] = $ t [ $ k + 1 ] ; $ t [ $ k + 1 ] = $ e ; ++ $ cur ; } $ nn = intval ( $ t ) ; if ( $ nn % 25 == 0 ) $ ans = min ( $ ans , $ cur ) ; } } if ( $ ans == PHP_INT_MAX ) return -1 ; return $ ans ; } $ n = 509201 ; echo minMoves ( $ n ) ; ? >
Maximum positive integer divisible by C and is in the range [ A , B ] | Function to return the required number ; If b % c = 0 then b is the required number ; Else get the maximum multiple of c smaller than b ; Driver code
< ? php function getMaxNum ( $ a , $ b , $ c ) { if ( $ b % $ c == 0 ) return $ b ; $ x = ( ( int ) ( $ b / $ c ) * $ c ) ; if ( $ x >= $ a && $ x <= $ b ) return $ x ; else return -1 ; } $ a = 2 ; $ b = 10 ; $ c = 3 ; echo ( getMaxNum ( $ a , $ b , $ c ) ) ; ? >
Count the total number of squares that can be visited by Bishop in one move | Function to return the count of total positions the Bishop can visit in a single move ; Count top left squares ; Count bottom right squares ; Count top right squares ; Count bottom left squares ; Return total count ; Bishop 's Position
< ? php function countSquares ( $ row , $ column ) { $ topLeft = min ( $ row , $ column ) - 1 ; $ bottomRight = 8 - max ( $ row , $ column ) ; $ topRight = min ( $ row , 9 - $ column ) - 1 ; $ bottomLeft = 8 - max ( $ row , 9 - $ column ) ; return ( $ topLeft + $ topRight + $ bottomRight + $ bottomLeft ) ; } $ row = 4 ; $ column = 4 ; echo countSquares ( $ row , $ column ) ; ? >
Check whether Bishop can take down Pawn or not | Function that return true if the Bishop can take down the pawn ; If pawn is at angle 45 or 225 degree from bishop 's Position ; If pawn is at angle 135 or 315 degree from bishop 's Position ; Bishop 's Position ; Pawn 's Position
< ? php function canTakeDown ( $ bishopX , $ bishopY , $ pawnX , $ pawnY ) { if ( $ pawnX - $ bishopX == $ pawnY - $ bishopY ) return true ; else if ( - $ pawnX + $ bishopX == $ pawnY - $ bishopY ) return true ; else return false ; } $ bishopX = 5 ; $ bishopY = 5 ; $ pawnX = 1 ; $ pawnY = 1 ; if ( canTakeDown ( $ bishopX , $ bishopY , $ pawnX , $ pawnY ) ) echo " Yes " ; else echo " No " ; ? >
Find maximum operations to reduce N to 1 | PHP program to find maximum number moves possible ; To store number of prime factors of each number ; Function to find number of prime factors of each number ; if i is a prime number ; increase value by one from it 's preveious multiple ; make prefix sum this will be helpful for multiple test cases ; Generate primeFactors array ; required answer
< ? php $ N = 10005 ; $ primeFactors = array_fill ( 0 , $ N , 0 ) ; function findPrimeFactors ( ) { global $ N , $ primeFactors ; for ( $ i = 2 ; $ i < $ N ; $ i ++ ) if ( $ primeFactors [ $ i ] == 0 ) for ( $ j = $ i ; $ j < $ N ; $ j += $ i ) $ primeFactors [ $ j ] = $ primeFactors [ ( int ) ( $ j / $ i ) ] + 1 ; for ( $ i = 1 ; $ i < $ N ; $ i ++ ) $ primeFactors [ $ i ] += $ primeFactors [ $ i - 1 ] ; } findPrimeFactors ( ) ; $ a = 6 ; $ b = 3 ; print ( ( $ primeFactors [ $ a ] - $ primeFactors [ $ b ] ) ) ; ? >
Smallest integer with digit sum M and multiple of N | Function to return digit sum ; Function to find out the smallest integer ; Start of the iterator ( Smallest multiple of n ) ; Driver code
< ? php function digitSum ( $ n ) { $ ans = 0 ; while ( $ n ) { $ ans += $ n % 10 ; $ n /= 10 ; } return $ ans ; } function findInt ( $ n , $ m ) { $ minDigit = floor ( $ m / 9 ) ; $ start = pow ( 10 , $ minDigit ) - ( int ) pow ( 10 , $ minDigit ) % $ n ; while ( $ start < PHP_INT_MAX ) { if ( digitSum ( $ start ) == $ m ) return $ start ; else $ start += $ n ; } return -1 ; } $ n = 13 ; $ m = 32 ; echo findInt ( $ n , $ m ) ; # This  code is contributed by ajit. NEW_LINE ? >
Maximum sum after repeatedly dividing N by a divisor | Function to find the smallest divisor ; Function to find the maximum sum ; Driver Code
< ? php function smallestDivisor ( $ n ) { $ mx = sqrt ( $ n ) ; for ( $ i = 2 ; $ i <= $ mx ; $ i ++ ) if ( $ n % $ i == 0 ) return $ i ; return $ n ; } function maxSum ( $ n ) { $ res = $ n ; while ( $ n > 1 ) { $ divi = smallestDivisor ( $ n ) ; $ n /= $ divi ; $ res += $ n ; } return $ res ; } $ n = 34 ; echo maxSum ( $ n ) ; #This  code is contributed by akt_mit. NEW_LINE ? >
Make all elements of an array equal with the given operation | Function that returns true if all the elements of the array can be made equal with the given operation ; To store the sum of the array elements and the maximum element from the array ; Driver code
< ? php function isPossible ( $ n , $ k , $ arr ) { $ sum = $ arr [ 0 ] ; $ maxVal = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ sum += $ arr [ $ i ] ; $ maxVal = max ( $ maxVal , $ arr [ $ i ] ) ; } if ( ( float ) $ maxVal > ( float ) ( $ sum + $ k ) / $ n ) return false ; return true ; } $ k = 8 ; $ arr = array ( 1 , 2 , 3 , 4 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; if ( isPossible ( $ n , $ k , $ arr ) ) echo " Yes " ; else echo " No " ; # This  code is contributed by akt_miit. NEW_LINE ? >
Maximize the value of x + y + z such that ax + by + cz = n | Function to return the maximum value of ( x + y + z ) such that ( ax + by + cz = n ) ; i represents possible values of a * x ; j represents possible values of b * y ; If z is an integer ; Driver code ; Function Call
< ? php function maxResult ( $ n , $ a , $ b , $ c ) { $ maxVal = 0 ; for ( $ i = 0 ; $ i <= $ n ; $ i += $ a ) for ( $ j = 0 ; $ j <= $ n - $ i ; $ j += $ b ) { $ z = ( $ n - ( $ i + $ j ) ) / $ c ; if ( floor ( $ z ) == ceil ( $ z ) ) { $ x = ( int ) ( $ i / $ a ) ; $ y = ( int ) ( $ j / $ b ) ; $ maxVal = max ( $ maxVal , $ x + $ y + ( int ) $ z ) ; } } return $ maxVal ; } $ n = 10 ; $ a = 5 ; $ b = 3 ; $ c = 4 ; echo maxResult ( $ n , $ a , $ b , $ c ) ; ? >
Make all numbers of an array equal | Function that returns true if all the array elements can be made equal with the given operation ; Divide number by 2 ; Divide number by 3 ; Driver code
< ? php function EqualNumbers ( $ a , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { while ( $ a [ $ i ] % 2 == 0 ) $ a [ $ i ] /= 2 ; while ( $ a [ $ i ] % 3 == 0 ) $ a [ $ i ] /= 3 ; if ( $ a [ $ i ] != $ a [ 0 ] ) { return false ; } } return true ; } $ a = array ( 50 , 75 , 150 ) ; $ n = sizeof ( $ a ) / sizeof ( $ a [ 0 ] ) ; if ( EqualNumbers ( $ a , $ n ) ) echo " Yes " ; else echo " No " ; #This  code is contributed by ajit.. NEW_LINE ? >
Maximum GCD from Given Product of Unknowns | Function to return the required gcd ; Count the number of times 2 divides p ; Equivalent to p = p / 2 ; ; If 2 divides p ; Check all the possible numbers that can divide p ; If n in the end is a prime number ; Return the required gcd ; Driver code
< ? php function max_gcd ( $ n , $ p ) { $ count = 0 ; $ gcd = 1 ; while ( $ p % 2 == 0 ) { $ p >>= 1 ; $ count ++ ; } if ( $ count > 0 ) $ gcd *= pow ( 2 , ( int ) ( $ count / $ n ) ) ; for ( $ i = 3 ; $ i <= ( int ) sqrt ( $ p ) ; $ i += 2 ) { $ count = 0 ; while ( $ p % $ i == 0 ) { $ count ++ ; $ p = ( int ) ( $ p / $ i ) ; } if ( $ count > 0 ) { $ gcd *= pow ( $ i , ( int ) ( $ count / $ n ) ) ; } } if ( $ p > 2 ) $ gcd *= pow ( $ p , ( int ) ( 1 / $ n ) ) ; return $ gcd ; } $ n = 3 ; $ p = 80 ; echo ( max_gcd ( $ n , $ p ) ) ;
Minimum positive integer divisible by C and is not in range [ A , B ] | Function to return the required number ; If doesn 't belong to the range  then c is the required number ; Else get the next multiple of c starting from b + 1 ; Driver code
< ? php function getMinNum ( $ a , $ b , $ c ) { if ( $ c < $ a $ c > $ b ) return $ c ; $ x = ( floor ( ( $ b / $ c ) ) * $ c ) + $ c ; return $ x ; } $ a = 2 ; $ b = 4 ; $ c = 4 ; echo getMinNum ( $ a , $ b , $ c ) ; ? >
Count of pairs of ( i , j ) such that ( ( n % i ) % j ) % n is maximized | Function to return the count of required pairs ; Special case ; Number which will give the max value . for ( ( n % i ) % j ) % n ; To store the maximum possible value of ( ( n % i ) % j ) % n ; Count of possible pairs ; Driver code
< ? php function countPairs ( $ n ) { if ( $ n == 2 ) return 4 ; $ num = ( ( int ) ( $ n / 2 ) + 1 ) ; $ max = $ n % $ num ; $ count = ( $ n - $ max ) ; return $ count ; } $ n = 5 ; echo ( countPairs ( $ n ) ) ; ? >
Remove characters from a numeric string such that string becomes divisible by 8 | Function that return true if sub is a sub - sequence in s ; Function to return a multiple of 8 formed after removing 0 or more characters from the given string ; Iterate over all multiples of 8 ; If current multiple exists as a subsequence in the given string ; Driver Code
< ? php function checkSub ( $ sub , $ s ) { $ j = 0 ; for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) if ( $ sub [ $ j ] == $ s [ $ i ] ) $ j ++ ; return $ j == strlen ( $ sub ) ; } function getMultiple ( $ s ) { for ( $ i = 0 ; $ i < 1e3 ; $ i += 8 ) { if ( checkSub ( ( string ) ( $ i ) , $ s ) ) return $ i ; } return -1 ; } $ s = "3454" ; echo getMultiple ( $ s ) ;
Program to find sum of harmonic series | PHP program to find sum of harmonic series using recursion ; Base condition ; Driver Code
< ? php function sum ( $ n ) { if ( $ n < 2 ) return 1 ; else return 1 / $ n + ( sum ( $ n - 1 ) ) ; } echo sum ( 8 ) . " STRNEWLINE " ; echo sum ( 10 ) ; ? >
Sum of P terms of an AP if Mth and Nth terms are given | Function to calculate the value of the ; Calculate value of d using formula ; Calculate value of a using formula ; Return pair ; Function to calculate value sum of first p numbers of the series ; First calculate value of a and d ; Calculate the sum by using formula ; Return the sum ; Driver Code
< ? php function findingValues ( $ m , $ n , $ mth , $ nth ) { $ d = ( abs ( $ mth - $ nth ) ) / abs ( ( $ m - 1 ) - ( $ n - 1 ) ) ; $ a = $ mth - ( ( $ m - 1 ) * $ d ) ; return array ( $ a , $ d ) ; } function findSum ( $ m , $ n , $ mth , $ nth , $ p ) { $ ad = findingValues ( $ m , $ n , $ mth , $ nth ) ; $ a = $ ad [ 0 ] ; $ d = $ ad [ 1 ] ; $ sum = ( $ p * ( 2 * $ a + ( $ p - 1 ) * $ d ) ) / 2 ; return $ sum ; } $ m = 6 ; $ n = 10 ; $ mTerm = 12 ; $ nTerm = 20 ; $ p = 5 ; echo findSum ( $ m , $ n , $ mTerm , $ nTerm , $ p ) ; ? >
Print all integers that are sum of powers of two given numbers | Function to print powerful integers ; Set is used to store distinct numbers in sorted order ; Store all the powers of y < bound in a vector to avoid calculating them again and again ; x ^ i ; If num is within limits insert it into the set ; Break out of the inner loop ; Adding any number to it will be out of bounds ; Increment i ; Print the contents of the set ; Driver code ; Print powerful integers
< ? php function powerfulIntegers ( $ x , $ y , $ bound ) { $ s = array ( ) ; $ powersOfY = array ( ) ; array_push ( $ powersOfY , 1 ) ; $ i = $ y ; while ( $ i < $ bound && $ y != 1 ) { array_push ( $ powersOfY , $ i ) ; $ i *= $ y ; } $ i = 0 ; while ( true ) { $ xPowI = pow ( $ x , $ i ) ; for ( $ j = 0 ; $ j < count ( $ powersOfY ) ; $ j ++ ) { $ num = $ xPowI + $ powersOfY [ $ j ] ; if ( $ num <= $ bound ) array_push ( $ s , $ num ) ; else break ; } if ( $ xPowI >= $ bound $ x == 1 ) break ; $ i += 1 ; } $ s = array_unique ( $ s ) ; sort ( $ s ) ; foreach ( $ s as & $ itr ) print ( $ itr . " " ) ; } $ x = 2 ; $ y = 3 ; $ bound = 10 ; powerfulIntegers ( $ x , $ y , $ bound ) ; ? >
Distribute N candies among K people | Function to find out the number of candies every person received ; Count number of complete turns ; Get the last term ; Stores the number of candies ; Last term of last and current series ; Sum of current and last series ; Sum of current series only ; If sum of current is less than N ; else Individually distribute ; First term ; Distribute candies till there ; Candies available ; Not available ; Count the total candies ; Print the total candies ; Driver Code
< ? php function candies ( $ n , $ k ) { $ count = 0 ; $ ind = 1 ; $ arr = array_fill ( 0 , $ k , 0 ) ; while ( $ n ) { $ f1 = ( $ ind - 1 ) * $ k ; $ f2 = $ ind * $ k ; $ sum1 = floor ( ( $ f1 * ( $ f1 + 1 ) ) / 2 ) ; $ sum2 = floor ( ( $ f2 * ( $ f2 + 1 ) ) / 2 ) ; $ res = $ sum2 - $ sum1 ; if ( $ res <= $ n ) { $ count ++ ; $ n -= $ res ; $ ind ++ ; } { $ i = 0 ; $ term = ( ( $ ind - 1 ) * $ k ) + 1 ; while ( $ n > 0 ) { if ( $ term <= $ n ) { $ arr [ $ i ++ ] = $ term ; $ n -= $ term ; $ term ++ ; } else { $ arr [ $ i ++ ] = $ n ; $ n = 0 ; } } } } for ( $ i = 0 ; $ i < $ k ; $ i ++ ) $ arr [ $ i ] += floor ( ( $ count * ( $ i + 1 ) ) + ( $ k * ( $ count * ( $ count - 1 ) ) / 2 ) ) ; for ( $ i = 0 ; $ i < $ k ; $ i ++ ) echo $ arr [ $ i ] , " ▁ " ; } $ n = 10 ; $ k = 3 ; candies ( $ n , $ k ) ; ? >
Distribute N candies among K people | Function to find out the number of candies every person received ; Count number of complete turns ; Get the last term ; Stores the number of candies ; Do a binary search to find the number whose sum is less than N . ; Get mide ; If sum is below N ; Find number of complete turns ; Right halve ; Left halve ; Last term of last complete series ; Subtract the sum till ; First term of incomplete series ; Count the total candies ; Print the total candies ; Driver Code
< ? php function candies ( $ n , $ k ) { $ count = 0 ; $ ind = 1 ; $ arr = array_fill ( 0 , $ k , 0 ) ; $ low = 0 ; $ high = $ n ; while ( $ low <= $ high ) { $ mid = ( $ low + $ high ) >> 1 ; $ sum = ( $ mid * ( $ mid + 1 ) ) >> 1 ; if ( $ sum <= $ n ) { $ count = ( int ) ( $ mid / $ k ) ; $ low = $ mid + 1 ; } else { $ high = $ mid - 1 ; } } $ last = ( $ count * $ k ) ; $ n -= ( int ) ( ( $ last * ( $ last + 1 ) ) / 2 ) ; $ i = 0 ; $ term = ( $ count * $ k ) + 1 ; while ( $ n ) { if ( $ term <= $ n ) { $ arr [ $ i ++ ] = $ term ; $ n -= $ term ; $ term ++ ; } else { $ arr [ $ i ] += $ n ; $ n = 0 ; } } for ( $ i = 0 ; $ i < $ k ; $ i ++ ) $ arr [ $ i ] += ( $ count * ( $ i + 1 ) ) + ( int ) ( $ k * ( $ count * ( $ count - 1 ) ) / 2 ) ; for ( $ i = 0 ; $ i < $ k ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; } $ n = 7 ; $ k = 4 ; candies ( $ n , $ k ) ; ? >
Smallest multiple of 3 which consists of three given non | Function to return the minimum number divisible by 3 formed by the given digits ; Sort the given array in ascending ; Check if any single digit is divisible by 3 ; Check if any two digit number formed by the given digits is divisible by 3 starting from the minimum ; Generate the two digit number ; If none of the above is true , we can form three digit number by taking a [ 0 ] three times . ; Driver code
< ? php function printSmallest ( $ a ) { sort ( $ a ) ; for ( $ i = 0 ; $ i < 3 ; $ i ++ ) { if ( $ a [ $ i ] % 3 == 0 ) return $ a [ $ i ] ; } for ( $ i = 0 ; $ i < 3 ; $ i ++ ) { for ( $ j = 0 ; $ j < 3 ; $ j ++ ) { $ num = ( $ a [ $ i ] * 10 ) + $ a [ $ j ] ; if ( $ num % 3 == 0 ) return $ num ; } } return $ a [ 0 ] * 100 + $ a [ 0 ] * 10 + $ a [ 0 ] ; } $ arr = array ( 7 , 7 , 1 ) ; echo printSmallest ( $ arr ) ; ? >
Print matrix after applying increment operations in M ranges | Function to update and print the matrix after performing queries ; Add 1 to the first element of the sub - matrix ; If there is an element after the last element of the sub - matrix then decrement it by 1 ; Calculate the running sum ; Print the updated element ; Next line ; Size of the matrix ; Queries
< ? php function updateMatrix ( $ n , $ q , $ mat ) { for ( $ i = 0 ; $ i < sizeof ( $ q ) ; $ i ++ ) { $ X1 = $ q [ $ i ] [ 0 ] ; $ Y1 = $ q [ $ i ] [ 1 ] ; $ X2 = $ q [ $ i ] [ 2 ] ; $ Y2 = $ q [ $ i ] [ 3 ] ; $ mat [ $ X1 ] [ $ Y1 ] ++ ; if ( $ Y2 + 1 < $ n ) $ mat [ $ X2 ] [ $ Y2 + 1 ] -- ; else if ( $ X2 + 1 < $ n ) $ mat [ $ X2 + 1 ] [ 0 ] -- ; } $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { $ sum += $ mat [ $ i ] [ $ j ] ; echo ( $ sum . " " ) ; } echo ( " STRNEWLINE " ) ; } } $ n = 5 ; $ mat = array_fill ( 0 , $ n , array_fill ( 0 , $ n , 0 ) ) ; $ q = array ( array ( 0 , 0 , 1 , 2 ) , array ( 1 , 2 , 3 , 4 ) , array ( 1 , 4 , 3 , 4 ) ) ; updateMatrix ( $ n , $ q , $ mat ) ; ? >
Replace the maximum element in the array by coefficient of range | Utility function to print the contents of the array ; Function to replace the maximum element from the array with the coefficient of range of the array ; Maximum element from the array ; Minimum element from the array ; Calculate the coefficient of range for the array ; Assuming all the array elements are distinct . Replace the maximum element with the coefficient of range of the array ; Print the updated array ; Driver code
< ? php function printArr ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; } function replaceMax ( $ arr , $ n ) { $ max = max ( $ arr ) ; $ min = min ( $ arr ) ; $ range = $ max - $ min ; $ coeffOfRange = round ( $ range / ( $ max + $ min ) , 6 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] == $ max ) { $ arr [ $ i ] = $ coeffOfRange ; break ; } } printArr ( $ arr , $ n ) ; } $ arr = array ( 15 , 16 , 10 , 9 , 6 , 7 , 17 ) ; $ n = count ( $ arr ) ; replaceMax ( $ arr , $ n ) ; ? >
Divide the two given numbers by their common divisors | print the numbers after dividing them by their common factors ; iterate from 1 to minimum of a and b ; if i is the common factor of both the numbers ; Driver code ; divide A and B by their common factors
< ? php function divide ( $ a , $ b ) { for ( $ i = 2 ; $ i <= min ( $ a , $ b ) ; $ i ++ ) { while ( $ a % $ i == 0 && $ b % $ i == 0 ) { $ a = $ a / $ i ; $ b = $ b / $ i ; } } echo " A = " , ▁ $ a , ▁ " , B = " , ▁ $ b , ▁ " " } $ A = 10 ; $ B = 15 ; divide ( $ A , $ B ) ; ? >
Divide the two given numbers by their common divisors | Function to calculate gcd of two numbers ; Function to calculate all common divisors of two given numbers a , b -- > input integer numbers ; find gcd of a , b ; Driver code
< ? php function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; } function commDiv ( $ a , $ b ) { $ n = gcd ( $ a , $ b ) ; $ a = ( int ) ( $ a / $ n ) ; $ b = ( int ) ( $ b / $ n ) ; echo " A = " ▁ . ▁ $ a ▁ . STRNEWLINE TABSYMBOL TABSYMBOL " , B = " ▁ . ▁ $ b ▁ . ▁ " " } $ a = 10 ; $ b = 15 ; commDiv ( $ a , $ b ) ; ? >
Minimum absolute difference between N and a power of 2 | Function to return the minimum difference between N and a power of 2 ; Power of 2 closest to n on its left ; Power of 2 closest to n on its right ; Return the minimum abs difference ; Driver code
< ? php function minAbsDiff ( $ n ) { $ left = 1 << ( ( floor ( log ( $ n ) / log ( 2 ) ) ) ) ; $ right = $ left * 2 ; return min ( ( $ n - $ left ) , ( $ right - $ n ) ) ; } $ n = 15 ; echo minAbsDiff ( $ n ) ; ? >
Find probability that a player wins when probabilities of hitting the target are given | Function to return the probability of the winner ; Driver Code ; Will print 9 digits after the decimal point
< ? php function find_probability ( $ p , $ q , $ r , $ s ) { $ t = ( 1 - $ p / $ q ) * ( 1 - $ r / $ s ) ; $ ans = ( $ p / $ q ) / ( 1 - $ t ) ; return $ ans ; } $ p = 1 ; $ q = 2 ; $ r = 1 ; $ s = 2 ; $ res = find_probability ( $ p , $ q , $ r , $ s ) ; $ update = number_format ( $ res , 7 ) ; echo $ update ; ? >
Represent n as the sum of exactly k powers of two | Set 2 | Function to print k numbers which are powers of two and whose sum is equal to n ; Initialising the sum with k ; Initialising an array A with k elements and filling all elements with 1 ; Iterating A [ ] from k - 1 to 0 ; Update sum and A [ i ] till sum + A [ i ] is less than equal to n ; Impossible to find the combination ; Possible solution is stored in A [ ] ; Driver code
< ? php function FindAllElements ( $ n , $ k ) { $ sum = $ k ; $ A = array_fill ( 0 , $ k , 1 ) ; for ( $ i = $ k - 1 ; $ i >= 0 ; -- $ i ) { while ( $ sum + $ A [ $ i ] <= $ n ) { $ sum += $ A [ $ i ] ; $ A [ $ i ] *= 2 ; } } if ( $ sum != $ n ) { echo " Impossible " ; } else { for ( $ i = 0 ; $ i < $ k ; ++ $ i ) echo $ A [ $ i ] , ' ▁ ' ; } } $ n = 12 ; $ k = 6 ; FindAllElements ( $ n , $ k ) ; ? >
Check whether a + b = c or not after removing all zeroes from a , b and c | Function to remove zeroes ; Initialize result to zero holds the Result after removing zeroes from no ; Initialize variable d to 1 that holds digits of no ; Loop while n is greater then zero ; Check if n mod 10 is not equal to zero ; store the result by removing zeroes and increment d by 10 ; Go to the next digit ; Return the result ; Function to check if sum is true after Removing all zeroes . ; Call removeZero ( ) for both sides and check whether they are equal After removing zeroes . ; Driver code
< ? php function removeZero ( $ n ) { $ res = 0 ; $ d = 1 ; while ( $ n > 0 ) { if ( $ n % 10 != 0 ) { $ res += ( $ n % 10 ) * $ d ; $ d *= 10 ; } $ n = floor ( $ n / 10 ) ; } return $ res ; } function isEqual ( $ a , $ b ) { if ( removeZero ( $ a ) + removeZero ( $ b ) == removeZero ( $ a + $ b ) ) return true ; return false ; } $ a = 105 ; $ b = 106 ; if ( isEqual ( $ a , $ b ) ) echo " Yes " ; else echo " No " ; ? >