"Return previous element in an expanding matrix | Returns left of str in an expanding matrix of a , b , c and d . ; Start from rightmost position ; If the current character is b or d , change to a or c respectively and break the loop ; If the current character is a or c , change it to b or d respectively ; Driver Code"
How can the above be solved in PHP?

< ? php function findLeft ( $ str ) { $ n = strlen ( $ str ) ; while ( $ n -- ) { if ( $ str [ $ n ] == ' d ' ) { $ str [ $ n ] = ' c ' ; break ; } if ( $ str [ $ n ] == ' b ' ) { $ str [ $ n ] = ' a ' ; break ; } if ( $ str [ $ n ] == ' a ' ) $ str [ $ n ] = ' b ' ; else if ( $ str [ $ n ] == ' c ' ) $ str [ $ n ] = ' d ' ; } return $ str ; } $ str = " aacbddc " ; echo " Left ▁ of ▁ " . $ str . " ▁ is ▁ " . findLeft ( $ str ) ; return 0 ; ? >


"Reversal algorithm for right rotation of an array | Function to reverse arr [ ] from index start to end ; Function to right rotate arr [ ] of size n by d ; function to print an array ; Driver code"
How can the above be solved in PHP?

< ? php function reverseArray ( & $ arr , $ start , $ end ) { while ( $ start < $ end ) { $ temp = $ arr [ $ start ] ; $ arr [ $ start ] = $ arr [ $ end ] ; $ arr [ $ end ] = $ temp ; $ start ++ ; $ end -- ; } } function rightRotate ( & $ arr , $ d , $ n ) { reverseArray ( $ arr , 0 , $ n - 1 ) ; reverseArray ( $ arr , 0 , $ d - 1 ) ; reverseArray ( $ arr , $ d , $ n - 1 ) ; } function printArray ( & $ arr , $ size ) { for ( $ i = 0 ; $ i < $ size ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; } $ arr = array ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ) ; $ n = sizeof ( $ arr ) ; $ k = 3 ; rightRotate ( $ arr , $ k , $ n ) ; printArray ( $ arr , $ n ) ; ? >


"Reverse Middle X Characters | Function to reverse the middle x characters in a string ; Find the position from where the characters have to be reversed ; Print the first n characters ; Print the middle x characters in reverse ; Print the last n characters ; Driver code"
How can the above be solved in PHP?

< ? php function reverse ( $ str , $ x ) { $ n = ( strlen ( $ str ) - $ x ) / 2 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo ( $ str [ $ i ] ) ; for ( $ i = $ n + $ x - 1 ; $ i >= $ n ; $ i -- ) echo ( $ str [ $ i ] ) ; for ( $ i = $ n + $ x ; $ i < strlen ( $ str ) ; $ i ++ ) echo $ str [ $ i ] ; } $ str = " geeksforgeeks " ; $ x = 3 ; reverse ( $ str , $ x ) ; ? >


"Reverse a number using stack | Stack to maintain order of digits ; Function to push digits into stack ; Function to reverse the number ; Function call to push number 's   digits to stack ; Popping the digits and forming the reversed number ; Return the reversed number formed ; Driver Code ; Function call to reverse number"
How can the above be solved in PHP?

< ? php $ st = array ( ) ; function push_digits ( $ number ) { global $ st ; while ( $ number != 0 ) { array_push ( $ st , $ number % 10 ) ; $ number = ( int ) ( $ number / 10 ) ; } } function reverse_number ( $ number ) { global $ st ; push_digits ( $ number ) ; $ reverse = 0 ; $ i = 1 ; while ( ! empty ( $ st ) ) { $ reverse = $ reverse + ( $ st [ count ( $ st ) - 1 ] * $ i ) ; array_pop ( $ st ) ; $ i = $ i * 10 ; } return $ reverse ; } $ number = 39997 ; echo reverse_number ( $ number ) ; ? >


"Reverse an array without using subtract sign â €˜ | Function to reverse array ; Reverse array in simple manner ; Swap ith index value with ( n - i - 1 ) th index value Note : A - B = A + ~ B + 1 So n - i = n + ~ i + 1 then n - i - 1 = ( n + ~ i + 1 ) + 1 + 1 ; Driver code ; print the reversed array"
How can the above be solved in PHP?

< ? php function reverseArray ( & $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n / 2 ; $ i ++ ) { swap ( $ arr , $ i , ( $ n + ~ $ i + 1 ) + ~ 1 + 1 ) ; } } function swap ( & $ arr , $ i , $ j ) { $ temp = $ arr [ $ i ] ; $ arr [ $ i ] = $ arr [ $ j ] ; $ arr [ $ j ] = $ temp ; return $ arr ; } { $ arr = array ( 5 , 3 , 7 , 2 , 1 , 6 ) ; $ n = sizeof ( $ arr ) ; reverseArray ( $ arr , $ n ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { echo ( $ arr [ $ i ] . " " ) ; } }


"Reverse an array without using subtract sign â €˜ | Function to reverse array ; Trick to assign - 1 to a variable ; Reverse array in simple manner ; Swap ith index value with ( n - i - 1 ) th index value ; Drivers code ; print the reversed array"
How can the above be solved in PHP?

< ? php function reverseArray ( & $ arr , $ n ) { $ x = ( PHP_INT_MIN / PHP_INT_MAX ) ; for ( $ i = 0 ; $ i < $ n / 2 ; $ i ++ ) swap ( $ arr , $ i , $ n + ( $ x * $ i ) + $ x ) ; } function swap ( & $ arr , $ i , $ j ) { $ temp = $ arr [ $ i ] ; $ arr [ $ i ] = $ arr [ $ j ] ; $ arr [ $ j ] = $ temp ; return $ arr ; } $ arr = array ( 5 , 3 , 7 , 2 , 1 , 6 ) ; $ n = sizeof ( $ arr ) ; reverseArray ( $ arr , $ n ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo ( $ arr [ $ i ] . " ▁ " ) ;


"Reverse and Add Function | Iterative function to reverse digits of num ; Function to check whether he number is palindrome or not ; Reverse and Add Function ; Reversing the digits of the number ; Adding the reversed number with the original ; Checking whether the number is palindrome or not ; Driver Code"
How can the above be solved in PHP?

< ? php function reversDigits ( $ num ) { $ rev_num = 0 ; while ( $ num > 0 ) { $ rev_num = $ rev_num * 10 + $ num % 10 ; $ num = ( int ) ( $ num / 10 ) ; } return $ rev_num ; } function isPalindrome ( $ num ) { return ( reversDigits ( $ num ) == $ num ) ; } function ReverseandAdd ( $ num ) { $ rev_num = 0 ; while ( $ num <= 4294967295 ) { $ rev_num = reversDigits ( $ num ) ; $ num = $ num + $ rev_num ; if ( isPalindrome ( $ num ) ) { print ( $ num . " " ) ; break ; } else if ( $ num > 4294967295 ) { print ( " No ▁ palindrome ▁ exist " ) ; } } } ReverseandAdd ( 195 ) ; ReverseandAdd ( 265 ) ; ? >


"Right | Generate all prime numbers less than n . ; Initialize all entries of boolean array as true . A value in isPrime [ i ] will finally be false if i is Not a prime , else true bool isPrime [ n + 1 ] ; ; If isPrime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Returns true if n is right - truncatable , else false ; Generating primes using Sieve ; Checking whether the number remains prime when the last ( " right " ) digit is successively removed ; Driver Code"
How can the above be solved in PHP?

 < ? php function sieveOfEratosthenes ( $ n , & $ isPrime ) { $ isPrime [ 0 ] = $ isPrime [ 1 ] = false ; for ( $ p = 2 ; $ p * $ p <= $ n ; $ p ++ ) { if ( $ isPrime [ $ p ] == true ) { for ( $ i = $ p * 2 ; $ i <= $ n ; $ i += $ p ) $ isPrime [ $ i ] = false ; } } } function rightTruPrime ( $ n ) { $ isPrime = array_fill ( 0 , $ n + 1 , true ) ; sieveOfEratosthenes ( $ n , $ isPrime ) ; while ( $ n ) { if ( $ isPrime [ $ n ] ) $ n = ( int ) ( $ n / 10 ) ; else return false ; } return true ; } $ n = 59399 ; if ( rightTruPrime ( $ n ) ) echo " Yes
 " ; else echo " No
 " ; ? >


"Roots of the quadratic equation when a + b + c = 0 without using Shridharacharya formula | Function to print the roots of the quadratic equation when a + b + c = 0 ; Driver code"
How can the above be solved in PHP?

< ? php function printRoots ( $ a , $ b , $ c ) { echo "1" ; echo " , ▁ " ; echo $ c / ( $ a * 1.0 ) ; } $ a = 2 ; $ b = 3 ; $ c = -5 ; printRoots ( $ a , $ b , $ c ) ; ? >


"Ropes left after every removal of smallest | Function print how many Ropes are Left AfterEvery Cutting operation ; sort all Ropes in increase of there length ; min length rope ; now traverse through the given Ropes in increase order of length ; After cutting if current rope length is greater than '0' that mean all ropes to it 's right side are also  greater than 0 ; now current rope become min length rope ; After first operation all ropes length become zero ; Driver Code"
How can the above be solved in PHP?

< ? php function cuttringRopes ( $ Ropes , $ n ) { sort ( $ Ropes ) ; $ singleOperation = 0 ; $ cuttingLenght = $ Ropes [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { if ( $ Ropes [ $ i ] - $ cuttingLenght > 0 ) { echo ( $ n - $ i ) . " ▁ " ; $ cuttingLenght = $ Ropes [ $ i ] ; $ singleOperation ++ ; } } if ( $ singleOperation == 0 ) echo "0 ▁ " ; } $ Ropes = array ( 5 , 1 , 1 , 2 , 3 , 5 ) ; $ n = count ( $ Ropes ) ; cuttringRopes ( $ Ropes , $ n ) ; ? >


"Rotate the matrix right by K times | size of matrix ; function to rotate matrix by k times ; temporary array of size M ; within the size of matrix ; copy first M - k elements to temporary array ; copy the elements from k to end to starting ; copy elements from temporary array to end ; function to display the matrix ; Driver code ; rotate matrix by k ; display rotated matrix"
How can the above be solved in PHP?

 < ? php $ M = 3 ; $ N = 3 ; function rotateMatrix ( & $ matrix , $ k ) { global $ M , $ N ; $ temp = array ( ) ; $ k = $ k % $ M ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { for ( $ t = 0 ; $ t < $ M - $ k ; $ t ++ ) $ temp [ $ t ] = $ matrix [ $ i ] [ $ t ] ; for ( $ j = $ M - $ k ; $ j < $ M ; $ j ++ ) $ matrix [ $ i ] [ $ j - $ M + $ k ] = $ matrix [ $ i ] [ $ j ] ; for ( $ j = $ k ; $ j < $ M ; $ j ++ ) $ matrix [ $ i ] [ $ j ] = $ temp [ $ j - $ k ] ; } } function displayMatrix ( & $ matrix ) { global $ M , $ N ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { for ( $ j = 0 ; $ j < $ M ; $ j ++ ) echo ( $ matrix [ $ i ] [ $ j ] . " ▁ " ) ; echo ( "
 " ) ; } } $ matrix = array ( array ( 12 , 23 , 34 ) , array ( 45 , 56 , 67 ) , array ( 78 , 89 , 91 ) ) ; $ k = 2 ; rotateMatrix ( $ matrix , $ k ) ; displayMatrix ( $ matrix ) ; ? >


"Rotations of a Binary String with Odd Value | function to calculate total odd decimal equivalent ; Driver code"
How can the above be solved in PHP?

< ? php function oddEquivalent ( $ s , $ n ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ s [ $ i ] == '1' ) $ count ++ ; } return $ count ; } $ s = "1011011" ; $ n = strlen ( $ s ) ; echo ( oddEquivalent ( $ s , $ n ) ) ; ? >


"Round the given number to nearest multiple of 10 | function to round the number ; Smaller multiple ; Larger multiple ; Return of closest of two ; Driver Code"
How can the above be solved in PHP?

 < ? php function roundFunation ( $ n ) { $ a = ( int ) ( $ n / 10 ) * 10 ; $ b = ( $ a + 10 ) ; return ( $ n - $ a > $ b - $ n ) ? $ b : $ a ; } $ n = 4722 ; echo roundFunation ( $ n ) , "
 " ; ? >


"Row | PHP program to find common elements in two diagonals . ; Returns count of row wise same elements in two diagonals of mat [ n ] [ n ] ; Driver Code"
How can the above be solved in PHP?

< ? php $ MAX = 100 ; function countCommon ( $ mat , $ n ) { global $ MAX ; $ res = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ mat [ $ i ] [ $ i ] == $ mat [ $ i ] [ $ n - $ i - 1 ] ) $ res ++ ; return $ res ; } $ mat = array ( array ( 1 , 2 , 3 ) , array ( 4 , 5 , 6 ) , array ( 7 , 8 , 9 ) ) ; echo countCommon ( $ mat , 3 ) ; ? >


"Saddleback Search Algorithm in a 2D array | PHP program to search an element in row - wise and column - wise sorted matrix ; Searches the element x in mat [ m ] [ n ] . If the element is found , then prints its position and returns true , otherwise prints " not ▁ found " and returns false ; set indexes for bottom left element ; if mat [ i ] [ j ] < x ; Driver Code"
How can the above be solved in PHP?


"Same Number Of Set Bits As N | returns number of set bits in a number ; function ; __builtin_popcount function that count set bits in n ; Iterate from n - 1 to 1 ; check if the number of set bits equals to temp increment count ; Driver Code"
How can the above be solved in PHP?

< ? php function __builtin_popcount ( $ n ) { $ t = 0 ; while ( $ n > 0 ) { $ d = $ n % 2 ; $ n = intval ( $ n / 2 ) ; if ( $ d == 1 ) $ t ++ ; } return $ t ; } function smallerNumsWithSameSetBits ( $ n ) { $ temp = __builtin_popcount ( $ n ) ; $ count = 0 ; for ( $ i = $ n - 1 ; $ i > 0 ; $ i -- ) { if ( $ temp == __builtin_popcount ( $ i ) ) $ count ++ ; } return $ count ; } $ n = 4 ; echo ( smallerNumsWithSameSetBits ( $ n ) ) ; ? >


"Save from Bishop in chessboard | function to calculate total safe position ; i , j denotes row and column of position of bishop ; calc distance in four direction ; calc total sum of distance + 1 for unsafe positions ; return total safe positions ; Driver Code"
How can the above be solved in PHP?

< ? php function calcSafe ( $ pos ) { $ j = $ pos % 10 ; $ i = $ pos / 10 ; $ dis_11 = min ( abs ( 1 - $ i ) , abs ( 1 - $ j ) ) ; $ dis_18 = min ( abs ( 1 - $ i ) , abs ( 8 - $ j ) ) ; $ dis_81 = min ( abs ( 8 - $ i ) , abs ( 1 - $ j ) ) ; $ dis_88 = min ( abs ( 8 - $ i ) , abs ( 8 - $ j ) ) ; $ sum = $ dis_11 + $ dis_18 + $ dis_81 + $ dis_88 + 1 ; return ceil ( 64 - $ sum ) ; } $ pos = 34 ; echo " Safe ▁ Positions ▁ = ▁ " , calcSafe ( $ pos ) ; ? >


"SchrÃ ¶ derâ €“ Hipparchus number | A memoization based optimized PHP program to find n - th SchrAderaHipparchus number ; Driver Code"
How can the above be solved in PHP?

 < ? php $ MAX = 500 ; function nthSHN ( $ n , $ dp ) { if ( $ n == 1 $ n == 2 ) return $ dp [ $ n ] = 1 ; if ( $ dp [ $ n ] != -1 ) return $ dp [ $ n ] ; return $ dp [ $ n ] = ( ( 6 * $ n - 9 ) * nthSHN ( $ n - 1 , $ dp ) - ( $ n - 3 ) * nthSHN ( $ n - 2 , $ dp ) ) / $ n ; } $ n = 6 ; $ dp = array_fill ( 0 , $ MAX , true ) ; echo nthSHN ( $ n , $ dp ) , "
 " ; ? >


"Search , insert and delete in a sorted array | function to implement binary search ; low + ( high - low ) / 2 ; Driver Code Let us search 3 in below array"
How can the above be solved in PHP?

< ? php function binarySearch ( $ arr , $ low , $ high , $ key ) { if ( $ high < $ low ) return -1 ; $ mid = ( $ low + $ high ) / 2 ; if ( $ key == $ arr [ ( int ) $ mid ] ) return $ mid ; if ( $ key > $ arr [ ( int ) $ mid ] ) return binarySearch ( $ arr , ( $ mid + 1 ) , $ high , $ key ) ; return ( binarySearch ( $ arr , $ low , ( $ mid -1 ) , $ key ) ) ; } $ arr = array ( 5 , 6 , 7 , 8 , 9 , 10 ) ; $ n = count ( $ arr ) ; $ key = 10 ; echo " Index : ▁ " , ( int ) binarySearch ( $ arr , 0 , $ n -1 , $ key ) ; ? >


"Search , insert and delete in an unsorted array | Function to implement search operation ; Driver Code ; Using a last element as search element"
How can the above be solved in PHP?

< ? php function findElement ( $ arr , $ n , $ key ) { $ i ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] == $ key ) return $ i ; return -1 ; } $ arr = array ( 12 , 34 , 10 , 6 , 40 ) ; $ n = sizeof ( $ arr ) ; $ key = 40 ; $ position = findElement ( $ arr , $ n , $ key ) ; if ( $ position == - 1 ) echo ( " Element ▁ not ▁ found " ) ; else echo ( " Element ▁ Found ▁ at ▁ Position : ▁ " . ( $ position + 1 ) ) ; ? >


"Search , insert and delete in an unsorted array | Inserts a key in arr [ ] of given capacity . n is current size of arr [ ] . This function returns n + 1 if insertion is successful , else n . ; Cannot insert more elements if n is already more than or equal to capcity ; Driver Code ; Inserting key"
How can the above be solved in PHP?

< ? php function insertSorted ( & $ arr , $ n , $ key , $ capacity ) { if ( $ n >= $ capacity ) return $ n ; array_push ( $ arr , $ key ) ; return ( $ n + 1 ) ; } $ arr = array ( 12 , 16 , 20 , 40 , 50 , 70 ) ; $ capacity = 20 ; $ n = 6 ; $ key = 26 ; echo " Before ▁ Insertion : ▁ " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; $ n = insertSorted ( $ arr , $ n , $ key , $ capacity ) ; echo " After Insertion : " for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; ? >


"Search , insert and delete in an unsorted array | To search a key to be deleted ; Function to delete an element ; Find position of element to be deleted ; Deleting element ; Driver code"
How can the above be solved in PHP?

 < ? php function findElement ( & $ arr , $ n , $ key ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] == $ key ) return $ i ; return -1 ; } function deleteElement ( & $ arr , $ n , $ key ) { $ pos = findElement ( $ arr , $ n , $ key ) ; if ( $ pos == -1 ) { echo " Element ▁ not ▁ found " ; return $ n ; } for ( $ i = $ pos ; $ i < $ n - 1 ; $ i ++ ) $ arr [ $ i ] = $ arr [ $ i + 1 ] ; return $ n - 1 ; } $ arr = array ( 10 , 50 , 30 , 40 , 20 ) ; $ n = count ( $ arr ) ; $ key = 30 ; echo " Array ▁ before ▁ deletion
 " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; $ n = deleteElement ( $ arr , $ n , $ key ) ; echo " Array after deletion " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; ? >


