"Sorting array with reverse around middle | PHP program to find possibility to sort by multiple subarray reverse operarion ; making the copy of the original array ; sorting the copied array ; checking mirror image of elements of sorted copy array and equivalent element of original array ; Driver code"
How can the above be solved in PHP?

< ? php function ifPossible ( & $ arr , $ n ) { $ cp = array ( ) ; $ cp = $ arr ; sort ( $ cp ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( ! ( $ arr [ $ i ] == $ cp [ $ i ] ) && ! ( $ arr [ $ n - 1 - $ i ] == $ cp [ $ i ] ) ) return false ; } return true ; } $ arr = array ( 1 , 7 , 6 , 4 , 5 , 3 , 2 , 8 ) ; $ n = sizeof ( $ arr ) ; if ( ifPossible ( $ arr , $ n ) ) echo " Yes " ; else echo " No " ; ? >


"Source to destination in 2 | Function that returns true if it is possible to move from source to the destination with the given moves ; Driver code"
How can the above be solved in PHP?

< ? php function isPossible ( $ Sx , $ Sy , $ Dx , $ Dy , $ x , $ y ) { if ( abs ( $ Sx - $ Dx ) % $ x == 0 && abs ( $ Sy - $ Dy ) % $ y == 0 && ( abs ( $ Sx - $ Dx ) / $ x ) % 2 == ( abs ( $ Sy - $ Dy ) / $ y ) % 2 ) return true ; return false ; } $ Sx = 0 ; $ Sy = 0 ; $ Dx = 0 ; $ Dy = 0 ; $ x = 3 ; $ y = 4 ; if ( isPossible ( $ Sx , $ Sy , $ Dx , $ Dy , $ x , $ y ) ) echo ( " Yes " ) ; else echo ( " No " ) ; ? >


"Space efficient iterative method to Fibonacci number | get second MSB ; consectutively set all the bits ; returns the second MSB ; Multiply function ; Function to calculate F [ ] [ ] raise to the power n ; Base case ; take 2D array to store number 's ; run loop till MSB > 0 ; To return fibonacci number ; Given n"
How can the above be solved in PHP?


"Space optimization using bit manipulations | Driver Code ; Iterate through a to b , If it is a multiple of 2 or 5 Mark index in array as 1"
How can the above be solved in PHP?


"Special prime numbers | PHP program to check whether there exist at least k or not in range [ 2. . n ] ; Generating all the prime numbers from 2 to n . ; If a prime number is Special prime number , then we increments the value of k . ; If at least k Special prime numbers are present , then we return 1. else we return 0 from outside of the outer loop . ; Driver Code"
How can the above be solved in PHP?

 < ? php $ primes = array ( ) ; function SieveofEratosthenes ( $ n ) { global $ primes ; $ visited = array_fill ( 0 , $ n , false ) ; for ( $ i = 2 ; $ i <= $ n + 1 ; $ i ++ ) if ( ! $ visited [ $ i ] ) { for ( $ j = $ i * $ i ; $ j <= $ n + 1 ; $ j += $ i ) $ visited [ $ j ] = true ; array_push ( $ primes , $ i ) ; } } function specialPrimeNumbers ( $ n , $ k ) { global $ primes ; SieveofEratosthenes ( $ n ) ; $ count = 0 ; for ( $ i = 0 ; $ i < count ( $ primes ) ; $ i ++ ) { for ( $ j = 0 ; $ j < $ i - 1 ; $ j ++ ) { if ( $ primes [ $ j ] + $ primes [ $ j + 1 ] + 1 == $ primes [ $ i ] ) { $ count ++ ; break ; } } if ( $ count == $ k ) return true ; } return false ; } $ n = 27 ; $ k = 2 ; if ( specialPrimeNumbers ( $ n , $ k ) ) echo " YES
 " ; else echo " NO
 " ; ? >


"Split N ^ 2 numbers into N groups of equal sum | Function to print N groups of equal sum ; No . of Groups ; n / 2 pairs ; Driver code"
How can the above be solved in PHP?


