codeparrot/xlcost-text-to-code huggingface dataset:
Median of two sorted arrays of different sizes | Set 1 ( Linear ) | This function returns median of a [ ] and b [ ] . Assumptions in this function : Both a [ ] and b [ ] are sorted arrays ; Current index of i / p array a [ ] ; Current index of i / p array b [ ] ; Below is to handle the case where all elements of a [ ] are smaller than smallest ( or first ) element of b [ ] or a [ ] is empty ; Below is to handle case where all elements of b [ ] are smaller than smallest ( or first ) element of a [ ] or b [ ] is empty ; Below is to handle the case where sum of number of elements of the arrays is even ; Below is to handle the case where sum of number of elements of the arrays is odd ; Driver Code
< ? php function findmedian ( $ a , $ n1 , $ b , $ n2 ) { $ i = 0 ; $ j = 0 ; $ k ; $ m1 = -1 ; $ m2 = -1 ; for ( $ k = 0 ; $ k <= ( $ n1 + $ n2 ) / 2 ; $ k ++ ) { if ( $ i < $ n1 and $ j < $ n2 ) { if ( $ a [ $ i ] < $ b [ $ j ] ) { $ m2 = $ m1 ; $ m1 = $ a [ $ i ] ; $ i ++ ; } else { $ m2 = $ m1 ; $ m1 = $ b [ $ j ] ; $ j ++ ; } } else if ( i == n1 ) { $ m2 = $ m1 ; $ m1 = $ b [ j ] ; $ j ++ ; } else if ( $ j == $ n2 ) { $ m2 = $ m1 ; $ m1 = $ a [ $ i ] ; $ i ++ ; } } if ( ( $ n1 + $ n2 ) % 2 == 0 ) return ( $ m1 + $ m2 ) * 1.0 / 2 ; return m1 ; } $ a = array ( 1 , 12 , 15 , 26 , 38 ) ; $ b = array ( 2 , 13 , 24 ) ; $ n1 = count ( $ a ) ; $ n2 = count ( $ b ) ; echo ( findmedian ( $ a , $ n1 , $ b , $ n2 ) ) ; ? >
Next Smaller Element | prints element and NSE pair for all elements of arr [ ] of size n ; Driver Code
< ? php function printNSE ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ next = -1 ; for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) { if ( $ arr [ $ i ] > $ arr [ $ j ] ) { $ next = $ arr [ $ j ] ; break ; } } echo $ arr [ $ i ] . " -- " . ▁ $ next . " " } } $ arr = array ( 11 , 13 , 21 , 3 ) ; $ n = count ( $ arr ) ; printNSE ( $ arr , $ n ) ; ? >
Longest subarray such that the difference of max and min is at | PHP code to find longest subarray with difference between max and min as at - most 1. ; longest constant range length ; first number ; If we see same number ; If we see different number , but same as previous . ; If number is neither same as previous nor as current . ; Driver Code
< ? php function longestSubarray ( $ input , $ length ) { $ prev = -1 ; $ prevCount = 0 ; $ currentCount = 1 ; $ longest = 1 ; $ current = $ input [ 0 ] ; for ( $ i = 1 ; $ i < $ length ; $ i ++ ) { $ next = $ input [ $ i ] ; if ( $ next == $ current ) { $ currentCount ++ ; } else if ( $ next == $ prev ) { $ prevCount += $ currentCount ; $ prev = $ current ; $ current = $ next ; $ currentCount = 1 ; } else { $ longest = max ( $ longest , $ currentCount + $ prevCount ) ; $ prev = $ current ; $ prevCount = $ currentCount ; $ current = $ next ; $ currentCount = 1 ; } } return max ( $ longest , $ currentCount + $ prevCount ) ; } $ input = array ( 5 , 5 , 6 , 7 , 6 ) ; echo ( longestSubarray ( $ input , count ( $ input ) ) ) ; ? >
Longest Subarray with first element greater than or equal to Last element | Search function for searching the first element of the subarray which is greater or equal to the last element ( num ) ; Returns length of the longest array with first element smaller than the last element . ; Initially the search space is empty . ; We will add an ith element in the search space if the search space is empty or if the ith element is greater than the last element of the search space . ; we will search for the index first element in the search space and we will use it find the index of it in the original array . ; Update the answer if the length of the subarray is greater than the previously calculated lengths . ; Driver code
< ? php function binarySearch ( $ searchSpace , $ s , $ e , $ num ) { $ ans = 0 ; while ( $ s <= $ e ) { $ mid = ( $ s + $ e ) / 2 ; if ( $ searchSpace [ $ mid ] >= $ num ) { $ ans = $ mid ; $ e = $ mid - 1 ; } else { $ s = $ mid + 1 ; } } return $ ans ; } function longestSubArr ( & $ arr , $ n ) { $ j = 0 ; $ ans = 0 ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) { if ( $ j == 0 or $ searchSpace [ $ j - 1 ] < $ arr [ $ i ] ) { $ searchSpace [ $ j ] = $ arr [ $ i ] ; $ index [ $ j ] = $ i ; $ j ++ ; } $ idx = binarySearch ( $ searchSpace , 0 , $ j - 1 , $ arr [ $ i ] ) ; $ ans = max ( $ ans , $ i - $ index [ $ idx ] + 1 ) ; } return $ ans ; } $ arr = array ( -5 , -1 , 7 , 5 , 1 , -2 ) ; $ n = sizeof ( $ arr ) ; echo ( longestSubArr ( $ arr , $ n ) ) ; ? >
Check if a string is suffix of another | PHP program to find if a string is suffix of another ; Driver Code ; Test case - sensitive implementation of endsWith function
< ? php function isSuffix ( $ s1 , $ s2 ) { $ n1 = ( $ s1 ) ; $ n2 = strlen ( $ s2 ) ; if ( $ n1 > $ n2 ) return false ; for ( $ i = 0 ; $ i < $ n1 ; $ i ++ ) if ( $ s1 [ $ n1 - $ i - 1 ] != $ s2 [ $ n2 - $ i - 1 ] ) return false ; return true ; } $ s1 = " geeks " ; $ s2 = " geeksforgeeks " ; $ result = isSuffix ( $ s1 , $ s2 ) ; if ( $ result ) echo " Yes " ; else echo " No " ; ? >
Check if all occurrences of a character appear together | To indicate if one or more occurrences of ' c ' are seen or not . ; Traverse given string ; If current character is same as c , we first check if c is already seen . ; If this is very first appearance of c , we traverse all consecutive occurrences . ; To indicate that character is seen once . ; Driver Code
< ? php $ oneSeen = false ; $ i = 0 ; $ n = strlen ( $ s ) ; while ( $ i < $ n ) { if ( $ s [ $ i ] == $ c ) { if ( $ oneSeen == true ) return false ; while ( $ i < $ n && $ s [ $ i ] == $ c ) $ i ++ ; $ oneSeen = true ; } else $ i ++ ; } return true ; } $ s = "110029" ; if ( checkIfAllTogether ( $ s , '1' ) ) echo ( " Yes STRNEWLINE " ) ; else echo ( " No STRNEWLINE " ) ; ? >
Front and Back Search in unsorted array | PHP program to implement front and back search ; Start searching from both ends ; Keep searching while two indexes do not cross . ; Driver Code
< ? php function search ( $ arr , $ n , $ x ) { $ front = 0 ; $ back = $ n - 1 ; while ( $ front <= $ back ) { if ( $ arr [ $ front ] == $ x $ arr [ $ back ] == $ x ) return true ; $ front ++ ; $ back -- ; } return false ; } $ arr = array ( 10 , 20 , 80 , 30 , 60 , 50 , 110 , 100 , 130 , 170 ) ; $ x = 130 ; $ n = sizeof ( $ arr ) ; if ( search ( $ arr , $ n , $ x ) ) echo " Yes " ; else echo " No " ; ? >
Even | PHP program to find max ( X , Y ) / min ( X , Y ) after P turns ; 1 st test case ; 2 nd test case
< ? php function findValue ( $ X , $ Y , $ P ) { if ( $ P % 2 == 0 ) return ( int ) ( max ( $ X , $ Y ) / min ( $ X , $ Y ) ) ; else return ( int ) ( max ( 2 * $ X , $ Y ) / min ( 2 * $ X , $ Y ) ) ; } $ X = 1 ; $ Y = 2 ; $ P = 1 ; echo findValue ( $ X , $ Y , $ P ) , " STRNEWLINE " ; $ X = 3 ; $ Y = 7 ; $ P = 2 ; echo findValue ( $ X , $ Y , $ P ) , " STRNEWLINE " ; ? >
The painter 's partition problem | function to calculate sum between two indices in array ; bottom up tabular dp ; initialize table ; base cases k = 1 ; n = 1 ; 2 to k partitions ; track minimum ; i - 1 th separator before position arr [ p = 1. . j ] ; required ; Driver Code ; Calculate size of array .
< ? php function sum ( $ arr , $ from , $ to ) { $ total = 0 ; for ( $ i = $ from ; $ i <= $ to ; $ i ++ ) $ total += $ arr [ $ i ] ; return $ total ; } function findMax ( $ arr , $ n , $ k ) { $ dp [ $ k + 1 ] [ $ n + 1 ] = array ( 0 ) ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ dp [ 1 ] [ $ i ] = sum ( $ arr , 0 , $ i - 1 ) ; for ( $ i = 1 ; $ i <= $ k ; $ i ++ ) $ dp [ $ i ] [ 1 ] = $ arr [ 0 ] ; for ( $ i = 2 ; $ i <= $ k ; $ i ++ ) { $ best = PHP_INT_MAX ; for ( $ p = 1 ; $ p <= $ j ; $ p ++ ) $ best = min ( $ best , max ( $ dp [ $ i - 1 ] [ $ p ] , sum ( $ arr , $ p , $ j - 1 ) ) ) ; $ dp [ $ i ] [ $ j ] = $ best ; } } return $ dp [ $ k ] [ $ n ] ; } $ arr = array ( 10 , 20 , 60 , 50 , 30 , 40 ) ; $ n = sizeof ( $ arr ) ; $ k = 3 ; echo findMax ( $ arr , $ n , $ k ) , " STRNEWLINE " ; ? >
Counting cross lines in an array | Function return count of cross line in an array ; Move elements of arr [ 0. . i - 1 ] , that are greater than key , to one position ahead of their current position ; increment cross line by one ; Driver Code
< ? php function countCrossLine ( $ arr , $ n ) { $ count_crossline = 0 ; $ i ; $ key ; $ j ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ key = $ arr [ $ i ] ; $ j = $ i - 1 ; while ( $ j >= 0 and $ arr [ $ j ] > $ key ) { $ arr [ $ j + 1 ] = $ arr [ $ j ] ; $ j = $ j - 1 ; $ count_crossline ++ ; } $ arr [ $ j + 1 ] = $ key ; } return $ count_crossline ; } $ arr = array ( 4 , 3 , 1 , 2 ) ; $ n = count ( $ arr ) ; echo countCrossLine ( $ arr , $ n ) ; ? >
Recursive Programs to find Minimum and Maximum elements of array | function to return maximum element using recursion ; if n = 0 means whole array has been traversed ; Driver Code ; Function calling
< ? php function findMaxRec ( $ A , $ n ) { if ( $ n == 1 ) return $ A [ 0 ] ; return max ( $ A [ $ n - 1 ] , findMaxRec ( $ A , $ n - 1 ) ) ; } $ A = array ( 1 , 4 , 45 , 6 , -50 , 10 , 2 ) ; $ n = sizeof ( $ A ) ; echo findMaxRec ( $ A , $ n ) ; ? >
Minimum length of square to contain at least half of the given Coordinates | Function to Calculate Absolute Value ; Function to Calculate the Minimum value of M ; To store the minimum M for each point in array ; Sort the array ; Index at which atleast required point are inside square of length 2 * M ; Driver Code
< ? php function mod ( $ x ) { if ( $ x >= 0 ) return $ x ; return - $ x ; } function findSquare ( $ n ) { $ points = array ( array ( 1 , 2 ) , array ( -3 , 4 ) , array ( 1 , 78 ) , array ( -3 , -7 ) ) ; $ a [ $ n ] = array ( ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ x ; $ y ; $ x = $ points [ $ i ] [ 0 ] ; $ y = $ points [ $ i ] [ 1 ] ; $ a [ $ i ] = max ( mod ( $ x ) , mod ( $ y ) ) ; } sort ( $ a ) ; $ index = floor ( $ n / 2 ) - 1 ; echo " Minimum ▁ M ▁ required ▁ is : ▁ " , $ a [ $ index ] , " STRNEWLINE " ; } $ N = 4 ; findSquare ( $ N ) ; ? >
Sort even and odd placed elements in increasing order | function to prin the odd and even indexed digits ; lists to store the odd and even positioned digits ; traverse through all the indexes in the integer ; if the digit is in odd_index position append it to odd_position list ; else append it to the even_position list ; print the elements in the list in sorted order ; Driver Code
< ? php function odd_even ( $ n ) { $ odd_indexes = array ( ) ; $ even_indexes = array ( ) ; for ( $ i = 0 ; $ i < sizeof ( $ n ) ; $ i ++ ) { if ( $ i % 2 == 0 ) array_push ( $ odd_indexes , $ n [ $ i ] ) ; else array_push ( $ even_indexes , $ n [ $ i ] ) ; } sort ( $ odd_indexes ) ; for ( $ i = 0 ; $ i < sizeof ( $ odd_indexes ) ; $ i ++ ) echo $ odd_indexes [ $ i ] , " ▁ " ; sort ( $ even_indexes ) ; for ( $ i = 0 ; $ i < sizeof ( $ even_indexes ) ; $ i ++ ) echo $ even_indexes [ $ i ] , " ▁ " ; } $ n = array ( 3 , 2 , 7 , 6 , 8 ) ; odd_even ( $ n ) ; ? >
Minimum increment in the sides required to get non | Function to return the minimum increase in side lengths of the triangle ; push the three sides to a array ; sort the array ; check if sum is greater than third side ; Driver Code
< ? php function minimumIncrease ( $ a , $ b , $ c ) { $ arr = array ( $ a , $ b , $ c ) ; sort ( $ arr ) ; if ( $ arr [ 0 ] + $ arr [ 1 ] >= $ arr [ 2 ] ) return 0 ; else return $ arr [ 2 ] - ( $ arr [ 0 ] + $ arr [ 1 ] ) ; } $ a = 3 ; $ b = 5 ; $ c = 10 ; echo minimumIncrease ( $ a , $ b , $ c ) ; ? >
Minimum sum of differences with an element in an array | function to find min sum after operation ; Sort the array ; Pick middle value ; Sum of absolute differences . ; Driver Code
< ? php function absSumDidd ( $ a , $ n ) { sort ( $ a ) ; $ midValue = $ a [ ( $ n / 2 ) ] ; $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ sum = $ sum + abs ( $ a [ $ i ] - $ midValue ) ; } return $ sum ; } $ arr = array ( 5 , 11 , 14 , 10 , 17 , 15 ) ; $ n = count ( $ arr ) ; echo absSumDidd ( $ arr , $ n ) ; ? >
Count subsequences in first string which are anagrams of the second string | PHP implementation to count subsequences in first $which are anagrams of the second string ; Returns value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- -- * 1 ] ; function to count subsequences in first string which are anagrams of the second string ; hash tables to store frequencies of each character ; store frequency of each character of ' str1' ; store frequency of each character of ' str2' ; to store the total count of subsequences ; if character ( i + ' a ' ) exists in ' str2' ; if this character ' s ▁ frequency ▁ ▁ in ▁ ' str2 ' ▁ in ▁ less ▁ than ▁ or ▁ ▁ equal ▁ to ▁ its ▁ frequency ▁ in ▁ ▁ ' str1 ' ▁ then ▁ accumulate ▁ its ▁ ▁ contribution ▁ to ▁ the ▁ count ▁ ▁ of ▁ subsequences . ▁ If ▁ its ▁ ▁ frequency ▁ in ▁ ' str1 ' ▁ is ▁ ' n ' ▁ ▁ and ▁ in ▁ ' str2 ' ▁ is ▁ ' r ', then  its contribution will be nCr,  where C is the binomial  coefficient. ; else return 0 as there could be no subsequence which is an anagram of ' str2' ; required count of subsequences ; Driver Code
< ? php $ SIZE = 26 ; function binomialCoeff ( $ n , $ k ) { $ res = 1 ; if ( $ k > $ n - $ k ) $ k = $ n - $ k ; for ( $ i = 0 ; $ i < $ k ; ++ $ i ) { $ res *= ( $ n - $ i ) ; $ res /= ( $ i + 1 ) ; } return $ res ; } function countSubsequences ( $ str1 , $ str2 ) { global $ SIZE ; $ freq1 = array ( ) ; $ freq2 = array ( ) ; for ( $ i = 0 ; $ i < $ SIZE ; $ i ++ ) { $ freq1 [ $ i ] = 0 ; $ freq2 [ $ i ] = 0 ; } $ n1 = strlen ( $ str1 ) ; $ n2 = strlen ( $ str2 ) ; for ( $ i = 0 ; $ i < $ n1 ; $ i ++ ) $ freq1 [ ord ( $ str1 [ $ i ] ) - ord ( ' a ' ) ] ++ ; for ( $ i = 0 ; $ i < $ n2 ; $ i ++ ) $ freq2 [ ord ( $ str2 [ $ i ] ) - ord ( ' a ' ) ] ++ ; $ count = 1 ; for ( $ i = 0 ; $ i < $ SIZE ; $ i ++ ) if ( $ freq2 [ $ i ] != 0 ) { if ( $ freq2 [ $ i ] <= $ freq1 [ $ i ] ) $ count = $ count * binomialCoeff ( $ freq1 [ $ i ] , $ freq2 [ $ i ] ) ; else return 0 ; } return $ count ; } $ str1 = " abacd " ; $ str2 = " abc " ; echo ( " Count ▁ = ▁ " . countSubsequences ( $ str1 , $ str2 ) ) ; ? >
Count subsequence of length three in a given string | Function to find number of occurrences of a subsequence of length three in a string ; variable to store no of occurrences ; loop to find first character ; loop to find 2 nd character ; loop to find 3 rd character ; increment count if subsequence is found ; Driver Code
< ? php function findOccurrences ( $ str , $ substr ) { $ counter = 0 ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) { if ( $ str [ $ i ] == $ substr [ 0 ] ) { for ( $ j = $ i + 1 ; $ j < strlen ( $ str ) ; $ j ++ ) { if ( $ str [ $ j ] == $ substr [ 1 ] ) { for ( $ k = $ j + 1 ; $ k < strlen ( $ str ) ; $ k ++ ) { if ( $ str [ $ k ] == $ substr [ 2 ] ) $ counter ++ ; } } } } } return $ counter ; } $ str = " GFGFGYSYIOIWIN " ; $ substr = " GFG " ; echo findOccurrences ( $ str , $ substr ) ; ? >
Count subsequence of length three in a given string | Function to find number of occurrences of a subsequence of length three in a string ; calculate length of string ; auxiliary array to store occurrences of first character ; auxiliary array to store occurrences of third character ; calculate occurrences of first character upto ith index from left ; calculate occurrences of third character upto ith index from right ; variable to store total number of occurrences ; loop to find the occurrences of middle element ; if middle character of subsequence is found in the string ; multiply the total occurrences of first character before middle character with the total occurrences of third character after middle character ; Driver Code
< ? php function findOccurrences ( $ str , $ substr ) { $ n = strlen ( $ str ) ; $ preLeft = array ( 0 ) ; $ preRight = array ( 0 ) ; if ( $ str [ 0 ] == $ substr [ 0 ] ) $ preLeft [ 0 ] ++ ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { if ( $ str [ $ i ] == $ substr [ 0 ] ) $ preLeft [ $ i ] = $ preLeft [ $ i - 1 ] + 1 ; else $ preLeft [ $ i ] = $ preLeft [ $ i - 1 ] ; } if ( $ str [ $ n - 1 ] == $ substr [ 2 ] ) $ preRight [ $ n - 1 ] ++ ; for ( $ i = $ n - 2 ; $ i >= 0 ; $ i -- ) { if ( $ str [ $ i ] == $ substr [ 2 ] ) $ preRight [ $ i ] = ( $ preRight [ $ i + 1 ] + 1 ) ; else $ preRight [ $ i ] = $ preRight [ $ i + 1 ] ; } $ counter = 0 ; for ( $ i = 1 ; $ i < $ n - 1 ; $ i ++ ) { if ( $ str [ $ i ] == $ str [ 1 ] ) { $ total = $ preLeft [ $ i - 1 ] * $ preRight [ $ i + 1 ] ; $ counter += $ total ; } } return $ counter ; } $ str = " GFGFGYSYIOIWIN " ; $ substr = " GFG " ; echo findOccurrences ( $ str , $ substr ) ; ? >
Lexicographically smallest string whose hamming distance from given string is exactly K | function to find Lexicographically smallest string with hamming distance k ; If k is 0 , output input string ; Copying input string into output string ; Traverse all the character of the string ; If current character is not ' a ' ; copy character ' a ' to output string ; str2 [ i ] = ' a ' ; ; If hamming distance became k , break ; ; If k is less than p ; Traversing string in reverse order ; Driver Code
< ? php function findString ( $ str , $ n , $ k ) { if ( $ k == 0 ) { echo $ str . " STRNEWLINE " ; return ; } $ str2 = $ str ; $ p = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ str2 [ $ i ] != ' a ' ) { $ str2 [ $ i ] = ' a ' ; $ p ++ ; p ++ ; if ( $ p == $ k ) break ; } } if ( $ p < $ k ) { for ( $ i = $ n - 1 ; $ i >= 0 ; $ i -- ) if ( $ str [ $ i ] == ' a ' ) { $ str2 [ $ i ] = ' b ' ; $ p ++ ; if ( $ p == $ k ) break ; } } echo $ str2 . " STRNEWLINE " ; } $ str = " pqrs " ; $ n = strlen ( $ str ) ; $ k = 2 ; findString ( $ str , $ n , $ k ) ; ? >
Print all possible strings that can be made by placing spaces | Function to print all subsequences ; Driver code
< ? php function printSubsequences ( $ str ) { $ n = strlen ( $ str ) ; $ opsize = pow ( 2 , $ n - 1 ) ; for ( $ counter = 0 ; $ counter < $ opsize ; $ counter ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { echo $ str [ $ j ] ; if ( $ counter & ( 1 << $ j ) ) echo " ▁ " ; } echo " STRNEWLINE " ; } } $ str = " ABC " ; printSubsequences ( $ str ) ; ? >
Minimum number of deletions so that no two consecutive are same | Function for counting deletions ; If two consecutive characters are the same , delete one of them . ; Driver Code ; Function call to print answer
< ? php function countDeletions ( $ str ) { $ ans = 0 ; for ( $ i = 0 ; $ i < strlen ( $ str ) - 1 ; $ i ++ ) if ( $ str [ $ i ] == $ str [ $ i + 1 ] ) $ ans ++ ; return $ ans ; } $ str = " AAABBB " ; echo countDeletions ( $ str ) ; ? >
Nth Even length Palindrome | Function to find nth even length Palindrome ; string r to store resultant palindrome . Initialize same as s ; In this loop string r stores reverse of string s after the string s in consecutive manner . ; Driver code ; Function call
< ? php function evenlength ( $ n ) { $ res = $ n ; for ( $ j = strlen ( $ n ) - 1 ; $ j >= 0 ; -- $ j ) $ res = $ res . $ n [ $ j ] ; return $ res ; } $ n = "10" ; echo evenlength ( $ n ) ; ? >
Determine if a string has all Unique Characters | PHP program to illustrate string with unique characters using brute force technique ; If at any time we encounter 2 same characters , return false ; If no duplicate characters encountered , return true ; Driver Code
< ? php function uniqueCharacters ( $ str ) { for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) { for ( $ j = $ i + 1 ; $ j < strlen ( $ str ) ; $ j ++ ) { if ( $ str [ $ i ] == $ str [ $ j ] ) { return false ; } } } return true ; } $ str = " GeeksforGeeks " ; if ( uniqueCharacters ( $ str ) ) { echo " The ▁ String ▁ " , $ str , " ▁ has ▁ all ▁ unique ▁ characters STRNEWLINE " ; } else { echo " The ▁ String ▁ " , $ str , " ▁ has ▁ duplicate ▁ characters STRNEWLINE " ; } ? >
Find an equal point in a string of brackets | Method to find an equal index ; Store the number of opening brackets at each index ; Store the number of closing brackets at each index ; check if there is no opening or closing brackets ; check if there is any index at which both brackets are equal ; Driver Code
< ? php function findIndex ( $ str ) { $ len = strlen ( $ str ) ; $ open = array ( 0 , $ len + 1 , NULL ) ; $ close = array ( 0 , $ len + 1 , NULL ) ; $ index = -1 ; $ open [ 0 ] = 0 ; $ close [ $ len ] = 0 ; if ( $ str [ 0 ] == ' ( ' ) $ open [ 1 ] = 1 ; if ( $ str [ $ len - 1 ] == ' ) ' ) $ close [ $ len - 1 ] = 1 ; for ( $ i = 1 ; $ i < $ len ; $ i ++ ) { if ( $ str [ $ i ] == ' ( ' ) $ open [ $ i + 1 ] = $ open [ $ i ] + 1 ; else $ open [ $ i + 1 ] = $ open [ $ i ] ; } for ( $ i = $ len - 2 ; $ i >= 0 ; $ i -- ) { if ( $ str [ $ i ] == ' ) ' ) $ close [ $ i ] = $ close [ $ i + 1 ] + 1 ; else $ close [ $ i ] = $ close [ $ i + 1 ] ; } if ( $ open [ $ len ] == 0 ) return $ len ; if ( $ close [ 0 ] == 0 ) return 0 ; for ( $ i = 0 ; $ i <= $ len ; $ i ++ ) if ( $ open [ $ i ] == $ close [ $ i ] ) $ index = $ i ; return $ index ; } $ str = " ( ( ) ) ) ( ( ) ( ) ( ) ) ) ) " ; echo ( findIndex ( $ str ) ) ; ? >
Convert decimal fraction to binary number | Function to convert decimal to binary upto k - precision after decimal point ; Fetch the integral part of decimal number ; Fetch the fractional part decimal number ; Conversion of integral part to binary equivalent ; Append 0 in binary ; Reverse string to get original binary equivalent ; Append point before conversion of fractional part ; Conversion of fractional part to binary equivalent ; Find next bit in fraction ; Driver code
< ? php function decimalToBinary ( $ num , $ k_prec ) { $ binary = " " ; $ Integral = ( int ) ( $ num ) ; $ fractional = $ num - $ Integral ; while ( $ Integral ) { $ rem = $ Integral % 2 ; $ binary . = chr ( $ rem + 48 ) ; $ Integral = ( int ) ( $ Integral / 2 ) ; } $ binary = strrev ( $ binary ) ; $ binary . = ' . ' ; while ( $ k_prec -- ) { $ fractional *= 2 ; $ fract_bit = ( int ) $ fractional ; if ( $ fract_bit == 1 ) { $ fractional -= $ fract_bit ; $ binary . = chr ( 1 + 48 ) ; } else $ binary . = chr ( 0 + 48 ) ; } return $ binary ; } $ n = 4.47 ; $ k = 3 ; echo decimalToBinary ( $ n , $ k ) . " STRNEWLINE " ; $ n = 6.986 ; $ k = 5 ; echo decimalToBinary ( $ n , $ k ) ; ? >
Difference of two large numbers | Returns true if str1 is smaller than str2 , else false . ; Calculate lengths of both string ; Function for finding difference of larger numbers ; Before proceeding further , make sure str1 is not smaller ; Take an empty string for storing result ; Calculate lengths of both string ; Initially take carry zero ; Traverse from end of both strings ; Do school mathematics , compute difference of current digits and carry ; subtract remaining digits of str1 [ ] ; if ( $i > 0 $sub > 0 ) remove preceding 0 's ; reverse resultant string ; Driver code ; Function call
< ? php function isSmaller ( $ str1 , $ str2 ) { $ n1 = strlen ( $ str1 ) ; $ n2 = strlen ( $ str2 ) ; if ( $ n1 < $ n2 ) return true ; if ( $ n2 < $ n1 ) return false ; for ( $ i = 0 ; $ i < $ n1 ; $ i ++ ) { if ( $ str1 [ $ i ] < $ str2 [ $ i ] ) return true ; else if ( $ str1 [ $ i ] > $ str2 [ $ i ] ) return false ; } return false ; } function findDiff ( $ str1 , $ str2 ) { if ( isSmaller ( $ str1 , $ str2 ) ) { $ t = $ str1 ; $ str1 = $ str2 ; $ str2 = $ t ; } $ str = " " ; $ n1 = strlen ( $ str1 ) ; $ n2 = strlen ( $ str2 ) ; $ diff = $ n1 - $ n2 ; $ carry = 0 ; for ( $ i = $ n2 - 1 ; $ i >= 0 ; $ i -- ) { $ sub = ( ( ord ( $ str1 [ $ i + $ diff ] ) - ord ( '0' ) ) - ( ord ( $ str2 [ $ i ] ) - ord ( '0' ) ) - $ carry ) ; if ( $ sub < 0 ) { $ sub = $ sub + 10 ; $ carry = 1 ; } else $ carry = 0 ; $ str . = chr ( $ sub + ord ( "0" ) ) ; } for ( $ i = $ n1 - $ n2 - 1 ; $ i >= 0 ; $ i -- ) { if ( $ str1 [ $ i ] == '0' && $ carry > 0 ) { $ str . = "9" ; continue ; } $ sub = ( ord ( $ str1 [ $ i ] ) - ord ( '0' ) - $ carry ) ; $ str . = chr ( $ sub + ord ( "0" ) ) ; $ carry = 0 ; } return strrev ( $ str ) ; } $ str1 = "88" ; $ str2 = "1079" ; print ( findDiff ( $ str1 , $ str2 ) ) ; ? >