"Search an element in an array where difference between adjacent elements is 1 | x is the element to be searched in arr [ 0. . n - 1 ] ; Traverse the given array starting from leftmost element ; If x is found at index i ; Jump the difference between current array element and x ; Driver Code"
How can the above be solved in PHP?

< ? php function search ( $ arr , $ n , $ x ) { $ i = 0 ; while ( $ i < $ n ) { if ( $ arr [ $ i ] == $ x ) return $ i ; $ i = $ i + abs ( $ arr [ $ i ] - $ x ) ; } echo " number ▁ is ▁ not ▁ present ! " ; return -1 ; } $ arr = array ( 8 , 7 , 6 , 7 , 6 , 5 , 4 , 3 , 2 , 3 , 4 , 3 ) ; $ n = sizeof ( $ arr ) ; $ x = 3 ; echo " Element ▁ " , $ x , " ▁ is ▁ present ▁ " , " at ▁ index ▁ " , search ( $ arr , $ n , 3 ) ; ? >


"Search an element in an unsorted array using minimum number of comparisons | function to search an element in minimum number of comparisons ; 1 st comparison ; no termination condition and thus no comparison ; this would be executed at - most n times and therefore at - most n comparisons ; replace arr [ n - 1 ] with its actual element as in original ' arr [ ] ' ; if ' x ' is found before the ' ( n - 1 ) th ' index , then it is present in the array final comparison ; else not present in the array ; Driver Code"
How can the above be solved in PHP?

< ? php function search ( $ arr , $ n , $ x ) { if ( $ arr [ $ n - 1 ] == $ x ) return " Found " ; $ backup = $ arr [ $ n - 1 ] ; $ arr [ $ n - 1 ] = $ x ; for ( $ i = 0 ; ; $ i ++ ) { if ( $ arr [ $ i ] == $ x ) { $ arr [ $ n - 1 ] = $ backup ; if ( $ i < $ n - 1 ) return " Found " ; return " Not ▁ Found " ; } } } $ arr = array ( 4 , 6 , 1 , 5 , 8 ) ; $ n = sizeof ( $ arr ) ; $ x = 1 ; echo ( search ( $ arr , $ n , $ x ) ) ; ? >


"Search element in a Spirally sorted Matrix | PHP implementation of the above approach ; Function to return the ring , the number x belongs to . ; Returns - 1 if number x is smaller than least element of arr ; l and r represent the diagonal elements to search in ; Returns - 1 if number x is greater than the largest element of arr ; Function to perform binary search on an array sorted in increasing order l and r represent the left and right index of the row to be searched ; Function to perform binary search on a particular column of the 2D array t and b represent top and bottom rows ; Function to perform binary search on an array sorted in decreasing order ; Function to perform binary search on a particular column of the 2D array ; Function to find the position of the number x ; Finding the ring ; To store row and column ; Edge case if n is odd ; Check which of the 4 sides , the number x lies in ; Printing the position ; Driver code"
How can the above be solved in PHP?