"Split a number into 3 parts such that none of the parts is divisible by 3 | PHP program to split a number into three parts such than none of them is divisible by 3. ; Print x = 1 , y = 1 and z = N - 2 ; Otherwise , print x = 1 , y = 2 and z = N - 3 ; Driver code"
How can the above be solved in PHP?

 < ? php function printThreeParts ( $ N ) { if ( $ N % 3 == 0 ) echo " ▁ x ▁ = ▁ 1 , ▁ y ▁ = ▁ 1 , ▁ z ▁ = ▁ " . ( $ N - 2 ) . "
 " ; else echo " ▁ x ▁ = ▁ 1 , ▁ y ▁ = ▁ 2 , ▁ z ▁ = ▁ " . ( $ N - 3 ) . "
 " ; } $ N = 10 ; printThreeParts ( $ N ) ; ? >


"Split n into maximum composite numbers | function to calculate the maximum number of composite numbers adding upto n ; 4 is the smallest composite number ; stores the remainder when n is divided by 4 ; if remainder is 0 , then it is perfectly divisible by 4. ; if the remainder is 1 ; If the number is less then 9 , that is 5 , then it cannot be expressed as 4 is the only composite number less than 5 ; If the number is greater then 8 , and has a remainder of 1 , then express n as n - 9 a and it is perfectly divisible by 4 and for 9 , count 1. ; When remainder is 2 , just subtract 6 from n , so that n is perfectly divisible by 4 and count 1 for 6 which is subtracted . ; if the number is 7 , 11 which cannot be expressed as sum of any composite numbers ; when the remainder is 3 , then subtract 15 from it and n becomes perfectly divisible by 4 and we add 2 for 9 and 6 , which is getting subtracted to make n perfectly divisible by 4. ; driver program to test the above function"
How can the above be solved in PHP?


"Split the array and add the first part to the end | PHP program to split array and move first part to end . ; Rotate array by 1. ; Driver code"
How can the above be solved in PHP?


"Split the array and add the first part to the end | Set 2 | Function to reverse arr [ ] from index start to end ; Function to print an array ; Function to left rotate arr [ ] of size n by k ; Driver program to test above functions ; Function calling"
How can the above be solved in PHP?

< ? php function rvereseArray ( & $ arr , $ start , $ end ) { while ( $ start < $ end ) { $ temp = $ arr [ $ start ] ; $ arr [ $ start ] = $ arr [ $ end ] ; $ arr [ $ end ] = $ temp ; $ start ++ ; $ end -- ; } } function printArray ( & $ arr , $ size ) { for ( $ i = 0 ; $ i < $ size ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; } function splitArr ( & $ arr , $ k , $ n ) { rvereseArray ( $ arr , 0 , $ n - 1 ) ; rvereseArray ( $ arr , 0 , $ n - $ k - 1 ) ; rvereseArray ( $ arr , $ n - $ k , $ n - 1 ) ; } $ arr = array ( 12 , 10 , 5 , 6 , 52 , 36 ) ; $ n = sizeof ( $ arr ) ; $ k = 2 ; splitArr ( $ arr , $ k , $ n ) ; printArray ( $ arr , $ n ) ; ? >


"Split the array into odd number of segments of odd lengths | Function to check ; Check the result by processing the first & last element and size ; Driver code"
How can the above be solved in PHP?

< ? php function checkArray ( $ arr , $ n ) { return ( $ arr [ 0 ] % 2 ) && ( $ arr [ $ n - 1 ] % 2 ) && ( $ n % 2 ) ; } $ arr = array ( 1 , 2 , 3 , 4 , 5 ) ; $ n = sizeof ( $ arr ) ; echo checkArray ( $ arr , $ n ) ; ? >


"Split the number into N parts such that difference between the smallest and the largest part is minimum | Function that prints the required sequence ; If we cannot split the number into exactly ' N ' parts ; If x % n == 0 then the minimum difference is 0 and all numbers are x / n ; upto n - ( x % n ) the values will be x / n after that the values will be x / n + 1 ; Driver code"
How can the above be solved in PHP?


"Square Free Number | Returns true if n is a square free number , else returns false . ; If 2 again divides n , then n is not a square free number . ; n must be odd at this point . So we can skip one element ( Note i = i + 2 ) ; Check if i is a prime factor ; If i again divides , then n is not square free ; Driver Code"
How can the above be solved in PHP?

< ? php function isSquareFree ( $ n ) { if ( $ n % 2 == 0 ) $ n = $ n / 2 ; if ( $ n % 2 == 0 ) return false ; for ( $ i = 3 ; $ i <= sqrt ( $ n ) ; $ i = $ i + 2 ) { if ( $ n % $ i == 0 ) { $ n = $ n / $ i ; if ( $ n % $ i == 0 ) return false ; } } return true ; } $ n = 10 ; if ( isSquareFree ( $ n ) ) echo ( " Yes " ) ; else echo ( " No " ) ; ? >


"Square root of a number using log | PHP program to demonstrate finding square root of a number using sqrt ( )"
How can the above be solved in PHP?

< ? php $ n = 12 ; echo sqrt ( $ n ) ; ? >


"Square root of an integer | Returns floor of square root of x ; Base cases ; Do Binary Search for floor ( sqrt ( x ) ) ; If x is a perfect square ; Since we need floor , we update answer when mid * mid is smaller than x , and move closer to sqrt ( x ) ; If mid * mid is greater than x ; Driver Code"
How can the above be solved in PHP?

 < ? php function floorSqrt ( $ x ) { if ( $ x == 0 $ x == 1 ) return $ x ; $ start = 1 ; $ end = $ x ; $ ans ; while ( $ start <= $ end ) { $ mid = ( $ start + $ end ) / 2 ; if ( $ mid * $ mid == $ x ) return $ mid ; if ( $ mid * $ mid < $ x ) { $ start = $ mid + 1 ; $ ans = $ mid ; } else $ end = $ mid - 1 ; } return $ ans ; } $ x = 11 ; echo floorSqrt ( $ x ) , "
 " ; ? >


"Square root of an integer | Returns floor of square root of x ; Base cases ; Starting from 1 , try all numbers until i * i is greater than or equal to x . ; Driver Code"
How can the above be solved in PHP?

 < ? php function floorSqrt ( $ x ) { if ( $ x == 0 $ x == 1 ) return $ x ; $ i = 1 ; $ result = 1 ; while ( $ result <= $ x ) { $ i ++ ; $ result = $ i * $ i ; } return $ i - 1 ; } $ x = 11 ; echo floorSqrt ( $ x ) , "
 " ; ? >


"Squared triangular number ( Sum of cubes ) | Function to find if the given number is sum of the cubes of first n natural numbers ; Start adding cubes of the numbers from 1 ; If sum becomes equal to s return n ; Driver code"
How can the above be solved in PHP?

< ? php function findS ( $ s ) { $ sum = 0 ; for ( $ n = 1 ; $ sum < $ s ; $ n ++ ) { $ sum += $ n * $ n * $ n ; if ( $ sum == $ s ) return $ n ; } return -1 ; } $ s = 9 ; $ n = findS ( $ s ) ; if ( $ n == -1 ) echo ( " - 1" ) ; else echo ( $ n ) ; ? >


"Squared triangular number ( Sum of cubes ) | Returns root of n ( n + 1 ) / 2 = num if num is triangular ( or integerroot exists ) . Else returns - 1. ; Considering the equation n * ( n + 1 ) / 2 = num . The equation is : a ( n ^ 2 ) + bn + c = 0 "; ; Find roots of equation ; checking if root1 is natural ; checking if root2 is natural ; Returns square root of x if it is perfect square . Else returns - 1. ; Find floating point value of square root of x . ; If square root is an integer ; Function to find if the given number is sum of the cubes of first n natural numbers ; Driver code"
How can the above be solved in PHP?

< ? php function isTriangular ( $ num ) { if ( $ num < 0 ) return false ; $ c = ( -2 * $ num ) ; $ b = 1 ; $ a = 1 ; $ d = ( $ b * $ b ) - ( 4 * $ a * $ c ) ; if ( $ d < 0 ) return -1 ; $ root1 = ( - $ b + sqrt ( $ d ) ) / ( 2 * $ a ) ; $ root2 = ( - $ b - sqrt ( $ d ) ) / ( 2 * $ a ) ; if ( $ root1 > 0 && floor ( $ root1 ) == $ root1 ) return $ root1 ; if ( $ root2 > 0 && floor ( $ root2 ) == $ root2 ) return $ root2 ; return -1 ; } function isPerfectSquare ( $ x ) { $ sr = sqrt ( $ x ) ; if ( ( $ sr - floor ( $ sr ) ) == 0 ) return floor ( $ sr ) ; else return -1 ; } function findS ( $ s ) { $ sr = isPerfectSquare ( $ s ) ; if ( $ sr == -1 ) return -1 ; return isTriangular ( $ sr ) ; } $ s = 9 ; $ n = findS ( $ s ) ; if ( $ n == -1 ) echo " - 1" ; else echo $ n ; ? >


"Stable sort for descending order | Bubble sort implementation to sort elements in descending order . ; Sorts a [ ] in descending order using bubble sort . ; Driver code"
How can the above be solved in PHP?


"Stein 's Algorithm for finding GCD | Function to implement Stein 's Algorithm ; GCD ( 0 , b ) == b ; GCD ( a , 0 ) == a , GCD ( 0 , 0 ) == 0 ; Finding K , where K is the greatest power of 2 that divides both a and b . ; Dividing a by 2 until a becomes odd ; From here on , ' a ' is always odd . ; If b is even , remove all factor of 2 in b ; Now a and b are both odd . Swap if necessary so a <= b , then set b = b - a ( which is even ) ; restore common factors of 2 ; Driver code"
How can the above be solved in PHP?


"Stein 's Algorithm for finding GCD | Function to implement Stein 's Algorithm ; GCD ( 0 , b ) == b ; GCD ( a , 0 ) == a , GCD ( 0 , 0 ) == 0 ; look for factors of 2 if ( ~ $a & 1 ) a is even ; if ( $b & 1 ) b is odd ; else both a and b are even ; if ( ~ $b & 1 ) a is odd , b is even ; reduce larger number ; Driver code"
How can the above be solved in PHP?

< ? php function gcd ( $ a , $ b ) { if ( $ a == $ b ) return $ a ; if ( $ a == 0 ) return $ b ; if ( $ b == 0 ) return $ a ; { return gcd ( $ a >> 1 , $ b ) ; return gcd ( $ a >> 1 , $ b >> 1 ) << 1 ; } return gcd ( $ a , $ b >> 1 ) ; if ( $ a > $ b ) return gcd ( ( $ a - $ b ) >> 1 , $ b ) ; return gcd ( ( $ b - $ a ) >> 1 , $ a ) ; } $ a = 34 ; $ b = 17 ; echo " Gcd ▁ of ▁ given ▁ numbers ▁ is : ▁ " , gcd ( $ a , $ b ) ; ? >


"Steps required to visit M points in order on a circular ring of N points | Function to count the steps required ; Start at 1 ; Initialize steps ; If nxt is greater than cur ; Now we are at a [ i ] ; Driver code"
How can the above be solved in PHP?

< ? php function findSteps ( $ n , $ m , $ a ) { $ cur = 1 ; $ steps = 0 ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) { if ( $ a [ $ i ] >= $ cur ) $ steps += ( $ a [ $ i ] - $ cur ) ; else $ steps += ( $ n - $ cur + $ a [ $ i ] ) ; $ cur = $ a [ $ i ] ; } return $ steps ; } $ n = 3 ; $ m = 3 ; $ a = array ( 2 , 1 , 2 ) ; echo findSteps ( $ n , $ m , $ a ) ; ? >


"Steps to reduce N to zero by subtracting its most significant digit at every step | Function to count the number of digits in a number m ; Function to count the number of steps to reach 0 ; count the total number of stesp ; iterate till we reach 0 ; count the digits in last ; decrease it by 1 ; find the number on whose division , we get the first digit ; first digit in last ; find the first number less than last where the first digit changes ; find the number of numbers with same first digit that are jumped ; count the steps ; the next number with a different first digit ; Driver code"
How can the above be solved in PHP?

< ? php function countdig ( $ m ) { if ( $ m == 0 ) return 0 ; else return 1 + countdig ( ( int ) ( $ m / 10 ) ) ; } function countSteps ( $ x ) { $ c = 0 ; $ last = $ x ; while ( $ last ) { $ digits = countdig ( $ last ) ; $ digits -= 1 ; $ divisor = pow ( 10 , $ digits ) ; $ first = ( int ) ( $ last / $ divisor ) ; $ lastnumber = $ first * $ divisor ; $ skipped = ( $ last - $ lastnumber ) / $ first ; $ skipped += 1 ; $ c += $ skipped ; $ last = $ last - ( $ first * $ skipped ) ; } return $ c ; } $ n = 14 ; echo countSteps ( $ n ) ;


"Stern | PHP program to print Brocot Sequence ; loop to create sequence ; adding sum of considered element and it 's precedent ; adding next considered element ; printing sequence . . ; Driver code ; adding first two element in the sequence"
How can the above be solved in PHP?

< ? php function SternSequenceFunc ( & $ BrocotSequence , $ n ) { for ( $ i = 1 ; count ( $ BrocotSequence ) < $ n ; $ i ++ ) { $ considered_element = $ BrocotSequence [ $ i ] ; $ precedent = $ BrocotSequence [ $ i - 1 ] ; array_push ( $ BrocotSequence , $ considered_element + $ precedent ) ; array_push ( $ BrocotSequence , $ considered_element ) ; } for ( $ i = 0 ; $ i < 15 ; ++ $ i ) echo ( $ BrocotSequence [ $ i ] . " ▁ " ) ; } $ n = 15 ; $ BrocotSequence = array ( ) ; array_push ( $ BrocotSequence , 1 ) ; array_push ( $ BrocotSequence , 1 ) ; SternSequenceFunc ( $ BrocotSequence , $ n ) ; ? >


"String matching with * ( that matches with any ) in any of the two strings | Function to check if the two strings can be matched or not ; if the string don 't have *  then character at that position  must be same. ; Driver Code"
How can the above be solved in PHP?

< ? php function doMatch ( $ A , $ B ) { for ( $ i = 0 ; $ i < strlen ( $ A ) ; $ i ++ ) if ( $ A [ $ i ] != ' * ' && $ B [ $ i ] != ' * ' ) if ( $ A [ $ i ] != $ B [ $ i ] ) return false ; return true ; } $ A = " gee * sforgeeks " ; $ B = " geeksforgeeks " ; echo doMatch ( $ A , $ B ) ; ? >


"String transformation using XOR and OR | function to check if conversion is possible or not ; if lengths are different ; iterate to check if both strings have 1 ; to check if there is even one 1 in string s1 ; to check if there is even one 1 in string s2 ; if both string do not have a '1' . ; Driver code"
How can the above be solved in PHP?


"String which when repeated exactly K times gives a permutation of S | Function to return a string which when repeated exactly k times gives a permutation of s ; size of string ; to frequency of each character ; get frequency of each character ; to store final answer ; check if frequency is divisible by k ; add to answer ; if frequency is not divisible by k ; Driver code ; function call"
How can the above be solved in PHP?


"String with k distinct characters and no same characters adjacent | Function to find a string of length n with k distinct characters . ; Initialize result with first k Latin letters ; Fill remaining n - k letters by repeating k letters again and again . ; Driver code"
How can the above be solved in PHP?

< ? php function findString ( $ n , $ k ) { $ res = " " ; for ( $ i = 0 ; $ i < $ k ; $ i ++ ) $ res = $ res . chr ( ord ( ' a ' ) + $ i ) ; $ count = 0 ; for ( $ i = 0 ; $ i < $ n - $ k ; $ i ++ ) { $ res = $ res . chr ( ord ( ' a ' ) + $ count ) ; $ count ++ ; if ( $ count == $ k ) $ count = 0 ; } return $ res ; } $ n = 5 ; $ k = 2 ; echo findString ( $ n , $ k ) ; ? >


"String with maximum number of unique characters | Function to find string with maximum number of unique characters ; Index of string with maximum unique characters ; iterate through all strings ; array indicating any alphabet included or not included ; count number of unique alphabets in each string ; keep track of maximum number of alphabets ; print result ; Driver code"
How can the above be solved in PHP?

 < ? php function LargestString ( $ na ) { $ N = sizeof ( $ na ) ; $ c = array_fill ( 0 , $ N , 0 ) ; $ m = 0 ; for ( $ j = 0 ; $ j < $ N ; $ j ++ ) { $ character = array_fill ( 0 , 26 , false ) ; for ( $ k = 0 ; $ k < strlen ( $ na [ $ j ] ) ; $ k ++ ) { $ x = ord ( $ na [ $ j ] [ $ k ] ) - 65 ; if ( ( $ na [ $ j ] [ $ k ] != ' ▁ ' ) && ( $ character [ $ x ] == false ) ) { $ c [ $ j ] ++ ; $ character [ $ x ] = true ; } } if ( $ c [ $ j ] > $ c [ $ m ] ) $ m = $ j ; } echo $ na [ $ m ] . "
 " ; } $ na = array ( " BOB " , " A ▁ AB ▁ C ▁ JOHNSON " , " ASKRIT " , " ARMAN ▁ MALLIK " , " ANJALI " ) ; LargestString ( $ na ) ; ? >


"Students with maximum average score of three subjects | Function to find the list of students having maximum average score ; Variables to store average score of a student and maximum average score ; List to store names of students having maximum average score ; Traversing the file data ; finding average score of a student ; Clear the list and add name of student having current maximum average score in the list ; Printing the maximum average score and names of students having this maximum average score as per the order in the file . ; Driver code ; Number of elements in string array"
How can the above be solved in PHP?

< ? php function getStudentsList ( $ file , $ n ) { $ maxAvgScore = PHP_INT_MIN ; $ names = array ( ) ; $ avgScore = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i += 4 ) { $ avgScore = ( int ) ( ( intval ( $ file [ $ i + 1 ] ) + intval ( $ file [ $ i + 2 ] ) + intval ( $ file [ $ i + 3 ] ) ) / 3 ) ; if ( $ avgScore > $ maxAvgScore ) { $ maxAvgScore = $ avgScore ; unset ( $ names ) ; $ names = array ( ) ; array_push ( $ names , $ file [ $ i ] ) ; } else if ( $ avgScore == $ maxAvgScore ) array_push ( $ names , $ file [ $ i ] ) ; } for ( $ i = 0 ; $ i < count ( $ names ) ; $ i ++ ) { echo $ names [ $ i ] . " " ; } echo $ maxAvgScore ; } $ file = array ( " Shrikanth " , "20" , "30" , "10" , " Ram " , "100" , "50" , "10" ) ; $ n = count ( $ file ) ; getStudentsList ( $ file , $ n ) ; ? >


"Sub | Function that counts all the sub - strings of length ' k ' which have all identical characters ; count of sub - strings , length , initial position of sliding window ; map to store the frequency of the characters of sub - string ; increase the frequency of the character and length of the sub - string ; if the length of the sub - string is greater than K ; remove the character from the beginning of sub - string ; if the length of the sub string is equal to k and frequency of one of its characters is equal to the length of the sub - string i . e . all the characters are same increase the count ; display the number of valid sub - strings ; Driver code"
How can the above be solved in PHP?

 < ? php function solve ( $ s , $ k ) { $ count = 0 ; $ length = 0 ; $ pos = 0 ; $ m = array ( ) ; for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) { $ m [ $ s [ $ i ] ] ++ ; $ length ++ ; if ( $ length > $ k ) { $ m [ $ s [ $ pos ++ ] ] -- ; $ length -- ; } if ( $ length == $ k && $ m [ $ s [ $ i ] ] == $ length ) $ count ++ ; } echo $ count . "
 " ; } $ s = " aaaabbbccdddd " ; $ k = 4 ; solve ( $ s , $ k ) ; ? >


"Sub | Function that returns the index of next occurrence of the character c in string str starting from index start ; Starting from start ; If current character = c ; Not found ; Function to return the count of required sub - strings ; Stores running count of ' x ' starting from the end ; Next index of ' x ' starting from index 0 ; Next index of ' y ' starting from index 0 ; To store the count of required sub - strings ; If ' y ' appears before ' x ' it won 't contribute to a valid sub-string ; Find next occurrence of ' y ' ; If ' y ' appears after ' x ' every sub - string ending at an ' x ' appearing after this ' y ' and starting with the current ' x ' is a valid sub - string ; Find next occurrence of ' x ' ; Return the count ; Driver code"
How can the above be solved in PHP?

< ? php function nextIndex ( $ str , $ start , $ c ) { for ( $ i = $ start ; $ i < strlen ( $ str ) ; $ i ++ ) { if ( $ str [ $ i ] == $ c ) return $ i ; } return -1 ; } function countSubStrings ( $ str ) { $ n = strlen ( $ str ) ; $ countX = array ( 0 , $ n , NULL ) ; $ count = 0 ; for ( $ i = $ n - 1 ; $ i >= 0 ; $ i -- ) { if ( $ str [ $ i ] == ' x ' ) $ count ++ ; $ countX [ $ i ] = $ count ; } $ nextIndexX = nextIndex ( $ str , 0 , ' x ' ) ; $ nextIndexY = nextIndex ( $ str , 0 , ' y ' ) ; $ count = 0 ; while ( $ nextIndexX != -1 && $ nextIndexY != -1 ) { if ( $ nextIndexX > $ nextIndexY ) { $ nextIndexY = nextIndex ( $ str , $ nextIndexY + 1 , ' y ' ) ; continue ; } else { $ count += $ countX [ $ nextIndexY ] ; $ nextIndexX = nextIndex ( $ str , $ nextIndexX + 1 , ' x ' ) ; } } return $ count ; } $ s = " xyyxx " ; echo countSubStrings ( $ s ) ; ? >


"Sub | Function that returns true if every lowercase character appears atmost once ; every character frequency must be not greater than one ; Function that returns the modified good string if possible ; If the length of the string is less than n ; Sub - strings of length 26 ; To store frequency of each character ; Get the frequency of each character in the current sub - string ; Check if we can get sub - string containing all the 26 characters ; Find which character is missing ; Fill with missing characters ; Find the next missing character ; Return the modified good string ; Driver code"
How can the above be solved in PHP?

< ? php function valid ( & $ cnt ) { for ( $ i = 0 ; $ i < 26 ; $ i ++ ) { if ( $ cnt [ $ i ] >= 2 ) return false ; } return true ; } function getGoodString ( $ s , $ n ) { if ( $ n < 26 ) return " - 1" ; for ( $ i = 25 ; $ i < $ n ; $ i ++ ) { $ cnt = array_fill ( 0 , 26 , NULL ) ; for ( $ j = $ i ; $ j >= $ i - 25 ; $ j -- ) { if ( $ s [ $ j ] != ' ? ' ) $ cnt [ ord ( $ s [ $ j ] ) - ord ( ' a ' ) ] ++ ; } if ( valid ( $ cnt ) ) { $ cur = 0 ; while ( $ cur < 26 && $ cnt [ $ cur ] > 0 ) $ cur ++ ; for ( $ j = $ i - 25 ; $ j <= $ i ; $ j ++ ) { if ( $ s [ $ j ] == ' ? ' ) { $ s [ $ j ] = chr ( $ cur + ord ( ' a ' ) ) ; $ cur ++ ; while ( $ cur < 26 && $ cnt [ $ cur ] > 0 ) $ cur ++ ; } } return $ s ; } } return " - 1" ; } $ s = " abcdefghijkl ? nopqrstuvwxy ? " ; $ n = strlen ( $ s ) ; echo getGoodString ( $ s , $ n ) ; ? >


"Subarray / Substring vs Subsequence and Programs to Generate them | PHP code to generate all possible subsequences . Time Complexity O ( n * 2 ^ n ) ; Number of subsequences is ( 2 * * n - 1 ) ; Run from counter 000. . 1 to 111. . 1 ; Check if jth bit in the counter is set If set then print jth element from arr [ ] ; Driver Code"
How can the above be solved in PHP?

 < ? php function printSubsequences ( $ arr , $ n ) { $ opsize = pow ( 2 , $ n ) ; for ( $ counter = 1 ; $ counter < $ opsize ; $ counter ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ counter & ( 1 << $ j ) ) echo $ arr [ $ j ] , " ▁ " ; } echo "
 " ; } } $ arr = array ( 1 , 2 , 3 , 4 ) ; $ n = sizeof ( $ arr ) ; echo " All ▁ Non - empty ▁ Subsequences
 " ; printSubsequences ( $ arr , $ n ) ; ? >


"Subarray / Substring vs Subsequence and Programs to Generate them | Prints all subarrays in arr [ 0. . n - 1 ] ; Pick starting point ; Pick ending point ; Print subarray between current starting and ending points ; Driver Code"
How can the above be solved in PHP?

 < ? php function subArray ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = $ i ; $ j < $ n ; $ j ++ ) { for ( $ k = $ i ; $ k <= $ j ; $ k ++ ) echo $ arr [ $ k ] , " ▁ " ; echo "
 " ; } } } $ arr = array ( 1 , 2 , 3 , 4 ) ; $ n = sizeof ( $ arr ) ; echo " All ▁ Non - empty ▁ Subarrays
 " ; subArray ( $ arr , $ n ) ; ? >


"Subarray with no pair sum divisible by K | function to find the subarray with no pair sum divisible by k ; hash table to store the remainders obtained on dividing by K ; s : starting index of the current subarray , e : ending index of the current subarray , maxs : starting index of the maximum size subarray so far , maxe : ending index of the maximum size subarray so far ; insert the first element in the set ; Removing starting elements of current subarray while there is an element in set which makes a pair with mod [ i ] such that the pair sum is divisible . ; include the current element in the current subarray the ending index of the current subarray increments by one ; compare the size of the current subarray with the maximum size so far ; Driver Code"
How can the above be solved in PHP?

 < ? php function subarrayDivisibleByK ( $ arr , $ n , $ k ) { $ mp = array_fill ( 0 , 1000 , 0 ) ; $ s = 0 ; $ e = 0 ; $ maxs = 0 ; $ maxe = 0 ; $ mp [ $ arr [ 0 ] % $ k ] ++ ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ mod = $ arr [ $ i ] % $ k ; while ( $ mp [ $ k - $ mod ] != 0 || ( $ mod == 0 && $ mp [ $ mod ] != 0 ) ) { $ mp [ $ arr [ $ s ] % $ k ] -- ; $ s ++ ; } $ mp [ $ mod ] ++ ; $ e ++ ; if ( ( $ e - $ s ) > ( $ maxe - $ maxs ) ) { $ maxe = $ e ; $ maxs = $ s ; } } echo ( " The ▁ maximum ▁ size ▁ is ▁ " . ( $ maxe - $ maxs + 1 ) . " ▁ and ▁ the ▁ subarray ▁ is " . " ▁ as ▁ follows
 " ) ; for ( $ i = $ maxs ; $ i <= $ maxe ; $ i ++ ) echo ( $ arr [ $ i ] . " ▁ " ) ; } $ k = 3 ; $ arr = array ( 5 , 10 , 15 , 20 , 25 ) ; $ n = count ( $ arr ) ; subarrayDivisibleByK ( $ arr , $ n , $ k ) ; ? >


"Submatrix Sum Queries | Function to preprcess input mat [ M ] [ N ] . This function mainly fills aux [ M ] [ N ] such that aux [ i ] [ j ] stores sum of elements from ( 0 , 0 ) to ( i , j ) ; Copy first row of mat [ ] [ ] to aux [ ] [ ] ; Do column wise sum ; Do row wise sum ; A O ( 1 ) time function to compute sum of submatrix between ( tli , tlj ) and ( rbi , rbj ) using aux [ ] [ ] which is built by the preprocess function ; result is now sum of elements between ( 0 , 0 ) and ( rbi , rbj ) ; Remove elements between ( 0 , 0 ) and ( tli - 1 , rbj ) ; Remove elements between ( 0 , 0 ) and ( rbi , tlj - 1 ) ; Add aux [ tli - 1 ] [ tlj - 1 ] as elements between ( 0 , 0 ) and ( tli - 1 , tlj - 1 ) are subtracted twice ; Driver Code"
How can the above be solved in PHP?


"Subsequences of size three in an array whose sum is divisible by m | Brute Force Implementation ( Time Complexity : O ( N ^ 3 ) ) PHP program to find count of subsequences of size three divisible by M . ; Three nested loop to find all the sub sequences of length three in the given array A [ ] . ; checking if the sum of the chosen three number is divisible by m . ; Driver code"
How can the above be solved in PHP?


"Subsequences of size three in an array whose sum is divisible by m | O ( M ^ 2 ) time complexity PHP program to find count of subsequences of size three divisible by M . ; Storing frequencies of all remainders when divided by M . ; including i and j in the sum rem calculate the remainder required to make the sum divisible by M ; if the required number is less than j , we skip as we have already calculated for that value before . As j here starts with i and rem is less than j . ; if satisfies the first case . ; if satisfies the second case ; if satisfies the third case ; Driver code"
How can the above be solved in PHP?


"Subset with sum divisible by m | Returns true if there is a subset of arr [ ] with sum divisible by m ; This array will keep track of all the possible sum ( after modulo m ) which can be made using subsets of arr [ ] initialising boolean array with all false ; we 'll loop through all the elements of arr[] ; anytime we encounter a sum divisible by m , we are done ; To store all the new encountered sum ( after modulo ) . It is used to make sure that arr [ i ] is added only to those entries for which DP [ j ] was true before current iteration . ; For each element of arr [ ] , we loop through all elements of DP table from 1 to m and we add current element i . e . , arr [ i ] to all those elements which are true in DP table ; if an element is true in DP table ; We update it in temp and update to DP once loop of j is over ; Updating all the elements of temp to DP table since iteration over j is over ; Also since arr [ i ] is a single element subset , arr [ i ] % m is one of the possible sum ; Driver Code"
How can the above be solved in PHP?

 < ? php function modularSum ( $ arr , $ n , $ m ) { if ( $ n > $ m ) return true ; $ DP = Array_fill ( 0 , $ m , false ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ DP [ 0 ] ) return true ; $ temp = array_fill ( 0 , $ m , false ) ; for ( $ j = 0 ; $ j < $ m ; $ j ++ ) { if ( $ DP [ $ j ] == true ) { if ( $ DP [ ( $ j + $ arr [ $ i ] ) % $ m ] == false ) $ temp [ ( $ j + $ arr [ $ i ] ) % $ m ] = true ; } } for ( $ j = 0 ; $ j < $ m ; $ j ++ ) if ( $ temp [ $ j ] ) $ DP [ $ j ] = true ; $ DP [ $ arr [ $ i ] % $ m ] = true ; } return $ DP [ 0 ] ; } $ arr = array ( 1 , 7 ) ; $ n = sizeof ( $ arr ) ; $ m = 5 ; if ( modularSum ( $ arr , $ n , $ m ) == true ) echo " YES
 " ; else echo " NO
 " ; ? >


"Sum and Product of digits in a number that divide the number | Print the sum and product of digits that divides the number . ; Fetching each digit of the number ; Checking if digit is greater than 0 and can divides n . ; Driver code"
How can the above be solved in PHP?

 < ? php function countDigit ( $ n ) { $ temp = $ n ; $ sum = 0 ; $ product = 1 ; while ( $ temp != 0 ) { $ d = $ temp % 10 ; $ temp = ( int ) ( $ temp / 10 ) ; if ( $ d > 0 && $ n % $ d == 0 ) { $ sum += $ d ; $ product *= $ d ; } } echo " Sum = " . $ sum ;
 	 echo ▁ " Product = " } $ n = 1012 ; countDigit ( $ n ) ; ? >


"Sum and Product of minimum and maximum element of an Array | Function to find minimum element ; Function to find maximum element ; Function to get Sum ; Function to get product ; Driver Code ; Sum of min and max element ; Product of min and max element"
How can the above be solved in PHP?

 < ? php function getMin ( $ arr , $ n ) { $ res = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ res = min ( $ res , $ arr [ $ i ] ) ; return $ res ; } function getMax ( $ arr , $ n ) { $ res = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ res = max ( $ res , $ arr [ $ i ] ) ; return $ res ; } function findSum ( $ arr , $ n ) { $ min = getMin ( $ arr , $ n ) ; $ max = getMax ( $ arr , $ n ) ; return $ min + $ max ; } function findProduct ( $ arr , $ n ) { $ min = getMin ( $ arr , $ n ) ; $ max = getMax ( $ arr , $ n ) ; return $ min * $ max ; } $ arr = array ( 12 , 1234 , 45 , 67 , 1 ) ; $ n = sizeof ( $ arr ) ; echo " Sum = " ▁ . ▁ findSum ( $ arr , ▁ $ n ) ▁ . ▁ " " ;
 echo ▁ " Product = "


"Sum of ( maximum element | PHP implementation of the above approach ; Function to return a ^ n % mod ; Compute sum of max ( A ) - min ( A ) for all subsets ; Sort the array . ; Maxs = 2 ^ i - 1 ; Mins = 2 ^ ( n - 1 - i ) - 1 ; Driver code"
How can the above be solved in PHP?

< ? php $ mod = 1000000007 ; function power ( $ a , $ n ) { global $ mod ; if ( $ n == 0 ) return 1 ; $ p = power ( $ a , $ n / 2 ) % $ mod ; $ p = ( $ p * $ p ) % $ mod ; if ( $ n & 1 ) { $ p = ( $ p * $ a ) % $ mod ; } return $ p ; } function computeSum ( & $ arr , $ n ) { global $ mod ; sort ( $ arr ) ; $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ maxs = ( power ( 2 , $ i ) - 1 + $ mod ) % $ mod ; $ maxs = ( $ maxs * $ arr [ $ i ] ) % $ mod ; $ mins = ( power ( 2 , $ n - 1 - $ i ) - 1 + $ mod ) % $ mod ; $ mins = ( $ mins * $ arr [ $ i ] ) % $ mod ; $ V = ( $ maxs - $ mins + $ mod ) % $ mod ; $ sum = ( $ sum + $ V ) % $ mod ; } return $ sum ; } $ arr = array ( 4 , 3 , 1 ) ; $ n = sizeof ( $ arr ) ; echo computeSum ( $ arr , $ n ) ; ? >


