codeparrot/xlcost-text-to-code huggingface dataset:
String transformation using XOR and OR | function to check if conversion is possible or not ; if lengths are different ; iterate to check if both strings have 1 ; to check if there is even one 1 in string s1 ; to check if there is even one 1 in string s2 ; if both string do not have a '1' . ; Driver code
< ? php function solve ( $ s1 , $ s2 ) { if ( strlen ( $ s1 ) != strlen ( $ s2 ) ) return false ; $ l = strlen ( $ s1 ) ; for ( $ i = 0 ; $ i < 1 ; $ i ++ ) { if ( $ s1 [ $ i ] == '1' ) $ flag1 = 1 ; if ( $ s2 [ $ i ] == '1' ) $ flag2 = 1 ; if ( ! $ flag1 && ! $ flag2 ) return true ; } return false ; } $ s1 = "100101" ; $ s2 = "100000" ; if ( solve ( $ s1 , $ s2 ) ) echo ( " Yes " ) ; else echo ( " No " ) ; ? >
Longest substring of 0 s in a string formed by k concatenations | Function to calculate maximum length of substring containing only zero ; loop to first calculate longest substring in string ; if all elements in string are '0' ; Else , find size of prefix and suffix containing only zeroes ; Calculate prefix containing only zeroes ; Calculate suffix containing only zeroes ; if k <= 1 then there is no need to take prefix + suffix into account ; Driver code
< ? php function subzero ( $ str , $ k ) { $ ans = 0 ; $ curr = 0 ; $ len = strlen ( $ str ) ; for ( $ i = 0 ; $ i < $ len ; ++ $ i ) { if ( $ str [ $ i ] == '0' ) $ curr ++ ; else $ curr = 0 ; $ ans = max ( $ ans , $ curr ) ; } if ( $ ans == $ len ) return $ len * $ k ; else { $ pre = 0 ; $ suff = 0 ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { if ( $ str [ $ i ] == '0' ) $ pre ++ ; else break ; } for ( $ i = $ len - 1 ; $ i >= 0 ; $ i -- ) { if ( $ str [ $ i ] == '0' ) $ suff ++ ; else break ; } if ( $ k > 1 ) $ ans = max ( $ ans , $ pre + $ suff ) ; return $ ans ; } } $ str = "00100110" ; $ k = 5 ; echo subzero ( $ str , $ k ) ; ? >
Find nth term of the Dragon Curve Sequence | function to generate the nth term ; first term ; generating each term of the sequence ; loop to generate the ith term ; add character from the original string ; add alternate 0 and 1 in between ; if previous added term was '0' then add '1' ; now current term becomes previous term ; if previous added term was '1' , then add '0' ; now current term becomes previous term ; s becomes the ith term of the sequence ; Taking inputs ; generate nth term of dragon curve sequence ; Printing output
< ? php function Dragon_Curve_Sequence ( $ n ) { $ s = "1" ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { $ temp = "1" ; $ prev = '1' ; $ zero = '0' ; $ one = '1' ; for ( $ j = 0 ; $ j < strlen ( $ s ) ; $ j ++ ) { $ temp . = $ s [ $ j ] ; if ( $ prev == '0' ) { $ temp . = $ one ; $ prev = $ one ; } else { $ temp . = $ zero ; $ prev = $ zero ; } } $ s = $ temp ; } return $ s ; } $ n = 4 ; $ s = Dragon_Curve_Sequence ( $ n ) ; echo $ s . " STRNEWLINE " ; ? >
Number of substrings of a string | PHP program to count number of substrings of a string ; Driver Code
< ? php function countNonEmptySubstr ( $ str ) { $ n = strlen ( $ str ) ; return $ n * ( $ n + 1 ) / 2 ; } $ s = " abcde " ; echo countNonEmptySubstr ( $ s ) ; ? >
Check if characters of one string can be swapped to form other | PHP program to check if characters of one string can be swapped to form other ; if length is not same print no ; Count frequencies of character in first string . ; iterate through the second string decrement counts of characters in second string ; Since lengths are same , some value would definitely become negative if result is false . ; Driver Code
< ? php $ MAX = 26 ; function targetstring ( $ str1 , $ str2 ) { global $ MAX ; $ l1 = strlen ( $ str1 ) ; $ l2 = strlen ( $ str2 ) ; if ( $ l1 != $ l2 ) return false ; $ map [ $ MAX ] = array ( 0 ) ; for ( $ i = 0 ; $ i < $ l1 ; $ i ++ ) $ map [ $ str1 [ $ i ] - ' a ' ] ++ ; for ( $ i = 0 ; $ i < $ l2 ; $ i ++ ) { $ map [ $ str2 [ $ i ] - ' a ' ] -- ; if ( $ map [ $ str2 [ $ i ] - ' a ' ] < 0 ) return false ; } return true ; } $ str1 = " geeksforgeeks " ; $ str2 = " geegeeksksfor " ; if ( targetstring ( $ str1 , $ str2 ) ) echo " YES " ; else echo " NO " ; ? >
Find the Number which contain the digit d | Returns true if d is present as digit in number x . ; Breal loop if d is present as digit ; If loop broke ; function to display the values ; Check all numbers one by one ; checking for digit ; Driver Code
< ? php function isDigitPresent ( $ x , $ d ) { while ( $ x > 0 ) { if ( $ x % 10 == $ d ) break ; $ x = $ x / 10 ; } return ( $ x > 0 ) ; } function printNumbers ( $ n , $ d ) { for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) if ( $ i == $ d || isDigitPresent ( $ i , $ d ) ) echo $ i , " ▁ " ; } $ n = 47 ; $ d = 7 ; printNumbers ( $ n , $ d ) ; ? >
Find one extra character in a string | PHP program to find extra character in one string ; result store the result ; traverse string A till end and xor with res ; xor with res ; traverse string B till end and xor with res ; xor with res ; print result at the end ; given string
< ? php function findExtraCharcter ( $ strA , $ strB ) { $ res = 0 ; for ( $ i = 0 ; $ i < strlen ( $ strA ) ; $ i ++ ) { $ res ^= ord ( $ strA [ $ i ] ) ; } for ( $ i = 0 ; $ i < strlen ( $ strB ) ; $ i ++ ) { $ res ^= ord ( $ strB [ $ i ] ) ; } return $ res ; } $ strA = " abcd " ; $ strB = " cbdad " ; echo chr ( findExtraCharcter ( $ strA , $ strB ) ) ; ? >
Evaluate an array expression with numbers , + and | Function to find the sum of given array ; if string is empty ; stoi function to convert string into integer ; cast to convert string into integer ; Find operator ; If operator is equal to ' + ' , add value in sum variable else subtract ; Driver code
< ? php function calculateSum ( $ arr , $ n ) { if ( $ n == 0 ) return 0 ; $ s = $ arr [ 0 ] ; $ value = ( int ) $ s ; $ sum = $ value ; for ( $ i = 2 ; $ i < $ n ; $ i = $ i + 2 ) { $ s = $ arr [ $ i ] ; $ value = ( int ) $ s ; $ operation = $ arr [ $ i - 1 ] ; if ( $ operation == ' + ' ) $ sum += $ value ; else if ( $ operation == ' - ' ) $ sum -= $ value ; } return $ sum ; } $ arr = array ( "3" , " + " , "4" , " - " , "7" , " + " , "13" ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; echo calculateSum ( $ arr , $ n ) ; ? >
String with maximum number of unique characters | Function to find string with maximum number of unique characters ; Index of string with maximum unique characters ; iterate through all strings ; array indicating any alphabet included or not included ; count number of unique alphabets in each string ; keep track of maximum number of alphabets ; print result ; Driver code
< ? php function LargestString ( $ na ) { $ N = sizeof ( $ na ) ; $ c = array_fill ( 0 , $ N , 0 ) ; $ m = 0 ; for ( $ j = 0 ; $ j < $ N ; $ j ++ ) { $ character = array_fill ( 0 , 26 , false ) ; for ( $ k = 0 ; $ k < strlen ( $ na [ $ j ] ) ; $ k ++ ) { $ x = ord ( $ na [ $ j ] [ $ k ] ) - 65 ; if ( ( $ na [ $ j ] [ $ k ] != ' ▁ ' ) && ( $ character [ $ x ] == false ) ) { $ c [ $ j ] ++ ; $ character [ $ x ] = true ; } } if ( $ c [ $ j ] > $ c [ $ m ] ) $ m = $ j ; } echo $ na [ $ m ] . " STRNEWLINE " ; } $ na = array ( " BOB " , " A ▁ AB ▁ C ▁ JOHNSON " , " ASKRIT " , " ARMAN ▁ MALLIK " , " ANJALI " ) ; LargestString ( $ na ) ; ? >
Morse Code Implementation | function to encode a alphabet as Morse code ; refer to the Morse table image attached in the article ; for space ; Character by character print Morse code ; Driver code
< ? php function morseEncode ( $ x ) { switch ( $ x ) { case ' a ' : return " . - " ; case ' b ' : return " - . . . " ; case ' c ' : return " - . - . " ; case ' d ' : return " - . . " ; case ' e ' : return " . " ; case ' f ' : return " . . - . " ; case ' g ' : return " - - . " ; case ' h ' : return " . . . . " ; case ' i ' : return " . . " ; case ' j ' : return " . - - - " ; case ' k ' : return " - . - " ; case ' l ' : return " . - . . " ; case ' m ' : return " - - " ; case ' n ' : return " - . " ; case ' o ' : return " - - - " ; case ' p ' : return " . - - . " ; case ' q ' : return " - - . - " ; case ' r ' : return " . - . " ; case ' s ' : return " . . . " ; case ' t ' : return " - " ; case ' u ' : return " . . - " ; case ' v ' : return " . . . - " ; case ' w ' : return " . - - " ; case ' x ' : return " - . . - " ; case ' y ' : return " - . - - " ; case ' z ' : return " - - . . " ; case '1' : return " . - - - - " ; case '2' : return " . . - - - " ; case '3' : return " . . . - - " ; case '4' : return " . . . . - " ; case '5' : return " . . . . . " ; case '6' : return " - . . . . " ; case '7' : return " - - . . . " ; case '8' : return " - - - . . " ; case '9' : return " - - - - . " ; case '0' : return " - - - - - " ; } } function morseCode ( $ s ) { for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) echo morseEncode ( $ s [ $ i ] ) ; echo " STRNEWLINE " ; } $ s = " geeksforgeeks " ; morseCode ( $ s ) ; ? >
Polybius Square Cipher | function to display polybius cipher text ; convert each character to its encrypted code ; finding row of the table ; finding column of the table ; if character is ' k ' ; if character is greater than ' j ' ; Driver Code ; print the cipher of "geeksforgeeks
< ? php function polybiusCipher ( $ s ) { $ row = 0 ; $ col = 0 ; for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) { $ row = floor ( ( ord ( $ s [ $ i ] ) - ord ( ' a ' ) ) / 5 ) + 1 ; $ col = ( ( ord ( $ s [ $ i ] ) - ord ( ' a ' ) ) % 5 ) + 1 ; if ( $ s [ $ i ] == ' k ' ) { $ row = $ row - 1 ; $ col = 5 - $ col + 1 ; } else if ( $ s [ $ i ] >= ' j ' ) { if ( $ col == 1 ) { $ col = 6 ; $ row = $ row - 1 ; } $ col = $ col - 1 ; } echo ( $ row . $ col ) ; } echo ( " STRNEWLINE " ) ; } $ s = " geeksforgeeks " ; polybiusCipher ( $ s ) ; ? >
Minimum removal to make palindrome permutation | function to find minimum removal of characters ; hash to store frequency of each character and to set hash array to zeros ; count frequency of each character ; count the odd frequency characters ; if count is - 1 return 0 otherwise return count ; Driver Code
< ? php function minRemoval ( $ str ) { $ hash = array_fill ( 0 , 26 , 0 ) ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) $ hash [ ord ( $ str [ $ i ] ) - 97 ] ++ ; $ count = 0 ; for ( $ i = 0 ; $ i < 26 ; $ i ++ ) if ( $ hash [ $ i ] % 2 ) $ count ++ ; return ( $ count == 0 ) ? 0 : $ count - 1 ; } $ str = " geeksforgeeks " ; echo minRemoval ( $ str ) . " STRNEWLINE " ; ? >
Longest Uncommon Subsequence | function to calculate length of longest uncommon subsequence ; Case 1 : If strings are equal ; for case 2 and case 3 ; input strings
< ? php function findLUSlength ( $ a , $ b ) { if ( ! strcmp ( $ a , $ b ) ) return 0 ; return max ( strlen ( $ a ) , strlen ( $ b ) ) ; } $ a = " abcdabcd " ; $ b = " abcabc " ; echo ( findLUSlength ( $ a , $ b ) ) ; ? >
Interchanging first and second halves of strings | Function to concatenate two different halves of given strings ; Creating new strings by exchanging the first half of a and b . ; Driver Code ; Calling function
< ? php function swapTwoHalves ( $ a , $ b ) { $ la = strlen ( $ a ) ; $ lb = strlen ( $ b ) ; $ c = substr ( $ a , 0 , intval ( $ la / 2 ) ) . substr ( $ b , intval ( $ lb / 2 ) , $ lb ) ; $ d = substr ( $ b , 0 , intval ( $ lb / 2 ) ) . substr ( $ a , intval ( $ la / 2 ) , $ la ) ; echo ( $ c . " " ▁ . ▁ $ d ▁ . ▁ " " } $ a = " remuneration " ; $ b = " day " ; swapTwoHalves ( $ a , $ b ) ; ? >
Longest sub | PHP implementation to find the length of the longest sub$having frequency of each character less than equal to k ; function to find the length of the longest sub$ having frequency of each character less than equal to k ; hash table to store frequency of each table ; ' start ' index of the current substring ; to store the maximum length ; traverse the $ ' str ' ; get the current character as ' ch ' ; increase frequency of ' ch ' in ' freq [ ] ' ; if frequency of ' ch ' becomes more than ' k ' ; update ' maxLen ' ; decrease frequency of each character as they are encountered from the ' start ' index until frequency of ' ch ' is greater than ' k ' ; decrement frequency by '1' ; increment ' start ' ; update maxLen ; required length ; Driver Code
< ? php $ SIZE = 26 ; function longSubstring ( $ str , $ k ) { global $ SIZE ; $ freq = array ( ) ; for ( $ i = 0 ; $ i < $ SIZE ; $ i ++ ) $ freq [ $ i ] = 0 ; $ start = 0 ; $ maxLen = 0 ; $ ch = ' ' $ n = strlen ( $ str ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ ch = $ str [ $ i ] ; $ freq [ ord ( $ ch ) - ord ( ' a ' ) ] ++ ; if ( $ freq [ ord ( $ ch ) - ord ( ' a ' ) ] > $ k ) { if ( $ maxLen < ( $ i - $ start ) ) $ maxLen = $ i - $ start ; while ( $ freq [ ord ( $ ch ) - ord ( ' a ' ) ] > $ k ) { $ freq [ ord ( $ str [ $ start ] ) - ord ( ' a ' ) ] -- ; $ start ++ ; } } } if ( $ maxLen < ( $ n - $ start ) ) $ maxLen = $ n - $ start ; return $ maxLen ; } $ str = " babcaag " ; $ k = 1 ; echo ( " Length ▁ = ▁ " . longSubstring ( $ str , $ k ) ) ; ? >
Magical Indices in an array | Function to count number of magical indices . ; Array to store parent node of traversal . ; Array to determine whether current node is already counted in the cycle . ; Initialize the arrays . ; Check if current node is already traversed or not . If node is not traversed yet then parent value will be - 1. ; Traverse the graph until an already visited node is not found . ; Check parent value to ensure a cycle is present . ; Count number of nodes in the cycle . ; Driver code
< ? php function solve ( $ A , $ n ) { $ i = 0 ; $ cnt = 0 ; $ j = 0 ; $ parent = array ( ) ; $ vis = array ( ) ; for ( $ i = 0 ; $ i < $ n + 1 ; $ i ++ ) { $ parent [ $ i ] = -1 ; $ vis [ $ i ] = 0 ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ j = $ i ; if ( $ parent [ $ j ] == -1 ) { while ( $ parent [ $ j ] == -1 ) { $ parent [ $ j ] = $ i ; $ j = ( $ j + $ A [ $ j ] + 1 ) % $ n ; } if ( $ parent [ $ j ] == $ i ) { while ( $ vis [ $ j ] == 0 ) { $ vis [ $ j ] = 1 ; $ cnt ++ ; $ j = ( $ j + $ A [ $ j ] + 1 ) % $ n ; } } } } return $ cnt ; } $ A = array ( 0 , 0 , 0 , 2 ) ; $ n = count ( $ A ) ; echo ( solve ( $ A , $ n ) ) ; ? >
Tug of War | solution by calling itself recursively ; checks whether the it is going out of bound ; checks that the numbers of elements left are not less than the number of elements required to form the solution ; consider the cases when current element is not included in the solution ; add the current element to the solution ; checks if a solution is formed ; checks if the solution formed is better than the best solution so far ; consider the cases where current element is included in the solution ; removes current element before returning to the caller of this function ; main function that generate an arr ; the boolean array that contains the inclusion and exclusion of an element in current set . The number excluded automatically form the other set ; The inclusion / exclusion array for final solution ; Find the solution using recursive function TOWUtil ( ) ; Print the solution ; Driver Code
< ? php function TOWUtil ( & $ arr , $ n , & $ curr_elements , $ no_of_selected_elements , & $ soln , & $ min_diff , $ sum , $ curr_sum , $ curr_position ) { if ( $ curr_position == $ n ) return ; if ( ( intval ( $ n / 2 ) - $ no_of_selected_elements ) > ( $ n - $ curr_position ) ) return ; TOWUtil ( $ arr , $ n , $ curr_elements , $ no_of_selected_elements , $ soln , $ min_diff , $ sum , $ curr_sum , $ curr_position + 1 ) ; $ no_of_selected_elements ++ ; $ curr_sum = ( $ curr_sum + $ arr [ $ curr_position ] ) ; $ curr_elements [ $ curr_position ] = true ; if ( $ no_of_selected_elements == intval ( $ n / 2 ) ) { if ( abs ( intval ( $ sum / 2 ) - $ curr_sum ) < $ min_diff ) { $ min_diff = abs ( intval ( $ sum / 2 ) - $ curr_sum ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ soln [ $ i ] = $ curr_elements [ $ i ] ; } } else { TOWUtil ( $ arr , $ n , $ curr_elements , $ no_of_selected_elements , $ soln , $ min_diff , $ sum , $ curr_sum , $ curr_position + 1 ) ; } $ curr_elements [ $ curr_position ] = false ; } function tugOfWar ( & $ arr , $ n ) { $ curr_elements = array_fill ( 0 , $ n , 0 ) ; $ soln = array_fill ( 0 , $ n , 0 ) ; $ min_diff = PHP_INT_MAX ; $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ sum += $ arr [ $ i ] ; $ curr_elements [ $ i ] = $ soln [ $ i ] = false ; } TOWUtil ( $ arr , $ n , $ curr_elements , 0 , $ soln , $ min_diff , $ sum , 0 , 0 ) ; echo " The ▁ first ▁ subset ▁ is : ▁ " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ soln [ $ i ] == true ) echo $ arr [ $ i ] . " ▁ " ; } echo " The second subset is : " for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ soln [ $ i ] == false ) echo $ arr [ $ i ] . " ▁ " ; } } $ arr = array ( 23 , 45 , -34 , 12 , 0 , 98 , -99 , 4 , 189 , -1 , 4 ) ; $ n = count ( $ arr ) ; tugOfWar ( $ arr , $ n ) ; ? >
Find an index such that difference between product of elements before and after it is minimum | Function to return the index i such that the absolute difference between product of elements up to that index and the product of rest of the elements of the array is minimum ; To store the required index ; Prefix product array ; Compute the product array ; Iterate the product array to find the index ; Driver code
< ? php function findIndex ( $ a , $ n ) { $ min_diff = PHP_INT_MAX ; $ prod = array ( ) ; $ prod [ 0 ] = $ a [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ prod [ $ i ] = $ prod [ $ i - 1 ] * $ a [ $ i ] ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { $ curr_diff = abs ( ( $ prod [ $ n - 1 ] / $ prod [ $ i ] ) - $ prod [ $ i ] ) ; if ( $ curr_diff < $ min_diff ) { $ min_diff = $ curr_diff ; $ res = $ i ; } } return $ res ; } $ arr = array ( 3 , 2 , 5 , 7 , 2 , 9 ) ; $ N = count ( $ arr ) ; echo findIndex ( $ arr , $ N ) ; ? >
Print all numbers whose set of prime factors is a subset of the set of the prime factors of X | Function to print all the numbers ; Iterate for every element in the array ; Find the gcd ; Iterate till gcd is 1 of number and x ; Divide the number by gcd ; Find the new gcdg ; If the number is 1 at the end then print the number ; If no numbers have been there ; Driver code
< ? php function printNumbers ( $ a , $ n , $ x ) { $ flag = false ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ num = $ a [ $ i ] ; $ g = __gcd ( $ num , $ x ) ; while ( $ g != 1 ) { $ num /= $ g ; $ g = __gcd ( $ num , $ x ) ; } if ( $ num == 1 ) { $ flag = true ; echo $ a [ $ i ] , " " ; } } if ( ! $ flag ) echo ( " There ▁ are ▁ no ▁ such ▁ numbers " ) ; } function __gcd ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; return __gcd ( $ b , $ a % $ b ) ; } $ x = 60 ; $ a = array ( 2 , 5 , 10 , 7 , 17 ) ; $ n = count ( $ a ) ; printNumbers ( $ a , $ n , $ x ) ; ? >
Find the final radiations of each Radiated Stations | Function to print the final radiations ; Function to create the array of the resultant radiations ; Resultant radiations ; Declaring index counter for left and right radiation ; Effective radiation for left and right case ; Radiation for i - th station ; Radiation increment for left stations ; Radiation increment for right stations ; Print the resultant radiation for each of the stations ; 1 - based indexing
< ? php function print_radiation ( $ rStation , $ n ) { for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { echo $ rStation [ $ i ] . " " ; } echo " STRNEWLINE " ; } function radiated_Station ( $ station , $ n ) { $ rStation = array ( ) ; $ rStation = array_fill ( 0 , $ n + 1 , 0 ) ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ li = $ i - 1 ; $ ri = $ i + 1 ; $ lRad = $ station [ $ i ] - 1 ; $ rRad = $ station [ $ i ] - 1 ; $ rStation [ $ i ] += $ station [ $ i ] ; while ( $ li >= 1 && $ lRad >= 1 ) { $ rStation [ $ li -- ] += $ lRad -- ; } while ( $ ri <= $ n && $ rRad >= 1 ) { $ rStation [ $ ri ++ ] += $ rRad -- ; } } print_radiation ( $ rStation , $ n ) ; } $ station = array ( 0 , 7 , 9 , 12 , 2 , 5 ) ; $ n = ( sizeof ( $ station ) / sizeof ( $ station [ 0 ] ) ) - 1 ; radiated_Station ( $ station , $ n ) ; ? >
Program to find the maximum difference between the index of any two different numbers | Function to return the maximum difference ; Iteratively check from back ; Different numbers ; Iteratively check from the beginning ; Different numbers ; Driver code
< ? php function findMaximumDiff ( $ a , $ n ) { $ ind1 = 0 ; for ( $ i = $ n - 1 ; $ i > 0 ; $ i -- ) { if ( $ a [ 0 ] != $ a [ $ i ] ) { $ ind1 = $ i ; break ; } } $ ind2 = 0 ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { if ( $ a [ $ n - 1 ] != $ a [ $ i ] ) { $ ind2 = ( $ n - 1 - $ i ) ; break ; } } return max ( $ ind1 , $ ind2 ) ; } $ a = array ( 1 , 2 , 3 , 2 , 3 ) ; $ n = count ( $ a ) ; echo findMaximumDiff ( $ a , $ n ) ; ? >
Sum of the series Kn + ( K ( n | Function to return sum ; Driver code
< ? php function sum ( $ k , $ n ) { $ sum = pow ( $ k , $ n + 1 ) - pow ( $ k - 1 , $ n + 1 ) ; return $ sum ; } $ n = 3 ; $ K = 3 ; echo sum ( $ K , $ n ) ;
Check whether factorial of N is divisible by sum of first N natural numbers | Function to check whether a number is prime or not . ; Count variable to store the number of factors of ' num ' ; Counting the number of factors ; If number is prime return true ; Function to check for divisibility ; if ' n ' equals 1 then divisibility is possible ; Else check whether ' n + 1' is prime or not ; If ' n + 1' is prime then ' n ! ' is not divisible by ' n * ( n + 1 ) / 2' ; else divisibility occurs ; Test for n = 3 ; Test for n = 4
< ? php function is_prime ( $ num ) { $ count1 = 0 ; for ( $ i = 1 ; $ i * $ i <= ( $ num ) ; $ i ++ ) { if ( ( $ num ) % $ i == 0 ) { if ( $ i * $ i != ( $ num ) ) $ count1 += 2 ; else $ count1 ++ ; } } if ( $ count1 == 2 ) return true ; else return false ; } function is_divisible ( $ n ) { if ( $ n == 1 ) { return " YES " ; } else { if ( is_prime ( $ n + 1 ) ) return " NO " ; else return " YES " ; } } $ n = 3 ; echo is_divisible ( $ n ) . " STRNEWLINE " ; $ n = 4 ; echo is_divisible ( $ n ) . " STRNEWLINE " ; ? >
Given two binary strings perform operation until B > 0 and print the result | PHP implementation of the approach ; Function to return the required result ; Reverse the strings ; Count the number of set bits in b ; To store the powers of 2 ; power [ i ] = pow ( 2 , i ) % mod ; To store the final answer ; Add power [ i ] to the ans after multiplying it with the number of set bits in b ; Divide by 2 means right shift b >> 1 if b has 1 at right most side than number of set bits will get decreased ; If no more set bits in b i . e . b = 0 ; Return the required answer ; Driver code
< ? php $ GLOBALS [ ' mod ' ] = ( 1e9 + 7 ) ; function BitOperations ( $ a , $ n , $ b , $ m ) { $ a = strrev ( $ a ) ; $ b = strrev ( $ b ) ; $ c = 0 ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) if ( $ b [ $ i ] == '1' ) $ c ++ ; $ power = array ( ) ; $ power [ 0 ] = 1 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ power [ $ i ] = ( $ power [ $ i - 1 ] * 2 ) % $ GLOBALS [ ' mod ' ] ; $ ans = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] == '1' ) { $ ans += $ c * $ power [ $ i ] ; if ( $ ans >= $ GLOBALS [ ' mod ' ] ) $ ans %= $ GLOBALS [ ' mod ' ] ; } if ( $ b [ $ i ] == '1' ) $ c -- ; if ( $ c == 0 ) break ; } return $ ans ; } $ a = "1001" ; $ b = "10101" ; $ n = strlen ( $ a ) ; $ m = strlen ( $ b ) ; echo BitOperations ( $ a , $ n , $ b , $ m ) ; ? >
Print prime numbers with prime sum of digits in an array | Function to store the primes ; Function to return the sum of digits ; Function to print additive primes ; If the number is prime ; Check if it 's digit sum is prime ; Driver code
< ? php function sieve ( $ maxEle , & $ prime ) { $ prime [ 0 ] = $ prime [ 1 ] = 1 ; for ( $ i = 2 ; $ i * $ i <= $ maxEle ; $ i ++ ) { if ( ! $ prime [ $ i ] ) { for ( $ j = 2 * $ i ; $ j <= $ maxEle ; $ j += $ i ) $ prime [ $ j ] = 1 ; } } } function digitSum ( $ n ) { $ sum = 0 ; while ( $ n ) { $ sum += $ n % 10 ; $ n = $ n / 10 ; } return $ sum ; } function printAdditivePrime ( $ arr , $ n ) { $ maxEle = max ( $ arr ) ; $ prime = array_fill ( 0 , $ maxEle + 1 , 0 ) ; sieve ( $ maxEle , $ prime ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ prime [ $ arr [ $ i ] ] == 0 ) { $ sum = digitSum ( $ arr [ $ i ] ) ; if ( $ prime [ $ sum ] == 0 ) print ( $ arr [ $ i ] . " ▁ " ) ; } } } $ a = array ( 2 , 4 , 6 , 11 , 12 , 18 , 7 ) ; $ n = count ( $ a ) ; printAdditivePrime ( $ a , $ n ) ; ? >