< ? php $ n = 4 ; function findRing ( $ arr , $ x ) { global $ n ; if ( $ arr [ 0 ] [ 0 ] > $ x ) return -1 ; $ l = 0 ; $ r = ( int ) ( ( $ n + 1 ) / 2 - 1 ) ; if ( $ n % 2 == 1 && $ arr [ $ r ] [ $ r ] < $ x ) return -1 ; if ( $ n % 2 == 0 && $ arr [ $ r + 1 ] [ $ r ] < $ x ) return -1 ; while ( $ l < $ r ) { $ mid = ( int ) ( ( $ l + $ r ) / 2 ) ; if ( $ arr [ $ mid ] [ $ mid ] <= $ x ) if ( $ mid == ( int ) ( ( $ n + 1 ) / 2 - 1 ) $ arr [ $ mid + 1 ] [ $ mid + 1 ] > $ x ) return $ mid ; else $ l = $ mid + 1 ; else $ r = $ mid - 1 ; } return $ r ; } function binarySearchRowInc ( $ arr , $ row , $ l , $ r , $ x ) { while ( $ l <= $ r ) { $ mid = ( int ) ( ( $ l + $ r ) / 2 ) ; if ( $ arr [ $ row ] [ $ mid ] == $ x ) return $ mid ; if ( $ arr [ $ row ] [ $ mid ] < $ x ) $ l = $ mid + 1 ; else $ r = $ mid - 1 ; } return -1 ; } function binarySearchColumnInc ( $ arr , $ col , $ t , $ b , $ x ) { while ( $ t <= $ b ) { $ mid = ( int ) ( ( $ t + b ) / 2 ) ; if ( $ arr [ $ mid ] [ $ col ] == $ x ) return $ mid ; if ( $ arr [ $ mid ] [ $ col ] < $ x ) $ t = $ mid + 1 ; else $ b = $ mid - 1 ; } return -1 ; } function binarySearchRowDec ( $ arr , $ row , $ l , $ r , $ x ) { while ( $ l <= $ r ) { $ mid = ( int ) ( ( $ l + $ r ) / 2 ) ; if ( $ arr [ $ row ] [ $ mid ] == $ x ) return $ mid ; if ( $ arr [ $ row ] [ $ mid ] < $ x ) $ r = $ mid - 1 ; else $ l = $ mid + 1 ; } return -1 ; } function binarySearchColumnDec ( $ arr , $ col , $ t , $ b , $ x ) { while ( $ t <= $ b ) { $ mid = ( int ) ( ( $ t + $ b ) / 2 ) ; if ( $ arr [ $ mid ] [ $ col ] == $ x ) return $ mid ; if ( $ arr [ $ mid ] [ $ col ] < $ x ) $ b = $ mid - 1 ; else $ t = $ mid + 1 ; } return -1 ; } function spiralBinary ( $ arr , $ x ) { global $ n ; $ f1 = findRing ( $ arr , $ x ) ; $ r = -1 ; $ c = -1 ; if ( $ f1 == -1 ) { echo " - 1" ; return ; } if ( $ n % 2 == 1 && $ f1 == ( int ) ( ( $ n + 1 ) / 2 - 1 ) ) { echo $ f1 . " " ▁ . ▁ $ f1 ▁ . ▁ " " return ; } if ( $ x < $ arr [ $ f1 ] [ $ n - $ f1 - 1 ] ) { $ c = binarySearchRowInc ( $ arr , $ f1 , $ f1...


"Search in a row wise and column wise sorted matrix | Searches the element $x in mat [ ] [ ] . If the element is found , then prints its position and returns true , otherwise prints " not ▁ found " and returns false ; set indexes for top right element ; if $mat [ $i ] [ $j ] < $x ; if ( $i == $n $j == - 1 ) ; Driver Code"
How can the above be solved in PHP?


"Search in a sorted 2D matrix ( Stored in row major order ) | Php program to find whether a given element is present in the given 2 - D matrix ; Basic binary search to find an element in a 1 - D array ; if element found return true ; if middle less than K then skip the left part of the array else skip the right part ; if not found return false ; Function to search an element in a matrix based on Divide and conquer approach ; if the element lies in the range of this row then call 1 - D binary search on this row ; if the element is less then the starting element of that row then search in upper rows else search in the lower rows ; if not found ; Driver code"
How can the above be solved in PHP?

< ? php $ M = 3 ; $ N = 4 ; function binarySearch1D ( $ arr , $ K ) { $ low = 0 ; $ high = $ GLOBALS [ ' N ' ] - 1 ; while ( $ low <= $ high ) { $ mid = $ low + ( int ) ( $ high - $ low ) / 2 ; if ( $ arr [ $ mid ] == $ K ) return True ; if ( $ arr [ $ mid ] < $ K ) $ low = $ mid + 1 ; else $ high = $ mid - 1 ; } return False ; } function searchMatrix ( $ matrix , $ K ) { $ low = 0 ; $ high = $ GLOBALS [ ' M ' ] - 1 ; while ( $ low <= $ high ) { $ mid = $ low + ( int ) ( $ high - $ low ) / 2 ; if ( $ K >= $ matrix [ $ mid ] [ 0 ] && $ K <= $ matrix [ $ mid ] [ $ GLOBALS [ ' N ' ] - 1 ] ) return binarySearch1D ( $ matrix [ $ mid ] , $ K ) ; if ( $ K < $ matrix [ $ mid ] [ 0 ] ) $ high = $ mid - 1 ; else $ low = $ mid + 1 ; } return False ; } $ matrix = array ( [ 1 , 3 , 5 , 7 ] , [ 10 , 11 , 16 , 20 ] , [ 23 , 30 , 34 , 50 ] ) ; $ K = 3 ; $ M = 3 ; $ N = 4 ; if ( searchMatrix ( $ matrix , $ K ) ) echo " Found " ; else echo " Not ▁ found " ; ? >


"Search in an almost sorted array | A recursive binary search based function . It returns index of x in given array arr [ l . . r ] is present , otherwise - 1 ; If the element is present at one of the middle 3 positions ; If element is smaller than mid , then it can only be present in left subarray ; Else the element can only be present in right subarray ; We reach here when element is not present in array ; Driver Code"
How can the above be solved in PHP?

< ? php function binarySearch ( $ arr , $ l , $ r , $ x ) { if ( $ r >= $ l ) { $ mid = $ l + ( $ r - $ l ) / 2 ; if ( $ arr [ $ mid ] == $ x ) return $ mid ; if ( $ mid > $ l && $ arr [ $ mid - 1 ] == $ x ) return ( $ mid - 1 ) ; if ( $ mid < $ r && $ arr [ $ mid + 1 ] == $ x ) return ( $ mid + 1 ) ; if ( $ arr [ $ mid ] > $ x ) return binarySearch ( $ arr , $ l , $ mid - 2 , $ x ) ; return binarySearch ( $ arr , $ mid + 2 , $ r , $ x ) ; } return -1 ; } $ arr = array ( 3 , 2 , 10 , 4 , 40 ) ; $ n = sizeof ( $ arr ) ; $ x = 4 ; $ result = binarySearch ( $ arr , 0 , $ n - 1 , $ x ) ; if ( $ result == -1 ) echo ( " Element ▁ is ▁ not ▁ present ▁ in ▁ array " ) ; else echo ( " Element ▁ is ▁ present ▁ at ▁ index ▁ $ result " ) ; ? >


"Secretary Problem ( A Optimal Stopping Problem ) | PHP Program to test 1 / e law for Secretary Problem : ; To find closest integer of num . ; Finds best candidate using n / e rule . candidate [ ] represents talents of n candidates . ; Calculating sample size for benchmarking . ; Finding best candidate in sample size ; Finding the first best candidate that is better than benchmark set . ; Driver code ; n = 8 candidates and candidate array contains talents of n candidate where the largest number means highest talented candidate . ; generating random numbers between 1 to 8 for talent of candidate"
How can the above be solved in PHP?

 < ? php $ e = 2.71828 ; function roundNo ( $ num ) { return $ num < 0 ? $ num - 0.5 : $ num + 0.5 ; } function printBestCandidate ( $ candidate , $ n ) { global $ e ; $ sample_size = roundNo ( $ n / $ e ) ; echo " Sample size is " floor ( $ sample_size ) . "
 " ; $ best = 0 ; for ( $ i = 1 ; $ i < $ sample_size ; $ i ++ ) if ( $ candidate [ $ i ] > $ candidate [ $ best ] ) $ best = $ i ; for ( $ i = $ sample_size ; $ i < $ n ; $ i ++ ) if ( $ candidate [ $ i ] >= $ candidate [ $ best ] ) { $ best = $ i ; break ; } if ( $ best >= $ sample_size ) echo " Best candidate found is " . floor ( $ best + 1 ) . " ▁ with ▁ talent ▁ " . floor ( $ candidate [ $ best ] ) . "
 " ; else echo " Couldn ' t ▁ find ▁ a ▁ best ▁ candidate
 " ; } $ n = 8 ; $ candidate = array_fill ( 0 , $ n , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ candidate [ $ i ] = 1 + rand ( 1 , 8 ) ; echo " Candidate ▁ : ▁ " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo ( $ i + 1 ) . " ▁ " ; echo " Talents : " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ candidate [ $ i ] . " ▁ " ; printBestCandidate ( $ candidate , $ n ) ; ? >


"Section formula for 3 D | Function to find the section of the line ; Applying section formula ; Printing result ; Driver code"
How can the above be solved in PHP?


"Segregate even and odd numbers | Set 3 | Function to segregate even odd numbers ; Swapping even and odd numbers ; Printing segregated array ; Driver code"
How can the above be solved in PHP?


"Sentence Palindrome ( Palindrome after removing spaces , dots , . . etc ) | To check sentence is palindrome or not ; Lowercase string ; Compares character until they are equal ; If there is another symbol in left of sentence ; If there is another symbol in right of sentence ; If characters are equal ; If characters are not equal then sentence is not palindrome ; Returns true if sentence is palindrome ; Driver program to test sentencePalindrome ( )"
How can the above be solved in PHP?

< ? php function sentencePalindrome ( $ str ) { $ l = 0 ; $ h = strlen ( $ str ) - 1 ; for ( $ i = 0 ; $ i < $ h ; $ i ++ ) $ str [ $ i ] = strtolower ( $ str [ $ i ] ) ; while ( $ l <= $ h ) { if ( ! ( $ str [ $ l ] >= ' a ' && $ str [ $ l ] <= ' z ' ) ) $ l ++ ; else if ( ! ( $ str [ $ h ] >= ' a ' && $ str [ $ h ] <= ' z ' ) ) $ h -- ; else if ( $ str [ $ l ] == $ str [ $ h ] ) { $ l ++ ; $ h -- ; } else return false ; } return true ; } $ str = " Too ▁ hot ▁ to ▁ hoot . " ; if ( sentencePalindrome ( $ str ) ) echo " Sentence ▁ is ▁ palindrome . " ; else echo " Sentence ▁ is ▁ not ▁ palindrome . " ; return 0 ; ? >


"Sequence Alignment problem | function to find out the minimum penalty ; table for storing optimal substructure answers ; initialising the table ; calculating the minimum penalty ; Reconstructing the solution $l = $n + $m ; maximum possible length ; Final answers for the respective strings $xans [ $l + 1 ] ; $yans [ $l + 1 ] ; ; Since we have assumed the answer to be n + m long , we need to remove the extra gaps in the starting id represents the index from which the arrays xans , yans are useful ; Printing the final answer ; input strings ; initialising penalties of different types ; calling the function to calculate the result"
How can the above be solved in PHP?


"Sequences of given length where every element is more than or equal to twice of previous | DP based function to find the number of special sequences ; define T and build in bottom manner to store number of special sequences of length n and maximum value m ; Base case : If length of sequence is 0 or maximum value is 0 , there cannot exist any special sequence ; if length of sequence is more than the maximum value , special sequence cannot exist ; If length of sequence is 1 then the number of special sequences is equal to the maximum value For example with maximum value 2 and length 1 , there can be 2 special sequences { 1 } , { 2 } ; otherwise calculate ; Driver Code"
How can the above be solved in PHP?

< ? php function getTotalNumberOfSequences ( $ m , $ n ) { $ T = array ( array ( ) ) ; for ( $ i = 0 ; $ i < $ m + 1 ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n + 1 ; $ j ++ ) { if ( $ i == 0 or $ j == 0 ) $ T [ $ i ] [ $ j ] = 0 ; else if ( $ i < $ j ) $ T [ $ i ] [ $ j ] = 0 ; else if ( $ j == 1 ) $ T [ $ i ] [ $ j ] = $ i ; else $ T [ $ i ] [ $ j ] = $ T [ $ i - 1 ] [ $ j ] + $ T [ $ i / 2 ] [ $ j - 1 ] ; } } return $ T [ $ m ] [ $ n ] ; } $ m = 10 ; $ n = 4 ; echo " Total ▁ number ▁ of ▁ possible ▁ sequences ▁ " , getTotalNumberOfSequences ( $ m , $ n ) ; ? >


"Sequences of given length where every element is more than or equal to twice of previous | Recursive function to find the number of special sequences ; A special sequence cannot exist if length n is more than the maximum value m . ; If n is 0 , found an empty special sequence ; There can be two possibilities : ( 1 ) Reduce last element value ( 2 ) Consider last element as m and reduce number of terms ; Driver Code"
How can the above be solved in PHP?

< ? php function getTotalNumberOfSequences ( $ m , $ n ) { if ( $ m < $ n ) return 0 ; if ( $ n == 0 ) return 1 ; return getTotalNumberOfSequences ( $ m - 1 , $ n ) + getTotalNumberOfSequences ( $ m / 2 , $ n - 1 ) ; } $ m = 10 ; $ n = 4 ; echo ( " Total ▁ number ▁ of ▁ possible ▁ sequences ▁ " ) ; echo ( getTotalNumberOfSequences ( $ m , $ n ) ) ; ? >


"Series summation if T ( n ) is given and n is very large | PHP implementation of the approach ; Function to return the sum of the given series ; Driver code"
How can the above be solved in PHP?

< ? php $ GLOBALS [ ' MOD ' ] = 1000000007 ; function sumOfSeries ( $ n ) { $ ans = pow ( $ n % $ GLOBALS [ ' MOD ' ] , 2 ) ; return ( $ ans % $ GLOBALS [ ' MOD ' ] ) ; } $ n = 10 ; echo sumOfSeries ( $ n ) ; ? >


"Series with largest GCD and sum equals to n | function to generate and print the sequence ; stores the maximum gcd that can be possible of sequence . ; if maximum gcd comes out to be zero then not possible ; the smallest gcd possible is 1 ; traverse the array to find out the max gcd possible ; checks if the number is divisible or not ; checks if x is smaller then the max gcd possible and x is greater then the resultant gcd till now , then r = x ; checks if n / x is smaller than the max gcd possible and n / x is greater then the resultant gcd till now , then r = x ; traverses and prints d , 2d , 3d , ... , ( k - 1 ) d , ; computes the last element of the sequence n - s . ; prints the last element ; Driver Code"
How can the above be solved in PHP?


"Set all even bits of a number | Sets even bits of n and returns modified number . ; Generate 101010. . .10 number and store in res . ; if bit is even then generate number and or with res ; return OR number ; Driver Code"
How can the above be solved in PHP?

< ? php function evenbitsetnumber ( $ n ) { $ count = 0 ; $ res = 0 ; for ( $ temp = $ n ; $ temp > 0 ; $ temp >>= 1 ) { if ( $ count % 2 == 1 ) $ res |= ( 1 << $ count ) ; $ count ++ ; } return ( $ n $ res ) ; } $ n = 10 ; echo evenbitsetnumber ( $ n ) ; ? >


"Set all even bits of a number | return msb set number ; set all bits ; return msb increment n by 1 and shift by 1 ; return even seted number ; get msb here ; generate even bits like 101010. . ; if bits is odd then shift by 1 ; return even set bits number ; set all even bits here ; take or with even set bits number ; Driver code"
How can the above be solved in PHP?

< ? php function getmsb ( $ n ) { $ n |= $ n >> 1 ; $ n |= $ n >> 2 ; $ n |= $ n >> 4 ; $ n |= $ n >> 8 ; $ n |= $ n >> 16 ; return ( $ n + 1 ) >> 1 ; } function getevenbits ( $ n ) { $ n = getmsb ( $ n ) ; $ n |= $ n >> 2 ; $ n |= $ n >> 4 ; $ n |= $ n >> 8 ; $ n |= $ n >> 16 ; if ( $ n & 1 ) $ n = $ n >> 1 ; return $ n ; } function setallevenbits ( $ n ) { return $ n | getevenbits ( $ n ) ; } $ n = 10 ; echo setallevenbits ( $ n ) ; ? >


"Set all odd bits of a number | return MSB set number ; set all bits including MSB . ; return MSB ; Returns a number of same size ( MSB atsame position ) as n and all odd bits set ; generate odd bits like 010101. . ; if bits is even then shift by 1 ; return odd set bits number ; set all odd bits here ; take OR with odd set bits number ; Driver code"
How can the above be solved in PHP?

< ? php function getmsb ( $ n ) { $ n |= $ n >> 1 ; $ n |= $ n >> 2 ; $ n |= $ n >> 4 ; $ n |= $ n >> 8 ; $ n |= $ n >> 16 ; return ( $ n + 1 ) >> 1 ; } function getevenbits ( $ n ) { $ n = getmsb ( $ n ) ; $ n |= $ n >> 2 ; $ n |= $ n >> 4 ; $ n |= $ n >> 8 ; $ n |= $ n >> 16 ; if ( ( $ n & 1 ) == 0 ) $ n = $ n >> 1 ; return $ n ; } function setalloddbits ( $ n ) { return $ n | getevenbits ( $ n ) ; } $ n = 10 ; echo setalloddbits ( $ n ) ; ? >


"Set all odd bits of a number | set all odd bit ; res for store 010101. . number ; generate number form of 010101. . . till temp size ; if bit is odd , then generate number and or with res ; Driver code"
How can the above be solved in PHP?

< ? php function oddbitsetnumber ( $ n ) { $ count = 0 ; $ res = 0 ; for ( $ temp = $ n ; $ temp > 0 ; $ temp >>= 1 ) { if ( $ count % 2 == 0 ) $ res |= ( 1 << $ count ) ; $ count ++ ; } return ( $ n $ res ) ; } $ n = 10 ; echo oddbitsetnumber ( $ n ) ; ? >


"Set all the bits in given range of a number | function to toggle bits in the given range ; calculating a number ' range ' having set bits in the range from l to r and all other bits as 0 ( or unset ) . ; Driver code"
How can the above be solved in PHP?

< ? php function setallbitgivenrange ( $ n , $ l , $ r ) { $ range = ( ( ( 1 << ( $ l - 1 ) ) - 1 ) ^ ( ( 1 << ( $ r ) ) - 1 ) ) ; return ( $ n $ range ) ; } $ n = 17 ; $ l = 2 ; $ r = 3 ; echo setallbitgivenrange ( $ n , $ l , $ r ) ; ? >


"Set the K | function to set the kth bit ; kth bit of n is being set by this operation ; Driver Code"
How can the above be solved in PHP?

< ? php function setKthBit ( $ n , $ k ) { return ( ( 1 << $ k ) $ n ) ; } $ n = 10 ; $ k = 2 ; echo " Kth ▁ bit ▁ set ▁ number ▁ = ▁ " , setKthBit ( $ n , $ k ) ; ? >


"Set the Left most unset bit | set left most unset bit ; if number contain all 1 then return n ; Find position of leftmost unset bit . ; if temp L . S . B is zero then unset bit pos is change ; return OR of number and unset bit pos ; Driver code"
How can the above be solved in PHP?

< ? php function setleftmostunsetbit ( $ n ) { if ( ( $ n & ( $ n + 1 ) ) == 0 ) return $ n ; $ pos = 0 ; for ( $ temp = $ n , $ count = 0 ; $ temp > 0 ; $ temp >>= 1 , $ count ++ ) if ( ( $ temp & 1 ) == 0 ) $ pos = $ count ; return ( $ n | ( 1 << ( $ pos ) ) ) ; } $ n = 10 ; echo setleftmostunsetbit ( $ n ) ; ? >


"Set the rightmost off bit | PHP program to set the rightmost unset bit ; If all bits are set ; Set rightmost 0 bit ; Driver Code"
How can the above be solved in PHP?

< ? php function setRightmostUnsetBit ( $ n ) { if ( ( $ n & ( $ n + 1 ) ) == 0 ) return $ n ; return $ n | ( $ n + 1 ) ; } $ n = 21 ; echo setRightmostUnsetBit ( $ n ) ; ? >


"Shell | Function to sort arr [ ] using Shell Metzner sort ; Set initial step size to the size of the array ; Step size decreases by half each time ; k is the upper limit for j ; j is the starting point ; i equals to smaller value ; l equals to larger value ; Compare and swap arr [ i ] with arr [ l ] ; Decrease smaller value by step size ; Increment the lower limit of i ; Function to print the contents of an array ; Driver code ; Sort the array using Shell Metzner Sort ; Print the sorted array"
How can the above be solved in PHP?

< ? php function sort_shell_metzner ( $ arr , $ n ) { $ m = $ n ; while ( $ m > 0 ) { $ m = $ m / 2 ; $ k = $ n - $ m ; $ j = 0 ; do { $ i = $ j ; do { $ l = $ i + $ m ; if ( $ arr [ $ i ] > $ arr [ $ l ] ) { $ temp = $ arr [ $ i ] ; $ arr [ $ i ] = $ arr [ $ l ] ; $ arr [ $ l ] = $ temp ; $ i -= $ m ; } else break ; } while ( $ i >= 0 ) ; $ j ++ ; } while ( $ j <= $ k ) ; } return $ arr ; } function printArray ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] , " ▁ " ; } $ arr = array ( 0 , -2 , 8 , 5 , 1 ) ; $ n = count ( $ arr ) ; $ result_array = sort_shell_metzner ( $ arr , $ n ) ; printArray ( $ result_array , $ n ) ; ? >


"Shortest Common Supersequence | A Naive recursive PHP program to find length of the shortest supersequence ; Driver Code"
How can the above be solved in PHP?

< ? php function superSeq ( $ X , $ Y , $ m , $ n ) { if ( ! $ m ) return $ n ; if ( ! $ n ) return $ m ; if ( $ X [ $ m - 1 ] == $ Y [ $ n - 1 ] ) return 1 + superSeq ( $ X , $ Y , $ m - 1 , $ n - 1 ) ; return 1 + min ( superSeq ( $ X , $ Y , $ m - 1 , $ n ) , superSeq ( $ X , $ Y , $ m , $ n - 1 ) ) ; } $ X = " AGGTAB " ; $ Y = " GXTXAYB " ; echo " Length ▁ of ▁ the ▁ shortest ▁ supersequence ▁ is ▁ " , superSeq ( $ X , $ Y , strlen ( $ X ) , strlen ( $ Y ) ) ; ? >


"Shortest Common Supersequence | Returns length of the shortest supersequence of X and Y ; Fill table in bottom up manner ; Below steps follow above recurrence ; Driver Code"
How can the above be solved in PHP?

< ? php function superSeq ( $ X , $ Y , $ m , $ n ) { $ dp = array_fill ( 0 , $ m + 1 , array_fill ( 0 , $ n + 1 , 0 ) ) ; for ( $ i = 0 ; $ i <= $ m ; $ i ++ ) { for ( $ j = 0 ; $ j <= $ n ; $ j ++ ) { if ( ! $ i ) $ dp [ $ i ] [ $ j ] = $ j ; else if ( ! $ j ) $ dp [ $ i ] [ $ j ] = $ i ; else if ( $ X [ $ i - 1 ] == $ Y [ $ j - 1 ] ) $ dp [ $ i ] [ $ j ] = 1 + $ dp [ $ i - 1 ] [ $ j - 1 ] ; else $ dp [ $ i ] [ $ j ] = 1 + min ( $ dp [ $ i - 1 ] [ $ j ] , $ dp [ $ i ] [ $ j - 1 ] ) ; } } return $ dp [ $ m ] [ $ n ] ; } $ X = " AGGTAB " ; $ Y = " GXTXAYB " ; echo " Length ▁ of ▁ the ▁ shortest ▁ supersequence ▁ is ▁ " . superSeq ( $ X , $ Y , strlen ( $ X ) , strlen ( $ Y ) ) ; ? >


"Shortest Un | bool function for checking an array elements are in increasing . ; bool function for checking an array elements are in decreasing . ; increasing and decreasing are two functions . if function return true value then print 0 otherwise 3. ; Driver code"
How can the above be solved in PHP?

< ? php function increasing ( $ a , $ n ) { for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) if ( $ a [ $ i ] >= $ a [ $ i + 1 ] ) return false ; return true ; } function decreasing ( $ a , $ n ) { for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) if ( $ a [ $ i ] < $ a [ $ i + 1 ] ) return false ; return true ; } function shortestUnsorted ( $ a , $ n ) { if ( increasing ( $ a , $ n ) == true || decreasing ( $ a , $ n ) == true ) return 0 ; else return 3 ; } $ ar = array ( 7 , 9 , 10 , 8 , 11 ) ; $ n = sizeof ( $ ar ) ; echo shortestUnsorted ( $ ar , $ n ) ; ? >