"Sum of Areas of Rectangles possible for an array | Function to find area of rectangles ; sorting the array in descending order ; store the final sum of all the rectangles area possible ; temporary variable to store the length of rectangle ; Selecting the length of rectangle so that difference between any two number is 1 only . Here length is selected so flag is set ; flag is set means we have got length of rectangle ; length is set to a [ i + 1 ] so that if a [ i + 1 ] is less than a [ i ] by 1 then also we have the correct chice for length ; incrementing the counter one time more as we have considered a [ i + 1 ] element also so . ; Selecting the width of rectangle so that difference between any two number is 1 only . Here width is selected so now flag is again unset for next rectangle ; area is calculated for rectangle ; flag is set false for another rectangle which we can get from elements in array ; incrementing the counter one time more as we have considered a [ i + 1 ] element also so . ; Driver code"
How can the above be solved in PHP?


"Sum of Arithmetic Geometric Sequence | Return the sum of first n term of AGP ; finding the each term of AGP and adding it to sum . ; Driver Code"
How can the above be solved in PHP?


"Sum of Binomial coefficients | Returns value of Binomial Coefficient Sum which is 2 raised to power n . ; Driver Code"
How can the above be solved in PHP?

< ? php function binomialCoeffSum ( $ n ) { return ( 1 << $ n ) ; } $ n = 4 ; echo binomialCoeffSum ( $ n ) ; ? >


"Sum of Bitwise And of all pairs in a given array | Returns value of " arr [ 0 ] ▁ & ▁ arr [ 1 ] ▁ + ▁ arr [ 0 ] ▁ & ▁ arr [ 2 ] ▁ + ▁ . . . ▁ arr [ i ] ▁ & ▁ arr [ j ] ▁ + ▁ . . . . . ▁ arr [ n - 2 ] ▁ & ▁ arr [ n - 1 ] " ; Initialize result ; Consider all pairs ( arr [ i ] , arr [ j ) such that i < j ; Driver Code"
How can the above be solved in PHP?

 < ? php function pairAndSum ( $ arr , $ n ) { $ ans = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) $ ans += $ arr [ $ i ] & $ arr [ $ j ] ; return $ ans ; } $ arr = array ( 5 , 10 , 15 ) ; $ n = sizeof ( $ arr ) ; echo pairAndSum ( $ arr , $ n ) , "
 " ; ? >


"Sum of Bitwise And of all pairs in a given array | Returns value of " arr [ 0 ] ▁ & ▁ arr [ 1 ] ▁ + ▁ arr [ 0 ] ▁ & ▁ arr [ 2 ] ▁ + ▁ . . . ▁ arr [ i ] ▁ & ▁ arr [ j ] ▁ + ▁ . . . . . ▁ arr [ n - 2 ] ▁ & ▁ arr [ n - 1 ] " ; Initialize result ; Traverse over all bits ; Count number of elements with i 'th bit set  Initialize the count ; There are k set bits , means k ( k - 1 ) / 2 pairs . Every pair adds 2 ^ i to the answer . Therefore , we add "2 ^ i ▁ * ▁ [ k * ( k - 1 ) / 2 ] " to the answer . ; Driver Code"
How can the above be solved in PHP?


"Sum of Bitwise OR of all pairs in a given array | Returns value of " arr [ 0 ] ▁ | ▁ arr [ 1 ] ▁ + ▁ arr [ 0 ] ▁ | ▁ arr [ 2 ] ▁ + ▁ . . . ▁ arr [ i ] ▁ | ▁ arr [ j ] ▁ + ▁ . . . . . ▁ arr [ n - 2 ] ▁ | ▁ arr [ n - 1 ] " ; Initialize result ; Consider all pairs ( arr [ i ] , arr [ j ) such that i < j ; Driver Code"
How can the above be solved in PHP?

 < ? php function pairORSum ( $ arr , $ n ) { $ ans = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) $ ans += $ arr [ $ i ] | $ arr [ $ j ] ; return $ ans ; } $ arr = array ( 1 , 2 , 3 , 4 ) ; $ n = sizeof ( $ arr ) ; echo pairORSum ( $ arr , $ n ) , "
 " ; ? >


"Sum of Digits in a ^ n till a single digit | This function finds single digit sum of n . ; Returns single digit sum of a ^ n . We use modular exponentiation technique . ; Driver code"
How can the above be solved in PHP?

< ? php function digSum ( $ n ) { if ( $ n == 0 ) return 0 ; return ( $ n % 9 == 0 ) ? 9 : ( $ n % 9 ) ; } function powerDigitSum ( $ a , $ n ) { $ res = 1 ; while ( $ n ) { if ( $ n % 2 == 1 ) { $ res = $ res * digSum ( $ a ) ; $ res = digSum ( $ res ) ; } $ a = digSum ( digSum ( $ a ) * digSum ( $ a ) ) ; $ n /= 2 ; } return $ res ; } $ a = 9 ; $ n = 4 ; echo powerDigitSum ( $ a , $ n ) ; ? >


"Sum of Factors of a Number using Prime Factorization | Using SieveOfEratosthenes to find smallest prime factor of all the numbers . For example , if N is 10 , s [ 2 ] = s [ 4 ] = s [ 6 ] = s [ 10 ] = 2 s [ 3 ] = s [ 9 ] = 3 s [ 5 ] = 5 s [ 7 ] = 7 ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries in it as false . ; Initializing smallest factor equal to 2 for all the even numbers ; For odd numbers less then equal to n ; s ( i ) for a prime is the number itself ; For all multiples of current prime number ; i is the smallest prime factor for number " i * j " . ; Function to find sum of all prime factors ; Declaring array to store smallest prime factor of i at i - th index ; Filling values in s [ ] using sieve ; Current prime factor of N ; Power of current prime factor ; N is now N / s [ N ] . If new N als has smallest prime factor as currFactor , increment power ; Update current prime factor as s [ N ] and initializing power of factor as 1. ; Driver code"
How can the above be solved in PHP?

