codeparrot/xlcost-text-to-code huggingface dataset:
Check if the number is even or odd whose digits and base ( radix ) is given | Function that returns true if the number represented by arr [ ] is even in base r ; If the base is even , then the last digit is checked ; If base is odd , then the number of odd digits are checked ; To store the count of odd digits ; Number is odd ; Driver code
< ? php function isEven ( $ arr , $ n , $ r ) { if ( $ r % 2 == 0 ) { if ( $ arr [ $ n - 1 ] % 2 == 0 ) return true ; } else { $ oddCount = 0 ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { if ( $ arr [ $ i ] % 2 != 0 ) $ oddCount ++ ; } if ( $ oddCount % 2 == 0 ) return true ; } return false ; } $ arr = array ( 1 , 0 ) ; $ n = Count ( $ arr ) ; $ r = 2 ; if ( isEven ( $ arr , $ n , $ r ) ) echo " Even " ; else echo " Odd " ; ? >
Rearrange the array to maximize the number of primes in prefix sum of the array | Function to print the re - arranged array ; Count the number of ones and twos in a [ ] ; If the array element is 1 ; Array element is 2 ; If it has at least one 2 Fill up first 2 ; Decrease the cnt of ones if even ; Fill up with odd count of ones ; Fill up with remaining twos ; If even ones , then fill last position ; Print the rearranged array ; Driver code
< ? php function solve ( $ a , $ n ) { $ ones = 0 ; $ twos = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] == 1 ) $ ones ++ ; else $ twos ++ ; } $ ind = 0 ; if ( $ twos ) $ a [ $ ind ++ ] = 2 ; $ evenOnes = ( $ ones % 2 == 0 ) ? true : false ; if ( $ evenOnes ) $ ones -= 1 ; for ( $ i = 0 ; $ i < $ ones ; $ i ++ ) $ a [ $ ind ++ ] = 1 ; for ( $ i = 0 ; $ i < $ twos - 1 ; $ i ++ ) $ a [ $ ind ++ ] = 2 ; if ( $ evenOnes ) $ a [ $ ind ++ ] = 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ a [ $ i ] , " ▁ " ; } $ a = array ( 1 , 2 , 1 , 2 , 1 ) ; $ n = count ( $ a ) ; solve ( $ a , $ n ) ; ? >
Generate an Array in which count of even and odd sum sub | Function to generate and print the required array ; Find the number of odd prefix sums ; If no odd prefix sum found ; Calculating the number of even prefix sums ; Stores the current prefix sum ; If current prefix sum is even ; Print 0 until e = EvenPreSums - 1 ; Print 1 when e = EvenPreSums ; Print 0 for rest of the values ; Driver code
< ? php function CreateArray ( $ N , $ even , $ odd ) { $ temp = -1 ; $ OddPreSums = 0 ; for ( $ i = 0 ; $ i <= $ N + 1 ; $ i ++ ) { if ( $ i * ( ( $ N + 1 ) - $ i ) == $ odd ) { $ temp = 0 ; $ OddPreSums = $ i ; break ; } } if ( $ temp == -1 ) { echo temp ; } else { $ EvenPreSums = ( $ N + 1 ) - $ OddPreSums ; $ e = 1 ; $ o = 0 ; $ CurrSum = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { if ( $ CurrSum % 2 == 0 ) { if ( $ e < $ EvenPreSums ) { $ e ++ ; echo "0 ▁ " ; } else { $ o ++ ; echo "1 ▁ " ; $ CurrSum ++ ; } } else { if ( $ e < $ EvenPreSums ) { $ e ++ ; echo "1 ▁ " ; $ CurrSum ++ ; } else { $ o ++ ; echo "0 ▁ " ; } } } echo " STRNEWLINE " ; } } $ N = 15 ; $ even = 60 ; $ odd = 60 ; CreateArray ( $ N , $ even , $ odd ) ; ? >
Choose X such that ( A xor X ) + ( B xor X ) is minimized | Function to return the integer X such that ( A xor X ) + ( B ^ X ) is minimized ; While either A or B is non - zero ; Position at which both A and B have a set bit ; Inserting a set bit in x ; Right shifting both numbers to traverse all the bits ; Driver code
< ? php function findX ( $ A , $ B ) { $ j = 0 ; $ x = 0 ; while ( $ A $ B ) { if ( ( $ A & 1 ) && ( $ B & 1 ) ) { $ x += ( 1 << $ j ) ; } $ A >>= 1 ; $ B >>= 1 ; $ j += 1 ; } return $ x ; } $ A = 2 ; $ B = 3 ; $ X = findX ( $ A , $ B ) ; echo " X = " ▁ , ▁ $ X ▁ , ▁ " , Sum = " ( $ A ^ $ X ) + ( $ B ^ $ X ) ; ? >
Compare sum of first N | Function that returns true if sum of first n - 1 elements of the array is equal to the last element ; Find the sum of first n - 1 elements of the array ; If sum equals to the last element ; Driver code
< ? php function isSumEqual ( $ ar , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) $ sum += $ ar [ $ i ] ; if ( $ sum == $ ar [ $ n - 1 ] ) return true ; return false ; } $ arr = array ( 1 , 2 , 3 , 4 , 10 ) ; $ n = count ( $ arr ) ; if ( isSumEqual ( $ arr , $ n ) ) echo " Yes " ; else echo " No " ; ? >
Count number of 1 s in the array after N moves | Function to count number of perfect squares ; Counting number of perfect squares between a and b ; Function to count number of 1 s in array after N moves ; Initialize array size ; Initialize all elements to 0
< ? php function perfectSquares ( $ a , $ b ) { return ( floor ( sqrt ( $ b ) ) - ceil ( sqrt ( $ a ) ) + 1 ) ; } function countOnes ( $ arr , $ n ) { return perfectSquares ( 1 , $ n ) ; } $ N = 10 ; $ arr [ 10 ] = array ( 0 ) ; echo countOnes ( $ arr , $ 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
< ? 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 " STRNEWLINE " ; } } $ A = array ( 2 , 2 , 2 , 2 ) ; $ B = array ( 1 , 2 , 3 , 4 ) ; $ sizeOfA = count ( $ A ) ; $ sizeOfB = count ( $ B ) ; printPosition ( $ A , $ B , $ sizeOfA , $ sizeOfB ) ; ? >
Highest power of a number that divides other number | Function to get the prime factors and its count of times it divides ; Count the number of 2 s that divide n ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; While i divides n , count i and divide n ; This condition is to handle the case when n is a prime number greater than 2 ; Function to return the highest power ;  ; Get the prime factors of n and m ; Iterate and find the maximum power ; If i not a prime factor of n and m ; If i is a prime factor of n and m If count of i dividing m is more than i dividing n , then power will be 0 ; If i is a prime factor of M ; get the maximum power ; Drivers code
< ? php function primeFactors ( $ n , $ freq ) { $ cnt = 0 ; while ( $ n % 2 == 0 ) { $ cnt ++ ; $ n = floor ( $ n / 2 ) ; } $ freq [ 2 ] = $ cnt ; for ( $ i = 3 ; $ i <= sqrt ( $ n ) ; $ i = $ i + 2 ) { $ cnt = 0 ; while ( $ n % $ i == 0 ) { $ cnt ++ ; $ n = floor ( $ n / $ i ) ; } $ freq [ $ i ] = $ cnt ; } if ( $ n > 2 ) $ freq [ $ n ] = 1 ; return $ freq ; } function getMaximumPower ( $ n , $ m ) { ' ' ' Initialize two arrays ' ' ' $ freq1 = array_fill ( 0 , $ n + 1 , 0 ) ; $ freq2 = array_fill ( 0 , $ m + 1 , 0 ) ; $ freq1 = primeFactors ( $ n , $ freq1 ) ; $ freq2 = primeFactors ( $ m , $ freq2 ) ; $ maxi = 0 ; for ( $ i = 2 ; $ i <= $ m ; $ i ++ ) { if ( $ freq1 [ $ i ] == 0 && $ freq2 [ $ i ] == 0 ) continue ; if ( $ freq2 [ $ i ] > $ freq1 [ $ i ] ) return 0 ; if ( $ freq2 [ $ i ] ) { $ maxi = max ( $ maxi , floor ( $ freq1 [ $ i ] / $ freq2 [ $ i ] ) ) ; } } return $ maxi ; } $ n = 48 ; $ m = 4 ; echo getMaximumPower ( $ n , $ m ) ; ? >
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
< ? php function findDivisors ( $ n ) { $ div = array_fill ( 0 , $ n + 2 , 0 ) ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { for ( $ j = 1 ; $ j * $ i <= $ n ; $ j ++ ) $ div [ $ i * $ j ] ++ ; } for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) echo $ div [ $ i ] , " ▁ " ; } $ n = 10 ; findDivisors ( $ n ) ; ? >
Predict the winner of the game on the basis of absolute difference of sum by selecting numbers | Function to decide the winner ; Iterate for all numbers in the array ; If mod gives 0 ; If mod gives 1 ; If mod gives 2 ; If mod gives 3 ; Check the winning condition for X ; Driver code
< ? php function decideWinner ( $ a , $ n ) { $ count0 = 0 ; $ count1 = 0 ; $ count2 = 0 ; $ count3 = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] % 4 == 0 ) $ count0 ++ ; else if ( $ a [ $ i ] % 4 == 1 ) $ count1 ++ ; else if ( $ a [ $ i ] % 4 == 2 ) $ count2 ++ ; else if ( $ a [ $ i ] % 4 == 3 ) $ count3 ++ ; } if ( $ count0 % 2 == 0 && $ count1 % 2 == 0 && $ count2 % 2 == 0 && $ count3 == 0 ) return 1 ; else return 2 ; } $ a = array ( 4 , 8 , 5 , 9 ) ; $ n = count ( $ a ) ; if ( decideWinner ( $ a , $ n ) == 1 ) echo " X ▁ wins " ; else echo " Y ▁ wins " ; ? >
Count all prefixes of the given binary array which are divisible by x | Function to return the count of total binary prefix which are divisible by x ; Initialize with zero ; Instead of converting all prefixes to decimal , take reminder with x ; If number is divisible by x then reminder = 0 ; Driver code
< ? php function CntDivbyX ( $ arr , $ n , $ x ) { $ number = 0 ; $ count1 = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ number = ( $ number * 2 + $ arr [ $ i ] ) % $ x ; if ( $ number == 0 ) $ count1 += 1 ; } return $ count1 ; } $ arr = array ( 1 , 0 , 1 , 0 , 1 , 1 , 0 ) ; $ n = sizeof ( $ arr ) ; $ x = 2 ; echo CntDivbyX ( $ arr , $ n , $ x ) ; ? >
Length of the smallest number which is divisible by K and formed by using 1 's only | Function to return length of the resultant number ; If K is a multiple of 2 or 5 ; Instead of generating all possible numbers 1 , 11 , 111 , 111 , ... , K 1 's  Take remainder with K ; If number is divisible by k then remainder will be 0 ; Driver code
< ? php function numLen ( $ K ) { if ( $ K % 2 == 0 $ K % 5 == 0 ) return -1 ; $ number = 0 ; $ len = 1 ; for ( $ len = 1 ; $ len <= $ K ; $ len ++ ) { $ number = ( $ number * 10 + 1 ) % $ K ; if ( $ number == 0 ) return $ len ; } return -1 ; } $ K = 7 ; echo numLen ( $ K ) ; ? >
Sum of multiplication of triplet of divisors of a number | PHP implementation of the approach ; Global array declaration ; Function to find the sum of multiplication of every triplet in the divisors of a number ; sum1 [ x ] represents the sum of all the divisors of x ; Adding i to sum1 [ j ] because i is a divisor of j ; sum2 [ x ] represents the sum of all the divisors of x ; Here i is divisor of j and sum1 [ j ] - i represents sum of all divisors of j which do not include i so we add i * ( sum1 [ j ] - i ) to sum2 [ j ] ; In the above implementation we have considered every pair two times so we have to divide every sum2 array element by 2 ; Here i is the divisor of j and we are trying to add the sum of multiplication of all triplets of divisors of j such that one of the divisors is i ; In the above implementation we have considered every triplet three times so we have to divide every sum3 array element by 3 ; Print the results ; Driver code ; Precomputing
< ? php $ max_Element = 1005 ; $ sum1 = array_fill ( 0 , $ max_Element , 0 ) ; $ sum2 = array_fill ( 0 , $ max_Element , 0 ) ; $ sum3 = array_fill ( 0 , $ max_Element , 0 ) ; function precomputation ( $ arr , $ n ) { global $ max_Element , $ sum3 , $ sum2 , $ sum1 ; for ( $ i = 1 ; $ i < $ max_Element ; $ i ++ ) for ( $ j = $ i ; $ j < $ max_Element ; $ j += $ i ) $ sum1 [ $ j ] += $ i ; for ( $ i = 1 ; $ i < $ max_Element ; $ i ++ ) for ( $ j = $ i ; $ j < $ max_Element ; $ j += $ i ) $ sum2 [ $ j ] += ( $ sum1 [ $ j ] - $ i ) * $ i ; for ( $ i = 1 ; $ i < $ max_Element ; $ i ++ ) $ sum2 [ $ i ] = ( int ) ( $ sum2 [ $ i ] / 2 ) ; for ( $ i = 1 ; $ i < $ max_Element ; $ i ++ ) for ( $ j = $ i ; $ j < $ max_Element ; $ j += $ i ) $ sum3 [ $ j ] += $ i * ( $ sum2 [ $ j ] - $ i * ( $ sum1 [ $ j ] - $ i ) ) ; for ( $ i = 1 ; $ i < $ max_Element ; $ i ++ ) $ sum3 [ $ i ] = ( int ) ( $ sum3 [ $ i ] / 3 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ sum3 [ $ arr [ $ i ] ] . " ▁ " ; } $ arr = array ( 9 , 5 , 6 ) ; $ n = count ( $ arr ) ; precomputation ( $ arr , $ n ) ; ? >
Sum of Fibonacci Numbers in a range | Function to return the nth Fibonacci number ; Function to return the required sum ; Using our deduced result ; Driver code
< ? php function fib ( $ n ) { $ phi = ( 1 + sqrt ( 5 ) ) / 2 ; return ( int ) round ( pow ( $ phi , $ n ) / sqrt ( 5 ) ) ; } function calculateSum ( $ l , $ r ) { $ sum = fib ( $ r + 2 ) - fib ( $ l + 1 ) ; return $ sum ; } $ l = 4 ; $ r = 8 ; echo ( calculateSum ( $ l , $ r ) ) ; ? >
Print the balanced bracket expression using given brackets | Function to print balanced bracket expression if it is possible ; If the condition is met ; Print brackets of type - 1 ; Print brackets of type - 3 ; Print brackets of type - 4 ; Print brackets of type - 2 ; If the condition is not met ; Driver code
< ? php function printBalancedExpression ( $ a , $ b , $ c , $ d ) { if ( ( $ a == $ d && $ a ) || ( $ a == 0 && $ c == 0 && $ d == 0 ) ) { for ( $ i = 1 ; $ i <= $ a ; $ i ++ ) echo " ( ( " ; for ( $ i = 1 ; $ i <= $ c ; $ i ++ ) echo " ) ( " ; for ( $ i = 1 ; $ i <= $ d ; $ i ++ ) echo " ) ) " ; for ( $ i = 1 ; $ i <= $ b ; $ i ++ ) echo " ( ) " ; } else echo - 1 ; } $ a = 3 ; $ b = 1 ; $ c = 4 ; $ d = 3 ; printBalancedExpression ( $ a , $ b , $ c , $ d ) ; ? >
Count numbers having N 0 ' s ▁ and ▁ and ▁ M ▁ 1' s with no leading zeros | Function to return the factorial of a number ; Function to return the count of distinct ( N + M ) digit numbers having N 0 ' s ▁ and ▁ and ▁ M ▁ 1' s with no leading zeros ; Driver code
< ? php function factorial ( $ f ) { $ fact = 1 ; for ( $ i = 2 ; $ i <= $ f ; $ i ++ ) $ fact *= $ i ; return $ fact ; } function findPermutation ( $ N , $ M ) { $ permutation = factorial ( $ N + $ M - 1 ) / ( factorial ( $ N ) * factorial ( $ M - 1 ) ) ; return $ permutation ; } $ N = 3 ; $ M = 3 ; echo findPermutation ( $ N , $ M ) ; ? >
Maximum value of | arr [ 0 ] | Function to return the maximum required value ; Driver code
< ? php function maxValue ( $ n ) { if ( $ n == 1 ) return 0 ; return ( ( $ n * $ n / 2 ) - 1 ) ; } $ n = 4 ; echo maxValue ( $ n ) ; ? >
Count of a , b & c after n seconds for given reproduction rate | Function to print the count of a , b and c after n seconds ; Number of multiples of 60 below n ; Multiple of 60 nearest to n ; Change all a to b ; Change all b to c ; Change each c to two a ; Print the updated values of a , b and c ; Driver code
< ? php function findCount ( $ n ) { $ a = 1 ; $ b = 0 ; $ c = 0 ; $ x = $ n / 60 ; $ a = pow ( 32 , $ x ) ; $ x = 60 * $ x ; for ( $ i = $ x + 1 ; $ i <= $ n ; $ i ++ ) { if ( $ i % 2 == 0 ) { $ b += $ a ; $ a = 0 ; } if ( $ i % 5 == 0 ) { $ c += $ b ; $ b = 0 ; } if ( $ i % 12 == 0 ) { $ a += ( 2 * $ c ) ; $ c = 0 ; } } echo ( " a ▁ = ▁ " . $ a . " , b = " ▁ . ▁ $ b ▁ . ▁ " , c = " } $ n = 72 ; findCount ( $ n ) ; ? >
Find GCD of factorial of elements of given array | Implementation of factorial function ; Function to find GCD of factorial of elements from array ; find the minimum element of array ; return the factorial of minimum element ; Driver Code
< ? php function factorial ( $ n ) { return ( $ n == 1 $ n == 0 ) ? 1 : factorial ( $ n - 1 ) * $ n ; } function gcdOfFactorial ( $ arr , $ n ) { $ minm = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ minm = $ minm > $ arr [ $ i ] ? $ arr [ $ i ] : $ minm ; return factorial ( $ minm ) ; } $ arr = array ( 9 , 12 , 122 , 34 , 15 ) ; $ n = count ( $ arr ) ; echo gcdOfFactorial ( $ arr , $ n ) ; ? >
Sum of the series 1 ^ 1 + 2 ^ 2 + 3 ^ 3 + ... . . + n ^ n using recursion | Recursive function to return the sum of the given series ; 1 ^ 1 = 1 ; Recursive call ; Driver code
< ? php function sum ( $ n ) { if ( $ n == 1 ) return 1 ; else return ( pow ( $ n , $ n ) + sum ( $ n - 1 ) ) ; } $ n = 2 ; echo ( sum ( $ n ) ) ; ? >
Count permutations that are first decreasing then increasing . | PHP implementation of the above approach ; Function to compute a ^ n % mod ; Function to count permutations that are first decreasing and then increasing ; For n = 1 return 0 ; Calculate and return result ; Driver Code
< ? php $ mod = 1000000007 ; function power ( $ a , $ n ) { global $ mod ; if ( $ n == 0 ) return 1 ; $ p = power ( $ a , $ n / 2 ) % $ mod ; $ p = ( $ p * $ p ) % $ mod ; if ( $ n & 1 ) $ p = ( $ p * $ a ) % $ mod ; return $ p ; } function countPermutations ( $ n ) { global $ mod ; if ( $ n == 1 ) { return 0 ; } return ( power ( 2 , $ n - 1 ) - 2 ) % $ mod ; } $ n = 5 ; echo countPermutations ( $ n ) ; ? >
Find the count of numbers that can be formed using digits 3 , 4 only and having length at max N . | Function to find the count of numbers that can be formed using digits 3 , 4 only and having length at max N . ; Driver code
< ? php function numbers ( $ n ) { return ( pow ( 2 , $ n + 1 ) ) - 2 ; } $ n = 2 ; echo numbers ( $ n ) ; ? >
Ways to place 4 items in n ^ 2 positions such that no row / column contains more than one | Function to return the number of ways to place 4 items in n ^ 2 positions ; Driver code
< ? php function NumberofWays ( $ n ) { $ x = ( 1 * ( $ n ) * ( $ n - 1 ) * ( $ n - 2 ) * ( $ n - 3 ) ) / ( 4 * 3 * 2 * 1 ) ; $ y = ( 1 * ( $ n ) * ( $ n - 1 ) * ( $ n - 2 ) * ( $ n - 3 ) ) ; return ( 1 * $ x * $ y ) ; } $ n = 4 ; echo NumberofWays ( $ n ) ; ? >
Find Nth term of the series 1 , 6 , 18 , 40 , 75 , ... . | Function to generate a fixed number ; ( N ^ 2 * ( N + 1 ) ) / 2 ; Driver Code
< ? php function nthTerm ( $ N ) { $ nth = 0 ; $ nth = ( $ N * $ N * ( $ N + 1 ) ) / 2 ; return $ nth ; } $ N = 5 ; echo nthTerm ( $ N ) ; ? >
Print n numbers such that their sum is a perfect square | Function to prn numbers such that their sum is a perfect square ; Print ith odd number ; Driver code
< ? php function findNumbers ( $ n ) { $ i = 1 ; while ( $ i <= $ n ) { echo ( ( 2 * $ i ) - 1 ) . " ▁ " ; $ i ++ ; } } $ n = 3 ; findNumbers ( $ n ) ; ? >
Missing even and odd elements from the given arrays | Function to find the missing numbers ; To store the minimum and the maximum odd and even elements from the arrays ; To store the sum of the array elements ; Get the minimum and the maximum even elements from the array ; Get the minimum and the maximum odd elements from the array ; To store the total terms in the series and the required sum of the array ; Total terms from 2 to minEven ; Sum of all even numbers from 2 to minEven ; Total terms from 2 to maxEven ; Sum of all even numbers from 2 to maxEven ; Required sum for the even array ; Missing even number ; Total terms from 1 to minOdd ; Sum of all odd numbers from 1 to minOdd ; Total terms from 1 to maxOdd ; Sum of all odd numbers from 1 to maxOdd ; Required sum for the odd array ; Missing odd number ; Driver code
< ? php function findMissingNums ( $ even , $ sizeEven , $ odd , $ sizeOdd ) { $ minEven = PHP_INT_MAX ; $ maxEven = PHP_INT_MIN ; $ minOdd = PHP_INT_MAX ; $ maxOdd = PHP_INT_MIN ; $ sumEvenArr = $ sumOddArr = 0 ; for ( $ i = 0 ; $ i < $ sizeEven ; $ i ++ ) { $ minEven = min ( $ minEven , $ even [ $ i ] ) ; $ maxEven = max ( $ maxEven , $ even [ $ i ] ) ; $ sumEvenArr += $ even [ $ i ] ; } for ( $ i = 0 ; $ i < $ sizeOdd ; $ i ++ ) { $ minOdd = min ( $ minOdd , $ odd [ $ i ] ) ; $ maxOdd = max ( $ maxOdd , $ odd [ $ i ] ) ; $ sumOddArr += $ odd [ $ i ] ; } $ totalTerms = $ reqSum = 0 ; $ totalTerms = ( int ) ( $ minEven / 2 ) ; $ evenSumMin = $ totalTerms * ( $ totalTerms + 1 ) ; $ totalTerms = ( int ) ( $ maxEven / 2 ) ; $ evenSumMax = $ totalTerms * ( $ totalTerms + 1 ) ; $ reqSum = ( $ evenSumMax - $ evenSumMin + $ minEven ) ; echo " Even = " ▁ . ▁ ( $ reqSum ▁ - ▁ $ sumEvenArr ) ▁ . ▁ " " $ totalTerms = ( int ) ( ( $ minOdd / 2 ) + 1 ) ; $ oddSumMin = $ totalTerms * $ totalTerms ; $ totalTerms = ( int ) ( ( $ maxOdd / 2 ) + 1 ) ; $ oddSumMax = $ totalTerms * $ totalTerms ; $ reqSum = ( $ oddSumMax - $ oddSumMin + $ minOdd ) ; echo " Odd = " } $ even = array ( 6 , 4 , 8 , 14 , 10 ) ; $ sizeEven = count ( $ even ) ; $ odd = array ( 7 , 5 , 3 , 11 , 13 ) ; $ sizeOdd = count ( $ odd ) ; findMissingNums ( $ even , $ sizeEven , $ odd , $ sizeOdd ) ; ? >
Minimum matches the team needs to win to qualify | Function to return the minimum number of matches to win to qualify for next round ; Do a binary search to find ; Find mid element ; Check for condition$ to qualify for next round ; Driver Code
< ? php function findMinimum ( $ x , $ y ) { $ low = 0 ; $ high = $ y ; while ( $ low <= $ high ) { $ mid = ( $ low + $ high ) >> 1 ; if ( ( $ mid * 2 + ( $ y - $ mid ) ) >= $ x ) $ high = $ mid - 1 ; else $ low = $ mid + 1 ; } return $ low ; } $ x = 6 ; $ y = 5 ; echo findMinimum ( $ x , $ y ) ; ? >
Count of all even numbers in the range [ L , R ] whose sum of digits is divisible by 3 | Function to return the sum of digits of x ; Function to return the count of required numbers ; If i is divisible by 2 and sum of digits of i is divisible by 3 ; Return the required count ; Driver code
< ? php function sumOfDigits ( $ x ) { $ sum = 0 ; while ( $ x != 0 ) { $ sum += $ x % 10 ; $ x = $ x / 10 ; } return $ sum ; } function countNumbers ( $ l , $ r ) { $ count = 0 ; for ( $ i = $ l ; $ i <= $ r ; $ i ++ ) { if ( $ i % 2 == 0 && sumOfDigits ( $ i ) % 3 == 0 ) $ count ++ ; } return $ count ; } $ l = 1000 ; $ r = 6000 ; echo countNumbers ( $ l , $ r ) ; ? >
Sum of minimum element of all subarrays of a sorted array | Function to find the sum of minimum of all subarrays ; Driver code
< ? php function findMinSum ( $ arr , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += $ arr [ $ i ] * ( $ n - $ i ) ; return $ sum ; } $ arr = array ( 3 , 5 , 7 , 8 ) ; $ n = count ( $ arr ) ; echo findMinSum ( $ arr , $ n ) ; ? >
Longest Sub | Function to return the max length of the sub - array that have the maximum average ( average value of the elements ) ; Finding the maximum value ; If consecutive maximum found ; Find the max length of consecutive max ; Driver code
< ? php function maxLenSubArr ( $ a , $ n ) { $ cm = 1 ; $ max = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] > $ max ) $ max = $ a [ $ i ] ; } for ( $ i = 0 ; $ i < $ n - 1 ; ) { $ count = 1 ; if ( $ a [ $ i ] == $ a [ $ i + 1 ] && $ a [ $ i ] == $ max ) { for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) { if ( $ a [ $ j ] == $ max ) { $ count ++ ; $ i ++ ; } else break ; } if ( $ count > $ cm ) $ cm = $ count ; } else $ i ++ ; } return $ cm ; } $ arr = array ( 6 , 1 , 6 , 6 , 0 ) ; $ n = sizeof ( $ arr ) ; echo maxLenSubArr ( $ arr , $ n ) ; ? >
Minimum possible sum of array elements after performing the given operation | Function to return the minimized sum ; To store the largest element from the array which is divisible by x ; Sum of array elements before performing any operation ; If current element is divisible by x and it is maximum so far ; Update the minimum element ; If no element can be reduced then there 's no point in performing the  operation as we will end up increasing  the sum when an element is multiplied by x ; Subtract the chosen elements from the sum and then add their updated values ; Return the minimized sum ; Driver code
< ? php function minSum ( $ arr , $ n , $ x ) { $ sum = 0 ; $ largestDivisible = -1 ; $ minimum = $ arr [ 0 ] ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ sum += $ arr [ $ i ] ; if ( $ arr [ $ i ] % $ x == 0 && $ largestDivisible < $ arr [ $ i ] ) $ largestDivisible = $ arr [ $ i ] ; if ( $ arr [ $ i ] < $ minimum ) $ minimum = $ arr [ $ i ] ; } if ( $ largestDivisible == -1 ) return $ sum ; $ sumAfterOperation = $ sum - $ minimum - $ largestDivisible + ( $ x * $ minimum ) + ( $ largestDivisible / $ x ) ; return min ( $ sum , $ sumAfterOperation ) ; } $ arr = array ( 5 , 5 , 5 , 5 , 6 ) ; $ n = sizeof ( $ arr ) ; $ x = 3 ; print ( minSum ( $ arr , $ n , $ x ) ) ; ? >