"Shortest Uncommon Subsequence | A dynamic programming based PHP program to find shortest uncommon subsequence . ; Returns length of shortest common subsequence ; declaring 2D array of m + 1 rows and n + 1 columns dynamically ; T string is empty ; S string is empty ; char not present in T ; Driver Code"
How can the above be solved in PHP?

< ? php $ GLOBALS [ ' MAX ' ] = 1005 ; function shortestSeq ( $ S , $ T ) { $ m = strlen ( $ S ) ; $ n = strlen ( $ T ) ; $ dp = array ( array ( ) ) ; for ( $ i = 0 ; $ i <= $ m ; $ i ++ ) $ dp [ $ i ] [ 0 ] = 1 ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) $ dp [ 0 ] [ $ i ] = $ GLOBALS [ ' MAX ' ] ; for ( $ i = 1 ; $ i <= $ m ; $ i ++ ) { for ( $ j = 1 ; $ j <= $ n ; $ j ++ ) { $ ch = $ S [ $ i - 1 ] ; for ( $ k = $ j - 1 ; $ k >= 0 ; $ k -- ) if ( $ T [ $ k ] == $ ch ) break ; if ( $ k == -1 ) $ dp [ $ i ] [ $ j ] = 1 ; else $ dp [ $ i ] [ $ j ] = min ( $ dp [ $ i - 1 ] [ $ j ] , $ dp [ $ i - 1 ] [ $ k ] + 1 ) ; } } $ ans = $ dp [ $ m ] [ $ n ] ; if ( $ ans >= $ GLOBALS [ ' MAX ' ] ) $ ans = -1 ; return $ ans ; } $ S = " babab " ; $ T = " babba " ; $ m = strlen ( $ S ) ; $ n = strlen ( $ T ) ; echo " Length ▁ of ▁ shortest ▁ subsequence ▁ is ▁ : ▁ " , shortestSeq ( $ S , $ T ) ; ? >


"Shortest distance between a point and a circle | Function to find the shortest distance ; Driver code"
How can the above be solved in PHP?

< ? php function dist ( $ x1 , $ y1 , $ x2 , $ y2 , $ r ) { echo " The ▁ shortest ▁ distance ▁ between ▁ a ▁ point ▁ and ▁ a ▁ circle ▁ is ▁ " , sqrt ( ( pow ( ( $ x2 - $ x1 ) , 2 ) ) + ( pow ( ( $ y2 - $ y1 ) , 2 ) ) ) - $ r ; } $ x1 = 4 ; $ y1 = 6 ; $ x2 = 35 ; $ y2 = 42 ; $ r = 5 ; dist ( $ x1 , $ y1 , $ x2 , $ y2 , $ r ) ; ? >


"Shuffle 2 n integers as a1 | function to rearrange the array ; if size is null or odd return because it is not possible to rearrange ; start from the middle index ; each time we will set two elements from the start to the valid position by swapping ; Driver Code"
How can the above be solved in PHP?

< ? php function rearrange ( & $ arr , $ n ) { if ( $ arr == NULL $ n % 2 == 1 ) return ; $ currIdx = intval ( ( $ n - 1 ) / 2 ) ; while ( $ currIdx > 0 ) { $ count = $ currIdx ; $ swapIdx = $ currIdx ; while ( $ count -- > 0 ) { $ temp = $ arr [ $ swapIdx + 1 ] ; $ arr [ $ swapIdx + 1 ] = $ arr [ $ swapIdx ] ; $ arr [ $ swapIdx ] = $ temp ; $ swapIdx ++ ; } $ currIdx -- ; } } $ arr = array ( 1 , 3 , 5 , 2 , 4 , 6 ) ; $ n = count ( $ arr ) ; rearrange ( $ arr , $ n ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo ( $ arr [ $ i ] . " ▁ " ) ; ? >


"Slope of the line parallel to the line with the given slope | Function to return the slope of the line which is parallel to the line with the given slope ; Driver code"
How can the above be solved in PHP?

< ? php function getSlope ( $ m ) { return $ m ; } $ m = 2 ; echo getSlope ( $ m ) ; ? >


"Smallest Derangement of Sequence | Efficient PHP program to find smallest derangement . ; Generate Sequence S ; Generate Derangement ; Only if i is odd Swap S [ N - 1 ] and S [ N ] ; Print Derangement ; Driver Program"
How can the above be solved in PHP?


"Smallest Difference Triplet from Three arrays | function to find maximum number ; function to find minimum number ; Finds and prints the smallest Difference Triplet ; sorting all the three arrays ; To store resultant three numbers ; pointers to arr1 , arr2 , arr3 respectively ; Loop until one array reaches to its end Find the smallest difference . ; maximum number ; Find minimum and increment its index . ; comparing new difference with the previous one and updating accordingly ; Print result ; Driver Code"
How can the above be solved in PHP?

< ? php function maximum ( $ a , $ b , $ c ) { return max ( max ( $ a , $ b ) , $ c ) ; } function minimum ( $ a , $ b , $ c ) { return min ( min ( $ a , $ b ) , $ c ) ; } function smallestDifferenceTriplet ( $ arr1 , $ arr2 , $ arr3 , $ n ) { sort ( $ arr1 ) ; sort ( $ arr2 ) ; sort ( $ arr3 ) ; $ res_min ; $ res_max ; $ res_mid ; $ i = 0 ; $ j = 0 ; $ k = 0 ; $ diff = PHP_INT_MAX ; while ( $ i < $ n && $ j < $ n && $ k < $ n ) { $ sum = $ arr1 [ $ i ] + $ arr2 [ $ j ] + $ arr3 [ $ k ] ; $ max = maximum ( $ arr1 [ $ i ] , $ arr2 [ $ j ] , $ arr3 [ $ k ] ) ; $ min = minimum ( $ arr1 [ $ i ] , $ arr2 [ $ j ] , $ arr3 [ $ k ] ) ; if ( $ min == $ arr1 [ $ i ] ) $ i ++ ; else if ( $ min == $ arr2 [ $ j ] ) $ j ++ ; else $ k ++ ; if ( $ diff > ( $ max - $ min ) ) { $ diff = $ max - $ min ; $ res_max = $ max ; $ res_mid = $ sum - ( $ max + $ min ) ; $ res_min = $ min ; } } echo $ res_max , " , ▁ " , $ res_mid , " , ▁ " , $ res_min ; } $ arr1 = array ( 5 , 2 , 8 ) ; $ arr2 = array ( 10 , 7 , 12 ) ; $ arr3 = array ( 9 , 14 , 6 ) ; $ n = sizeof ( $ arr1 ) ; smallestDifferenceTriplet ( $ arr1 , $ arr2 , $ arr3 , $ n ) ; ? >


"Smallest Difference pair of values between two unsorted Arrays | function to calculate Small result between two arrays ; Sort both arrays using sort function ; Initialize result as max value ; Scan Both Arrays upto sizeof of the Arrays ; Move Smaller Value ; return final sma result ; Driver Code ; Input given array A ; Input given array B ; Calculate size of Both arrays ; Call function to print smallest result"
How can the above be solved in PHP?

< ? php function findSmallestDifference ( $ A , $ B , $ m , $ n ) { sort ( $ A ) ; sort ( $ A , $ m ) ; sort ( $ B ) ; sort ( $ B , $ n ) ; $ a = 0 ; $ b = 0 ; $ INT_MAX = 1 ; $ result = $ INT_MAX ; while ( $ a < $ m && $ b < $ n ) { if ( abs ( $ A [ $ a ] - $ B [ $ b ] ) < $ result ) $ result = abs ( $ A [ $ a ] - $ B [ $ b ] ) ; if ( $ A [ $ a ] < $ B [ $ b ] ) $ a ++ ; else $ b ++ ; } return $ result ; } { $ A = array ( 1 , 2 , 11 , 5 ) ; $ B = array ( 4 , 12 , 19 , 23 , 127 , 235 ) ; $ m = sizeof ( $ A ) / sizeof ( $ A [ 0 ] ) ; $ n = sizeof ( $ B ) / sizeof ( $ B [ 0 ] ) ; echo findSmallestDifference ( $ A , $ B , $ m , $ n ) ; return 0 ; } ? >


"Smallest Even number with N digits | Function to return smallest even number with n digits ; Driver Code"
How can the above be solved in PHP?

< ? php function smallestEven ( $ n ) { if ( $ n == 1 ) return 0 ; return pow ( 10 , $ n - 1 ) ; } $ n = 4 ; echo smallestEven ( $ n ) ; ? >


"Smallest Integer to be inserted to have equal sums | Function to find the minimum value to be added ; Variable to store entire array sum ; Variables to store sum of subarray1 and subarray 2 ; minimum value to be added ; Traverse through the array ; Sum of both halves ; Calculate minimum number to be added ; Driver code ; Length of array"
How can the above be solved in PHP?

< ? php function findMinEqualSums ( $ a , $ N ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { $ sum += $ a [ $ i ] ; } $ sum1 = 0 ; $ sum2 = 0 ; $ min = PHP_INT_MAX ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { $ sum1 += $ a [ $ i ] ; $ sum2 = $ sum - $ sum1 ; if ( abs ( $ sum1 - $ sum2 ) < $ min ) { $ min = abs ( $ sum1 - $ sum2 ) ; } if ( $ min == 0 ) { break ; } } return $ min ; } $ a = array ( 3 , 2 , 1 , 5 , 7 , 8 ) ; $ N = count ( $ a ) ; echo ( findMinEqualSums ( $ a , $ N ) ) ; ? >


"Smallest N digit number which is a multiple of 5 | Function to return the smallest n digit number which is a multiple of 5 ; Driver code"
How can the above be solved in PHP?

< ? php function smallestMultiple ( $ n ) { if ( $ n == 1 ) return 5 ; return pow ( 10 , $ n - 1 ) ; } $ n = 4 ; echo smallestMultiple ( $ n ) ; ? >


"Smallest Pair Sum in an array | Function to return the sum of the minimum pair from the array ; If found new minimum ; Minimum now becomes second minimum ; Update minimum ; If current element is > min and < secondMin ; Update secondMin ; Return the sum of the minimum pair ; Driver code"
How can the above be solved in PHP?

< ? php function smallest_pair ( $ a , $ n ) { $ min = PHP_INT_MAX ; $ secondMin = PHP_INT_MAX ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ a [ $ j ] < $ min ) { $ secondMin = $ min ; $ min = $ a [ $ j ] ; } else if ( ( $ a [ $ j ] < $ secondMin ) && $ a [ $ j ] != $ min ) $ secondMin = $ a [ $ j ] ; } return ( $ secondMin + $ min ) ; } $ arr = array ( 1 , 2 , 3 ) ; $ n = sizeof ( $ arr ) ; echo smallest_pair ( $ arr , $ n ) ; ? >


"Smallest Palindrome after replacement | Utility method to check str is possible palindrome after ignoring ; If both left and right character are not dot and they are not equal also , then it is not possible to make this string a palindrome ; Returns lexicographically smallest palindrome string , if possible ; loop through character of string ; if one of character is dot , replace dot with other character ; if both character are dot , then replace them with smallest character ' a ' ; return the result ; Driver code"
How can the above be solved in PHP?

< ? php function isPossiblePalindrome ( $ str ) { $ n = strlen ( $ str ) ; for ( $ i = 0 ; $ i < $ n / 2 ; $ i ++ ) { if ( $ str [ $ i ] != ' . ' && $ str [ $ n - $ i - 1 ] != ' . ' && $ str [ $ i ] != $ str [ $ n - $ i - 1 ] ) return false ; } return true ; } function smallestPalindrome ( $ str ) { if ( ! isPossiblePalindrome ( $ str ) ) return " Not ▁ Possible " ; $ n = strlen ( $ str ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ str [ $ i ] == ' . ' ) { if ( $ str [ $ n - $ i - 1 ] != ' . ' ) $ str [ $ i ] = $ str [ $ n - $ i - 1 ] ; else $ str [ $ i ] = $ str [ $ n - $ i - 1 ] = ' a ' ; } } return $ str ; } $ str = " ab . . e . c . a " ; echo smallestPalindrome ( $ str ) ; ? >