< ? php function sieveOfEratosthenes ( $ N , & $ s ) { $ prime = array_fill ( 0 , $ N + 1 , false ) ; for ( $ i = 2 ; $ i <= $ N ; $ i += 2 ) $ s [ $ i ] = 2 ; for ( $ i = 3 ; $ i <= $ N ; $ i += 2 ) { if ( $ prime [ $ i ] == false ) { $ s [ $ i ] = $ i ; for ( $ j = $ i ; $ j * $ i <= $ N ; $ j += 2 ) { if ( $ prime [ $ i * $ j ] == false ) { $ prime [ $ i * $ j ] = true ; $ s [ $ i * $ j ] = $ i ; } } } } } function findSum ( $ N ) { $ s = array_fill ( 0 , $ N + 1 , 0 ) ; $ ans = 1 ; sieveOfEratosthenes ( $ N , $ s ) ; $ currFactor = $ s [ $ N ] ; $ power = 1 ; while ( $ N > 1 ) { $ N /= $ s [ $ N ] ; if ( $ currFactor == $ s [ $ N ] ) { $ power ++ ; continue ; } $ sum = 0 ; for ( $ i = 0 ; $ i <= $ power ; $ i ++ ) $ sum += ( int ) pow ( $ currFactor , $ i ) ; $ ans *= $ sum ; $ currFactor = $ s [ $ N ] ; $ power = 1 ; } return $ ans ; } $ n = 12 ; echo " Sum ▁ of ▁ the ▁ factors ▁ is ▁ : ▁ " ; echo findSum ( $ n ) ; ? >


"Sum of Fibonacci Numbers in a range | Function to return the nth Fibonacci number ; Function to return the required sum ; To store the sum ; Calculate the sum ; Driver code"
How can the above be solved in PHP?

< ? php function fib ( $ n ) { $ phi = ( 1 + sqrt ( 5 ) ) / 2 ; return ( int ) round ( pow ( $ phi , $ n ) / sqrt ( 5 ) ) ; } function calculateSum ( $ l , $ r ) { $ sum = 0 ; for ( $ i = $ l ; $ i <= $ r ; $ i ++ ) $ sum += fib ( $ i ) ; return $ sum ; } $ l = 4 ; $ r = 8 ; echo calculateSum ( $ l , $ r ) ; ? >


"Sum of Fibonacci Numbers in a range | Function to return the nth Fibonacci number ; Function to return the required sum ; Using our deduced result ; Driver code"
How can the above be solved in PHP?

< ? php function fib ( $ n ) { $ phi = ( 1 + sqrt ( 5 ) ) / 2 ; return ( int ) round ( pow ( $ phi , $ n ) / sqrt ( 5 ) ) ; } function calculateSum ( $ l , $ r ) { $ sum = fib ( $ r + 2 ) - fib ( $ l + 1 ) ; return $ sum ; } $ l = 4 ; $ r = 8 ; echo ( calculateSum ( $ l , $ r ) ) ; ? >


"Sum of Fibonacci Numbers | Computes value of first fibonacci numbers ; Initialize result ; Add remaining terms ; Driver Code"
How can the above be solved in PHP?

 < ? php function calculateSum ( $ n ) { if ( $ n <= 0 ) return 0 ; $ fibo [ 0 ] = 0 ; $ fibo [ 1 ] = 1 ; $ sum = $ fibo [ 0 ] + $ fibo [ 1 ] ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { $ fibo [ $ i ] = $ fibo [ $ i - 1 ] + $ fibo [ $ i - 2 ] ; $ sum += $ fibo [ $ i ] ; } return $ sum ; } $ n = 4 ; echo " Sum ▁ of ▁ Fibonacci ▁ numbers ▁ is ▁ : ▁ " , calculateSum ( $ n ) , "
 " ; ? >


"Sum of Fibonacci Numbers | PHP Program to find sum of Fibonacci numbers in O ( Log n ) time . ; Create an array for memoization ; Returns n 'th Fibonacci number using table f[] ; Base cases ; If fib ( n ) is already computed ; Applying above formula [ Note value n & 1 is 1 if n is odd , else 0 ] . ; Computes value of first Fibonacci numbers ; Driver Code"
How can the above be solved in PHP?

< ? php $ MAX = 1000 ; $ f = array_fill ( 0 , $ MAX , 0 ) ; function fib ( $ n ) { global $ f ; if ( $ n == 0 ) return 0 ; if ( $ n == 1 $ n == 2 ) return ( $ f [ $ n ] = 1 ) ; if ( $ f [ $ n ] ) return $ f [ $ n ] ; $ k = ( $ n & 1 ) ? ( $ n + 1 ) / 2 : $ n / 2 ; $ f [ $ n ] = ( $ n & 1 ) ? ( fib ( $ k ) * fib ( $ k ) + fib ( $ k - 1 ) * fib ( $ k - 1 ) ) : ( 2 * fib ( $ k - 1 ) + fib ( $ k ) ) * fib ( $ k ) ; return $ f [ $ n ] ; } function calculateSum ( $ n ) { return fib ( $ n + 2 ) - 1 ; } $ n = 4 ; print ( " Sum ▁ of ▁ Fibonacci ▁ numbers ▁ is ▁ : ▁ " . calculateSum ( $ n ) ) ; ? >


"Sum of Fibonacci numbers at even indexes upto N terms | Computes value of first fibonacci numbers and stores the even - indexed sum ; Initialize result ; Add remaining terms ; For even indices ; Return the alternting sum ; Get n ; Find the even - indiced sum"
How can the above be solved in PHP?

 < ? php function calculateEvenSum ( $ n ) { if ( $ n <= 0 ) return 0 ; $ fibo [ 2 * $ n + 1 ] = array ( ) ; $ fibo [ 0 ] = 0 ; $ fibo [ 1 ] = 1 ; $ sum = 0 ; for ( $ i = 2 ; $ i <= 2 * $ n ; $ i ++ ) { $ fibo [ $ i ] = $ fibo [ $ i - 1 ] + $ fibo [ $ i - 2 ] ; if ( $ i % 2 == 0 ) $ sum += $ fibo [ $ i ] ; } return $ sum ; } $ n = 8 ; echo " Even ▁ indexed ▁ Fibonacci ▁ Sum ▁ upto ▁ " . $ n . " ▁ terms : ▁ " . calculateEvenSum ( $ n ) . "
 " ; ? >


"Sum of LCM ( 1 , n ) , LCM ( 2 , n ) , LCM ( 3 , n ) , ... , LCM ( n , n ) | PHP implementation of the approach ; Euler totient Function ; Function to return the required LCM sum ; Summation of d * ETF ( d ) where d belongs to set of divisors of n ; Driver code"
How can the above be solved in PHP?


"Sum of Manhattan distances between all pairs of points | Return the sum of distance between all the pair of points . ; for each point , finding distance to rest of the point ; Driver Code"
How can the above be solved in PHP?


"Sum of Manhattan distances between all pairs of points | Return the sum of distance of one axis . ; sorting the array . ; for each point , finding the distance . ; Driver Code"
How can the above be solved in PHP?

< ? php function distancesum ( $ arr , $ n ) { sort ( $ arr ) ; $ res = 0 ; $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ res += ( $ arr [ $ i ] * $ i - $ sum ) ; $ sum += $ arr [ $ i ] ; } return $ res ; } function totaldistancesum ( $ x , $ y , $ n ) { return distancesum ( $ x , $ n ) + distancesum ( $ y , $ n ) ; } $ x = array ( -1 , 1 , 3 , 2 ) ; $ y = array ( 5 , 6 , 5 , 3 ) ; $ n = sizeof ( $ x ) ; echo totaldistancesum ( $ x , $ y , $ n ) , " " ; ? >


"Sum of P terms of an AP if Mth and Nth terms are given | Function to calculate the value of the ; Calculate value of d using formula ; Calculate value of a using formula ; Return pair ; Function to calculate value sum of first p numbers of the series ; First calculate value of a and d ; Calculate the sum by using formula ; Return the sum ; Driver Code"
How can the above be solved in PHP?

< ? php function findingValues ( $ m , $ n , $ mth , $ nth ) { $ d = ( abs ( $ mth - $ nth ) ) / abs ( ( $ m - 1 ) - ( $ n - 1 ) ) ; $ a = $ mth - ( ( $ m - 1 ) * $ d ) ; return array ( $ a , $ d ) ; } function findSum ( $ m , $ n , $ mth , $ nth , $ p ) { $ ad = findingValues ( $ m , $ n , $ mth , $ nth ) ; $ a = $ ad [ 0 ] ; $ d = $ ad [ 1 ] ; $ sum = ( $ p * ( 2 * $ a + ( $ p - 1 ) * $ d ) ) / 2 ; return $ sum ; } $ m = 6 ; $ n = 10 ; $ mTerm = 12 ; $ nTerm = 20 ; $ p = 5 ; echo findSum ( $ m , $ n , $ mTerm , $ nTerm , $ p ) ; ? >


"Sum of Perrin Numbers | function for sum of first n Perrin number . ; if ( $n == 0 ) n = 0 ; if ( $n == 1 ) n = 1 ; if ( $n == 2 ) n = 2 ; calculate k = 5 sum of three previous step . ; Sum remaining numbers ; calculate next term ; Driver code"
How can the above be solved in PHP?

< ? php function calSum ( $ n ) { $ a = 3 ; $ b = 0 ; $ c = 2 ; return 3 ; return 3 ; return 5 ; $ sum = 5 ; while ( $ n > 2 ) { $ d = $ a + $ b ; $ sum += $ d ; $ a = $ b ; $ b = $ c ; $ c = $ d ; $ n -- ; } return $ sum ; } $ n = 9 ; echo calSum ( $ n ) ; ? >


"Sum of Series ( n ^ 2 | function that calculate the sum of the nth series ; using formula of the nth term ; Driver Code"
How can the above be solved in PHP?

< ? php function sum_series ( $ n ) { $ nSquare = $ n * $ n ; return $ nSquare * ( $ nSquare - 1 ) / 4 ; } $ n = 2 ; echo ( sum_series ( $ n ) ) ; ? >


"Sum of XOR of all subarrays | Function to calculate the sum of XOR of all subarrays ; variable to store the final sum ; multiplier ; variable to store number of sub - arrays with odd number of elements with ith bits starting from the first element to the end of the array ; variable to check the status of the odd - even count while calculating c_odd ; loop to calculate initial value of c_odd ; loop to iterate through all the elements of the array and update sum ; updating the multiplier ; returning the sum ; Driver Code"
How can the above be solved in PHP?


"Sum of XOR of sum of all pairs in an array | PHP program to find XOR of pair sums . ; Driver Code"
How can the above be solved in PHP?

< ? php function xor_pair_sum ( $ ar , $ n ) { $ total = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ total = $ total ^ $ ar [ $ i ] ; return ( 2 * $ total ) ; } $ data = array ( 1 , 2 , 3 ) ; $ n = sizeof ( $ data ) ; echo xor_pair_sum ( $ data , $ n ) ; ? >


"Sum of all Submatrices of a Given Matrix | Function to find the sum of all possible submatrices of a given Matrix ; Variable to store the required sum ; Nested loop to find the number of submatrices , each number belongs to ; Number of ways to choose from top - left elements ; Number of ways to choose from bottom - right elements ; Driver Code"
How can the above be solved in PHP?

< ? php function matrixSum ( $ arr ) { $ n = 3 ; $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { $ top_left = ( $ i + 1 ) * ( $ j + 1 ) ; $ bottom_right = ( $ n - $ i ) * ( $ n - $ j ) ; $ sum += ( $ top_left * $ bottom_right * $ arr [ $ i ] [ $ j ] ) ; } return $ sum ; } $ arr = array ( array ( 1 , 1 , 1 ) , array ( 1 , 1 , 1 ) , array ( 1 , 1 , 1 ) ) ; echo matrixSum ( $ arr ) ; ? >


"Sum of all divisors from 1 to n | Utility function to find sum of all divisor of number up to ' n ' ; Driver code"
How can the above be solved in PHP?

 < ? php function divisorSum ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; ++ $ i ) $ sum += floor ( $ n / $ i ) * $ i ; return $ sum ; } $ n = 4 ; echo divisorSum ( $ n ) , "
 " ; $ n = 5 ; echo divisorSum ( $ n ) , "
 " ; ? >


"Sum of all divisors from 1 to n | Utility function to find sum of all divisor of number up to ' n ' ; Find all divisors of i and add them ; Driver code"
How can the above be solved in PHP?

 < ? php function divisorSum ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; ++ $ i ) { for ( $ j = 1 ; $ j * $ j <= $ i ; ++ $ j ) { if ( $ i % $ j == 0 ) { if ( $ i / $ j == $ j ) $ sum += $ j ; else $ sum += $ j + $ i / $ j ; } } } return $ sum ; } $ n = 4 ; echo " " , ▁ divisorSum ( $ n ) , ▁ " " $ n = 5 ; echo divisorSum ( $ n ) , "
 " ; ? >


"Sum of all elements between k1 ' th ▁ and ▁ k2' th smallest elements | Returns sum between two kth smallest elements of the array ; Sort the given array ; Below code is equivalent to ; Driver program"
How can the above be solved in PHP?

< ? php function sumBetweenTwoKth ( $ arr , $ n , $ k1 , $ k2 ) { sort ( $ arr ) ; $ result = 0 ; for ( $ i = $ k1 ; $ i < $ k2 - 1 ; $ i ++ ) $ result += $ arr [ $ i ] ; return $ result ; } $ arr = array ( 20 , 8 , 22 , 4 , 12 , 10 , 14 ) ; $ k1 = 3 ; $ k2 = 6 ; $ n = count ( $ arr ) ; ; echo sumBetweenTwoKth ( $ arr , $ n , $ k1 , $ k2 ) ; ? >


"Sum of all elements up to Nth row in a Pascal triangle | Function to find sum of all elements upto nth row . ; Initialize sum with 0 ; Calculate 2 ^ n ; Driver Code"
How can the above be solved in PHP?

< ? php function calculateSum ( $ n ) { $ sum = 0 ; $ sum = 1 << $ n ; return ( $ sum - 1 ) ; } $ n = 10 ; echo " ▁ Sum ▁ of ▁ all ▁ elements : " , calculateSum ( $ n ) ; ? >


"Sum of all elements up to Nth row in a Pascal triangle | Function to find sum of all elements upto nth row . ; Initialize sum with 0 ; Loop to calculate power of 2 upto n and add them ; Driver Code"
How can the above be solved in PHP?

< ? php function calculateSum ( $ n ) { $ sum = 0 ; for ( $ row = 0 ; $ row < $ n ; $ row ++ ) { $ sum = $ sum + ( 1 << $ row ) ; } return $ sum ; } $ n = 10 ; echo " ▁ Sum ▁ of ▁ all ▁ elements ▁ : ▁ " . calculateSum ( $ n ) ; ? >


"Sum of all even factors of numbers in the range [ l , r ] | PHP implementation of the approach ; Function to calculate the prefix sum of all the even factors ; Add i to all the multiples of i ; Update the prefix sum ; Function to return the sum of all the even factors of the numbers in the given range ; Driver code"
How can the above be solved in PHP?

