codeparrot/xlcost-text-to-code huggingface dataset:
Find the repeating and the missing number using two equations | Function to print the required numbers ; Sum of first n natural numbers ; Sum of squares of first n natural numbers ; To store the sum and sum of squares of the array elements ; Driver code
< ? php function findNumbers ( $ arr , $ n ) { $ sumN = ( $ n * ( $ n + 1 ) ) / 2 ; $ sumSqN = ( $ n * ( $ n + 1 ) * ( 2 * $ n + 1 ) ) / 6 ; $ sum = 0 ; $ sumSq = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ sum += $ arr [ $ i ] ; $ sumSq += pow ( $ arr [ $ i ] , 2 ) ; } $ B = ( ( ( $ sumSq - $ sumSqN ) / ( $ sum - $ sumN ) ) + $ sumN - $ sum ) / 2 ; $ A = $ sum - $ sumN + $ B ; echo " A = " , ▁ $ A , ▁ " B = " } $ arr = array ( 1 , 2 , 2 , 3 , 4 ) ; $ n = sizeof ( $ arr ) ; findNumbers ( $ arr , $ n ) ; ? >
Count the triplets such that A [ i ] < B [ j ] < C [ k ] | Function to return the count of elements in arr [ ] which are less than the given key ; Modified binary search ; Function to return the count of elements in arr [ ] which are greater than the given key ; Modified binary search ; Function to return the count of the required triplets ; Sort all three arrays ; Iterate for all the elements of array B ; Count of elements in A [ ] which are less than the chosen element from B [ ] ; Count of elements in C [ ] which are greater than the chosen element from B [ ] ; Update the count ; Driver code
< ? php function countLessThan ( & $ arr , $ n , $ key ) { $ l = 0 ; $ r = $ n - 1 ; $ index = -1 ; while ( $ l <= $ r ) { $ m = intval ( ( $ l + $ r ) / 2 ) ; if ( $ arr [ $ m ] < $ key ) { $ l = $ m + 1 ; $ index = $ m ; } else { $ r = $ m - 1 ; } } return ( $ index + 1 ) ; } function countGreaterThan ( & $ arr , $ n , $ key ) { $ l = 0 ; $ r = $ n - 1 ; $ index = -1 ; while ( $ l <= $ r ) { $ m = intval ( ( $ l + $ r ) / 2 ) ; if ( $ arr [ $ m ] <= $ key ) { $ l = $ m + 1 ; } else { $ r = $ m - 1 ; $ index = $ m ; } } if ( $ index == -1 ) return 0 ; return ( $ n - $ index ) ; } function countTriplets ( $ n , & $ a , & $ b , & $ c ) { sort ( $ a ) ; sort ( $ b ) ; sort ( $ c ) ; $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { $ current = $ b [ $ i ] ; $ a_index = -1 ; $ c_index = -1 ; $ low = countLessThan ( $ a , $ n , $ current ) ; $ high = countGreaterThan ( $ c , $ n , $ current ) ; $ count += ( $ low * $ high ) ; } return $ count ; } $ a = array ( 1 , 5 ) ; $ b = array ( 2 , 4 ) ; $ c = array ( 3 , 6 ) ; $ size = sizeof ( $ a ) ; echo countTriplets ( $ size , $ a , $ b , $ c ) ; ? >
Middle of three using minimum comparisons | Function to find the middle of three number ; x is positive if a is greater than b . x is negative if b is greater than a . ; Similar to x ; Similar to x and y . ; Checking if b is middle ( x and y both are positive ) ; Checking if c is middle ( x and z both are positive ) ; Driver Code
< ? php function middleOfThree ( $ a , $ b , $ c ) { $ x = $ a - $ b ; $ y = $ b - $ c ; $ z = $ a - $ c ; if ( $ x * $ y > 0 ) return $ b ; else if ( $ x * $ z > 0 ) return $ c ; else return $ a ; } $ a = 20 ; $ b = 30 ; $ c = 40 ; echo middleOfThree ( $ a , $ b , $ c ) ; ? >
Find four missing numbers in an array containing elements from 1 to N | Finds missing 4 numbers in O ( N ) time and O ( 1 ) auxiliary space . ; To keep track of 4 possible numbers greater than length of input array initialized as 0. ; Traverse the input array and mark visited elements either by marking them as negative in arr [ ] or in helper [ ] . ; If element is smaller than or equal to length , mark its presence in arr [ ] ; Mark presence in helper [ ] ; Print all those elements whose presence is not marked . ; Driver code
< ? php function missing4 ( $ arr , $ n ) { $ helper = array ( 0 , 0 , 0 , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ temp = abs ( $ arr [ $ i ] ) ; if ( $ temp <= $ n ) $ arr [ $ temp - 1 ] = $ arr [ $ temp - 1 ] * ( -1 ) ; else if ( $ temp > $ n ) { if ( $ temp % $ n != 0 ) $ helper [ $ temp % $ n - 1 ] = -1 ; else $ helper [ ( $ temp % $ n ) + $ n - 1 ] = -1 ; } } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] > 0 ) { $ a = $ i + 1 ; echo " $ a " , " ▁ " ; } for ( $ i = 0 ; $ i < 4 ; $ i ++ ) if ( $ helper [ $ i ] >= 0 ) { $ b = $ n + $ i + 1 ; echo " $ b " , " ▁ " ; } echo " STRNEWLINE " ; return ; } $ arr = array ( 1 , 7 , 3 , 12 , 5 , 10 , 8 , 4 , 9 ) ; $ n = sizeof ( $ arr ) ; missing4 ( $ arr , $ n ) ; ? >
Minimum range increment operations to Sort an array | Function to find minimum range increments to sort an array ; If current element is found greater than last element Increment all terms in range i + 1 to n - 1 ; $mn = $arr [ $i ] ; Minimum in range i to n - 1 ; Driver Code
< ? php function minMovesToSort ( $ arr , $ n ) { $ moves = 0 ; $ mn = $ arr [ $ n - 1 ] ; for ( $ i = $ n - 2 ; $ i >= 0 ; $ i -- ) { if ( $ arr [ $ i ] > $ mn ) $ moves += $ arr [ $ i ] - $ mn ; } return $ moves ; } $ arr = array ( 3 , 5 , 2 , 8 , 4 ) ; $ n = sizeof ( $ arr ) ; echo minMovesToSort ( $ arr , $ n ) ; ? >
Pair formation such that maximum pair sum is minimized | PHP Program to divide the array into N pairs such that maximum pair is minimized ; After Sorting Maintain two variables i and j pointing to start and end of array Such that smallest element of array pairs with largest element ; Driver Code
< ? php function findOptimalPairs ( $ arr , $ N ) { sort ( $ arr ) ; for ( $ i = 0 , $ j = $ N - 1 ; $ i <= $ j ; $ i ++ , $ j -- ) echo " ( " , $ arr [ $ i ] , " , ▁ " , $ arr [ $ j ] , " ) " , " ▁ " ; } $ arr = array ( 9 , 6 , 5 , 1 ) ; $ N = sizeof ( $ arr ) ; findOptimalPairs ( $ arr , $ N ) ; ? >
Minimum number of given operations required to reduce the array to 0 element | Function to return the minimum operations required ; Count the frequency of each element ; Maximum element from the array ; Find all the multiples of i ; Delete the multiples ; Increment the operations ; Driver code
< ? php function minOperations ( $ arr , $ n ) { $ result = 0 ; $ freq = array ( ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ freq [ $ arr [ $ i ] ] = 0 ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ x = $ arr [ $ i ] ; $ freq [ $ x ] ++ ; } $ maxi = max ( $ arr ) ; for ( $ i = 1 ; $ i <= $ maxi ; $ i ++ ) { if ( $ freq [ $ i ] != 0 ) { for ( $ j = $ i * 2 ; $ j <= $ maxi ; $ j = $ j + $ i ) { $ freq [ $ j ] = 0 ; } $ result ++ ; } } return $ result ; } $ arr = array ( 2 , 4 , 2 , 4 , 4 , 4 ) ; $ n = count ( $ arr ) ; echo minOperations ( $ arr , $ n ) ; ? >
Minimum LCM and GCD possible among all possible sub | Function to return minimum GCD among all subarrays ; Minimum GCD among all sub - arrays will be the GCD of all the elements of the array ; Function to return minimum LCM among all subarrays ; Minimum LCM among all sub - arrays will be the minimum element from the array ; Driver code
< ? php function __gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return __gcd ( $ b % $ a , $ a ) ; } function minGCD ( $ arr , $ n ) { $ minGCD = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ minGCD = __gcd ( $ minGCD , $ arr [ $ i ] ) ; return $ minGCD ; } function minLCM ( $ arr , $ n ) { $ minLCM = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ minLCM = min ( $ minLCM , $ arr [ $ i ] ) ; return $ minLCM ; } $ arr = array ( 2 , 66 , 14 , 521 ) ; $ n = sizeof ( $ arr ) ; echo " LCM = " ▁ . ▁ minLCM ( $ arr , ▁ $ n ) ▁ . ▁ " , " echo " GCD ▁ = ▁ " . minGCD ( $ arr , $ n ) ; ? >
Form lexicographically smallest string with minimum replacements having equal number of 0 s , 1 s and 2 s | Function that returns the modified lexicographically smallest string after performing minimum number of given operations ; Stores the initial frequencies of characters 0 s , 1 s and 2 s ; Stores number of processed characters upto that point of each type ; Required number of characters of each type ; If the current type has already reqd number of characters , no need to perform any operation ; Process all 3 cases ; Check for 1 first ; Else 2 ; Here we need to check processed [ 1 ] only for 2 since 0 is less than 1 and we can replace it anytime ; Here we can replace 2 with 0 and 1 anytime ; keep count of processed characters of each type ; Driver Code
< ? php function formStringMinOperations ( $ s ) { $ count = array_fill ( 0 , 3 , 0 ) ; for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) $ count [ $ s [ $ i ] - '0' ] ++ ; $ processed = array_fill ( 0 , 3 , 0 ) ; $ reqd = floor ( strlen ( $ s ) / 3 ) ; for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) { if ( $ count [ $ s [ $ i ] - '0' ] == $ reqd ) continue ; if ( $ s [ $ i ] == '0' && $ count [ 0 ] > $ reqd && $ processed [ 0 ] >= $ reqd ) { if ( $ count [ 1 ] < $ reqd ) { $ s [ $ i ] = '1' ; $ count [ 1 ] ++ ; $ count [ 0 ] -- ; } else if ( $ count [ 2 ] < $ reqd ) { $ s [ $ i ] = '2' ; $ count [ 2 ] ++ ; $ count [ 0 ] -- ; } } if ( $ s [ $ i ] == '1' && $ count [ 1 ] > $ reqd ) { if ( $ count [ 0 ] < $ reqd ) { $ s [ $ i ] = '0' ; $ count [ 0 ] ++ ; $ count [ 1 ] -- ; } else if ( count [ 2 ] < $ reqd && $ processed [ 1 ] >= $ reqd ) { $ s [ $ i ] = '2' ; $ count [ 2 ] ++ ; $ count [ 1 ] -- ; } } if ( $ s [ $ i ] == '2' && $ count [ 2 ] > $ reqd ) { if ( $ count [ 0 ] < $ reqd ) { $ s [ $ i ] = '0' ; $ count [ 0 ] ++ ; $ count [ 2 ] -- ; } else if ( $ count [ 1 ] < $ reqd ) { $ s [ $ i ] = '1' ; $ count [ 1 ] ++ ; $ count [ 2 ] -- ; } } $ processed [ $ s [ $ i ] - '0' ] ++ ; } return $ s ; } $ s = "011200" ; echo formStringMinOperations ( $ s ) ; ? >
Maximum sum from three arrays such that picking elements consecutively from same is not allowed | PHP implementation of the approach ; Function to return the maximum sum ; Base case ; Already visited ; If the element has been taken from first array in previous step ; If the element has been taken from second array in previous step ; If the element has been taken from third array in previous step ; Driver code ; Pick element from first array ; Pick element from second array ; Pick element from third array ; Print the maximum of them
< ? php $ N = 3 ; function FindMaximumSum ( $ ind , $ kon , $ a , $ b , $ c , $ n , $ dp ) { global $ N ; if ( $ ind == $ n ) return 0 ; if ( $ dp [ $ ind ] [ $ kon ] != -1 ) return $ dp [ $ ind ] [ $ kon ] ; $ ans = -1e9 + 5 ; if ( $ kon == 0 ) { $ ans = max ( $ ans , $ b [ $ ind ] + FindMaximumSum ( $ ind + 1 , 1 , $ a , $ b , $ c , $ n , $ dp ) ) ; $ ans = max ( $ ans , $ c [ $ ind ] + FindMaximumSum ( $ ind + 1 , 2 , $ a , $ b , $ c , $ n , $ dp ) ) ; } else if ( $ kon == 1 ) { $ ans = max ( $ ans , $ a [ $ ind ] + FindMaximumSum ( $ ind + 1 , 0 , $ a , $ b , $ c , $ n , $ dp ) ) ; $ ans = max ( $ ans , $ c [ $ ind ] + FindMaximumSum ( $ ind + 1 , 2 , $ a , $ b , $ c , $ n , $ dp ) ) ; } else if ( $ kon == 2 ) { $ ans = max ( $ ans , $ a [ $ ind ] + FindMaximumSum ( $ ind + 1 , 1 , $ a , $ b , $ c , $ n , $ dp ) ) ; $ ans = max ( $ ans , $ b [ $ ind ] + FindMaximumSum ( $ ind + 1 , 0 , $ a , $ b , $ c , $ n , $ dp ) ) ; } return $ dp [ $ ind ] [ $ kon ] = $ ans ; } $ a = array ( 6 , 8 , 2 , 7 , 4 , 2 , 7 ) ; $ b = array ( 7 , 8 , 5 , 8 , 6 , 3 , 5 ) ; $ c = array ( 8 , 3 , 2 , 6 , 8 , 4 , 1 ) ; $ n = count ( $ a ) ; $ dp = array_fill ( 0 , $ n , array_fill ( 0 , $ N , -1 ) ) ; $ x = FindMaximumSum ( 0 , 0 , $ a , $ b , $ c , $ n , $ dp ) ; $ y = FindMaximumSum ( 0 , 1 , $ a , $ b , $ c , $ n , $ dp ) ; $ z = FindMaximumSum ( 0 , 2 , $ a , $ b , $ c , $ n , $ dp ) ; print ( max ( $ x , max ( $ y , $ z ) ) ) ; ? >
Number of ways to make binary string of length N such that 0 s always occur together in groups of size K | PHP implementation of the above approach ; Function to return no of ways to build a binary string of length N such that 0 s always occur in groups of size K ; Driver Code
< ? php $ mod = 1000000007 ; function noOfBinaryStrings ( $ N , $ k ) { global $ mod ; $ dp = array ( 0 , 100002 , NULL ) ; for ( $ i = 1 ; $ i <= $ k - 1 ; $ i ++ ) { $ dp [ $ i ] = 1 ; } $ dp [ $ k ] = 2 ; for ( $ i = $ k + 1 ; $ i <= $ N ; $ i ++ ) { $ dp [ $ i ] = ( $ dp [ $ i - 1 ] + $ dp [ $ i - $ k ] ) % $ mod ; } return $ dp [ $ N ] ; } $ N = 4 ; $ K = 2 ; echo noOfBinaryStrings ( $ N , $ K ) ; ? >
Number of ways to pair people | Function to find number of ways to pair people in party ; To store count of number of ways . ; Using the recurrence defined find count for different values of p . ; Driver code
< ? php function findWaysToPair ( $ p ) { $ dp = array ( ) ; $ dp [ 1 ] = 1 ; $ dp [ 2 ] = 2 ; for ( $ i = 3 ; $ i <= $ p ; $ i ++ ) { $ dp [ $ i ] = $ dp [ $ i - 1 ] + ( $ i - 1 ) * $ dp [ $ i - 2 ] ; } return $ dp [ $ p ] ; } $ p = 3 ; echo findWaysToPair ( $ p ) ; ? >
Count ways to reach a score using 1 and 2 with no consecutive 2 s | A simple recursive implementation for counting ways to reach a score using 1 and 2 with consecutive 2 allowed ; base cases ; For cases n > 2 ; Driver Code
< ? php function CountWays ( $ n ) { if ( $ n == 0 ) { return 1 ; } if ( $ n == 1 ) { return 1 ; } if ( $ n == 2 ) { return 1 + 1 ; } return CountWays ( $ n - 1 ) + CountWays ( $ n - 3 ) ; } $ n = 5 ; echo CountWays ( $ n ) ; ? >
Maximum subarray sum in an array created after repeated concatenation | Returns sum of maximum sum subarray created after concatenating a [ 0. . n - 1 ] k times . ; This is where it differs from Kadane 's algorithm.  We use modular arithmetic  to find next element. ; Driver Code
< ? php function maxSubArraySumRepeated ( $ a , $ n , $ k ) { $ INT_MIN = 0 ; $ max_so_far = $ INT_MIN ; $ max_ending_here = 0 ; for ( $ i = 0 ; $ i < $ n * $ k ; $ i ++ ) { $ max_ending_here = $ max_ending_here + $ a [ $ i % $ n ] ; if ( $ max_so_far < $ max_ending_here ) $ max_so_far = $ max_ending_here ; if ( $ max_ending_here < 0 ) $ max_ending_here = 0 ; } return $ max_so_far ; } $ a = array ( 10 , 20 , -30 , -1 ) ; $ n = sizeof ( $ a ) ; $ k = 3 ; echo " Maximum ▁ contiguous ▁ sum ▁ is ▁ " , maxSubArraySumRepeated ( $ a , $ n , $ k ) ; ? >
Longest Increasing Odd Even Subsequence | function to find the longest increasing odd even subsequence ; lioes [ i ] stores longest increasing odd even subsequence ending at arr [ i ] ; to store the length of longest increasing odd even subsequence ; Initialize LIOES values for all indexes ; Compute optimized LIOES values in bottom up manner ; Pick maximum of all LIOES values ; required maximum length ; Driver Code
< ? php function longOddEvenIncSeq ( & $ arr , $ n ) { $ lioes = array_fill ( 0 , $ n , NULL ) ; $ maxLen = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ lioes [ $ i ] = 1 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ i ; $ j ++ ) if ( $ arr [ $ i ] > $ arr [ $ j ] && ( $ arr [ $ i ] + $ arr [ $ j ] ) % 2 != 0 && $ lioes [ $ i ] < $ lioes [ $ j ] + 1 ) $ lioes [ $ i ] = $ lioes [ $ j ] + 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ maxLen < $ lioes [ $ i ] ) $ maxLen = $ lioes [ $ i ] ; return $ maxLen ; } $ arr = array ( 1 , 12 , 2 , 22 , 5 , 30 , 31 , 14 , 17 , 11 ) ; $ n = sizeof ( $ arr ) ; echo " Longest ▁ Increasing ▁ Odd ▁ Even ▁ " . " Subsequence : ▁ " . longOddEvenIncSeq ( $ arr , $ n ) ; ? >
Matrix Chain Multiplication | DP | Matrix Ai has dimension p [ i - 1 ] x p [ i ] for i = 1. . n ; place parenthesis at different places between first and last matrix , recursively calculate count of multiplications for each parenthesis placement and return the minimum count ; Return minimum count ; Driver Code
< ? php function MatrixChainOrder ( & $ p , $ i , $ j ) { if ( $ i == $ j ) return 0 ; $ min = PHP_INT_MAX ; for ( $ k = $ i ; $ k < $ j ; $ k ++ ) { $ count = MatrixChainOrder ( $ p , $ i , $ k ) + MatrixChainOrder ( $ p , $ k + 1 , $ j ) + $ p [ $ i - 1 ] * $ p [ $ k ] * $ p [ $ j ] ; if ( $ count < $ min ) $ min = $ count ; } return $ min ; } $ arr = array ( 1 , 2 , 3 , 4 , 3 ) ; $ n = sizeof ( $ arr ) ; echo " Minimum ▁ number ▁ of ▁ multiplications ▁ is ▁ " . MatrixChainOrder ( $ arr , 1 , $ n - 1 ) ; ? >
Check if a string can be repeated to make another string | Function to return the count of repetitions of string a to generate string b ; If b cannot be generated by repeating a ; Repeat a count number of times Repeat a count number of times ; Driver code
< ? php function getCount ( $ a , $ b ) { if ( strlen ( $ b ) % strlen ( $ a ) != 0 ) return -1 ; $ count = floor ( strlen ( $ b ) / strlen ( $ a ) ) ; $ str = " " ; for ( $ i = 0 ; $ i < $ count ; $ i ++ ) { $ str = $ str . $ a ; } if ( strcmp ( $ a , $ b ) ) return $ count ; return -1 ; } $ a = ' eeks ' $ b = ' eeksgeeks ' echo getCount ( $ a , $ b ) ; ? >
Count of occurrences of a "1(0 + ) 1" pattern in a string | Returns count of occurrences of "1(0 + ) 1" ; $count = 0 ; Initialize result ; Check if encountered '1' forms a valid pattern as specified ; if 1 encountered for first time set oneSeen to 1 ; Check if there is any other character other than '0' or '1' . If so then set oneSeen to 0 to search again for new pattern ; Driver Code
< ? php function countPattern ( $ str ) { $ len = strlen ( $ str ) ; $ oneSeen = 0 ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { if ( $ str [ $ i ] == '1' && $ oneSeen == 1 ) if ( $ str [ $ i - 1 ] == '0' ) $ count ++ ; if ( $ str [ $ i ] == '1' && $ oneSeen == 0 ) $ oneSeen = 1 ; if ( $ str [ $ i ] != '0' && $ str [ $ i ] != '1' ) $ oneSeen = 0 ; } return $ count ; } $ str = "100001abc101" ; echo countPattern ( $ str ) ; ? >
Minimum number of given operations required to convert a string to another string | Function to return the minimum operations of the given type required to convert string s to string t ; Characters are already equal ; Increment count of 0 s ; Increment count of 1 s ; Driver code
< ? php function minOperations ( $ s , $ t , $ n ) { $ ct0 = 0 ; $ ct1 = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ s [ $ i ] == $ t [ $ i ] ) continue ; if ( $ s [ $ i ] == '0' ) $ ct0 ++ ; else $ ct1 ++ ; } return max ( $ ct0 , $ ct1 ) ; } $ s = "010" ; $ t = "101" ; $ n = strlen ( $ s ) ; echo minOperations ( $ s , $ t , $ n ) ; ? >
Decrypt a string encrypted by repeating i | Function to return the decrypted string ; Initial jump will be 1 ; Increment jump by 1 with every character ; Driver code
< ? php function decryptString ( $ str , $ n ) { $ i = 0 ; $ jump = 1 ; $ decryptedStr = " " ; while ( $ i < $ n ) { $ decryptedStr . = $ str [ $ i ] ; $ i += $ jump ; $ jump ++ ; } return $ decryptedStr ; } $ str = " geeeeekkkksssss " ; $ n = strlen ( $ str ) ; echo decryptString ( $ str , $ n ) ; ? >
Find Bit whose minimum sequence flips makes all bits same | Function to check which bit is to be flipped ; variable to store first and last character of string ; Check if first and last characters are equal , if yes , then return the character which is not at last ; else return last ; Driver Code
< ? php function bitToBeFlipped ( $ s ) { $ last = $ s [ strlen ( $ s ) - 1 ] ; $ first = $ s [ 0 ] ; if ( $ last == $ first ) { if ( $ last == '0' ) { return '1' ; } else { return '0' ; } } else if ( $ last != $ first ) { return $ last ; } } $ s = "1101011000" ; echo bitToBeFlipped ( $ s ) ; ? >
Sum of all subsequences of a number | Returns numeric value of a subsequence of s . The subsequence to be picked is decided using bit pattern of num ( We pick all thosedigits for which there is a set bit in num ) ; Initialize the result ; till n != 0 ; if i - th bit is set then add this number ; right shintift i ; function to find combined sum of all individual subsequence sum ; length of string ; stores the combined ; 2 ^ n - 1 subsequences ; loop for all subsequences ; returns the combined sum ; Driver Code
< ? php function findSubSequence ( $ s , $ num ) { $ res = 0 ; $ i = 0 ; while ( $ num ) { if ( $ num & 1 ) $ res += $ s [ $ i ] - '0' ; $ i ++ ; $ num = $ num >> 1 ; } return $ res ; } function combinedSum ( string $ s ) { $ n = strlen ( $ s ) ; $ c_sum = 0 ; $ range = ( 1 << $ n ) - 1 ; for ( $ i = 0 ; $ i <= $ range ; $ i ++ ) $ c_sum += findSubSequence ( $ s , $ i ) ; return $ c_sum ; } $ s = "123" ; echo combinedSum ( $ s ) ; ? >
Longest subsequence where each character occurs at least k times | Function to find the subsequence ; Taking an extra array to keep record for character count in s ; Counting occurrences of all characters in str [ ] ; Printing characters with count >= k in same order as they appear in str . ; Driver code
< ? php function findSubsequence ( $ str , $ k ) { $ a = array ( 1024 ) ; for ( $ i = 0 ; $ i < 26 ; $ i ++ ) $ a [ $ i ] = 0 ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) { $ temp = ord ( $ str [ $ i ] ) - ord ( ' a ' ) ; $ a [ $ temp ] += 1 ; } for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) if ( $ a [ ord ( $ str [ $ i ] ) - ord ( ' a ' ) ] >= $ k ) echo $ str [ $ i ] ; } $ k = 2 ; findSubsequence ( " geeksforgeeks " , $ k ) ; ? >
gOOGLE cASE of a given sentence | PHP program to convert a sentence to gOOGLE cASE . ; empty strings ; convert input to upper case ; checki if character is not a space and adding it to $w ; converting first character to lower case and subsequent initial letter of another word to lower case ; Driver code
< ? php function convert ( $ str ) { $ w = " " ; $ z = " " ; $ str = strtoupper ( $ str ) . " ▁ " ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) { $ ch = $ str [ $ i ] ; if ( $ ch != ' ▁ ' ) $ w = $ w . $ ch ; else { $ z = $ z . strtolower ( $ w [ 0 ] ) . substr ( $ w , 1 ) . " ▁ " ; $ w = " " ; } } return $ z ; } $ str = " I ▁ got ▁ intern ▁ at ▁ geeksforgeeks " ; echo ( convert ( $ str ) ) ; ? >
Count occurrences of a word in string | PHP program to count the number of occurrence of a word in the given string ; split the string by spaces ; search for pattern in string ; if match found increase count ; Driver code
< ? php function countOccurrences ( $ str , $ word ) { $ a = explode ( " ▁ " , $ str ) ; $ count = 0 ; for ( $ i = 0 ; $ i < sizeof ( $ a ) ; $ i ++ ) { if ( $ word == $ a [ $ i ] ) $ count ++ ; } return $ count ; } $ str = " GeeksforGeeks ▁ A ▁ computer ▁ science ▁ portal ▁ for ▁ geeks ▁ " ; $ word = " portal " ; echo ( countOccurrences ( $ str , $ word ) ) ; ? >
Permute a string by changing case | Function to generate permutations ; Number of permutations is 2 ^ n ; Converting string to lower case ; Using all subsequences and permuting them ; If j - th bit is set , we convert it to upper case ; Printing current combination ; Driver Code
< ? php function permute ( $ input ) { $ n = strlen ( $ input ) ; $ max = 1 << $ n ; $ input = strtolower ( $ input ) ; for ( $ i = 0 ; $ i < $ max ; $ i ++ ) { $ combination = $ input ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( ( ( $ i >> $ j ) & 1 ) == 1 ) $ combination [ $ j ] = chr ( ord ( $ combination [ $ j ] ) - 32 ) ; } echo $ combination . " " ; } } permute ( " ABC " ) ; ? >
Minimum number of palindromic subsequences to be removed to empty a binary string | A function to check if a string str is palindrome ; Start from leftmost and rightmost corners of str ; Keep comparing characters while they are same ; Returns count of minimum palindromic subsequences to be removed to make string empty ; If string is empty ; If string is palindrome ; If string is not palindrome ; Driver Code
< ? php function isPalindrome ( $ str ) { $ l = 0 ; $ h = strlen ( $ str ) - 1 ; while ( $ h > $ l ) if ( $ str [ $ l ++ ] != $ str [ $ h -- ] ) return false ; return true ; } function minRemovals ( $ str ) { if ( $ str [ 0 ] == ' ' ) return 0 ; if ( isPalindrome ( $ str ) ) return 1 ; return 2 ; } echo minRemovals ( " 010010 " ) , " STRNEWLINE " ; echo minRemovals ( " 0100101 " ) , " STRNEWLINE " ; ? >