"Smallest Special Prime which is greater than or equal to a given number | Function to check whether the number is a special prime or not ; While number is not equal to zero ; If the number is not prime return false . ; Else remove the last digit by dividing the number by 10. ; If the number has become zero then the number is special prime , hence return true ; Function to find the Smallest Special Prime which is greater than or equal to a given number ; Initially all numbers are considered Primes . ; There is always an answer possible ; Checking if the number is a special prime or not ; If yes print the number and break the loop . ; Else increment the number . ; Driver code"
How can the above be solved in PHP?

 < ? php function checkSpecialPrime ( $ sieve , $ num ) { while ( $ num ) { if ( ! $ sieve [ $ num ] ) { return false ; } $ num = floor ( $ num / 10 ) ; } return true ; } function findSpecialPrime ( $ N ) { $ sieve = array_fill ( 0 , $ N * 10 , true ) ; $ sieve [ 0 ] = $ sieve [ 1 ] = false ; for ( $ i = 2 ; $ i <= $ N * 10 ; $ i ++ ) { if ( $ sieve [ $ i ] ) { for ( $ j = $ i * $ i ; $ j <= $ N * 10 ; $ j += $ i ) { $ sieve [ $ j ] = false ; } } } while ( true ) { if ( checkSpecialPrime ( $ sieve , $ N ) ) { echo $ N , "
 " ; break ; } else $ N ++ ; } } $ N = 379 ; findSpecialPrime ( $ N ) ; $ N = 100 ; findSpecialPrime ( $ N ) ; ? >


"Smallest and Largest N | Function to print the largest and the smallest n - digit perfect cube ; Smallest n - digit perfect cube ; Largest n - digit perfect cube ; Driver code"
How can the above be solved in PHP?

< ? php function nDigitPerfectCubes ( $ n ) { print ( pow ( ceil ( pow ( pow ( 10 , ( $ n - 1 ) ) , 1 / 3 ) ) , 3 ) . " ▁ " ) ; print ( ( int ) pow ( ceil ( pow ( pow ( 10 , ( $ n ) ) , 1 / 3 ) ) - 1 , 3 ) ) ; } $ n = 3 ; nDigitPerfectCubes ( $ n ) ; ? >


"Smallest and Largest N | Function to print the largest and the smallest n - digit perfect squares ; Smallest n - digit perfect square ; Largest n - digit perfect square ; Driver code"
How can the above be solved in PHP?

< ? php function nDigitPerfectSquares ( $ n ) { echo pow ( ceil ( sqrt ( pow ( 10 , $ n - 1 ) ) ) , 2 ) , " " ; echo pow ( ceil ( sqrt ( pow ( 10 , $ n ) ) ) - 1 , 2 ) ; } $ n = 4 ; nDigitPerfectSquares ( $ n ) ; ? >


"Smallest and Largest Palindrome with N Digits | Function to print the smallest and largest palindrome with N digits ; Driver Code"
How can the above be solved in PHP?

 < ? php function printPalindrome ( $ n ) { if ( $ n == 1 ) { echo " Smallest ▁ Palindrome : ▁ 0
 " ; echo " Largest ▁ Palindrome : ▁ 9" ; } else { echo " Smallest ▁ Palindrome : ▁ " , pow ( 10 , $ n - 1 ) + 1 ; echo " Largest Palindrome : " pow ( 10 , $ n ) - 1 ; } } $ n = 4 ; printPalindrome ( $ n ) ; ? >


"Smallest and Largest sum of two n | Function to return the smallest sum of 2 n - digit numbers ; Function to return the largest sum of 2 n - digit numbers ; Driver code"
How can the above be solved in PHP?

 < ? php function smallestSum ( $ n ) { if ( $ n == 1 ) return 0 ; return ( 2 * pow ( 10 , $ n - 1 ) ) ; } function largestSum ( $ n ) { return 2 * ( pow ( 10 , $ n ) - 1 ) ; } $ n = 4 ; echo " Largest = " ▁ . ▁ largestSum ( $ n ) ▁ . ▁ " " ;
 echo ▁ " Smallest = " ? >


"Smallest divisor D of N such that gcd ( D , M ) is greater than 1 | PHP implementation of the above approach ; Function to find the minimum divisor ; Iterate for all factors of N ; Check for gcd > 1 ; Check for gcd > 1 ; If gcd is m itself ; Driver code"
How can the above be solved in PHP?

< ? php function __gcd ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; return __gcd ( $ b , $ a % $ b ) ; } function findMinimum ( $ n , $ m ) { $ mini = $ m ; for ( $ i = 1 ; $ i * $ i <= $ n ; $ i ++ ) { if ( $ n % $ i == 0 ) { $ sec = $ n / $ i ; if ( __gcd ( $ m , $ i ) > 1 ) { return $ i ; } else if ( __gcd ( $ sec , $ m ) > 1 ) { $ mini = min ( $ sec , $ mini ) ; } } } if ( $ mini == $ m ) return -1 ; else return $ mini ; } $ n = 8 ; $ m = 10 ; echo ( findMinimum ( $ n , $ m ) ) ;


"Smallest element greater than X not present in the array | Function to return the smallest element greater than x which is not present in a [ ] ; Sort the array ; Continue until low is less than or equals to high ; Find mid ; If element at mid is less than or equals to searching element ; If mid is equals to searching element ; Increment searching element ; Make high as N - 1 ; Make low as mid + 1 ; Make high as mid - 1 ; Return the next greater element ; Driver code"
How can the above be solved in PHP?

< ? php function Next_greater ( $ a , $ n , $ x ) { sort ( $ a ) ; $ low = 0 ; $ high = $ n - 1 ; $ ans = $ x + 1 ; while ( $ low <= $ high ) { $ mid = ( $ low + $ high ) / 2 ; if ( $ a [ $ mid ] <= $ ans ) { if ( $ a [ $ mid ] == $ ans ) { $ ans ++ ; $ high = $ n - 1 ; } $ low = $ mid + 1 ; } else $ high = $ mid - 1 ; } return $ ans ; } $ a = array ( 1 , 5 , 10 , 4 , 7 ) ; $ x = 4 ; $ n = count ( $ a ) ; echo Next_greater ( $ a , $ n , $ x ) ; ? >


"Smallest element in an array that is repeated exactly ' k ' times . | PHP program to find smallest number in array that is repeated exactly ' k ' times . ; Computing frequencies of all elements ; Finding the smallest element with frequency as k ; If frequency of any of the number is equal to k starting from 0 then return the number ; Driver code"
How can the above be solved in PHP?

< ? php $ MAX = 1000 ; function findDuplicate ( $ arr , $ n , $ k ) { global $ MAX ; $ freq = array_fill ( 0 , $ MAX , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] < 1 && $ arr [ $ i ] > $ MAX ) { echo " Out ▁ of ▁ range " ; return -1 ; } $ freq [ $ arr [ $ i ] ] += 1 ; } for ( $ i = 0 ; $ i < $ MAX ; $ i ++ ) { if ( $ freq [ $ i ] == $ k ) return $ i ; } return -1 ; } $ arr = array ( 2 , 2 , 1 , 3 , 1 ) ; $ k = 2 ; $ n = count ( $ arr ) ; echo findDuplicate ( $ arr , $ n , $ k ) ; ? >


"Smallest element in an array that is repeated exactly ' k ' times . | PHP program to find smallest number in array that is repeated exactly ' k ' times . ; finds the smallest number in arr that is repeated k times ; Computing frequencies of all elements ; Finding the smallest element with frequency as k ; If frequency of any of the number is equal to k starting from 0 then return the number ; Driver code"
How can the above be solved in PHP?


"Smallest element in an array that is repeated exactly ' k ' times . | finds the smallest number in arr [ ] that is repeated k times ; Since arr [ ] has numbers in range from 1 to MAX ; set count to 1 as number is present once ; If frequency of number is equal to ' k ' ; Driver code"
How can the above be solved in PHP?

< ? php function findDuplicate ( $ arr , $ n , $ k ) { $ MAX = 1000 ; $ res = $ MAX + 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] > 0 ) { $ count = 1 ; for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) if ( $ arr [ $ i ] == $ arr [ $ j ] ) $ count += 1 ; if ( $ count == $ k ) $ res = min ( $ res , $ arr [ $ i ] ) ; } } return $ res ; } $ arr = array ( 2 , 2 , 1 , 3 , 1 ) ; $ k = 2 ; $ n = count ( $ arr ) ; echo findDuplicate ( $ arr , $ n , $ k ) ; ? >


"Smallest element in an array that is repeated exactly ' k ' times . | finds the smallest number in arr [ ] that is repeated k times ; Sort the array ; Find the first element with exactly k occurrences . ; Driver code"
How can the above be solved in PHP?

< ? php function findDuplicate ( $ arr , $ n , $ k ) { sort ( $ arr ) ; $ i = 0 ; while ( $ i < $ n ) { $ j ; $ count = 1 ; for ( $ j = $ i + 1 ; $ j < $ n && $ arr [ $ j ] == $ arr [ $ i ] ; $ j ++ ) $ count ++ ; if ( $ count == $ k ) return $ arr [ $ i ] ; $ i = $ j ; } return -1 ; } $ arr = array ( 2 , 2 , 1 , 3 , 1 ) ; $ k = 2 ; $ n = sizeof ( $ arr ) ; echo ( findDuplicate ( $ arr , $ n , $ k ) ) ; ? >


"Smallest even digits number not less than N | function to check if all digits are even of a given number ; iterate for all digits ; if digit is odd ; all digits are even ; function to return the smallest number with all digits even ; iterate till we find a number with all digits even ; Driver Code"
How can the above be solved in PHP?

< ? php function check_digits ( $ n ) { while ( $ n ) { if ( ( $ n % 10 ) % 2 ) return 0 ; $ n /= 10 ; } return 1 ; } function smallest_number ( $ n ) { for ( $ i = $ n ; ; $ i ++ ) if ( check_digits ( $ i ) ) return $ i ; } $ N = 2397 ; echo smallest_number ( $ N ) ; ? >


"Smallest even digits number not less than N | function to return the answer when the first odd digit is 9 ; traverse towwars the left to find the non - 8 digit ; index digit ; if digit is not 8 , then break ; if on the left side of the '9' , no 8 is found then we return by adding a 2 and 0 's ; till non - 8 digit add all numbers ; if non - 8 is even or odd than add the next even . ; add 0 to right of 9 ; function to return the smallest number with all digits even ; convert the number to string to perform operations ; find out the first odd number ; if no odd numbers are there , than n is the answer ; if the odd number is 9 , than tricky case handles it ; add all digits till first odd ; increase the odd digit by 1 ; add 0 to the right of the odd number ; Driver Code"
How can the above be solved in PHP?

< ? php function trickyCase ( $ s , $ index ) { $ index1 = -1 ; for ( $ i = $ index - 1 ; $ i >= 0 ; $ i -- ) { $ digit = $ s [ $ i ] - '0' ; if ( $ digit != 8 ) { $ index1 = $ i ; break ; } } if ( $ index1 == -1 ) return 2 * pow ( 10 , strlen ( $ s ) ) ; $ num = 0 ; for ( $ i = 0 ; $ i < $ index1 ; $ i ++ ) $ num = $ num * 10 + ( $ s [ $ i ] - '0' ) ; if ( $ s [ $ index1 ] % 2 == 0 ) $ num = $ num * 10 + ( $ s [ $ index1 ] - '0' + 2 ) ; else $ num = $ num * 10 + ( $ s [ $ index1 ] - '0' + 1 ) ; for ( $ i = $ index1 + 1 ; $ i < strlen ( $ s ) ; $ i ++ ) $ num = $ num * 10 ; return $ num ; } function smallestNumber ( $ n ) { $ num = 0 ; $ s = " " ; $ duplicate = $ n ; while ( $ n ) { $ s = chr ( $ n % 10 + 48 ) . $ s ; $ n = ( int ) ( $ n / 10 ) ; } $ index = -1 ; for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) { $ digit = $ s [ $ i ] - '0' ; if ( $ digit & 1 ) { $ index = $ i ; break ; } } if ( $ index == -1 ) return $ duplicate ; if ( $ s [ $ index ] == '9' ) { $ num = trickyCase ( $ s , $ index ) ; return $ num ; } for ( $ i = 0 ; $ i < $ index ; $ i ++ ) $ num = $ num * 10 + ( $ s [ $ i ] - '0' ) ; $ num = $ num * 10 + ( $ s [ $ index ] - '0' + 1 ) ; for ( $ i = $ index + 1 ; $ i < strlen ( $ s ) ; $ i ++ ) $ num = $ num * 10 ; return $ num ; } $ N = 2397 ; echo smallestNumber ( $ N ) ; ? >


"Smallest greater elements in whole array | Simple PHP program to find smallest greater element in whole array for every element . ; Find the closest greater element for arr [ j ] in the entire array . ; Check if arr [ i ] is largest ; Driver code"
How can the above be solved in PHP?

< ? php function smallestGreater ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ diff = PHP_INT_MAX ; $ closest = -1 ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ arr [ $ i ] < $ arr [ $ j ] && $ arr [ $ j ] - $ arr [ $ i ] < $ diff ) { $ diff = $ arr [ $ j ] - $ arr [ $ i ] ; $ closest = $ j ; } } if ( $ closest == -1 ) echo " _ ▁ " ; else echo $ arr [ $ closest ] , " ▁ " ; } } $ ar = array ( 6 , 3 , 9 , 8 , 10 , 2 , 1 , 15 , 7 ) ; $ n = sizeof ( $ ar ) ; smallestGreater ( $ ar , $ n ) ; ? >


"Smallest integer which has n factors or more | PHP program to print the smallest integer with n factors or more ; array to store prime factors ; function to generate all prime factors of numbers from 1 to 10 ^ 6 ; Initializes all the positions with their value . ; Initializes all multiples of 2 with 2 ; A modified version of Sieve of Eratosthenes to store the smallest prime factor that divides every number . ; check if it has no prime factor . ; Initializes of j starting from i * i ; if it has no prime factor before , then stores the smallest prime divisor ; function to calculate number of factors ; stores the smallest prime number that divides n ; stores the count of number of times a prime number divides n . ; reduces to the next number after prime factorization of n ; false when prime factorization is done ; if the same prime number is dividing n , then we increase the count ; if its a new prime factor that is factorizing n , then we again set c = 1 and change dup to the new prime factor , and apply the formula explained above . ; prime factorizes a number ; for the last prime factor ; function to find the smallest integer with n factors or more . ; check if no of factors is more than n or not ; generate prime factors of number upto 10 ^ 6"
How can the above be solved in PHP?