< ? php $ MAX = 10000 ; $ prefix = array_fill ( 0 , $ MAX , 0 ) ; function sieve_modified ( ) { global $ MAX , $ prefix ; for ( $ i = 2 ; $ i < $ MAX ; $ i += 2 ) { for ( $ j = $ i ; $ j < $ MAX ; $ j += $ i ) $ prefix [ $ j ] += $ i ; } for ( $ i = 1 ; $ i < $ MAX ; $ i ++ ) $ prefix [ $ i ] += $ prefix [ $ i - 1 ] ; } function sumEvenFactors ( $ L , $ R ) { global $ MAX , $ prefix ; return ( $ prefix [ $ R ] - $ prefix [ $ L - 1 ] ) ; } sieve_modified ( ) ; $ l = 6 ; $ r = 10 ; echo sumEvenFactors ( $ l , $ r ) ; ? >


"Sum of all even numbers in range L and R | Function to return the sum of all natural numbers ; Function to return sum of even numbers in range L and R ; Driver Code"
How can the above be solved in PHP?

< ? php function sumNatural ( $ n ) { $ sum = ( $ n * ( $ n + 1 ) ) ; return $ sum ; } function sumEven ( $ l , $ r ) { return sumNatural ( ( int ) ( $ r / 2 ) ) - sumNatural ( ( int ) ( ( $ l - 1 ) / 2 ) ) ; } $ l = 2 ; $ r = 5 ; echo " Sum ▁ of ▁ Natural ▁ numbers ▁ " . " from ▁ L ▁ to ▁ R ▁ is ▁ " . sumEven ( $ l , $ r ) ; ? >


"Sum of all i such that ( 2 ^ i + 1 ) % 3 = 0 where i is in range [ 1 , n ] | Function to return the required sum ; Total odd numbers from 1 to n ; Sum of first n odd numbers ; Driver code"
How can the above be solved in PHP?

< ? php function sumN ( $ n ) { $ n = ( int ) ( ( $ n + 1 ) / 2 ) ; return ( $ n * $ n ) ; } $ n = 3 ; echo sumN ( $ n ) ; ? >


"Sum of all natural numbers in range L to R | Function to return the sum of all natural numbers ; Function to return the sum of all numbers in range L and R ; Driver Code"
How can the above be solved in PHP?

< ? php function sumNatural ( $ n ) { $ sum = ( $ n * ( $ n + 1 ) ) / 2 ; return $ sum ; } function suminRange ( $ l , $ r ) { return sumNatural ( $ r ) - sumNatural ( $ l - 1 ) ; } $ l = 2 ; $ r = 5 ; echo " Sum ▁ of ▁ Natural ▁ numbers ▁ " . " from ▁ L ▁ to ▁ R ▁ is ▁ " , suminRange ( $ l , $ r ) ; ? >


"Sum of all odd factors of numbers in the range [ l , r ] | PHP implementation of the approach ; Function to calculate the prefix sum of all the odd factors ; Add i to all the multiples of i ; Update the prefix sum ; Function to return the sum of all the odd factors of the numbers in the given range ; Driver code"
How can the above be solved in PHP?

< ? php $ MAX = 10001 ; $ prefix = array_fill ( 0 , $ MAX , 0 ) ; function sieve_modified ( ) { global $ prefix , $ MAX ; for ( $ i = 1 ; $ i < $ MAX ; $ i += 2 ) { for ( $ j = $ i ; $ j < $ MAX ; $ j += $ i ) $ prefix [ $ j ] += $ i ; } for ( $ i = 1 ; $ i < $ MAX ; $ i ++ ) $ prefix [ $ i ] += $ prefix [ $ i - 1 ] ; } function sumOddFactors ( $ L , $ R ) { global $ prefix ; return ( $ prefix [ $ R ] - $ prefix [ $ L - 1 ] ) ; } sieve_modified ( ) ; $ l = 6 ; $ r = 10 ; echo sumOddFactors ( $ l , $ r ) ;


"Sum of all odd natural numbers in range L and R | Function to return the sum of all odd natural numbers ; Function to return the sum of all odd numbers in range L and R ; Driver Code"
How can the above be solved in PHP?

< ? php function sumOdd ( $ n ) { $ terms = ( int ) ( $ n + 1 ) / 2 ; $ sum = $ terms * $ terms ; return $ sum ; } function suminRange ( $ l , $ r ) { return sumOdd ( $ r ) - sumOdd ( $ l - 1 ) ; } $ l = 2 ; $ r = 5 ; echo " Sum ▁ of ▁ odd ▁ natural ▁ numbers ▁ from ▁ L ▁ to ▁ R ▁ is ▁ " , suminRange ( $ l , $ r ) ; ? >


"Sum of all subsets of a set formed by first n natural numbers | PHP program to find sum of all subsets of a set ; sum of subsets is ( n * ( n + 1 ) / 2 ) * pow ( 2 , n - 1 ) ; Driver Code"
How can the above be solved in PHP?

< ? php function findSumSubsets ( $ n ) { return ( $ n * ( $ n + 1 ) / 2 ) * ( 1 << ( $ n - 1 ) ) ; } $ n = 3 ; echo findSumSubsets ( $ n ) ; ? >


"Sum of all substrings of a string representing a number | Set 1 | Method to convert character digit to integer digit ; Returns sum of all substring of num ; allocate memory equal to length of string ; initialize first value with first digit ; loop over all digits of string ; update each sumofdigit from previous value ; add current value to the result ; Driver Code"
How can the above be solved in PHP?

< ? php function toDigit ( $ ch ) { return ( $ ch - '0' ) ; } function sumOfSubstrings ( $ num ) { $ n = strlen ( $ num ) ; $ sumofdigit [ $ n ] = 0 ; $ sumofdigit [ 0 ] = toDigit ( $ num [ 0 ] ) ; $ res = $ sumofdigit [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ numi = toDigit ( $ num [ $ i ] ) ; $ sumofdigit [ $ i ] = ( $ i + 1 ) * $ numi + 10 * $ sumofdigit [ $ i - 1 ] ; $ res += $ sumofdigit [ $ i ] ; } return $ res ; } $ num = "1234" ; echo sumOfSubstrings ( $ num ) ; ? >


"Sum of all substrings of a string representing a number | Set 2 ( Constant Extra Space ) | Returns sum of all substring of num ; Initialize result ; Here traversing the array in reverse order . Initializing loop from last element . mf is multiplying factor . ; Each time sum is added to its previous sum . Multiplying the three factors as explained above . s [ i ] - '0' is done to convert char to int . ; Making new multiplying factor as explained above . ; Driver Code"
How can the above be solved in PHP?

 < ? php function sumOfSubstrings ( $ num ) { $ sum = 0 ; $ mf = 1 ; for ( $ i = strlen ( $ num ) - 1 ; $ i >= 0 ; $ i -- ) { $ sum += ( $ num [ $ i ] - '0' ) * ( $ i + 1 ) * $ mf ; $ mf = $ mf * 10 + 1 ; } return $ sum ; } $ num = "6759" ; echo sumOfSubstrings ( $ num ) , "
 " ; ? >


"Sum of all the multiples of 3 and 7 below N | Function to find sum of AP series ; Number of terms ; Function to find the sum of all multiples of 3 and 7 below N ; Since , we need the sum of multiples less than N ; Driver code"
How can the above be solved in PHP?

< ? php function sumAP ( $ n , $ d ) { $ n = ( int ) ( $ n / $ d ) ; return ( $ n ) * ( 1 + $ n ) * ( $ d / 2 ) ; } function sumMultiples ( $ n ) { $ n -- ; return sumAP ( $ n , 3 ) + sumAP ( $ n , 7 ) - sumAP ( $ n , 21 ) ; } $ n = 24 ; echo sumMultiples ( $ n ) ; ? >


"Sum of array elements that is first continuously increasing then decreasing | Efficient PHP method to find sum of the elements of array that is halfway increasing and then halfway decreassing ; Driver code"
How can the above be solved in PHP?


"Sum of average of all subsets | Returns value of Binomial Coefficient C ( n , k ) ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; method returns sum of average of all subsets ; Initialize result ; Find sum of elements ; looping once for all subset of same size ; each element occurs nCr ( N - 1 , n - 1 ) times while considering subset of size n ; Driver Code"
How can the above be solved in PHP?


"Sum of bit differences among all pairs | PHP program to compute sum of pairwise bit differences ; Initialize result ; traverse over all bits ; count number of elements with i 'th bit set ; Add " count ▁ * ▁ ( n ▁ - ▁ count ) ▁ * ▁ 2" to the answer ; Driver Code"
How can the above be solved in PHP?

 < ? php function sumBitDifferences ( $ arr , $ n ) { $ ans = 0 ; for ( $ i = 0 ; $ i < 32 ; $ i ++ ) { $ count = 0 ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) if ( ( $ arr [ $ j ] & ( 1 << $ i ) ) ) $ count ++ ; $ ans += ( $ count * ( $ n - $ count ) * 2 ) ; } return $ ans ; } $ arr = array ( 1 , 3 , 5 ) ; $ n = sizeof ( $ arr ) ; echo sumBitDifferences ( $ arr , $ n ) , "
 " ; ? >


"Sum of bitwise AND of all possible subsets of given set | PHP program to calculate sum of Bit - wise and sum of all subsets of an array ; assuming representation of each element is in 32 bit ; iterating array element ; Counting the set bit of array in ith position ; counting subset which produce sum when particular bit position is set . ; multiplying every position subset with 2 ^ i to count the sum . ; Driver code"
How can the above be solved in PHP?

< ? php $ BITS = 32 ; function andSum ( $ arr , $ n ) { global $ BITS ; $ ans = 0 ; for ( $ i = 0 ; $ i < $ BITS ; $ i ++ ) { $ countSetBits = 0 ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ arr [ $ j ] & ( 1 << $ i ) ) $ countSetBits ++ ; } $ subset = ( 1 << $ countSetBits ) - 1 ; $ subset = ( $ subset * ( 1 << $ i ) ) ; $ ans += $ subset ; } return $ ans ; } $ arr = array ( 1 , 2 , 3 ) ; $ size = count ( $ arr ) ; echo andSum ( $ arr , $ size ) ; ? >


"Sum of bitwise AND of all subarrays | Function to find the sum of bitwise AND of all subarrays ; variable to store the final sum ; multiplier ; variable to check if counting is on ; variable to store the length of the subarrays ; loop to find the contiguous segments ; updating the multiplier ; returning the sum ; Driver Code"
How can the above be solved in PHP?


"Sum of both diagonals of a spiral odd | function returns sum of diagonals ; as order should be only odd we should pass only odd - integers ; Driver Code"
How can the above be solved in PHP?

< ? php function spiralDiaSum ( $ n ) { if ( $ n == 1 ) return 1 ; return ( 4 * $ n * $ n - 6 * $ n + 6 + spiralDiaSum ( $ n - 2 ) ) ; } $ n = 7 ; echo spiralDiaSum ( $ n ) ; ? >


"Sum of common divisors of two numbers A and B | Function to calculate gcd of two numbers ; Function to calculate all common divisors of two given numbers a , b -- > input integer numbers ; find gcd of a , b ; Find the sum of divisors of n . ; if ' i ' is factor of n ; check if divisors are equal ; Driver program to run the case"
How can the above be solved in PHP?

< ? php function gcd ( $ a , $ b ) { if ( $ a == 0 ) return $ b ; return gcd ( $ b % $ a , $ a ) ; } function sumcommDiv ( $ a , $ b ) { $ n = gcd ( $ a , $ b ) ; $ sum = 0 ; for ( $ i = 1 ; $ i <= sqrt ( $ n ) ; $ i ++ ) { if ( $ n % $ i == 0 ) { if ( $ n / $ i == $ i ) $ sum += $ i ; else $ sum += ( $ n / $ i ) + $ i ; } } return $ sum ; } $ a = 10 ; $ b = 15 ; echo " Sum = " ? >


"Sum of common divisors of two numbers A and B | print the sum of common factors ; sum of common factors ; iterate from 1 to minimum of a and b ; if i is the common factor of both the numbers ; Driver code ; print the sum of common factors"
How can the above be solved in PHP?


"Sum of digits of a given number to a given power | Function to calculate sum ; Driver Code"
How can the above be solved in PHP?

< ? php function calculate ( $ n , $ power ) { $ sum = 0 ; $ bp = ( int ) pow ( $ n , $ power ) ; while ( $ bp != 0 ) { $ d = $ bp % 10 ; $ sum += $ d ; $ bp /= 10 ; } return $ sum ; } $ n = 5 ; $ power = 4 ; echo ( calculate ( $ n , $ power ) ) ; ? >


"Sum of digits written in different bases from 2 to n | function to calculate sum of digit for a given base ; Sum of digits ; Calculating the number ( n ) by taking mod with the base and adding remainder to the result and parallelly reducing the num value . ; returning the result ; function calling for multiple bases ; Driver code"
How can the above be solved in PHP?

< ? php function solve ( $ n , $ base ) { $ result = 0 ; while ( $ n > 0 ) { $ remainder = $ n % $ base ; $ result = $ result + $ remainder ; $ n = $ n / $ base ; } return $ result ; } function printSumsOfDigits ( $ n ) { for ( $ base = 2 ; $ base < $ n ; ++ $ base ) { echo ( solve ( $ n , $ base ) ) ; echo ( " ▁ " ) ; } } $ n = 8 ; printSumsOfDigits ( $ n ) ; ? >


"Sum of each element raised to ( prime | Function to return the required sum ; Driver code"
How can the above be solved in PHP?

< ? php function getSum ( $ arr , $ p ) { return count ( $ arr ) ; } $ arr = array ( 5 , 6 , 8 ) ; $ p = 7 ; echo ( getSum ( $ arr , $ p ) ) ; ? >


"Sum of elements from an array having even parity | Function that returns true if x has even parity ; We basically count set bits https : www . geeksforgeeks . org / count - set - bits - in - an - integer / ; Function to return the sum of the elements from an array which have even parity ; If a [ i ] has even parity ; Driver code"
How can the above be solved in PHP?

< ? php function checkEvenParity ( $ x ) { $ parity = 0 ; while ( $ x != 0 ) { $ x = ( $ x & ( $ x - 1 ) ) ; $ parity ++ ; } if ( $ parity % 2 == 0 ) return true ; else return false ; } function sumlist ( $ a , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( checkEvenParity ( $ a [ $ i ] ) ) $ sum += $ a [ $ i ] ; } return $ sum ; } $ arr = array ( 2 , 4 , 3 , 5 , 9 ) ; $ n = sizeof ( $ arr ) ; echo sumlist ( $ arr , $ n ) ; ? >


