codeparrot/xlcost-text-to-code huggingface dataset:
Count ways to express a number as sum of consecutive numbers | Utility method to compute number of ways in which N can be represented as sum of consecutive number ; constraint on values of L gives us the time Complexity as O ( N ^ 0.5 ) ; Driver Code
< ? php function countConsecutive ( $ N ) { $ count = 0 ; for ( $ L = 1 ; $ L * ( $ L + 1 ) < 2 * $ N ; $ L ++ ) { $ a = ( int ) ( 1.0 * $ N - ( $ L * ( int ) ( $ L + 1 ) ) / 2 ) / ( $ L + 1 ) ; if ( $ a - ( int ) $ a == 0.0 ) $ count ++ ; } return $ count ; } $ N = 15 ; echo countConsecutive ( $ N ) , " STRNEWLINE " ; $ N = 10 ; echo countConsecutive ( $ N ) , " STRNEWLINE " ; ? >
Automorphic Number | Function to check Automorphic number ; Store the square ; Start Comparing digits ; Return false , if any digit of N doesn ' t ▁ ▁ match ▁ with ▁ its ▁ square ' s digits from last ; Reduce N and square ; Driver code
< ? php function isAutomorphic ( $ N ) { $ sq = $ N * $ N ; while ( $ N > 0 ) { if ( $ N % 10 != $ sq % 10 ) return -1 ; $ N /= 10 ; $ sq /= 10 ; } return 1 ; } $ N = 5 ; $ geeks = isAutomorphic ( $ N ) ? " Automorphic " : " Not ▁ Automorphic " ; echo $ geeks ; ? >
Number with maximum number of prime factors | Return smallest number having maximum prime factors . ; default value of boolean is false ; Sieve of eratosthenes ; Storing prime numbers . ; Generating number having maximum prime factors . ; Driver Code
< ? php function maxPrimefactorNum ( $ N ) { $ arr = array_fill ( 0 , $ N + 5 , true ) ; for ( $ i = 3 ; $ i * $ i <= $ N ; $ i += 2 ) { if ( $ arr [ $ i ] ) for ( $ j = $ i * $ i ; $ j <= $ N ; $ j += $ i ) $ arr [ $ j ] = false ; } $ prime = array ( ) ; array_push ( $ prime , 2 ) ; for ( $ i = 3 ; $ i <= $ N ; $ i += 2 ) if ( $ arr [ $ i ] ) array_push ( $ prime , $ i ) ; $ i = 0 ; $ ans = 1 ; while ( $ ans * $ prime [ $ i ] <= $ N && $ i < count ( $ prime ) ) { $ ans *= $ prime [ $ i ] ; $ i ++ ; } return $ ans ; } $ N = 40 ; print ( maxPrimefactorNum ( $ N ) ) ; ? >
Sum of all proper divisors of a natural number | Function to calculate sum of all proper divisors num -- > given natural number ; Final result of summation of divisors ; find all divisors which divides ' num ' ; if ' i ' is divisor of ' num ' ; if both divisors are same then add it only once else add both ; Add 1 to the result as 1 is also a divisor ; Driver Code
< ? php function divSum ( $ num ) { $ result = 0 ; for ( $ i = 2 ; $ i <= sqrt ( $ num ) ; $ i ++ ) { if ( $ num % $ i == 0 ) { if ( $ i == ( $ num / $ i ) ) $ result += $ i ; else $ result += ( $ i + $ num / $ i ) ; } } return ( $ result + 1 ) ; } $ num = 36 ; echo ( divSum ( $ num ) ) ; ? >
Find Square Root under Modulo p | Set 1 ( When p is in form of 4 * i + 3 ) | Utility function to do modular exponentiation . It returns ( x ^ y ) % p . ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y / 2 ; Returns true if square root of n under modulo p exists Assumption : p is of the form 3 * i + 4 where i >= 1 ; Try " + ( n ^ ( ( p ▁ + ▁ 1 ) / 4 ) ) " ; Try " - ( n ▁ ^ ▁ ( ( p ▁ + ▁ 1 ) / 4 ) ) " ; If none of the above two work , then square root doesn 't exist ; Driver Code
< ? php function power ( $ x , $ y , $ p ) { $ res = 1 ; $ x = $ x % $ p ; while ( $ y > 0 ) { if ( $ y & 1 ) $ res = ( $ res * $ x ) % $ p ; $ y = $ y >> 1 ; $ x = ( $ x * $ x ) % $ p ; } return $ res ; } function squareRoot ( $ n , $ p ) { if ( $ p % 4 != 3 ) { echo " Invalid ▁ Input " ; return ; } $ n = $ n % $ p ; $ x = power ( $ n , ( $ p + 1 ) / 4 , $ p ) ; if ( ( $ x * $ x ) % $ p == $ n ) { echo " Square ▁ root ▁ is ▁ " , $ x ; return ; } $ x = $ p - $ x ; if ( ( $ x * $ x ) % $ p == $ n ) { echo " Square ▁ root ▁ is ▁ " , $ x ; return ; } echo " Square ▁ root ▁ doesn ' t ▁ exist ▁ " ; } $ p = 7 ; $ n = 2 ; squareRoot ( $ n , $ p ) ; ? >
Primality Test | Set 3 ( Millerâ €“ Rabin ) | Utility function to do modular exponentiation . It returns ( x ^ y ) % p ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now $y = $y >> 1 ; $y = $y / 2 ; This function is called for all k trials . It returns false if n is composite and returns false if n is probably prime . d is an odd number such that d * 2 < sup > r < / sup > = n - 1 for some r >= 1 ; Pick a random number in [ 2. . n - 2 ] Corner cases make sure that n > 4 ; Compute a ^ d % n ; Keep squaring x while one of the following doesn 't  happen  (i) d does not reach n-1  (ii) (x^2) % n is not 1  (iii) (x^2) % n is not n-1 ; Return composite ; It returns false if n is composite and returns true if n is probably prime . k is an input parameter that determines accuracy level . Higher value of k indicates more accuracy . ; Corner cases ; Find r such that n = 2 ^ d * r + 1 for some r >= 1 ; Iterate given nber of ' k ' times ; Driver Code Number of iterations
< ? php function power ( $ x , $ y , $ p ) { $ res = 1 ; $ x = $ x % $ p ; while ( $ y > 0 ) { if ( $ y & 1 ) $ res = ( $ res * $ x ) % $ p ; $ x = ( $ x * $ x ) % $ p ; } return $ res ; } function miillerTest ( $ d , $ n ) { $ a = 2 + rand ( ) % ( $ n - 4 ) ; $ x = power ( $ a , $ d , $ n ) ; if ( $ x == 1 $ x == $ n - 1 ) return true ; while ( $ d != $ n - 1 ) { $ x = ( $ x * $ x ) % $ n ; $ d *= 2 ; if ( $ x == 1 ) return false ; if ( $ x == $ n - 1 ) return true ; } return false ; } function isPrime ( $ n , $ k ) { if ( $ n <= 1 $ n == 4 ) return false ; if ( $ n <= 3 ) return true ; $ d = $ n - 1 ; while ( $ d % 2 == 0 ) $ d /= 2 ; for ( $ i = 0 ; $ i < $ k ; $ i ++ ) if ( ! miillerTest ( $ d , $ n ) ) return false ; return true ; } $ k = 4 ; echo " All ▁ primes ▁ smaller ▁ than ▁ 100 : ▁ STRNEWLINE " ; for ( $ n = 1 ; $ n < 100 ; $ n ++ ) if ( isPrime ( $ n , $ k ) ) echo $ n , " ▁ " ; ? >
Length of the Longest Consecutive 1 s in Binary Representation | Function to find length of the longest consecutive 1 s in binary representation of a number ; Initialize result ; Count the number of iterations to reach x = 0. ; This operation reduces length of every sequence of 1 s by one . ; Driver code
< ? php function maxConsecutiveOnes ( $ x ) { $ count = 0 ; while ( $ x != 0 ) { $ x = ( $ x & ( $ x << 1 ) ) ; $ count ++ ; } return $ count ; } echo maxConsecutiveOnes ( 14 ) , " STRNEWLINE " ; echo maxConsecutiveOnes ( 222 ) , " STRNEWLINE " ; ? >
Subtract two numbers without using arithmetic operators | PHP Program to subtract two Number without using arithmetic operator ; Iterate till there is no carry ; borrow contains common set bits of y and unset bits of x ; Subtraction of bits of x and y where at least one of the bits is not set ; Borrow is shifted by one so that subtracting it from x gives the required sum ; Driver Code
< ? php function subtract ( $ x , $ y ) { while ( $ y != 0 ) { $ borrow = ( ~ $ x ) & $ y ; $ x = $ x ^ $ y ; $ y = $ borrow << 1 ; } return $ x ; } $ x = 29 ; $ y = 13 ; echo " x ▁ - ▁ y ▁ is ▁ " , subtract ( $ x , $ y ) ; ? >
Subtract two numbers without using arithmetic operators | PHP Program to subtract two Number without using arithmetic operator Recursive implementation . ; Driver Code
< ? php function subtract ( $ x , $ y ) { if ( $ y == 0 ) return $ x ; return subtract ( $ x ^ $ y , ( ~ $ x & $ y ) << 1 ) ; } $ x = 29 ; $ y = 13 ; echo " x ▁ - ▁ y ▁ is ▁ " , subtract ( $ x , $ y ) ; # This  code is contributed by ajit NEW_LINE ? >
Number of ways to choose a pair containing an even and an odd number from 1 to N | Driver code
< ? php $ N = 6 ; $ Even = $ N / 2 ; $ Odd = $ N - $ Even ; echo $ Even * $ Odd ; ? >
Program to print Step Pattern | function to print the steps ; declare a flag ; traverse through all the characters in the string ; if the x value is 0. . then we must increment till n ... set flag to true ; if the x value is n - 1 then we must decrement till 0 ... set flag as false ; print x * s ; checking whether to increment or decrement x ; Get the String and the number n ; calling the function
< ? php function steps ( $ str , $ n ) { $ x = 0 ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) { if ( $ x == 0 ) $ flag = true ; if ( $ x == $ n - 1 ) $ flag = false ; for ( $ j = 0 ; $ j < $ x ; $ j ++ ) echo " * " ; echo $ str [ $ i ] , " STRNEWLINE " ; if ( $ flag == true ) $ x ++ ; else $ x -- ; } } $ n = 4 ; $ str = " GeeksForGeeks " ; echo " String : " , ▁ $ str , ▁ " " ; STRNEWLINE echo ▁ " Max Length of Steps : " , ▁ $ n , ▁ " " steps ( $ str , $ n ) ; ? >
Check divisibility of binary string by 2 ^ k | function to check whether given binary number is evenly divisible by 2 ^ k or not ; count of number of 0 from last ; if count = k , number is evenly divisible , so returns true else false ; first example ; Second example
< ? php function isDivisible ( $ str , $ k ) { $ n = strlen ( $ str ) ; $ c = 0 ; for ( $ i = 0 ; $ i < $ k ; $ i ++ ) if ( $ str [ $ n - $ i - 1 ] == '0' ) $ c ++ ; return ( $ c == $ k ) ; } $ str1 = "10101100" ; $ k = 2 ; if ( isDivisible ( $ str1 , $ k ) ) echo " Yes " , " STRNEWLINE " ; else echo " No " , " STRNEWLINE " ; $ str2 = "111010100" ; $ k = 2 ; if ( isDivisible ( $ str2 , $ k ) ) echo " Yes " , " STRNEWLINE " ; else echo " No " , " STRNEWLINE " ; ? >
Program to check if input is an integer or a string | Returns true if s is a number else false ; Saving the input in a string ; Function returns 1 if all elements are in range '0-9' ; Function returns 0 if the input is not an integer
< ? php function isNumber ( $ s ) { for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) if ( is_numeric ( $ s [ $ i ] ) == false ) return false ; return true ; } $ str = "6790" ; if ( isNumber ( $ str ) ) echo " Integer " ; else echo " String " ; ? >
Print reverse of a string using recursion | Function to print reverse of the passed string ; Driver Code
< ? php function reverse ( $ str ) { if ( ( $ str == null ) || ( strlen ( $ str ) <= 1 ) ) echo ( $ str ) ; else { echo ( $ str [ strlen ( $ str ) - 1 ] ) ; reverse ( substr ( $ str , 0 , ( strlen ( $ str ) - 1 ) ) ) ; } } $ str = " Geeks ▁ for ▁ Geeks " ; reverse ( $ str ) ; ? >
Area of a n | Function to find the area of a regular polygon ; Side and radius cannot be negative ; Area degree converted to radians ; Driver code
< ? php function polyarea ( $ n , $ r ) { if ( $ r < 0 && $ n < 0 ) return -1 ; $ A = ( ( $ r * $ r * $ n ) * sin ( ( 360 / $ n ) * 3.14159 / 180 ) ) / 2 ; return $ A ; } $ r = 9 ; $ n = 6 ; echo polyarea ( $ n , $ r ) . " STRNEWLINE " ; ? >
Slope of perpendicular to line | Function to find the Slope of other line ; Driver Code
< ? php function findPCSlope ( $ m ) { return -1.0 / $ m ; } $ m = 2.0 ; echo findPCSlope ( $ m ) ; ? >
Program to find area of a Circular Segment | Function to find area of segment ; Calculating area of sector ; Calculating area of triangle ; Driver Code
< ? php function area_of_segment ( $ radius , $ angle ) { $ pi = 3.14159 ; $ area_of_sector = $ pi * ( $ radius * $ radius ) * ( $ angle / 360 ) ; $ area_of_triangle = 1 / 2 * ( $ radius * $ radius ) * sin ( ( $ angle * $ pi ) / 180 ) ; return $ area_of_sector - $ area_of_triangle ; } $ radius = 10.0 ; $ angle = 90.0 ; echo ( " Area ▁ of ▁ minor ▁ segment ▁ = ▁ " ) ; echo ( area_of_segment ( $ radius , $ angle ) ) ; echo ( " STRNEWLINE " ) ; echo ( " Area ▁ of ▁ major ▁ segment ▁ = ▁ " ) ; echo ( area_of_segment ( $ radius , ( 360 - $ angle ) ) ) ; ? >
Area of a Circular Sector | PHP program to find Area of a Sector ; Calculating area of the sector ; Driver code
< ? php function SectorArea ( $ radius , $ angle ) { if ( $ angle >= 360 ) echo ( " Angle ▁ not ▁ possible " ) ; else { $ sector = ( ( 22 * $ radius * $ radius ) / 7 ) * ( $ angle / 360 ) ; echo ( $ sector ) ; } } $ radius = 9 ; $ angle = 60 ; SectorArea ( $ radius , $ angle ) ; ? >
Recursive Insertion Sort | Recursive function to sort an array using insertion sort ; Base case ; Sort first n - 1 elements ; Insert last element at its correct position in sorted array . ; Move elements of arr [ 0. . i - 1 ] , that are greater than key , to one position ahead of their current position ; A utility function to print an array of size n ; Driver Code
< ? php function insertionSortRecursive ( & $ arr , $ n ) { if ( $ n <= 1 ) return ; insertionSortRecursive ( $ arr , $ n - 1 ) ; $ last = $ arr [ $ n - 1 ] ; $ j = $ n - 2 ; while ( $ j >= 0 && $ arr [ $ j ] > $ last ) { $ arr [ $ j + 1 ] = $ arr [ $ j ] ; $ j -- ; } $ arr [ $ j + 1 ] = $ last ; } function printArray ( & $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; } $ arr = array ( 12 , 11 , 13 , 5 , 6 ) ; $ n = sizeof ( $ arr ) ; insertionSortRecursive ( $ arr , $ n ) ; printArray ( $ arr , $ n ) ; ? >
Check if an array is Wave Array | Function to check if array is wave array arr : input array n : size of array ; Check the wave form * If arr [ 1 ] is greater than left and right * Same pattern will be followed by whole * elements , else reverse pattern * will be followed by array elements ; Check for last element ; Check for last element ; Array
< ? php function isWaveArray ( $ arr , $ n ) { $ result = true ; if ( $ arr [ 1 ] > $ arr [ 0 ] && $ arr [ 1 ] > $ arr [ 2 ] ) { for ( $ i = 1 ; $ i < ( $ n - 1 ) ; $ i += 2 ) { if ( $ arr [ $ i ] > $ arr [ $ i - 1 ] && $ arr [ $ i ] > $ arr [ $ i + 1 ] ) { $ result = true ; } else { $ result = false ; break ; } } if ( $ result == true && $ n % 2 == 0 ) { if ( $ arr [ $ n - 1 ] <= $ arr [ $ n - 2 ] ) { $ result = false ; } } } else if ( $ arr [ 1 ] < $ arr [ 0 ] && $ arr [ 1 ] < $ arr [ 2 ] ) { for ( $ i = 1 ; $ i < $ n - 1 ; $ i += 2 ) { if ( $ arr [ $ i ] < $ arr [ $ i - 1 ] && $ arr [ $ i ] < $ arr [ $ i + 1 ] ) { $ result = true ; } else { $ result = false ; break ; } } if ( $ result == true && $ n % 2 == 0 ) { if ( $ arr [ $ n - 1 ] >= $ arr [ $ n - 2 ] ) { $ result = false ; } } } return $ result ; } $ arr = array ( 1 , 3 , 2 , 4 ) ; $ n = sizeof ( $ arr ) ; if ( isWaveArray ( $ arr , $ n ) ) { echo " YES " ; } else { echo " NO " ; } ? >
Find the sum of first N odd Fibonacci numbers | PHP program to Find the sum of first N odd Fibonacci numbers ; Function to calculate sum of first N odd Fibonacci numbers ; base values ; Driver code
< ? php $ mod = 1000000007 ; function sumOddFibonacci ( $ n ) { global $ mod ; $ Sum [ $ n + 1 ] = array ( ) ; $ Sum [ 0 ] = 0 ; $ Sum [ 1 ] = 1 ; $ Sum [ 2 ] = 2 ; $ Sum [ 3 ] = 5 ; $ Sum [ 4 ] = 10 ; $ Sum [ 5 ] = 23 ; for ( $ i = 6 ; $ i <= $ n ; $ i ++ ) { $ Sum [ $ i ] = ( ( $ Sum [ $ i - 1 ] + ( 4 * $ Sum [ $ i - 2 ] ) % $ mod - ( 4 * $ Sum [ $ i - 3 ] ) % $ mod + $ mod ) % $ mod + ( $ Sum [ $ i - 4 ] - $ Sum [ $ i - 5 ] + $ mod ) % $ mod ) % $ mod ; } return $ Sum [ $ n ] ; } $ n = 6 ; echo sumOddFibonacci ( $ n ) ; ? >
Number of ways to reach Nth floor by taking at | PHP program to reach N - th stair by taking a maximum of K leap ; elements of combo [ ] stores the no of possible ways to reach it by all combinations of k leaps or less ; assuming leap 0 exist and assigning its value to 1 for calculation ; loop to iterate over all possible leaps upto k ; ; in this loop we count all possible leaps to reach the jth stair with the help of ith leap or less ; if the leap is not more than the i - j ; calculate the value and store in combo [ j ] to reuse it for next leap calculation for the jth stair ; returns the no of possible number of leaps to reach the top of building of n stairs ; N i the no of total stairs K is the value of the greatest leap
< ? php function solve ( $ N , $ K ) { $ combo [ $ N + 1 ] = array ( ) ; $ combo [ 0 ] = 1 ; for ( $ i = 1 ; $ i <= $ K ; $ i ++ ) { for ( $ j = 0 ; $ j <= $ N ; $ j ++ ) { if ( $ j >= $ i ) { $ combo [ $ j ] += $ combo [ $ j - $ i ] ; } } } return $ combo [ $ N ] ; } $ N = 29 ; $ K = 5 ; echo solve ( $ N , $ K ) ; solve ( $ N , $ K ) ; ? >
Find the Longest Increasing Subsequence in Circular manner | Utility function to find LIS using Dynamic programming ; Initialize LIS values for all indexes ; Compute optimized LIS values in bottom up manner ; Set j on the basis of current window i . e . first element of the current window ; Pick maximum of all LIS values ; Function to find LIS in Circular manner ; Make a copy of given array by appending same array elements to itself ; Perform LIS for each window of size n ; Driver Code
< ? php function computeLIS ( $ circBuff , $ start , $ end , $ n ) { $ LIS = Array ( ) ; for ( $ i = $ start ; $ i < $ end ; $ i ++ ) $ LIS [ $ i ] = 1 ; for ( $ i = $ start + 1 ; $ i < $ end ; $ i ++ ) for ( $ j = $ start ; $ j < $ i ; $ j ++ ) if ( $ circBuff [ $ i ] > $ circBuff [ $ j ] && $ LIS [ $ i ] < $ LIS [ $ j ] + 1 ) $ LIS [ $ i ] = $ LIS [ $ j ] + 1 ; $ res = PHP_INT_MIN ; for ( $ i = $ start ; $ i < $ end ; $ i ++ ) $ res = max ( $ res , $ LIS [ $ i ] ) ; return $ res ; } function LICS ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ circBuff [ $ i ] = $ arr [ $ i ] ; for ( $ i = $ n ; $ i < 2 * $ n ; $ i ++ ) $ circBuff [ $ i ] = $ arr [ $ i - $ n ] ; $ res = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ res = max ( computeLIS ( $ circBuff , $ i , $ i + $ n , $ n ) , $ res ) ; return $ res ; } $ arr = array ( 1 , 4 , 6 , 2 , 3 ) ; $ n = sizeof ( $ arr ) ; echo " Length ▁ of ▁ LICS ▁ is ▁ " , LICS ( $ arr , $ n ) ; ? >
Longest Common Increasing Subsequence ( LCS + LIS ) | Returns the length and the LCIS of two arrays arr1 [ 0. . n - 1 ] and arr2 [ 0. . m - 1 ] ; table [ j ] is going to store length of LCIS ending with arr2 [ j ] . We initialize it as 0 , ; Traverse all elements of arr1 [ ] ; Initialize current length of LCIS ; For each element of arr1 [ ] , trvarse all elements of arr2 [ ] . ; If both the array have same elements . Note that we don 't break the loop here. ; Now seek for previous smaller common element for current element of arr1 ; The maximum value in table [ ] is out result ; Driver Code
< ? php function LCIS ( $ arr1 , $ n , $ arr2 , $ m ) { $ table = Array ( ) ; for ( $ j = 0 ; $ j < $ m ; $ j ++ ) $ table [ $ j ] = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ current = 0 ; for ( $ j = 0 ; $ j < $ m ; $ j ++ ) { if ( $ arr1 [ $ i ] == $ arr2 [ $ j ] ) if ( $ current + 1 > $ table [ $ j ] ) $ table [ $ j ] = $ current + 1 ; if ( $ arr1 [ $ i ] > $ arr2 [ $ j ] ) if ( $ table [ $ j ] > $ current ) $ current = $ table [ $ j ] ; } } $ result = 0 ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) if ( $ table [ $ i ] > $ result ) $ result = $ table [ $ i ] ; return $ result ; } $ arr1 = array ( 3 , 4 , 9 , 1 ) ; $ arr2 = array ( 5 , 3 , 8 , 9 , 10 , 2 , 1 ) ; $ n = sizeof ( $ arr1 ) ; $ m = sizeof ( $ arr2 ) ; echo " Length ▁ of ▁ LCIS ▁ is ▁ " , LCIS ( $ arr1 , $ n , $ arr2 , $ m ) ; ? >
Maximize the value of A by replacing some of its digits with digits of B | Function to return the maximized value of A ; Sort digits in ascending order ; j points to largest digit in B ; If all the digits of b have been used ; Current digit has been used ; Convert array into string ; Return the maximized value ; Driver code
< ? php function maxValue ( $ a , $ b ) { sort ( $ b ) ; $ n = sizeof ( $ a ) ; $ m = sizeof ( $ b ) ; $ j = $ m - 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ j < 0 ) break ; if ( $ b [ $ j ] > $ a [ $ i ] ) { $ a [ $ i ] = $ b [ $ j ] ; $ j -- ; } } $ a = implode ( " " , $ a ) ; return $ a ; } # convert  string into array NEW_LINE $ a = str_split ( "1234" ) ; $ b = str_split ( "4321" ) ; echo maxValue ( $ a , $ b ) ; ? >
Count numbers in range such that digits in it and it 's product with q are unequal | Function to check if all of the digits in a number and it 's product with q are unequal or not ; convert first number into string ; Insert elements from 1 st number to hash ; Calculate corresponding product ; Convert the product to string ; Using the hash check if any digit of product matches with the digits of input number ; If yes , return false ; Else , return true ; Function to count numbers in the range [ l , r ] such that all of the digits of the number and it 's product with q are unequal ; check for every number between l and r ; Driver Code ; Function call
< ? php function checkIfUnequal ( $ n , $ q ) { $ s1 = strval ( $ n ) ; $ a = array_fill ( 0 , 26 , NULL ) ; for ( $ i = 0 ; $ i < strlen ( $ s1 ) ; $ i ++ ) $ a [ ord ( $ s1 [ $ i ] ) - ord ( '0' ) ] ++ ; $ prod = $ n * $ q ; $ s2 = strval ( $ prod ) ; for ( $ i = 0 ; $ i < strlen ( $ s2 ) ; $ i ++ ) { if ( $ a [ ord ( $ s2 [ $ i ] ) - ord ( '0' ) ] ) return false ; } return true ; } function countInRange ( $ l , $ r , $ q ) { $ count = 0 ; for ( $ i = $ l ; $ i <= $ r ; $ i ++ ) { if ( checkIfUnequal ( $ i , $ q ) ) $ count ++ ; } return $ count ; } $ l = 10 ; $ r = 12 ; $ q = 2 ; echo countInRange ( $ l , $ r , $ q ) ; ? >
Check if it is possible to rearrange a binary string with alternate 0 s and 1 s | function to check the binary string ; length of string ; count zero 's ; count one 's ; if length is even ; if length is odd ; Driver code
< ? php function is_possible ( $ s ) { $ l = strlen ( $ s ) ; $ one = 0 ; $ zero = 0 ; for ( $ i = 0 ; $ i < $ l ; $ i ++ ) { if ( $ s [ $ i ] == '0' ) $ zero ++ ; else $ one ++ ; } if ( $ l % 2 == 0 ) return ( $ one == $ zero ) ; else return ( abs ( $ one - $ zero ) == 1 ) ; } $ s = "100110" ; if ( is_possible ( $ s ) ) echo ( " Yes " ) ; else echo ( " No " ) ; ? >
gOOGLE cASE of a given sentence | Function to remove spaces and convert into camel case ; check for spaces in the sentence ; conversion into upper case ; If not space , copy character ; return string to main ; Driver Code
< ? php function convert ( $ s ) { $ n = strlen ( $ s ) ; $ s [ 0 ] = strtolower ( $ s [ 0 ] ) ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { if ( $ s [ $ i ] == ' ▁ ' && $ i < $ n ) { $ s [ $ i + 1 ] = strtolower ( $ s [ $ i + 1 ] ) ; $ i ++ ; } else $ s [ $ i ] = strtoupper ( $ s [ $ i ] ) ; } return $ s ; } $ str = " I ▁ get ▁ intern ▁ at ▁ geeksforgeeks " ; echo ( convert ( $ str ) ) ; ? >
Transform the string | Function to change character 's case ; If character is lowercase change to uppercase ; If character is uppercase change to lowercase ; Function to delete vowels ; If character is consonant ; Function to insert " # " ; If character is not special ; Function to transform string ; Driver Code ; Calling function
< ? php function change_case ( $ a ) { $ l = strlen ( $ a ) ; for ( $ i = 0 ; $ i < $ l ; $ i ++ ) { if ( $ a [ $ i ] >= ' a ' && $ a [ $ i ] <= ' z ' ) $ a [ $ i ] = chr ( 65 + ( ord ( $ a [ $ i ] ) - ord ( ' a ' ) ) ) ; else if ( $ a [ $ i ] >= ' A ' && $ a [ $ i ] <= ' Z ' ) $ a [ $ i ] = chr ( 97 + ( ord ( $ a [ $ i ] ) - ord ( ' a ' ) ) ) ; } return $ a ; } function delete_vowels ( $ a ) { $ temp = " " ; $ l = strlen ( $ a ) ; for ( $ i = 0 ; $ i < $ l ; $ i ++ ) { if ( $ a [ $ i ] != ' a ' && $ a [ $ i ] != ' e ' && $ a [ $ i ] != ' i ' && $ a [ $ i ] != ' o ' && $ a [ $ i ] != ' u ' && $ a [ $ i ] != ' A ' && $ a [ $ i ] != ' E ' && $ a [ $ i ] != ' O ' && $ a [ $ i ] != ' U ' && $ a [ $ i ] != ' I ' ) $ temp = $ temp . $ a [ $ i ] ; } return $ temp ; } function insert_hash ( $ a ) { $ temp = " " ; $ l = strlen ( $ a ) ; for ( $ i = 0 ; $ i < $ l ; $ i ++ ) { if ( ( $ a [ $ i ] >= ' a ' && $ a [ $ i ] <= ' z ' ) || ( $ a [ $ i ] >= ' A ' && $ a [ $ i ] <= ' Z ' ) ) $ temp = $ temp . ' # ' . $ a [ $ i ] ; else $ temp = $ temp . $ a [ $ i ] ; } return $ temp ; } function transformSting ( $ a ) { $ b = delete_vowels ( $ a ) ; $ c = change_case ( $ b ) ; $ d = insert_hash ( $ c ) ; echo ( $ d ) ; } $ a = " SunshinE ! ! " ; transformSting ( $ a ) ; ? >
Program to find the N | PHP program to find n - th number containing only 3 and 5. ; If n is odd , append 3 and move to parent ; If n is even , append 5 and move to parent ; Reverse res and return . ; Driver code
< ? php function findNthNo ( $ n ) { $ res = " " ; while ( $ n >= 1 ) { if ( $ n & 1 ) { $ res = $ res + "3" ; $ n = ( $ n - 1 ) / 2 ; } else { $ res = $ res . "5" ; $ n = ( $ n - 2 ) / 2 ; } } $ res = strrev ( $ res ) ; return $ res ; } $ n = 5 ; echo findNthNo ( $ n ) ; ? >
Nth non | function to find the nth Non - Square Number ; conversion from int to long double is necessary in order to preserve decimal places after square root . ; calculating the result ; initializing the term number ; Print the result
< ? php function findNthNonSquare ( $ n ) { $ x = $ n ; $ ans = $ x + floor ( 0.5 + sqrt ( $ x ) ) ; return ( int ) $ ans ; } $ n = 16 ; echo " The ▁ " . $ n . " th ▁ Non - Square ▁ number ▁ is ▁ " ; echo findNthNonSquare ( $ n ) ;