< ? php $ MAX = 100001 ; $ factor = array_fill ( 0 , $ MAX , 0 ) ; function generatePrimeFactors ( ) { global $ MAX ; global $ factor ; $ factor [ 1 ] = 1 ; for ( $ i = 2 ; $ i < $ MAX ; $ i ++ ) $ factor [ $ i ] = $ i ; for ( $ i = 4 ; $ i < $ MAX ; $ i += 2 ) $ factor [ $ i ] = 2 ; for ( $ i = 3 ; $ i * $ i < $ MAX ; $ i ++ ) { if ( $ factor [ $ i ] == $ i ) { for ( $ j = $ i * $ i ; $ j < $ MAX ; $ j += $ i ) { if ( $ factor [ $ j ] == $ j ) $ factor [ $ j ] = $ i ; } } } } function calculateNoOFactors ( $ n ) { global $ factor ; if ( $ n == 1 ) return 1 ; $ ans = 1 ; $ dup = $ factor [ $ n ] ; $ c = 1 ; $ j = ( int ) ( $ n / $ factor [ $ n ] ) ; while ( $ j != 1 ) { if ( $ factor [ $ j ] == $ dup ) $ c += 1 ; else { $ dup = $ factor [ $ j ] ; $ ans = $ ans * ( $ c + 1 ) ; $ c = 1 ; } $ j = ( int ) ( $ j / $ factor [ $ j ] ) ; } $ ans = $ ans * ( $ c + 1 ) ; return $ ans ; } function smallest ( $ n ) { for ( $ i = 1 ; ; $ i ++ ) if ( calculateNoOFactors ( $ i ) >= $ n ) return $ i ; } generatePrimeFactors ( ) ; $ n = 4 ; echo smallest ( $ n ) ; ? >


"Smallest integer with digit sum M and multiple of N | Function to return digit sum ; Function to find out the smallest integer ; Start of the iterator ( Smallest multiple of n ) ; Driver code"
How can the above be solved in PHP?

 < ? php function digitSum ( $ n ) { $ ans = 0 ; while ( $ n ) { $ ans += $ n % 10 ; $ n /= 10 ; } return $ ans ; } function findInt ( $ n , $ m ) { $ minDigit = floor ( $ m / 9 ) ; $ start = pow ( 10 , $ minDigit ) - ( int ) pow ( 10 , $ minDigit ) % $ n ; while ( $ start < PHP_INT_MAX ) { if ( digitSum ( $ start ) == $ m ) return $ start ; else $ start += $ n ; } return -1 ; } $ n = 13 ; $ m = 32 ; echo findInt ( $ n , $ m ) ; # This  code is contributed by ajit.
? >


"Smallest length string with repeated replacement of two distinct adjacent | Returns smallest possible length with given operation allowed . ; Counint occurrences of three different characters ' a ' , ' b ' and ' c ' in str ; If all characters are same . ; If all characters are present even number of times or all are present odd number of times . ; Answer is 1 for all other cases . ; Driver code"
How can the above be solved in PHP?

< ? php function stringReduction ( $ str ) { $ n = strlen ( $ str ) ; $ count = array_fill ( 0 , 3 , 0 ) ; for ( $ i = 0 ; $ i < $ n ; ++ $ i ) $ count [ ord ( $ str [ $ i ] ) - ord ( ' a ' ) ] ++ ; if ( $ count [ 0 ] == $ n $ count [ 1 ] == $ n $ count [ 2 ] == $ n ) return $ n ; if ( ( $ count [ 0 ] % 2 ) == ( $ count [ 1 ] % 2 ) && ( $ count [ 1 ] % 2 ) == ( $ count [ 2 ] % 2 ) ) return 2 ; return 1 ; } $ str = " abcbbaacb " ; print ( stringReduction ( $ str ) ) ; ? >


"Smallest multiple of 3 which consists of three given non | Function to return the minimum number divisible by 3 formed by the given digits ; Sort the given array in ascending ; Check if any single digit is divisible by 3 ; Check if any two digit number formed by the given digits is divisible by 3 starting from the minimum ; Generate the two digit number ; If none of the above is true , we can form three digit number by taking a [ 0 ] three times . ; Driver code"
How can the above be solved in PHP?


"Smallest n digit number divisible by given three numbers |  ; LCM for x , y , z ; returns smallest n digit number divisible by x , y and z ; find the LCM ; find power of 10 for least number ; reminder after ; If smallest number itself divides lcm . ; add lcm - reminder number for next n digit number ; this condition check the n digit number is possible or not if it is possible it return the number else return 0 ; driver code ; if number is possible then it print the number"
How can the above be solved in PHP?

< ? php function gcd ( $ a , $ b ) { return ( $ a % $ b ) ? gcd ( $ b , $ a % $ b ) : $ b ; } function LCM ( $ x , $ y , $ z ) { $ ans = floor ( ( $ x * $ y ) / ( gcd ( $ x , $ y ) ) ) ; return floor ( ( $ z * $ ans ) / ( gcd ( $ ans , $ z ) ) ) ; } function findDivisible ( $ n , $ x , $ y , $ z ) { $ lcm = LCM ( $ x , $ y , $ z ) ; $ ndigitnumber = pow ( 10 , $ n - 1 ) ; $ reminder = $ ndigitnumber % $ lcm ; if ( $ reminder == 0 ) return $ ndigitnumber ; $ ndigitnumber += $ lcm - $ reminder ; if ( $ ndigitnumber < pow ( 10 , $ n ) ) return $ ndigitnumber ; else return 0 ; } $ n = 4 ; $ x = 2 ; $ y = 3 ; $ z = 5 ; $ res = findDivisible ( $ n , $ x , $ y , $ z ) ; if ( $ res != 0 ) echo $ res ; else echo " Not ▁ possible " ; ? >


"Smallest number divisible by first n numbers | Function returns the lcm of first n numbers ; Driver Code"
How can the above be solved in PHP?

< ? php function lcm ( $ n ) { $ ans = 1 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ ans = ( $ ans * $ i ) / ( gmp_gcd ( strval ( ans ) , strval ( i ) ) ) ; return $ ans ; } $ n = 20 ; echo lcm ( $ n ) ; ? >


"Smallest number greater than or equal to N divisible by K | Function to find the smallest number greater than or equal to N that is divisible by k ; Driver code"
How can the above be solved in PHP?

 < ? php function findNum ( $ N , $ K ) { $ rem = ( $ N + $ K ) % $ K ; if ( $ rem == 0 ) return $ N ; else return $ N + $ K - $ rem ; } $ N = 45 ; $ K = 6 ; echo " Smallest ▁ number ▁ greater ▁ than ▁ " . " or ▁ equal ▁ to ▁ " , $ N ; echo " that is divisible by " ▁ , ▁ $ K ▁ ,
 	 	 	 " is " ? >


"Smallest number k such that the product of digits of k is equal to n | function to find smallest number k such that the product of digits of k is equal to n ; if ' n ' is a single digit number , then it is the required number ; stack the store the digits ; repeatedly divide ' n ' by the numbers from 9 to 2 until all the numbers are used or ' n ' > 1 ; save the digit ' i ' that divides ' n ' onto the stack ; if true , then no number ' k ' can be formed ; pop digits from the stack ' digits ' and add them to ' k ' ; required smallest number ; Driver code"
How can the above be solved in PHP?

< ? php function smallestNumber ( $ n ) { if ( $ n >= 0 && $ n <= 9 ) return $ n ; $ digits = array ( ) ; for ( $ i = 9 ; $ i >= 2 && $ n > 1 ; $ i -- ) { while ( $ n % $ i == 0 ) { array_push ( $ digits , $ i ) ; $ n = ( int ) ( $ n / $ i ) ; } } if ( $ n != 1 ) return -1 ; $ k = 0 ; while ( ! empty ( $ digits ) ) $ k = $ k * 10 + array_pop ( $ digits ) ; return $ k ; } $ n = 100 ; echo smallestNumber ( $ n ) ; ? >


"Smallest number to multiply to convert floating point to natural | Finding GCD of two number ; Returns smallest integer k such that k * str becomes natural . str is an input floating point number ; Find size of string representing a floating point number . ; Below is used to find denominator in fraction form . ; Used to find value of count_after_dot ; To find numerator in fraction form of given number . For example , for 30.25 , numerator would be 3025. ; If there was no dot , then number is already a natural . ; Find denominator in fraction form . For example , for 30.25 , denominator is 100 ; Result is denominator divided by GCD - of - numerator - and - denominator . For example , for 30.25 , result is 100 / GCD ( 3025 , 100 ) = 100 / 25 = 4 ; Driver Code"
How can the above be solved in PHP?

< ? php function gcd ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; return gcd ( $ b , $ a % $ b ) ; } function findnum ( $ str ) { $ n = strlen ( $ str ) ; $ count_after_dot = 0 ; $ dot_seen = false ; $ num = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ str [ $ i ] != ' . ' ) { $ num = $ num * 10 + ( $ str [ $ i ] - '0' ) ; if ( $ dot_seen == true ) $ count_after_dot ++ ; } else $ dot_seen = true ; } if ( $ dot_seen == false ) return 1 ; $ dem = pow ( 10 , $ count_after_dot ) ; return ( $ dem / gcd ( $ num , $ dem ) ) ; } { $ str = "5.125" ; echo findnum ( $ str ) ; return 0 ; } ? >


"Smallest number whose set bits are maximum in a given range | Returns smallest number whose set bits are maximum in given range . ; Driver code"
How can the above be solved in PHP?

 < ? php function countMaxSetBits ( $ left , $ right ) { while ( ( $ left | ( $ left + 1 ) ) <= $ right ) $ left |= $ left + 1 ; return $ left ; } $ l = 1 ; $ r = 5 ; echo countMaxSetBits ( $ l , $ r ) , "
 " ; $ l = 1 ; $ r = 10 ; echo countMaxSetBits ( $ l , $ r ) ; ? >


"Smallest number whose set bits are maximum in a given range | Returns smallest number whose set bits are maximum in given range . ; Initialize the maximum count and final answer as ' num ' ; Traverse for every bit of ' i ' number ; If count is greater than previous calculated max_count , update it ; Driver code"
How can the above be solved in PHP?

 < ? php function countMaxSetBits ( $ left , $ right ) { $ max_count = -1 ; $ num ; for ( $ i = $ left ; $ i <= $ right ; ++ $ i ) { $ temp = $ i ; $ cnt = 0 ; while ( $ temp ) { if ( $ temp & 1 ) ++ $ cnt ; $ temp >>= 1 ; } if ( $ cnt > $ max_count ) { $ max_count = $ cnt ; $ num = $ i ; } } return $ num ; } $ l = 1 ; $ r = 5 ; echo countMaxSetBits ( $ l , $ r ) , "
 " ; $ l = 1 ; $ r = 10 ; echo countMaxSetBits ( $ l , $ r ) ; ? >


"Smallest number with sum of digits as N and divisible by 10 ^ N | PHP program to find smallest number to find smallest number with N as sum of digits and divisible by 10 ^ N . ; If N = 0 the string will be 0 ; If n is not perfectly divisible by 9 output the remainder ; Print 9 N / 9 times ; Append N zero 's to the number so  as to make it divisible by 10^N ; Driver Code"
How can the above be solved in PHP?

 < ? php function digitsNum ( $ N ) { if ( $ N == 0 ) echo "0
 " ; if ( $ N % 9 != 0 ) echo ( $ N % 9 ) ; for ( $ i = 1 ; $ i <= ( $ N / 9 ) ; ++ $ i ) echo "9" ; for ( $ i = 1 ; $ i <= $ N ; ++ $ i ) echo "0" ; echo "
 " ; } $ N = 5 ; echo " The ▁ number ▁ is ▁ : ▁ " ; digitsNum ( $ N ) ; ? >


"Smallest odd digits number not less than N | function to check if all digits are odd of a given number ; iterate for all digits ; if digit is even ; all digits are odd ; function to return the smallest number with all digits odd ; iterate till we find a number with all digits odd ; Driver Code"
How can the above be solved in PHP?

< ? php function check_digits ( $ n ) { while ( $ n > 1 ) { if ( ( $ n % 10 ) % 2 == 0 ) return 0 ; $ n = ( int ) $ n / 10 ; } return 1 ; } function smallest_number ( $ n ) { for ( $ i = $ n ; ; $ i ++ ) if ( check_digits ( $ i ) ) return $ i ; } $ N = 2397 ; echo smallest_number ( $ N ) ; ? >


"Smallest odd number with N digits | Function to return smallest even number with n digits ; Driver Code"
How can the above be solved in PHP?

< ? php function smallestOdd ( $ n ) { if ( $ n == 1 ) return 1 ; return pow ( 10 , $ n - 1 ) + 1 ; } $ n = 4 ; echo smallestOdd ( $ n ) ; ? >


"Smallest of three integers without comparison operators | php program to find Smallest of three integers without comparison operators ; Driver code"
How can the above be solved in PHP?

< ? php function smallest ( $ x , $ y , $ z ) { $ c = 0 ; while ( $ x && $ y && $ z ) { $ x -- ; $ y -- ; $ z -- ; $ c ++ ; } return $ c ; } $ x = 12 ; $ y = 15 ; $ z = 5 ; echo " Minimum ▁ of ▁ 3 ▁ numbers ▁ is ▁ " . smallest ( $ x , $ y , $ z ) ; ? >


"Smallest perfect power of 2 greater than n ( without using arithmetic operators ) | Function to find smallest perfect power of 2 greater than n ; To store perfect power of 2 ; bitwise left shift by 1 ; bitwise right shift by 1 ; Required perfect power of 2 ; Driver code"
How can the above be solved in PHP?

< ? php function perfectPowerOf2 ( $ n ) { $ per_pow = 1 ; while ( $ n > 0 ) { $ per_pow = $ per_pow << 1 ; $ n = $ n >> 1 ; } return $ per_pow ; } $ n = 128 ; echo " Perfect ▁ power ▁ of ▁ 2 ▁ greater ▁ than ▁ " . $ n . " : ▁ " . perfectPowerOf2 ( $ n ) ; ? >


"Smallest prime divisor of a number | Function to find the smallest divisor ; if divisible by 2 ; iterate from 3 to sqrt ( n ) ; Driver Code"
How can the above be solved in PHP?

< ? php function smallestDivisor ( $ n ) { if ( $ n % 2 == 0 ) return 2 ; for ( $ i = 3 ; $ i * $ i <= $ n ; $ i += 2 ) { if ( $ n % $ i == 0 ) return $ i ; } return $ n ; } $ n = 31 ; echo smallestDivisor ( $ n ) ; ? >


"Smallest root of the equation x ^ 2 + s ( x ) * x | function to check if the sum of digits is equal to the summation assumed ; calculate the sum of digit ; function to find the largest root possible . ; iterate for all possible sum of digits . ; check if discriminent is a perfect square . ; check if discriminent is a perfect square and if it as perefect root of the equation ; function returns answer ; Driver Code"
How can the above be solved in PHP?


"Smallest square formed with given rectangles | Recursive function to return gcd of a and b ; Everything divides 0 ; Base case ; a is greater ; Function to find the area of the smallest square ; the length or breadth or side cannot be negative ; LCM of length and breadth ; squaring to get the area ; Driver code"
How can the above be solved in PHP?

 < ? php function gcd ( $ a , $ b ) { if ( $ a == 0 $ b == 0 ) return 0 ; if ( $ a == $ b ) return $ a ; if ( $ a > $ b ) return gcd ( $ a - $ b , $ b ) ; return gcd ( $ a , $ b - $ a ) ; } function squarearea ( $ l , $ b ) { if ( $ l < 0 $ b < 0 ) return -1 ; $ n = ( $ l * $ b ) / gcd ( $ l , $ b ) ; return $ n * $ n ; } $ l = 6 ; $ b = 4 ; echo squarearea ( $ l , $ b ) . "
 " ; ? >


"Smallest subarray containing minimum and maximum values | Function to return length of smallest subarray containing both maximum and minimum value ; find maximum and minimum values in the array ; iterate over the array and set answer to smallest difference between position of maximum and position of minimum value ; last occurrence of minValue ; last occurrence of maxValue ; Driver code"
How can the above be solved in PHP?

< ? php function minSubarray ( $ A , $ n ) { $ minValue = min ( $ A ) ; $ maxValue = max ( $ A ) ; $ pos_min = -1 ; $ pos_max = -1 ; $ ans = PHP_INT_MAX ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ A [ $ i ] == $ minValue ) $ pos_min = $ i ; if ( $ A [ $ i ] == $ maxValue ) $ pos_max = $ i ; if ( $ pos_max != -1 and $ pos_min != -1 ) $ ans = min ( $ ans , abs ( $ pos_min - $ pos_max ) + 1 ) ; } return $ ans ; } $ A = array ( 1 , 5 , 9 , 7 , 1 , 9 , 4 ) ; $ n = sizeof ( $ A ) ; echo minSubarray ( $ A , $ n ) ; ? >