"Sum of elements in 1 st array such that number of elements less than or equal to them in 2 nd array is maximum | PHP implementation of the approach ; Function to return the required sum ; Creating hash array initially filled with zero ; Calculate the frequency of elements of arr2 [ ] ; Running sum of hash array such that hash [ i ] will give count of elements less than or equal to i in arr2 [ ] ; To store the maximum value of the number of elements in arr2 [ ] which are smaller than or equal to some element of arr1 [ ] ; Calculate the sum of elements from arr1 [ ] corresponding to maximum frequency ; Return the required sum ; Driver code"
How can the above be solved in PHP?

< ? php $ MAX = 10000 ; function findSumofEle ( $ arr1 , $ m , $ arr2 , $ n ) { $ hash = array_fill ( 0 , $ GLOBALS [ ' MAX ' ] , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ hash [ $ arr2 [ $ i ] ] ++ ; for ( $ i = 1 ; $ i < $ GLOBALS [ ' MAX ' ] ; $ i ++ ) $ hash [ $ i ] = $ hash [ $ i ] + $ hash [ $ i - 1 ] ; $ maximumFreq = 0 ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) $ maximumFreq = max ( $ maximumFreq , $ hash [ $ arr1 [ $ i ] ] ) ; $ sumOfElements = 0 ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) $ sumOfElements += ( $ maximumFreq == $ hash [ $ arr1 [ $ i ] ] ) ? $ arr1 [ $ i ] : 0 ; return $ sumOfElements ; } $ arr1 = array ( 2 , 5 , 6 , 8 ) ; $ arr2 = array ( 4 , 10 ) ; $ m = count ( $ arr1 ) ; $ n = count ( $ arr2 ) ; echo findSumofEle ( $ arr1 , $ m , $ arr2 , $ n ) ; ? >


"Sum of elements in an array whose difference with the mean of another array is less than k | Function for finding sum of elements whose diff with mean is not more than k ; Find the mean of second array ; Find sum of elements from array1 whose difference with mean is not more than k ; Return result ; Driver code"
How can the above be solved in PHP?

< ? php function findSumofEle ( $ arr1 , $ m , $ arr2 , $ n , $ k ) { $ arraySum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ arraySum += $ arr2 [ $ i ] ; $ mean = $ arraySum / $ n ; $ sumOfElements = 0 ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) { $ difference = $ arr1 [ $ i ] - $ mean ; if ( ( $ difference < 0 ) && ( $ k > ( -1 ) * $ difference ) ) { $ sumOfElements += $ arr1 [ $ i ] ; } if ( ( $ difference >= 0 ) && ( $ k > $ difference ) ) { $ sumOfElements += $ arr1 [ $ i ] ; } } return $ sumOfElements ; } $ arr1 = array ( 1 , 2 , 3 , 4 , 7 , 9 ) ; $ arr2 = array ( 0 , 1 , 2 , 1 , 1 , 4 ) ; $ k = 2 ; $ m = count ( $ arr1 ) ; $ n = count ( $ arr2 ) ; print ( findSumofEle ( $ arr1 , $ m , $ arr2 , $ n , $ k ) ) ; ? >


"Sum of elements in range L | Function to find the sum between L and R ; array created ; fill the first half of array ; fill the second half of array ; find the sum between range ; Driver Code"
How can the above be solved in PHP?


"Sum of every Kâ €™ th prime number in an array | PHP implementation of the approach ; 0 and 1 are not prime numbers ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; compute the answer ; count of primes ; sum of the primes ; traverse the array ; if the number is a prime ; increase the count ; if it is the K 'th prime ; create the sieve"
How can the above be solved in PHP?

 < ? php $ MAX = 100000 ; $ prime = array_fill ( 0 , $ MAX + 1 , true ) ; function SieveOfEratosthenes ( ) { global $ MAX , $ prime ; $ prime [ 1 ] = false ; $ prime [ 0 ] = false ; for ( $ p = 2 ; $ p * $ p <= $ MAX ; $ p ++ ) { if ( $ prime [ $ p ] == true ) { for ( $ i = $ p * 2 ; $ i <= $ MAX ; $ i += $ p ) $ prime [ $ i ] = false ; } } } function SumOfKthPrimes ( $ arr , $ n , $ k ) { global $ MAX , $ prime ; $ c = 0 ; $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ prime [ $ arr [ $ i ] ] ) { $ c ++ ; if ( $ c % $ k == 0 ) { $ sum += $ arr [ $ i ] ; $ c = 0 ; } } } echo $ sum . "
 " ; } SieveOfEratosthenes ( ) ; $ arr = array ( 2 , 3 , 5 , 7 , 11 ) ; $ n = sizeof ( $ arr ) ; $ k = 2 ; SumOfKthPrimes ( $ arr , $ n , $ k ) ; ? >


"Sum of fifth powers of the first n natural numbers | calculate the sum of fifth power of first n natural numbers ; Driver Code"
How can the above be solved in PHP?


"Sum of first N natural numbers by taking powers of 2 as negative number | to store power of 2 ; to store presum of the power of 2 's ; function to find power of 2 ; to store power of 2 ; to store pre sum ; Function to find the sum ; first store sum of first n natural numbers . ; find the first greater number than given number then minus double of this from answer ; function call ; function call"
How can the above be solved in PHP?

< ? php $ power = array_fill ( 0 , 31 , 0 ) ; $ pre = array_fill ( 0 , 31 , 0 ) ; function PowerOfTwo ( ) { global $ power , $ pre ; $ x = 1 ; for ( $ i = 0 ; $ i < 31 ; $ i ++ ) { $ power [ $ i ] = $ x ; $ x *= 2 ; } $ pre [ 0 ] = 1 ; for ( $ i = 1 ; $ i < 31 ; $ i ++ ) $ pre [ $ i ] = $ pre [ $ i - 1 ] + $ power [ $ i ] ; } function Sum ( $ n ) { global $ power , $ pre ; $ ans = $ n * ( $ n + 1 ) / 2 ; for ( $ i = 0 ; $ i < 31 ; $ i ++ ) if ( $ power [ $ i ] > $ n ) { $ ans -= 2 * $ pre [ $ i - 1 ] ; break ; } return $ ans ; } PowerOfTwo ( ) ; $ n = 4 ; print ( Sum ( $ n ) ) ; ? >


"Sum of first N natural numbers which are divisible by 2 and 7 | Function to calculate the sum of numbers divisible by 2 or 7 ; Driver code"
How can the above be solved in PHP?


"Sum of first N natural numbers which are divisible by X or Y | Function to calculate the sum of numbers divisible by X or Y ; Driver code"
How can the above be solved in PHP?


"Sum of first N natural numbers which are not powers of K | Function to return the sum of first n natural numbers which are not positive powers of k ; sum of first n natural numbers ; subtract all positive powers of k which are less than n ; next power of k ; Driver code"
How can the above be solved in PHP?

< ? php function find_sum ( $ n , $ k ) { $ total_sum = ( $ n * ( $ n + 1 ) ) / 2 ; $ power = $ k ; while ( $ power <= $ n ) { $ total_sum -= $ power ; $ power *= $ k ; } return $ total_sum ; } $ n = 11 ; $ k = 2 ; echo find_sum ( $ n , $ k ) ; ? >


"Sum of first n even numbers | function to find sum of first n even numbers ; required sum ; Driver Code"
How can the above be solved in PHP?

< ? php function evenSum ( $ n ) { return ( $ n * ( $ n + 1 ) ) ; } $ n = 20 ; echo " Sum ▁ of ▁ first ▁ " , $ n , " ▁ Even ▁ numbers ▁ is : ▁ " , evenSum ( $ n ) ; ? >


"Sum of first n even numbers | function to find sum of first n even numbers ; sum of first n even numbers ; next even number ; required sum ; Driver program to test above"
How can the above be solved in PHP?

< ? php function evenSum ( $ n ) { $ curr = 2 ; $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ sum += $ curr ; $ curr += 2 ; } return $ sum ; } $ n = 20 ; echo " Sum ▁ of ▁ first ▁ " . $ n . " ▁ Even ▁ numbers ▁ is : ▁ " . evenSum ( $ n ) ; ? >


"Sum of first n natural numbers | Function to find the sum of series ; Driver code"
How can the above be solved in PHP?

< ? php function seriesSum ( $ n ) { return ( $ n * ( $ n + 1 ) * ( $ n + 2 ) ) / 6 ; } $ n = 4 ; echo ( seriesSum ( $ n ) ) ; ? >


< ? php function seriesSum ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ sum += $ i * ( $ i + 1 ) / 2 ; return $ sum ; } $ n = 4 ; echo ( seriesSum ( $ n ) ) ; ? >


"Sum of first n odd numbers in O ( 1 ) Complexity | Returns the sum of first n odd numbers ; Driver Code"
How can the above be solved in PHP?

< ? php function oddSum ( $ n ) { $ sum = 0 ; $ curr = 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ sum += $ curr ; $ curr += 2 ; } return $ sum ; } $ n = 20 ; echo " ▁ Sum ▁ of ▁ first ▁ " , $ n , " ▁ Odd ▁ Numbers ▁ is : ▁ " , oddSum ( $ n ) ; ? >


< ? php function oddSum ( $ n ) { return ( $ n * $ n ) ; } $ n = 20 ; echo " ▁ Sum ▁ of ▁ first ▁ " , $ n , " ▁ Odd ▁ Numbers ▁ is : ▁ " , oddSum ( $ n ) ; ? >


"Sum of first n terms of a given series 3 , 6 , 11 , ... . . | Function to calculate the sum ; starting number ; Common Ratio ; Common difference ; Nth term to be find ; find the Sn"
How can the above be solved in PHP?


"Sum of fourth power of first n even natural numbers | calculate the sum of fourth power of first n even natural numbers ; Driver Code"
How can the above be solved in PHP?


"Sum of fourth power of first n even natural numbers | calculate the sum of fourth power of first n even natural numbers ; made even number ; Driver Code"
How can the above be solved in PHP?

< ? php function evenPowerSum ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ j = 2 * $ i ; $ sum = $ sum + ( $ j * $ j * $ j * $ j ) ; } return $ sum ; } $ n = 5 ; echo ( evenPowerSum ( $ n ) ) ; ? >


"Sum of fourth powers of first n odd natural numbers | calculate the sum of fourth power of first n odd natural numbers ; Driver Code"
How can the above be solved in PHP?


"Sum of fourth powers of the first n natural numbers | Return the sum of fourth power of first n natural numbers ; Driver Code"
How can the above be solved in PHP?

< ? php function fourthPowerSum ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ sum = $ sum + ( $ i * $ i * $ i * $ i ) ; return $ sum ; } $ n = 6 ; echo ( fourthPowerSum ( $ n ) ) ; ? >


< ? php function fourthPowerSum ( $ n ) { return ( ( 6 * $ n * $ n * $ n * $ n * $ n ) + ( 15 * $ n * $ n * $ n * $ n ) + ( 10 * $ n * $ n * $ n ) - $ n ) / 30 ; } $ n = 6 ; echo ( fourthPowerSum ( $ n ) ) ; ? >


"Sum of greatest odd divisor of numbers in given range | Function to return sum of first n odd numbers ; Recursive function to return sum of greatest odd divisor of numbers in range [ 1 , n ] ; { Odd n ; { Even n ; Function to return sum of greatest odd divisor of numbers in range [ a , b ] ; Driver code"
How can the above be solved in PHP?

< ? php function square ( $ n ) { return $ n * $ n ; } function sum ( $ n ) { if ( $ n == 0 ) return 0 ; if ( $ n % 2 == 1 ) return square ( ( int ) ( ( $ n + 1 ) / 2 ) ) + sum ( ( int ) ( $ n / 2 ) ) ; } else return square ( ( int ) ( $ n / 2 ) ) + sum ( ( int ) ( $ n / 2 ) ) ; } } function oddDivSum ( $ a , $ b ) { return sum ( $ b ) - sum ( $ a - 1 ) ; } $ a = 3 ; $ b = 9 ; echo oddDivSum ( $ a , $ b ) ; ? >


"Sum of i * countDigits ( i ) ^ 2 for all i in range [ L , R ] | PHP implementation of the approach ; Function to return the required sum ; If range is valid ; Sum of AP ; Driver code"
How can the above be solved in PHP?


"Sum of integers upto N with given unit digit ( Set 2 ) | Function to return the required sum ; Decrement N ; Driver code"
How can the above be solved in PHP?


"Sum of integers upto N with given unit digit | Function to return the required sum ; Driver code"
How can the above be solved in PHP?

< ? php function getSum ( $ n , $ d ) { $ sum = 0 ; while ( $ d <= $ n ) { $ sum += $ d ; $ d += 10 ; } return $ sum ; } $ n = 30 ; $ d = 3 ; echo ( getSum ( $ n , $ d ) ) ; ? >


"Sum of integers upto N with given unit digit | Function to return the required sum ; If the unit digit is d ; Driver code"
How can the above be solved in PHP?


"Sum of internal angles of a Polygon | Function to return the sum of internal angles of an n - sided polygon ; Driver code"
How can the above be solved in PHP?

< ? php function sumOfInternalAngles ( $ n ) { if ( $ n < 3 ) return 0 ; return ( ( $ n - 2 ) * 180 ) ; } $ n = 5 ; echo ( sumOfInternalAngles ( $ n ) ) ; ? >


"Sum of kth powers of first n natural numbers | A global array to store factorial ; Function to calculate the factorials of all the numbers upto k ; Function to return the binomial coefficient ; nCr = ( n ! * ( n - r ) ! ) / r ! ; Function to return the sum of kth powers of n natural numbers ; When j is unity ; Calculating sum ( n ^ 1 ) of unity powers of n ; storing sum ( n ^ 1 ) for sum ( n ^ 2 ) ; If k = 1 then temp is the result ; For finding sum ( n ^ k ) removing 1 and n * kCk from ( n + 1 ) ^ k ; Removing all kC2 * sum ( n ^ ( k - 2 ) ) + ... + kCk - 1 * ( sum ( n ^ ( k - ( k - 1 ) ) ; Storing the result for next sum of next powers of k ; Driver code"
How can the above be solved in PHP?


"Sum of lengths of all 12 edges of any rectangular parallelepiped | function to find the sum of all the edges of parallelepiped ; to calculate the length of one edge ; sum of all the edges of one side ; net sum will be equal to the summation of edges of all the sides ; initialize the area of three faces which has a common vertex"
How can the above be solved in PHP?


"Sum of matrix element where each elements is integer division of row and column | Return sum of matrix element where each element is division of its corresponding row and column . ; For each column . ; count the number of elements of each column . Initialize to i - 1 because number of zeroes are i - 1. ; For multiply ; Driver Code"
How can the above be solved in PHP?

< ? php function findSum ( $ n ) { $ ans = 0 ; $ temp = 0 ; $ num ; for ( $ i = 1 ; $ i <= $ n and $ temp < $ n ; $ i ++ ) { $ temp = $ i - 1 ; $ num = 1 ; while ( $ temp < $ n ) { if ( $ temp + $ i <= $ n ) $ ans += ( $ i * $ num ) ; else $ ans += ( ( $ n - $ temp ) * $ num ) ; $ temp += $ i ; $ num ++ ; } } return $ ans ; } $ N = 2 ; echo findSum ( $ N ) ; ? >


"Sum of matrix in which each element is absolute difference of its row and column numbers | Retuen the sum of matrix in which each element is absolute difference of its corresponding row and column number row ; Driver Code"
How can the above be solved in PHP?


"Sum of matrix in which each element is absolute difference of its row and column numbers | Retuen the sum of matrix in which each element is absolute difference of its corresponding row and column number row ; Generate matrix ; Compute sum ; Driver Code"
How can the above be solved in PHP?


"Sum of matrix in which each element is absolute difference of its row and column numbers | Return the sum of matrix in which each element is absolute difference of its corresponding row and column number row ; Driver Code"
How can the above be solved in PHP?

< ? php function findSum ( $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += $ i * ( $ n - $ i ) ; return 2 * $ sum ; } $ n = 3 ; echo findSum ( $ n ) ; ? >


"Sum of middle row and column in Matrix | PHP program to find sum of middle row and column in matrix ; loop for sum of row ; loop for sum of column ; Driver function"
How can the above be solved in PHP?

 < ? php function middlesum ( $ mat , $ n ) { $ row_sum = 0 ; $ col_sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ row_sum += $ mat [ $ n / 2 ] [ $ i ] ; echo " Sum ▁ of ▁ middle ▁ row ▁ = ▁ " , $ row_sum , "
 " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ col_sum += $ mat [ $ i ] [ $ n / 2 ] ; echo " Sum ▁ of ▁ middle ▁ column ▁ = ▁ " , $ col_sum ; } $ mat = array ( array ( 2 , 5 , 7 ) , array ( 3 , 7 , 2 ) , array ( 5 , 6 , 9 ) ) ; middlesum ( $ mat , 3 ) ; ? >


"Sum of minimum absolute difference of each array element | function to find the sum of minimum absolute difference ; sort the given array ; initialize sum ; min absolute difference for the 1 st array element ; min absolute difference for the last array element ; find min absolute difference for rest of the array elements and add them to sum ; required sum ; Driver Code"
How can the above be solved in PHP?

< ? php function sumOfMinAbsDifferences ( $ arr , $ n ) { sort ( $ arr ) ; sort ( $ arr , $ n ) ; $ sum = 0 ; $ sum += abs ( $ arr [ 0 ] - $ arr [ 1 ] ) ; $ sum += abs ( $ arr [ $ n - 1 ] - $ arr [ $ n - 2 ] ) ; for ( $ i = 1 ; $ i < $ n - 1 ; $ i ++ ) $ sum += min ( abs ( $ arr [ $ i ] - $ arr [ $ i - 1 ] ) , abs ( $ arr [ $ i ] - $ arr [ $ i + 1 ] ) ) ; return $ sum ; } $ arr = array ( 5 , 10 , 1 , 4 , 8 , 7 ) ; $ n = sizeof ( $ arr ) ; echo " Sum = " ? >


"Sum of minimum difference between consecutive elements of an array | function to find minimum sum of difference of consecutive element ; ul to store upper limit ll to store lower limit ; storethe lower range in ll and upper range in ul ; initialize the answer with 0 ; iterate for all ranges ; case 1 , in this case the difference will be 0 ; change upper limit and lower limit ; case 2 ; store the difference ; case 3 ; store the difference ; array of range"
How can the above be solved in PHP?

< ? php function solve ( $ v , $ n ) { $ ans ; $ ul ; $ ll ; $ first = 0 ; $ second = 1 ; $ ll = $ v [ 0 ] [ $ first ] ; $ ul = $ v [ 0 ] [ $ second ] ; $ ans = 0 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { if ( ( $ v [ $ i ] [ $ first ] <= $ ul and $ v [ $ i ] [ $ first ] >= $ ll ) or ( $ v [ $ i ] [ $ second ] >= $ ll and $ v [ $ i ] [ $ second ] <= $ ul ) ) { if ( $ v [ $ i ] [ $ first ] > $ ll ) { $ ll = $ v [ $ i ] [ $ first ] ; } if ( $ v [ $ i ] [ $ second ] < $ ul ) { $ ul = $ v [ $ i ] [ $ second ] ; } } else if ( $ v [ $ i ] [ $ first ] > $ ul ) { $ ans += abs ( $ ul - $ v [ $ i ] [ $ first ] ) ; $ ul = $ v [ $ i ] [ $ first ] ; $ ll = $ v [ $ i ] [ $ first ] ; } else if ( $ v [ $ i ] [ $ second ] < $ ll ) { $ ans += abs ( $ ll - $ v [ $ i ] [ $ second ] ) ; $ ul = $ v [ $ i ] [ $ second ] ; $ ll = $ v [ $ i ] [ $ second ] ; } } return $ ans ; } $ v = array ( array ( 1 , 3 ) , array ( 2 , 5 ) , array ( 6 , 8 ) , array ( 1 , 2 ) , array ( 2 , 3 ) ) ; $ n = 5 ; echo ( solve ( $ v , $ n ) ) ; ? >


"Sum of minimum element of all sub | Function to find the sum of minimum of all subsequence ; Driver code"
How can the above be solved in PHP?

< ? php function findMinSum ( $ arr , $ n ) { $ occ1 = ( $ n ) ; $ occ = $ occ1 - 1 ; $ Sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ Sum += $ arr [ $ i ] * pow ( 2 , $ occ ) ; $ occ -= 1 ; } return $ Sum ; } $ arr = array ( 1 , 2 , 4 , 5 ) ; $ n = count ( $ arr ) ; echo findMinSum ( $ arr , $ n ) ; ? >


"Sum of minimum element of all subarrays of a sorted array | Function to find the sum of minimum of all subarrays ; Driver code"
How can the above be solved in PHP?

< ? php function findMinSum ( $ arr , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += $ arr [ $ i ] * ( $ n - $ i ) ; return $ sum ; } $ arr = array ( 3 , 5 , 7 , 8 ) ; $ n = count ( $ arr ) ; echo findMinSum ( $ arr , $ n ) ; ? >


"Sum of multiples of A and B less than N | PHP program to find the sum of all multiples of A and B below N ; Function to find sum of AP series ; Number of terms ; Function to find the sum of all multiples of A and B below N ; Since , we need the sum of multiples less than N ; common factors of A and B ; 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 sumAP ( $ n , $ d ) { $ n = ( int ) ( $ n / $ d ) ; return ( $ n ) * ( 1 + $ n ) * $ d / 2 ; } function sumMultiples ( $ A , $ B , $ n ) { $ n -- ; $ common = ( int ) ( ( $ A * $ B ) / __gcd ( $ A , $ B ) ) ; return sumAP ( $ n , $ A ) + sumAP ( $ n , $ B ) - sumAP ( $ n , $ common ) ; } $ n = 100 ; $ A = 5 ; $ B = 10 ; echo " Sum = " ? >


"Sum of multiplication of triplet of divisors of a number | PHP implementation of the approach ; Global array declaration ; Function to find the sum of multiplication of every triplet in the divisors of a number ; sum1 [ x ] represents the sum of all the divisors of x ; Adding i to sum1 [ j ] because i is a divisor of j ; sum2 [ x ] represents the sum of all the divisors of x ; Here i is divisor of j and sum1 [ j ] - i represents sum of all divisors of j which do not include i so we add i * ( sum1 [ j ] - i ) to sum2 [ j ] ; In the above implementation we have considered every pair two times so we have to divide every sum2 array element by 2 ; Here i is the divisor of j and we are trying to add the sum of multiplication of all triplets of divisors of j such that one of the divisors is i ; In the above implementation we have considered every triplet three times so we have to divide every sum3 array element by 3 ; Print the results ; Driver code ; Precomputing"
How can the above be solved in PHP?

< ? php $ max_Element = 1005 ; $ sum1 = array_fill ( 0 , $ max_Element , 0 ) ; $ sum2 = array_fill ( 0 , $ max_Element , 0 ) ; $ sum3 = array_fill ( 0 , $ max_Element , 0 ) ; function precomputation ( $ arr , $ n ) { global $ max_Element , $ sum3 , $ sum2 , $ sum1 ; for ( $ i = 1 ; $ i < $ max_Element ; $ i ++ ) for ( $ j = $ i ; $ j < $ max_Element ; $ j += $ i ) $ sum1 [ $ j ] += $ i ; for ( $ i = 1 ; $ i < $ max_Element ; $ i ++ ) for ( $ j = $ i ; $ j < $ max_Element ; $ j += $ i ) $ sum2 [ $ j ] += ( $ sum1 [ $ j ] - $ i ) * $ i ; for ( $ i = 1 ; $ i < $ max_Element ; $ i ++ ) $ sum2 [ $ i ] = ( int ) ( $ sum2 [ $ i ] / 2 ) ; for ( $ i = 1 ; $ i < $ max_Element ; $ i ++ ) for ( $ j = $ i ; $ j < $ max_Element ; $ j += $ i ) $ sum3 [ $ j ] += $ i * ( $ sum2 [ $ j ] - $ i * ( $ sum1 [ $ j ] - $ i ) ) ; for ( $ i = 1 ; $ i < $ max_Element ; $ i ++ ) $ sum3 [ $ i ] = ( int ) ( $ sum3 [ $ i ] / 3 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ sum3 [ $ arr [ $ i ] ] . " ▁ " ; } $ arr = array ( 9 , 5 , 6 ) ; $ n = count ( $ arr ) ; precomputation ( $ arr , $ n ) ; ? >


"Sum of n digit numbers divisible by a given number | Returns sum of n digit numbers divisible by ' number ' ; compute the first and last term ; sum of number which having n digit and divisible by number ; Driver code"
How can the above be solved in PHP?

< ? php function totalSumDivisibleByNum ( $ n , $ number ) { $ firstnum = pow ( 10 , $ n - 1 ) ; $ lastnum = pow ( 10 , $ n ) ; $ sum = 0 ; for ( $ i = $ firstnum ; $ i < $ lastnum ; $ i ++ ) if ( $ i % $ number == 0 ) $ sum += $ i ; return $ sum ; } $ n = 3 ; $ num = 7 ; echo totalSumDivisibleByNum ( $ n , $ num ) , " " ; ? >


"Sum of n digit numbers divisible by a given number | find the Sum of having n digit and divisible by the number ; compute the first and last term ; first number which is divisible by given number ; last number which is divisible by given number ; total divisible number ; return the total sum ; Driver Code"
How can the above be solved in PHP?

< ? php function totalSumDivisibleByNum ( $ digit , $ number ) { $ firstnum = pow ( 10 , $ digit - 1 ) ; $ lastnum = pow ( 10 , $ digit ) ; $ firstnum = ( $ firstnum - $ firstnum % $ number ) + $ number ; $ lastnum = ( $ lastnum - $ lastnum % $ number ) ; $ count = ( ( $ lastnum - $ firstnum ) / $ number + 1 ) ; return ( ( $ lastnum + $ firstnum ) * $ count ) / 2 ; } $ n = 3 ; $ number = 7 ; echo totalSumDivisibleByNum ( $ n , $ number ) ; ? >


"Sum of nth terms of Modified Fibonacci series made by every pair of two arrays | PHP program to find sum of n - th terms of a Fibonacci like series formed using first two terms of two arrays . ; if sum of first term is required ; if sum of second term is required ; fibonacci series used to find the nth term of every series ; as every b [ i ] term appears m times and every a [ i ] term also appears m times ; m is the size of the array"
How can the above be solved in PHP?


"Sum of numbers from 1 to N which are in Lucas Sequence | Function to return the required sum ; Generate lucas number and keep on adding them ; Driver code"
How can the above be solved in PHP?

< ? php function LucasSum ( $ N ) { $ sum = 0 ; $ a = 2 ; $ b = 1 ; $ c ; $ sum += $ a ; while ( $ b <= $ N ) { $ sum += $ b ; $ c = $ a + $ b ; $ a = $ b ; $ b = $ c ; } return $ sum ; } $ N = 20 ; echo ( LucasSum ( $ N ) ) ; ? >


"Sum of numbers with exactly 2 bits set | To calculate sum of numbers ; Find numbers whose 2 bits are set ; If number is greater then n we don 't include this in sum ; Return sum of numbers ; Driver Code"
How can the above be solved in PHP?


"Sum of numbers with exactly 2 bits set | To count number of set bits ; To calculate sum of numbers ; To count sum of number whose 2 bit are set ; Driver Code"
How can the above be solved in PHP?

< ? php function countSetBits ( $ n ) { $ count = 0 ; while ( $ n ) { $ n &= ( $ n - 1 ) ; $ count ++ ; } return $ count ; } function findSum ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) if ( countSetBits ( $ i ) == 2 ) $ sum += $ i ; return $ sum ; } $ n = 10 ; echo findSum ( $ n ) ; ? >


"Sum of pairwise products | Efficient PHP program to find sum of given series . ; Sum of multiples of 1 is 1 * ( 1 + 2 + . . ) ; Adding sum of multiples of numbers other than 1 , starting from 2. ; Subtract previous number from current multiple . ; For example , for 2 , we get sum as ( 2 + 3 + 4 + ... . ) * 2 ; Driver code"
How can the above be solved in PHP?

< ? php function findSum ( $ n ) { $ multiTerms = ( int ) ( $ n * ( $ n + 1 ) / 2 ) ; $ sum = $ multiTerms ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { $ multiTerms = $ multiTerms - ( $ i - 1 ) ; $ sum = $ sum + $ multiTerms * $ i ; } return $ sum ; } $ n = 5 ; echo findSum ( $ n ) ; ? >


"Sum of pairwise products | Simple PHP program to find sum of given series . ; Driver Code"
How can the above be solved in PHP?

< ? php function findSum ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) for ( $ j = $ i ; $ j <= $ n ; $ j ++ ) $ sum = $ sum + $ i * $ j ; return $ sum ; } $ n = 5 ; echo findSum ( $ n ) ; ? >


"Sum of product of consecutive Binomial Coefficients | PHP Program to find sum of product of consecutive Binomial Coefficient . ; Find the binomial coefficient up to nth term ; $C [ 0 ] = 1 ; nC0 is 1 ; Compute next row of pascal triangle using the previous row ; Return the sum of the product of consecutive binomial coefficient . ; Driver Code"
How can the above be solved in PHP?

< ? php $ MAX = 100 ; function binomialCoeff ( $ n , $ k ) { $ C = array_fill ( 0 , ( $ k + 1 ) , 0 ) ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { for ( $ j = min ( $ i , $ k ) ; $ j > 0 ; $ j -- ) $ C [ $ j ] = $ C [ $ j ] + $ C [ $ j - 1 ] ; } return $ C [ $ k ] ; } function sumOfproduct ( $ n ) { return binomialCoeff ( 2 * $ n , $ n - 1 ) ; } $ n = 3 ; echo sumOfproduct ( $ n ) ; ? >