"Smallest subarray with sum greater than a given value | Returns length of smallest subarray with sum greater than x . If there is no subarray with given sum , then returns n + 1 ; Initialize current sum and minimum length ; Initialize starting and ending indexes ; Keep adding array elements while current sum is smaller than or equal to x ; If current sum becomes greater than x . ; Update minimum length if needed ; remove starting elements ; Driver Code"
How can the above be solved in PHP?

 < ? php function smallestSubWithSum ( $ arr , $ n , $ x ) { $ curr_sum = 0 ; $ min_len = $ n + 1 ; $ start = 0 ; $ end = 0 ; while ( $ end < $ n ) { while ( $ curr_sum <= $ x && $ end < $ n ) $ curr_sum += $ arr [ $ end ++ ] ; while ( $ curr_sum > $ x && $ start < $ n ) { if ( $ end - $ start < $ min_len ) $ min_len = $ end - $ start ; $ curr_sum -= $ arr [ $ start ++ ] ; } } return $ min_len ; } $ arr1 = array ( 1 , 4 , 45 , 6 , 10 , 19 ) ; $ x = 51 ; $ n1 = sizeof ( $ arr1 ) ; $ res1 = smallestSubWithSum ( $ arr1 , $ n1 , $ x ) ; if ( $ res1 == $ n1 + 1 ) echo " Not ▁ possible
 " ; else echo $ res1 , "
 " ; $ arr2 = array ( 1 , 10 , 5 , 2 , 7 ) ; $ n2 = sizeof ( $ arr2 ) ; $ x = 9 ; $ res2 = smallestSubWithSum ( $ arr2 , $ n2 , $ x ) ; if ( $ res2 == $ n2 + 1 ) echo " Not ▁ possible
 " ; else echo $ res2 , "
 " ; $ arr3 = array ( 1 , 11 , 100 , 1 , 0 , 200 , 3 , 2 , 1 , 250 ) ; $ n3 = sizeof ( $ arr3 ) ; $ x = 280 ; $ res3 = smallestSubWithSum ( $ arr3 , $ n3 , $ x ) ; if ( $ res3 == $ n3 + 1 ) echo " Not ▁ possible
 " ; else echo $ res3 , "
 " ; ? >


"Smallest subarray with sum greater than a given value | Returns length of smallest subarray with sum greater than x . If there is no subarray with given sum , then returns n + 1 ; Initialize length of smallest subarray as n + 1 ; Pick every element as starting point ; Initialize sum starting with current start ; If first element itself is greater ; Try different ending points for curremt start ; add last element to current sum ; If sum becomes more than x and length of this subarray is smaller than current smallest length , update the smallest length ( or result ) ; Driver Code"
How can the above be solved in PHP?

 < ? php function smallestSubWithSum ( $ arr , $ n , $ x ) { $ min_len = $ n + 1 ; for ( $ start = 0 ; $ start < $ n ; $ start ++ ) { $ curr_sum = $ arr [ $ start ] ; if ( $ curr_sum > $ x ) return 1 ; for ( $ end = $ start + 1 ; $ end < $ n ; $ end ++ ) { $ curr_sum += $ arr [ $ end ] ; if ( $ curr_sum > $ x && ( $ end - $ start + 1 ) < $ min_len ) $ min_len = ( $ end - $ start + 1 ) ; } } return $ min_len ; } $ arr1 = array ( 1 , 4 , 45 , 6 , 10 , 19 ) ; $ x = 51 ; $ n1 = sizeof ( $ arr1 ) ; $ res1 = smallestSubWithSum ( $ arr1 , $ n1 , $ x ) ; if ( ( $ res1 == $ n1 + 1 ) == true ) echo " Not ▁ possible
 " ; else echo $ res1 , "
 " ; $ arr2 = array ( 1 , 10 , 5 , 2 , 7 ) ; $ n2 = sizeof ( $ arr2 ) ; $ x = 9 ; $ res2 = smallestSubWithSum ( $ arr2 , $ n2 , $ x ) ; if ( ( $ res2 == $ n2 + 1 ) == true ) echo " Not ▁ possible
 " ; else echo $ res2 , "
 " ; $ arr3 = array ( 1 , 11 , 100 , 1 , 0 , 200 , 3 , 2 , 1 , 250 ) ; $ n3 = sizeof ( $ arr3 ) ; $ x = 280 ; $ res3 = smallestSubWithSum ( $ arr3 , $ n3 , $ x ) ; if ( ( $ res3 == $ n3 + 1 ) == true ) echo " Not ▁ possible
 " ; else echo $ res3 , "
 " ; ? >


"Smallest sum contiguous subarray | Set | Function to find the smallest sum contiguous subarray ; First invert the sign of the elements ; Apply the normal Kadane algorithm but on the elements of the array having inverted sign ; Invert the answer to get minimum val ; Driver Code"
How can the above be solved in PHP?

< ? php function smallestSumSubarr ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ arr [ $ i ] = - $ arr [ $ i ] ; $ sum_here = $ arr [ 0 ] ; $ max_sum = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ sum_here = max ( $ sum_here + $ arr [ $ i ] , $ arr [ $ i ] ) ; $ max_sum = max ( $ max_sum , $ sum_here ) ; } return ( -1 ) * $ max_sum ; } $ arr = array ( 3 , -4 , 2 , -3 , -1 , 7 , -5 ) ; $ n = sizeof ( $ arr ) ; echo " Smallest ▁ sum : ▁ " , smallestSumSubarr ( $ arr , $ n ) ; ? >


"Smallest sum contiguous subarray | function to find the smallest sum contiguous subarray ; to store the minimum value that is ending up to the current index ; to store the minimum value encountered so far ; traverse the array elements ; if min_ending_here > 0 , then it could not possibly contribute to the minimum sum further ; else add the value arr [ i ] to min_ending_here ; update min_so_far ; required smallest sum contiguous subarray value ; Driver Code"
How can the above be solved in PHP?

< ? php function smallestSumSubarr ( $ arr , $ n ) { $ min_ending_here = 999999 ; $ min_so_far = 999999 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ min_ending_here > 0 ) $ min_ending_here = $ arr [ $ i ] ; else $ min_ending_here += $ arr [ $ i ] ; $ min_so_far = min ( $ min_so_far , $ min_ending_here ) ; } return $ min_so_far ; } $ arr = array ( 3 , -4 , 2 , -3 , -1 , 7 , -5 ) ; $ n = count ( $ arr ) ; echo " Smallest ▁ sum : ▁ " . smallestSumSubarr ( $ arr , $ n ) ; ? >


"Smallest triangular number larger than p | PHP code to find the bucket to choose for picking flowers out of it ; Driver code"
How can the above be solved in PHP?

< ? php function findBucketNo ( $ p ) { return ceil ( ( sqrt ( 8 * $ p + 1 ) - 1 ) / 2 ) ; } $ p = 10 ; echo ( findBucketNo ( $ p ) ) ; ? >


"Smallest x such that 1 * n , 2 * n , ... x * n have all digits from 1 to 9 | Returns smallest value x such that 1 * n , 2 * n , 3 * n ... x * n have all digits from 1 to 9 at least once ; taking temporary array and variable . ; iterate till we get all the 10 digits at least once ; checking all the digits ; Driver code"
How can the above be solved in PHP?

< ? php function smallestX ( $ n ) { $ temp = array_fill ( 0 , 10 , false ) ; if ( $ n == 0 ) return -1 ; $ count = 0 ; $ x = 0 ; for ( $ x = 1 ; $ count < 10 ; $ x ++ ) { $ y = $ x * $ n ; while ( $ y ) { if ( $ temp [ $ y % 10 ] == false ) { $ count ++ ; $ temp [ $ y % 10 ] = true ; } $ y = ( int ) ( $ y / 10 ) ; } } return $ x - 1 ; } $ n = 5 ; echo smallestX ( $ n ) ; ? >


"Smarandache | Function to collect first ' n ' prime numbers ; List to store first ' n ' primes ; Function to generate Smarandache - Wellin Sequence ; Storing the first ' n ' prime numbers in a list ; Driver Code"
How can the above be solved in PHP?

 < ? php function primes ( $ n ) { $ i = 2 ; $ j = 0 ; $ result ; $ z = 0 ; while ( $ j < $ n ) { $ flag = true ; for ( $ item = 2 ; $ item <= ( int ) ( $ i * 1 / 2 ) ; $ item ++ ) if ( $ i % $ item == 0 && $ i != $ item ) { $ flag = false ; break ; } if ( $ flag ) { $ result [ $ z ++ ] = $ i ; $ j += 1 ; } $ i += 1 ; } return $ result ; } function smar_wln ( $ n ) { $ arr = primes ( $ n ) ; for ( $ i = 0 ; $ i < count ( $ arr ) ; $ i ++ ) { for ( $ j = 0 ; $ j <= $ i ; $ j ++ ) echo $ arr [ $ j ] ; echo " ▁ " ; } } $ n = 5 ; echo " First ▁ $ n ▁ terms ▁ of ▁ the " . " ▁ Sequence ▁ are
 " ; smar_wln ( $ n ) ; ? >


"Smith Number | PHP program to to check whether a number is Smith Number or not . ; array to store all prime less than and equal to 10 ^ 6 ; utility function for sieve of sundaram ; In general Sieve of Sundaram , produces primes smaller than ( 2 * x + 2 ) for a number given number x . Since we want primes smaller than MAX , we reduce MAX to half . This array is used to separate numbers of the form i + j + 2 ij from others where 1 <= i <= j ; Main logic of Sundaram . Mark all numbers which do not generate prime number by doing 2 * i + 1 ; Since 2 is a prime number ; Print other primes . Remaining primes are of the form 2 * i + 1 such that marked [ i ] is false . ; Returns true if n is a Smith number , else false . ; Find sum the digits of prime factors of n ; If primes [ i ] is a prime factor , add its digits to pDigitSum . ; If n != 1 then one prime factor still to be summed up ; ; All prime factors digits summed up Now sum the original number digits ; If sum of digits in prime factors and sum of digits in original number are same , then return true . Else return false . ; Finding all prime numbers before limit . These numbers are used to find prime factors ."
How can the above be solved in PHP?

 < ? php $ MAX = 10000 ; $ primes = array ( ) ; function sieveSundaram ( ) { global $ MAX , $ primes ; $ marked = array_fill ( 0 , ( $ MAX / 2 + 100 ) , false ) ; for ( $ i = 1 ; $ i <= ( sqrt ( $ MAX ) - 1 ) / 2 ; $ i ++ ) for ( $ j = ( $ i * ( $ i + 1 ) ) << 1 ; $ j <= $ MAX / 2 ; $ j = $ j + 2 * $ i + 1 ) $ marked [ $ j ] = true ; array_push ( $ primes , 2 ) ; for ( $ i = 1 ; $ i <= $ MAX / 2 ; $ i ++ ) if ( $ marked [ $ i ] == false ) array_push ( $ primes , 2 * $ i + 1 ) ; } function isSmith ( $ n ) { global $ MAX , $ primes ; $ original_no = $ n ; $ pDigitSum = 0 ; for ( $ i = 0 ; $ primes [ $ i ] <= $ n / 2 ; $ i ++ ) { while ( $ n % $ primes [ $ i ] == 0 ) { $ p = $ primes [ $ i ] ; $ n = $ n / $ p ; while ( $ p > 0 ) { $ pDigitSum += ( $ p % 10 ) ; $ p = $ p / 10 ; } } } if ( $ n != 1 && $ n != $ original_no ) { while ( $ n > 0 ) { $ pDigitSum = $ pDigitSum + $ n % 10 ; $ n = $ n / 10 ; } } $ sumDigits = 0 ; while ( $ original_no > 0 ) { $ sumDigits = $ sumDigits + $ original_no % 10 ; $ original_no = $ original_no / 10 ; } return ( $ pDigitSum == $ sumDigits ) ; } sieveSundaram ( ) ; echo " Printing ▁ first ▁ few ▁ Smith ▁ Numbers " . " ▁ using ▁ isSmith ( )
 " ; for ( $ i = 1 ; $ i < 500 ; $ i ++ ) if ( isSmith ( $ i ) ) echo $ i . " " ; ? >


"Snake case of a given sentence | Function to replace spaces and convert into snake case ; Converting space to underscor ; If not space , convert into lower character ; Driver Code ; Calling function"
How can the above be solved in PHP?

< ? php function convert ( $ str ) { $ n = strlen ( $ str ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ str [ $ i ] == ' ▁ ' ) $ str [ $ i ] = ' _ ' ; else $ str [ $ i ] = strtolower ( $ str [ $ i ] ) ; } echo $ str ; } $ str = " I ▁ got ▁ intern ▁ at ▁ geeksforgeeks " ; convert ( $ str ) ; ? >