"Sum of product of consecutive Binomial Coefficients | PHP Program to find sum of product of consecutive Binomial Coefficient . ; Find the binomial coefficient upto nth term ; $C [ 0 ] = 1 ; nC0 is 1 ; Compute next row of pascal triangle using the previous row ; Return the sum of the product of consecutive binomial coefficient . ; finding the sum of product of consecutive coefficient . ; Driver Code"
How can the above be solved in PHP?

< ? php $ MAX = 100 ; function binomialCoeff ( $ C , $ n ) { for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { for ( $ j = min ( $ i , $ n ) ; $ j > 0 ; $ j -- ) $ C [ $ j ] = $ C [ $ j ] + $ C [ $ j - 1 ] ; } return $ C ; } function sumOfproduct ( $ n ) { global $ MAX ; $ sum = 0 ; $ C = array_fill ( 0 , $ MAX , 0 ) ; $ C = binomialCoeff ( $ C , $ n ) ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) $ sum += $ C [ $ i ] * $ C [ $ i + 1 ] ; return $ sum ; } $ n = 3 ; echo sumOfproduct ( $ n ) ; ? >


"Sum of product of r and rth Binomial Coefficient ( r * nCr ) | PHP Program to find sum of product of r and rth Binomial Coefficient i . e summation r * nCr ; Return the first n term of binomial coefficient . ; $C [ 0 ] = 1 ; nC0 is 1 ; Compute next row of pascal triangle using the previous row ; Return summation of r * nCr ; finding the first n term of binomial coefficient ; Iterate a loop to find the sum . ; Driver Code"
How can the above be solved in PHP?

< ? php $ MAX = 100 ; function binomialCoeff ( $ n , & $ C ) { for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { for ( $ j = min ( $ i , $ n ) ; $ j > 0 ; $ j -- ) $ C [ $ j ] = $ C [ $ j ] + $ C [ $ j - 1 ] ; } } function summation ( $ n ) { global $ MAX ; $ C = array_fill ( 0 , $ MAX , 0 ) ; binomialCoeff ( $ n , $ C ) ; $ sum = 0 ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) $ sum += ( $ i * $ C [ $ i ] ) ; return $ sum ; } $ n = 2 ; echo summation ( $ n ) ; ? >


"Sum of product of r and rth Binomial Coefficient ( r * nCr ) | Return summation of r * nCr ; Driver Code"
How can the above be solved in PHP?

< ? php function summation ( $ n ) { return $ n << ( $ n - 1 ) ; } $ n = 2 ; echo summation ( $ n ) ; ? >


"Sum of product of x and y such that floor ( n / x ) = y | Return the sum of natural number in a range . ; n * ( n + 1 ) / 2. ; Return the sum of product x * y . ; Iterating i from 1 to sqrt ( n ) ; Finding the upper limit . ; Finding the lower limit . ; Driven Code"
How can the above be solved in PHP?

 < ? php function sumOfRange ( $ a , $ b ) { $ i = ( $ a * ( $ a + 1 ) ) >> 1 ; $ j = ( $ b * ( $ b + 1 ) ) >> 1 ; return ( $ i - $ j ) ; } function sumofproduct ( $ n ) { $ sum = 0 ; $ root = sqrt ( $ n ) ; for ( $ i = 1 ; $ i <= $ root ; $ i ++ ) { $ up = ( int ) ( $ n / $ i ) ; $ low = max ( ( int ) ( $ n / ( $ i + 1 ) ) , $ root ) ; $ sum += ( $ i * sumOfRange ( $ up , $ low ) ) ; $ sum += ( $ i * ( int ) ( $ n / $ i ) ) ; } return $ sum ; } $ n = 10 ; echo sumofproduct ( $ n ) . "
 " ; ? >


"Sum of product of x and y such that floor ( n / x ) = y | Return the sum of product x * y . ; Iterating x from 1 to n ; Finding y = n / x . ; Adding product of x and y to answer . ; Driver Code"
How can the above be solved in PHP?

< ? php function sumofproduct ( $ n ) { $ ans = 0 ; for ( $ x = 1 ; $ x <= $ n ; $ x ++ ) { $ y = ( int ) ( $ n / $ x ) ; $ ans += ( $ y * $ x ) ; } return $ ans ; } $ n = 10 ; echo sumofproduct ( $ n ) ; ? >


"Sum of range in a series of first odd then even natural numbers | Function that returns sum in the range 1 to x in the sequence 1 3 5 7. ... . N 2 4 6. . . N - 1 ; number of odd numbers ; number of extra even numbers required ; Driver code"
How can the above be solved in PHP?

< ? php function sumTillX ( $ x , $ n ) { $ odd = ceil ( $ n / 2.0 ) ; if ( $ x <= $ odd ) return $ x * $ x ; $ even = $ x - $ odd ; return ( ( $ odd * $ odd ) + ( $ even * $ even ) + $ even ) ; } function rangeSum ( $ N , $ L , $ R ) { return sumTillX ( $ R , $ N ) - sumTillX ( $ L - 1 , $ N ) ; } $ N = 10 ; $ L = 1 ; $ R = 6 ; echo ( rangeSum ( $ N , $ L , $ R ) ) ; ? >


"Sum of series ( n / 1 ) + ( n / 2 ) + ( n / 3 ) + ( n / 4 ) + ... ... . + ( n / n ) | function to find sum of series ; Driver code"
How can the above be solved in PHP?

< ? php function sum ( $ n ) { $ root = intval ( sqrt ( $ n ) ) ; $ ans = 0 ; for ( $ i = 1 ; $ i <= $ root ; $ i ++ ) $ ans += intval ( $ n / $ i ) ; $ ans = ( 2 * $ ans ) - ( $ root * $ root ) ; return $ ans ; } $ n = 35 ; echo ( sum ( $ n ) ) ; ? >


"Sum of series 1 * 1 ! + 2 * 2 ! + …… . . + n * n ! | PHP program to find sum of the series . ; Function to calculate required series ; Driver code"
How can the above be solved in PHP?

< ? php function factorial ( $ n ) { $ res = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ res = $ res * $ i ; return $ res ; } function calculateSeries ( $ n ) { return factorial ( $ n + 1 ) - 1 ; } $ n = 3 ; echo calculateSeries ( $ n ) ; ? >


"Sum of series 1 * 1 * 2 ! + 2 * 2 * 3 ! + …… . . + n * n * ( n + 1 ) ! | PHP program to find sum of the series . ; Function to calculate required series ; Driver code"
How can the above be solved in PHP?

< ? php function factorial ( $ n ) { $ res = 1 ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ res = $ res * $ i ; return $ res ; } function calculateSeries ( $ n ) { return 2 + ( $ n * $ n + $ n - 2 ) * factorial ( $ n + 1 ) ; } $ n = 3 ; echo calculateSeries ( $ n ) ; ? >


"Sum of series 1 ^ 2 + 3 ^ 2 + 5 ^ 2 + . . . + ( 2 * n | Function that find sum of series . ; Formula to find sum of series . ; Driver code"
How can the above be solved in PHP?

< ? php function sumOfSeries ( $ n ) { return ( $ n * ( 2 * $ n - 1 ) * ( 2 * $ n + 1 ) ) / 3 ; } $ n = 10 ; echo ( sumOfSeries ( $ n ) ) ; ? >


"Sum of series 1 ^ 2 + 3 ^ 2 + 5 ^ 2 + . . . + ( 2 * n | Function to find sum of series . ; Driver code"
How can the above be solved in PHP?

< ? php function sumOfSeries ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ sum = $ sum + ( 2 * $ i - 1 ) * ( 2 * $ i - 1 ) ; return $ sum ; } $ n = 10 ; echo ( sumOfSeries ( $ n ) ) ; ? >


"Sum of series 2 / 3 | Function to find sum of series up - to n terms ; initializing counter by 1 ; variable to calculate result ; while loop until nth term is not reached ; boolean type variable for checking validation ; Driver Code"
How can the above be solved in PHP?

< ? php function seriesSum ( $ n ) { $ i = 1 ; $ res = 0.0 ; $ sign = true ; while ( $ n > 0 ) { $ n -- ; if ( $ sign ) { $ sign = ! $ sign ; $ res = $ res + ( double ) ++ $ i / ++ $ i ; } else { $ sign = ! $ sign ; $ res = $ res - ( double ) ++ $ i / ++ $ i ; } } return $ res ; } $ n = 5 ; echo ( seriesSum ( $ n ) ) ; ? >


"Sum of square of first n even numbers | Efficient PHP method to find sum of square of first n even numbers . ; Driver code"
How can the above be solved in PHP?

< ? php function squareSum ( $ n ) { return 2 * $ n * ( $ n + 1 ) * ( 2 * $ n + 1 ) / 3 ; } echo squareSum ( 8 ) ; ? >


"Sum of square of first n odd numbers | Efficient PHP method to find sum of square of first n odd numbers . ; driver code"
How can the above be solved in PHP?

< ? php < ? php function squareSum ( $ n ) { return $ n * ( 4 * $ n * $ n - 1 ) / 3 ; } echo squareSum ( 8 ) ; ? >


"Sum of square | Function to find sum of sum of square of first n natural number ; Driver Code"
How can the above be solved in PHP?


"Sum of squares of Fibonacci numbers | Function to calculate sum of squares of Fibonacci numbers ; Initialize result ; Add remaining terms ; Driver Code"
How can the above be solved in PHP?

< ? php function calculateSquareSum ( $ n ) { if ( $ n <= 0 ) return 0 ; $ fibo [ 0 ] = 0 ; $ fibo [ 1 ] = 1 ; $ sum = ( $ fibo [ 0 ] * $ fibo [ 0 ] ) + ( $ fibo [ 1 ] * $ fibo [ 1 ] ) ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) { $ fibo [ $ i ] = $ fibo [ $ i - 1 ] + $ fibo [ $ i - 2 ] ; $ sum += ( $ fibo [ $ i ] * $ fibo [ $ i ] ) ; } return $ sum ; } $ n = 6 ; echo " Sum ▁ of ▁ squares ▁ of ▁ Fibonacci ▁ numbers ▁ is ▁ : ▁ " , calculateSquareSum ( $ n ) ; ? >


"Sum of squares of Fibonacci numbers | PHP Program to find sum of squares of Fibonacci numbers in O ( Log n ) time . ; Create an array for memoization ; Returns n 'th Fibonacci number using table f[] ; Base cases ; If fib ( n ) is already computed ; Applying above formula [ Note value n & 1 is 1 if n is odd , else 0 ] . ; Function to calculate sum of squares of Fibonacci numbers ; Driver Code"
How can the above be solved in PHP?

< ? php $ MAX = 1000 ; global $ f ; $ f = array_fill ( 0 , $ MAX , 0 ) ; function fib ( $ n ) { if ( $ n == 0 ) return 0 ; if ( $ n == 1 $ n == 2 ) return ( $ f [ $ n ] = 1 ) ; $ k = ( $ n & 1 ) ? ( $ n + 1 ) / 2 : $ n / 2 ; $ f [ $ n ] = ( $ n & 1 ) ? ( fib ( $ k ) * fib ( $ k ) + fib ( $ k - 1 ) * fib ( $ k - 1 ) ) : ( 2 * fib ( $ k - 1 ) + fib ( $ k ) ) * fib ( $ k ) ; return $ f [ $ n ] ; } function calculateSumOfSquares ( $ n ) { return fib ( $ n ) * fib ( $ n + 1 ) ; } $ n = 6 ; echo " Sum ▁ of ▁ Squares ▁ of ▁ Fibonacci ▁ numbers ▁ is ▁ : ▁ " ; echo calculateSumOfSquares ( $ n ) ; ? >


"Sum of squares of binomial coefficients | Return the sum of square of binomial coefficient ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Finding the sum of square of binomial coefficient . ; Driven Program"
How can the above be solved in PHP?


"Sum of squares of binomial coefficients | function to return product of number from start to end . ; Return the sum of square of binomial coefficient ; Driver Code"
How can the above be solved in PHP?

 < ? php function factorial ( $ start , $ end ) { $ res = 1 ; for ( $ i = $ start ; $ i <= $ end ; $ i ++ ) $ res *= $ i ; return $ res ; } function sumofsquare ( $ n ) { return factorial ( $ n + 1 , 2 * $ n ) / factorial ( 1 , $ n ) ; } $ n = 4 ; echo sumofsquare ( $ n ) , "
 " ; ? >


"Sum of squares of first n natural numbers | Function to calculate sum ; Driver code"
How can the above be solved in PHP?

< ? php function summation ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ sum += ( $ i * $ i ) ; return $ sum ; } $ n = 2 ; echo summation ( $ n ) ; ? >


"Sum of squares of first n natural numbers | Return the sum of square of first n natural numbers ; Driver Code"
How can the above be solved in PHP?

< ? php function squaresum ( $ n ) { return ( $ n * ( $ n + 1 ) / 2 ) * ( 2 * $ n + 1 ) / 3 ; } $ n = 4 ; echo squaresum ( $ n ) ; ? >


"Sum of squares of first n natural numbers | Return the sum of square of first n natural numbers ; Iterate i from 1 and n finding square of i and add to sum . ; Driven Code"
How can the above be solved in PHP?

< ? php function squaresum ( $ n ) { $ sum = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) $ sum += ( $ i * $ i ) ; return $ sum ; } $ n = 4 ; echo ( squaresum ( $ n ) ) ; ? >


"Sum of sum of all subsets of a set formed by first N natural numbers | Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; $x = $x % $p ; Update x if it is more than or equal to p ; If y is odd , multiply x with the result ; y must be even now $y = $y >> 1 ; y = y / 2 ; function to find ff ( n ) ; In formula n is starting from zero ; calculate answer using formula 2 ^ n * ( n ^ 2 + n + 2 ) - 1 ; whenever answer is greater than or equals to mod then modulo it . ; adding modulo while subtraction is very necessary otherwise it will cause wrong answer ; Driver code ; function call"
How can the above be solved in PHP?

 < ? php function power ( $ x , $ y , $ p ) { while ( $ y > 0 ) { if ( $ y & 1 ) $ res = ( $ res * $ x ) % $ p ; $ x = ( $ x * $ x ) % $ p ; } return $ res ; } function check ( $ n ) { $ mod = 1e9 + 7 ; $ n -- ; $ ans = $ n * $ n ; if ( $ ans >= $ mod ) $ ans %= $ mod ; $ ans += $ n + 2 ; if ( $ ans >= $ mod ) $ ans %= $ mod ; $ ans = ( power ( 2 , $ n , $ mod ) % $ mod * $ ans % $ mod ) % $ mod ; $ ans = ( $ ans - 1 + $ mod ) % $ mod ; return $ ans ; } $ n = 4 ; echo check ( $ n ) . "
 " ; ? >