"Solve the Linear Equation of Single Variable | Function to solve the given equation ; Traverse the equation ; For cases such as : x , - x , + x ; Flip sign once ' = ' is seen ; There may be a number left in the end ; For infinite solutions ; For no solution ; x = total sum / coeff of x ' - ' sign indicates moving numeric value to right hand side ; Driver code"
How can the above be solved in PHP?

< ? php function solveEquation ( $ equation ) { $ n = strlen ( $ equation ) ; $ sign = 1 ; $ coeff = 0 ; $ total = 0 ; $ i = 0 ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ equation [ $ j ] == ' + ' $ equation [ $ j ] == ' - ' ) { if ( $ j > $ i ) $ total += $ sign * intval ( substr ( $ equation , $ i , $ j - $ i ) ) ; $ i = $ j ; } else if ( $ equation [ $ j ] == ' x ' ) { if ( ( $ i == $ j ) $ equation [ $ j - 1 ] == ' + ' ) $ coeff += $ sign ; else if ( $ equation [ $ j - 1 ] == ' - ' ) $ coeff -= $ sign ; else $ coeff += $ sign * intval ( substr ( $ equation , $ i , $ j - $ i ) ) ; $ i = $ j + 1 ; } else if ( $ equation [ $ j ] == ' = ' ) { if ( $ j > $ i ) $ total += $ sign * intval ( substr ( $ equation , $ i , $ j - $ i ) ) ; $ sign = -1 ; $ i = $ j + 1 ; } } if ( $ i < $ n ) $ total += $ sign * intval ( substr ( $ equation , $ i ) ) ; if ( $ coeff == 0 && $ total == 0 ) return " Infinite ▁ solutions " ; if ( $ coeff == 0 && $ total ) return " No ▁ solution " ; $ ans = - $ total / $ coeff ; return " x ▁ = ▁ " . $ ans ; } $ equation = " x + 5-3 + x = 6 + x - 2" ; echo ( solveEquation ( $ equation ) ) ; ? >


"Solving f ( n ) = ( 1 ) + ( 2 * 3 ) + ( 4 * 5 * 6 ) . . . n using Recursion | Recursive function for finding sum of series calculated - number of terms till which sum of terms has been calculated current - number of terms for which sum has to be calculated N - Number of terms in the function to be calculated ; checking termination condition ; product of terms till current ; recursive call for adding terms next in the series ; input number of terms in the series ; invoking the function to calculate the sum"
How can the above be solved in PHP?

< ? php function seriesSum ( $ calculated , $ current , $ N ) { $ i ; $ cur = 1 ; if ( $ current == $ N + 1 ) return 0 ; for ( $ i = $ calculated ; $ i < $ calculated + $ current ; $ i ++ ) $ cur *= $ i ; return $ cur + seriesSum ( $ i , $ current + 1 , $ N ) ; } $ N = 5 ; echo ( seriesSum ( 1 , 1 , $ N ) ) ; ? >


"Sort 1 to N by swapping adjacent elements | Return true if array can be sorted otherwise false ; Check bool array B and sorts elements for continuous sequence of 1 ; Sort array A from i to j ; Check if array is sorted or not ; Driver Code"
How can the above be solved in PHP?

 < ? php function sortedAfterSwap ( $ A , $ B , $ n ) { for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { if ( $ B [ $ i ] ) { $ j = $ i ; while ( $ B [ $ j ] ) $ j ++ ; sort ( $ A ) ; $ i = $ j ; } } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ A [ $ i ] != $ i + 1 ) return false ; } return true ; } $ A = array ( 1 , 2 , 5 , 3 , 4 , 6 ) ; $ B = array ( 0 , 1 , 1 , 1 , 0 ) ; $ n = count ( $ A ) ; if ( sortedAfterSwap ( $ A , $ B , $ n ) ) echo " A ▁ can ▁ be ▁ sorted
 " ; else echo " A ▁ can ▁ not ▁ be ▁ sorted
 " ; ? >


"Sort 1 to N by swapping adjacent elements | Return true if array can be sorted otherwise false ; Check if array is sorted or not ; Driver Code"
How can the above be solved in PHP?

 < ? php function sortedAfterSwap ( & $ A , & $ B , $ n ) { for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { if ( $ B [ $ i ] ) { if ( $ A [ $ i ] != $ i + 1 ) { $ t = $ A [ $ i ] ; $ A [ $ i ] = $ A [ $ i + 1 ] ; $ A [ $ i + 1 ] = $ t ; } } } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ A [ $ i ] != $ i + 1 ) return false ; } return true ; } $ A = array ( 1 , 2 , 5 , 3 , 4 , 6 ) ; $ B = array ( 0 , 1 , 1 , 1 , 0 ) ; $ n = sizeof ( $ A ) ; if ( sortedAfterSwap ( $ A , $ B , $ n ) ) echo " A ▁ can ▁ be ▁ sorted
 " ; else echo " A ▁ can ▁ not ▁ be ▁ sorted
 " ; ? >


"Sort 3 numbers | PHP program to sort an array of size 3 ; Insert arr [ 1 ] ; Insert arr [ 2 ] ; Driver Code"
How can the above be solved in PHP?


"Sort 3 numbers | PHP program to sort an array of size 3"
How can the above be solved in PHP?


"Sort a binary array using one traversal | PHP Code for Sort a binary array using one traversal ; if number is smaller than 1 then swap it with j - th number ; Driver Code"
How can the above be solved in PHP?


"Sort all even numbers in ascending order and then sort all odd numbers in descending order | To do two way sort . First sort even numbers in ascending order , then odd numbers in descending order . ; Make all odd numbers negative ; if ( $arr [ $i ] & 1 ) Check for odd ; Sort all numbers ; Retaining original array ; Driver code"
How can the above be solved in PHP?


"Sort an almost sorted array where only two elements are swapped | This function sorts an array that can be sorted by single swap ; Traverse the given array from rightmost side ; Check if arr [ i ] is not in order ; Find the other element to be swapped with arr [ i ] ; Swap the pair ; A utility function to print an array of size n ; Driver Code"
How can the above be solved in PHP?

 < ? php function sortByOneSwap ( & $ arr , $ n ) { for ( $ i = $ n - 1 ; $ i > 0 ; $ i -- ) { if ( $ arr [ $ i ] < $ arr [ $ i - 1 ] ) { $ j = $ i - 1 ; while ( $ j >= 0 && $ arr [ $ i ] < $ arr [ $ j ] ) $ j -- ; $ temp = $ arr [ $ i ] ; $ arr [ $ i ] = $ arr [ $ j + 1 ] ; $ arr [ $ j + 1 ] = $ temp ; break ; } } } function printArray ( & $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; echo "
 " ; } $ arr = array ( 10 , 30 , 20 , 40 , 50 , 60 , 70 ) ; $ n = sizeof ( $ arr ) ; echo " Given ▁ array ▁ is ▁ " . "
 " ; printArray ( $ arr , $ n ) ; sortByOneSwap ( $ arr , $ n ) ; echo " Sorted ▁ array ▁ is ▁ " . "
 " ; printArray ( $ arr , $ n ) ;


"Sort an array according to absolute difference with a given value " using ▁ constant ▁ extra ▁ space " | PHP program to sort an array based on absolute difference with a given value x . ; Below lines are similar to insertion sort ; Insert arr [ i ] at correct place ; Function to print the array ; Driver Code"
How can the above be solved in PHP?

< ? php function arrange ( $ arr , $ n , $ x ) { for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ diff = abs ( $ arr [ $ i ] - $ x ) ; $ j = $ i - 1 ; if ( abs ( $ arr [ $ j ] - $ x ) > $ diff ) { $ temp = $ arr [ $ i ] ; while ( abs ( $ arr [ $ j ] - $ x ) > $ diff && $ j >= 0 ) { $ arr [ $ j + 1 ] = $ arr [ $ j ] ; $ j -- ; } $ arr [ $ j + 1 ] = $ temp ; } } return $ arr ; } function print_arr ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; } $ arr = array ( 10 , 5 , 3 , 9 , 2 ) ; $ n = sizeof ( $ arr ) ; $ x = 7 ; $ arr1 = arrange ( $ arr , $ n , $ x ) ; print_arr ( $ arr1 , $ n ) ; ? >


"Sort an array containing two types of elements | Method for segregation 0 and 1 given input array ; Driver Code"
How can the above be solved in PHP?

< ? php function segregate0and1 ( $ arr , $ n ) { $ type0 = 0 ; $ type1 = $ n - 1 ; while ( $ type0 < $ type1 ) { if ( $ arr [ $ type0 ] == 1 ) { $ temp = $ arr [ $ type0 ] ; $ arr [ $ type0 ] = $ arr [ $ type1 ] ; $ arr [ $ type1 ] = $ temp ; $ type1 -- ; } else { $ type0 ++ ; } } return $ arr ; } $ arr = array ( 1 , 1 , 1 , 0 , 1 , 0 , 0 , 1 , 1 , 1 , 1 ) ; $ n = count ( $ arr ) ; $ arr1 = segregate0and1 ( $ arr , $ n ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr1 [ $ i ] . " ▁ " ; ? >


"Sort an array of 0 s , 1 s and 2 s | UTILITY FUNCTIONS ; Sort the input array , the array is assumed to have values in { 0 , 1 , 2 } ; Utility function to print array arr [ ] ; Driver Code"
How can the above be solved in PHP?

 < ? php function swap ( & $ a , & $ b ) { $ temp = $ a ; $ a = $ b ; $ b = $ temp ; } function sort012 ( & $ a , $ arr_size ) { $ lo = 0 ; $ hi = $ arr_size - 1 ; $ mid = 0 ; while ( $ mid <= $ hi ) { switch ( $ a [ $ mid ] ) { case 0 : swap ( $ a [ $ lo ++ ] , $ a [ $ mid ++ ] ) ; break ; case 1 : $ mid ++ ; break ; case 2 : swap ( $ a [ $ mid ] , $ a [ $ hi -- ] ) ; break ; } } } function printArray ( & $ arr , $ arr_size ) { for ( $ i = 0 ; $ i < $ arr_size ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; echo "
 " ; } $ arr = array ( 0 , 1 , 1 , 0 , 1 , 2 , 1 , 2 , 0 , 0 , 0 , 1 ) ; $ arr_size = sizeof ( $ arr ) ; sort012 ( $ arr , $ arr_size ) ; echo " array ▁ after ▁ segregation ▁ " ; printArray ( $ arr , $ arr_size ) ; ? >


"Sort an array when two halves are sorted | PHP program to Merge two sorted halves of array Into Single Sorted Array ; Sort the given array using sort STL ; Driver Code ; Print sorted Array"
How can the above be solved in PHP?

< ? php function mergeTwoHalf ( & $ A , $ n ) { sort ( $ A , 0 ) ; } $ A = array ( 2 , 3 , 8 , -1 , 7 , 10 ) ; $ n = sizeof ( $ A ) ; mergeTwoHalf ( $ A , $ n ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ A [ $ i ] . " ▁ " ; ? >


"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"
How can the above be solved in PHP?

< ? 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 ) ; ? >


"Sort first half in ascending and second half in descending order | 1 | function to print half of the array in ascending order and the other half in descending order ; sorting the array ; printing first half in ascending order ; printing second half in descending order ; Driver code"
How can the above be solved in PHP?

< ? php function printOrder ( $ arr , $ n ) { sort ( $ arr ) ; for ( $ i = 0 ; $ i < $ n / 2 ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; for ( $ j = $ n - 1 ; $ j >= $ n / 2 ; $ j -- ) echo $ arr [ $ j ] . " ▁ " ; } $ arr = array ( 5 , 4 , 6 , 2 , 1 , 3 , 8 , -1 ) ; $ n = sizeof ( $ arr ) ; printOrder ( $ arr , $ n ) ; ? >


"Sort first k values in ascending order and remaining n | Function to sort the array ; Store the k elements in an array ; Store the remaining n - k elements in an array ; sorting the array from 0 to k - 1 places ; sorting the array from k to n places ; storing the values in the final array arr ; printing the array ; Driver code"
How can the above be solved in PHP?


"Sort first k values in ascending order and remaining n | function to sort the array ; Sort first k elements in ascending order ; Sort remaining n - k elements in descending order ; Our arr contains 8 elements"
How can the above be solved in PHP?

< ? php function printOrder ( $ arr , $ n , $ k ) { $ a = array_slice ( $ arr , 0 , $ k ) ; sort ( $ a ) ; $ b = array_slice ( $ arr , $ k , $ n ) ; sort ( $ b ) ; $ b = array_reverse ( $ b ) ; unset ( $ arr ) ; $ arr = $ a ; return array_merge ( $ arr , $ b ) ; } $ arr = array ( 5 , 4 , 6 , 2 , 1 , 3 , 8 , 9 , -1 ) ; $ k = 4 ; $ n = count ( $ arr ) ; $ arr = printOrder ( $ arr , $ n , $ k ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; ? >


"Sorted order printing of a given array that represents a BST | PHP Code for Sorted order printing of a given array that represents a BST ; print left subtree ; print root ; print right subtree ; Driver Code"
How can the above be solved in PHP?

< ? php function printSorted ( $ arr , $ start , $ end ) { if ( $ start > $ end ) return ; printSorted ( $ arr , $ start * 2 + 1 , $ end ) ; echo ( $ arr [ $ start ] . " " ) ; printSorted ( $ arr , $ start * 2 + 2 , $ end ) ; } $ arr = array ( 4 , 2 , 5 , 1 , 3 ) ; printSorted ( $ arr , 0 , sizeof ( $ arr ) - 1 ) ;


"Sorting all array elements except one | PHP program to sort all elements except element at index k . ; Move k - th element to end ; Sort all elements except last ; Store last element ( originally k - th ) ; Move all elements from k - th to one position ahead . ; Restore k - th element ; Driver code"
How can the above be solved in PHP?


"Sorting array except elements in a subarray | Sort whole array a [ ] except elements in range a [ l . . r ] ; Copy all those element that need to be sorted to an auxiliary array b [ ] ; sort the array b ; Copy sorted elements back to a [ ] ; Driver code"
How can the above be solved in PHP?


"Sorting array with conditional swapping | Function to check if it is possible to sort the array ; Calculating max_element at each iteration . ; if we can not swap the i - th element . ; if it is impossible to swap the max_element then we can not sort the array . ; Otherwise , we can sort the array . ; Driver Code"
How can the above be solved in PHP?

< ? php function possibleToSort ( $ arr , $ n , $ str ) { $ max_element = -1 ; for ( $ i = 0 ; $ i < sizeof ( $ str ) ; $ i ++ ) { $ max_element = max ( $ max_element , $ arr [ $ i ] ) ; if ( $ str [ $ i ] == '0' ) { if ( $ max_element > $ i + 1 ) return " No " ; } } return " Yes " ; } $ arr = array ( 1 , 2 , 5 , 3 , 4 , 6 ) ; $ n = sizeof ( $ arr ) ; $ str = "01110" ; echo possibleToSort ( $ arr , $ n , $ str ) ; ? >