codeparrot/xlcost-text-to-code huggingface dataset:
Move all negative elements to end in order with extra space allowed | Moves all - ve element to end of array in same order . ; Create an empty array to store result ; Traversal array and store + ve element in temp array index of temp ; If array contains all positive or all negative . ; Store - ve element in temp array ; Copy contents of temp [ ] to arr [ ] ; Driver Code
< ? php function segregateElements ( & $ arr , $ n ) { $ temp = array ( 0 , $ n , NULL ) ; $ j = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] >= 0 ) $ temp [ $ j ++ ] = $ arr [ $ i ] ; if ( $ j == $ n $ j == 0 ) return ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] < 0 ) $ temp [ $ j ++ ] = $ arr [ $ i ] ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ arr [ $ i ] = $ temp [ $ i ] ; } $ arr = array ( 1 , -1 , -3 , -2 , 7 , 5 , 11 , 6 ) ; $ n = sizeof ( $ arr ) ; segregateElements ( $ arr , $ n ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; ? >
Rearrange array such that even index elements are smaller and odd index elements are greater | Swap ; Rearrange ; Utility that prints out an array in a line ; Driver Code
< ? php function swap ( & $ a , & $ b ) { $ temp = $ a ; $ a = $ b ; $ b = $ temp ; } function rearrange ( & $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { if ( $ i % 2 == 0 && $ arr [ $ i ] > $ arr [ $ i + 1 ] ) swap ( $ arr [ $ i ] , $ arr [ $ i + 1 ] ) ; if ( $ i % 2 != 0 && $ arr [ $ i ] < $ arr [ $ i + 1 ] ) swap ( $ arr [ $ i ] , $ arr [ $ i + 1 ] ) ; } } function printArray ( & $ arr , $ size ) { for ( $ i = 0 ; $ i < $ size ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; echo " STRNEWLINE " ; } $ arr = array ( 6 , 4 , 2 , 1 , 8 , 3 ) ; $ n = sizeof ( $ arr ) ; echo " Before ▁ rearranging : ▁ STRNEWLINE " ; printArray ( $ arr , $ n ) ; rearrange ( $ arr , $ n ) ; echo " After ▁ rearranging : ▁ STRNEWLINE " ; printArray ( $ arr , $ n ) ; ? >
Positive elements at even and negative at odd positions ( Relative order not maintained ) | PHP program to rearrange positive and negative numbers ; Move forward the positive pointer till negative number number not encountered ; Move forward the negative pointer till positive number number not encountered ; Swap array elements to fix their position . ; Break from the while loop when any index exceeds the size of the array ; Driver code
< ? php function rearrange ( & $ a , $ size ) { $ positive = 0 ; $ negative = 1 ; while ( true ) { while ( $ positive < $ size && $ a [ $ positive ] >= 0 ) $ positive += 2 ; while ( $ negative < $ size && $ a [ $ negative ] <= 0 ) $ negative += 2 ; if ( $ positive < $ size && $ negative < $ size ) { $ temp = $ a [ $ positive ] ; $ a [ $ positive ] = $ a [ $ negative ] ; $ a [ $ negative ] = $ temp ; } else break ; } } $ arr = array ( 1 , -3 , 5 , 6 , -3 , 6 , 7 , -4 , 9 , 10 ) ; $ n = sizeof ( $ arr ) ; rearrange ( $ arr , $ n ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; ? >
Segregate even and odd numbers | Set 3 | Function to segregate even odd numbers ; Swapping even and odd numbers ; Printing segregated array ; Driver code
< ? php function arrayEvenAndOdd ( $ arr , $ n ) { $ i = -1 ; $ j = 0 ; $ t ; while ( $ j != $ n ) { if ( $ arr [ $ j ] % 2 == 0 ) { $ i ++ ; $ x = $ arr [ $ i ] ; $ arr [ $ i ] = $ arr [ $ j ] ; $ arr [ $ j ] = $ x ; } $ j ++ ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; } $ arr = array ( 1 , 3 , 2 , 4 , 7 , 6 , 9 , 10 ) ; $ n = sizeof ( $ arr ) ; arrayEvenAndOdd ( $ arr , $ n ) ; ? >
Program to find largest element in an array | PHP function to find maximum in arr [ ] of size n ; Initialize maximum element ; Traverse array elements from second and compare every element with current max ; Driver Code
< ? php function largest ( $ arr , $ n ) { $ i ; $ max = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] > $ max ) $ max = $ arr [ $ i ] ; return $ max ; } $ arr = array ( 10 , 324 , 45 , 90 , 9808 ) ; $ n = sizeof ( $ arr ) ; echo " Largest ▁ in ▁ given ▁ array ▁ is ▁ " , largest ( $ arr , $ n ) ; ? >
Program to find largest element in an array | returns maximum in arr [ ] of size n ; Driver Code
< ? php function largest ( $ arr , $ n ) { return max ( $ arr ) ; } $ arr = array ( 10 , 324 , 45 , 90 , 9808 ) ; $ n = count ( $ arr ) ; echo largest ( $ arr , $ n ) ; ? >
Program for Mean and median of an unsorted array | Function for calculating mean ; Function for calculating median ; First we sort the array ; check for even case ; Driver Code ; Function call
< ? php function findMean ( & $ a , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += $ a [ $ i ] ; return ( double ) $ sum / ( double ) $ n ; } function findMedian ( & $ a , $ n ) { sort ( $ a ) ; if ( $ n % 2 != 0 ) return ( double ) $ a [ $ n / 2 ] ; return ( double ) ( $ a [ ( $ n - 1 ) / 2 ] + $ a [ $ n / 2 ] ) / 2.0 ; } $ a = array ( 1 , 3 , 4 , 2 , 7 , 5 , 8 , 6 ) ; $ n = sizeof ( $ a ) ; echo " Mean ▁ = ▁ " . findMean ( $ a , $ n ) . " STRNEWLINE " ; echo " Median ▁ = ▁ " . findMedian ( $ a , $ n ) ; ? >
k smallest elements in same order using O ( 1 ) extra space | Function to print smallest k numbers in arr [ 0. . n - 1 ] ; For each arr [ i ] find whether it is a part of n - smallest with insertion sort concept ; find largest from first k - elements ; if largest is greater than arr [ i ] shift all element one place left ; make arr [ k - 1 ] = arr [ i ] ; print result ; Driver Code
< ? php function printSmall ( $ arr , $ n , $ k ) { for ( $ i = $ k ; $ i < $ n ; ++ $ i ) { $ max_var = $ arr [ $ k - 1 ] ; $ pos = $ k - 1 ; for ( $ j = $ k - 2 ; $ j >= 0 ; $ j -- ) { if ( $ arr [ $ j ] > $ max_var ) { $ max_var = $ arr [ $ j ] ; $ pos = $ j ; } } if ( $ max_var > $ arr [ $ i ] ) { $ j = $ pos ; while ( $ j < $ k - 1 ) { $ arr [ $ j ] = $ arr [ $ j + 1 ] ; $ j ++ ; } $ arr [ $ k - 1 ] = $ arr [ $ i ] ; } } for ( $ i = 0 ; $ i < $ k ; $ i ++ ) echo $ arr [ $ i ] , " ▁ " ; } $ arr = array ( 1 , 5 , 8 , 9 , 6 , 7 , 3 , 4 , 2 , 0 ) ; $ n = count ( $ arr ) ; $ k = 5 ; printSmall ( $ arr , $ n , $ k ) ; ? >
Find Second largest element in an array | Function to print the second largest elements ; There should be atleast two elements ; If current element is smaller than first then update both first and second ; If arr [ i ] is in between first and second then update second ; Driver Code
< ? php function print2largest ( $ arr , $ arr_size ) { if ( $ arr_size < 2 ) { echo ( " ▁ Invalid ▁ Input ▁ " ) ; return ; } $ first = $ second = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ arr_size ; $ i ++ ) { if ( $ arr [ $ i ] > $ first ) { $ second = $ first ; $ first = $ arr [ $ i ] ; } else if ( $ arr [ $ i ] > $ second && $ arr [ $ i ] != $ first ) $ second = $ arr [ $ i ] ; } if ( $ second == PHP_INT_MIN ) echo ( " There ▁ is ▁ no ▁ second ▁ largest ▁ element STRNEWLINE " ) ; else echo ( " The ▁ second ▁ largest ▁ element ▁ is ▁ " . $ second . " STRNEWLINE " ) ; } $ arr = array ( 12 , 35 , 1 , 10 , 34 , 1 ) ; $ n = sizeof ( $ arr ) ; print2largest ( $ arr , $ n ) ; ? >
Find sum of all nodes of the given perfect binary tree | function to find sum of all of the nodes of given perfect binary tree ; no of leaf nodes ; sum of nodes at last level ; sum of all nodes ; Driver Code
< ? php function sumNodes ( $ l ) { $ leafNodeCount = ( $ l - 1 ) * ( $ l - 1 ) ; $ sumLastLevel = 0 ; $ sumLastLevel = ( $ leafNodeCount * ( $ leafNodeCount + 1 ) ) / 2 ; $ sum = $ sumLastLevel * $ l ; return $ sum ; } $ l = 3 ; echo ( sumNodes ( $ l ) ) ; ? >
Constant time range add operation on an array | Utility method to add value val , to range [ lo , hi ] ; Utility method to get actual array from operation array ; convert array into prefix sum array ; method to print final updated array ; Driver Code ; Range add Queries
< ? php function add ( & $ arr , $ N , $ lo , $ hi , $ val ) { $ arr [ $ lo ] += $ val ; if ( $ hi != $ N - 1 ) $ arr [ $ hi + 1 ] -= $ val ; } function updateArray ( & $ arr , $ N ) { for ( $ i = 1 ; $ i < $ N ; $ i ++ ) $ arr [ $ i ] += $ arr [ $ i - 1 ] ; } function printArr ( & $ arr , $ N ) { updateArray ( $ arr , $ N ) ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; echo " STRNEWLINE " ; } $ N = 6 ; $ arr = array_fill ( 0 , $ N , NULL ) ; add ( $ arr , $ N , 0 , 2 , 100 ) ; add ( $ arr , $ N , 1 , 5 , 100 ) ; add ( $ arr , $ N , 2 , 3 , 100 ) ; printArr ( $ arr , $ N ) ; ? >
Queries for GCD of all numbers of an array except elements in a given range | Calculating GCD using euclid algorithm ; Filling the prefix and suffix array ; Filling the prefix array following relation prefix ( i ) = GCD ( prefix ( i - 1 ) , arr ( i ) ) ; Filling the suffix array following the relation suffix ( i ) = GCD ( suffix ( i + 1 ) , arr ( i ) ) ; To calculate gcd of the numbers outside range ; If l = 0 , we need to tell GCD of numbers from r + 1 to n ; If r = n - 1 we need to return the gcd of numbers from 1 to l ; Driver Code
< ? php function GCD ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; return GCD ( $ b , $ a % $ b ) ; } function FillPrefixSuffix ( & $ prefix , & $ arr , & $ suffix , $ n ) { $ prefix [ 0 ] = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ prefix [ $ i ] = GCD ( $ prefix [ $ i - 1 ] , $ arr [ $ i ] ) ; $ suffix [ $ n - 1 ] = $ arr [ $ n - 1 ] ; for ( $ i = $ n - 2 ; $ i >= 0 ; $ i -- ) $ suffix [ $ i ] = GCD ( $ suffix [ $ i + 1 ] , $ arr [ $ i ] ) ; } function GCDoutsideRange ( $ l , $ r , & $ prefix , & $ suffix , $ n ) { if ( $ l == 0 ) return $ suffix [ $ r + 1 ] ; if ( $ r == $ n - 1 ) return $ prefix [ $ l - 1 ] ; return GCD ( $ prefix [ $ l - 1 ] , $ suffix [ $ r + 1 ] ) ; } $ arr = array ( 2 , 6 , 9 ) ; $ n = sizeof ( $ arr ) ; $ prefix = array_fill ( 0 , $ n , NULL ) ; $ suffix = array_fill ( 0 , $ n , NULL ) ; FillPrefixSuffix ( $ prefix , $ arr , $ suffix , $ n ) ; $ l = 0 ; $ r = 0 ; echo GCDoutsideRange ( $ l , $ r , $ prefix , $ suffix , $ n ) . " " ; $ l = 1 ; $ r = 1 ; echo GCDoutsideRange ( $ l , $ r , $ prefix , $ suffix , $ n ) . " " ; $ l = 1 ; $ r = 2 ; echo GCDoutsideRange ( $ l , $ r , $ prefix , $ suffix , $ n ) . " " ; ? >
Queries for counts of array elements with values in given range | function to count elements within given range ; initialize result ; check if element is in range ; Driver Code ; Answer queries
< ? php function countInRange ( $ arr , $ n , $ x , $ y ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] >= $ x && $ arr [ $ i ] <= $ y ) $ count ++ ; } return $ count ; } $ arr = array ( 1 , 3 , 4 , 9 , 10 , 3 ) ; $ n = count ( $ arr ) ; $ i = 1 ; $ j = 4 ; echo countInRange ( $ arr , $ n , $ i , $ j ) . " " ; $ i = 9 ; $ j = 12 ; echo countInRange ( $ arr , $ n , $ i , $ j ) . " " ; ? >
Queries for counts of array elements with values in given range | function to find first index >= x ; function to find last index <= y ; function to count elements within given range ; initialize result ; Driver Code ; Preprocess array ; Answer queries
< ? php function lowerIndex ( $ arr , $ n , $ x ) { $ l = 0 ; $ h = $ n - 1 ; while ( $ l <= $ h ) { $ mid = ( $ l + $ h ) / 2 ; if ( $ arr [ $ mid ] >= $ x ) $ h = $ mid - 1 ; else $ l = $ mid + 1 ; } return $ l ; } function upperIndex ( $ arr , $ n , $ y ) { $ l = 0 ; $ h = $ n - 1 ; while ( $ l <= $ h ) { $ mid = ( $ l + $ h ) / 2 ; if ( $ arr [ $ mid ] <= $ y ) $ l = $ mid + 1 ; else $ h = $ mid - 1 ; } return $ h ; } function countInRange ( $ arr , $ n , $ x , $ y ) { $ count = 0 ; $ count = ( upperIndex ( $ arr , $ n , $ y ) - lowerIndex ( $ arr , $ n , $ x ) + 1 ) ; $ t = floor ( $ count ) ; return $ t ; } $ arr = array ( 1 , 4 , 4 , 9 , 10 , 3 ) ; $ n = sizeof ( $ arr ) ; sort ( $ arr ) ; $ i = 1 ; $ j = 4 ; echo countInRange ( $ arr , $ n , $ i , $ j ) , " " ; $ i = 9 ; $ j = 12 ; echo countInRange ( $ arr , $ n , $ i , $ j ) , " " ; ? >
Queries for decimal values of subarrays of a binary array | Fills pre [ ] ; returns the number represented by a binary subarray l to r ; if r is equal to n - 1 r + 1 does not exist ; Driver Code
< ? php function precompute ( & $ arr , $ n , & $ pre ) { $ pre [ $ n - 1 ] = $ arr [ $ n - 1 ] * pow ( 2 , 0 ) ; for ( $ i = $ n - 2 ; $ i >= 0 ; $ i -- ) $ pre [ $ i ] = $ pre [ $ i + 1 ] + $ arr [ $ i ] * ( 1 << ( $ n - 1 - $ i ) ) ; } function decimalOfSubarr ( & $ arr , $ l , $ r , $ n , & $ pre ) { if ( $ r != $ n - 1 ) return ( $ pre [ $ l ] - $ pre [ $ r + 1 ] ) / ( 1 << ( $ n - 1 - $ r ) ) ; return $ pre [ $ l ] / ( 1 << ( $ n - 1 - $ r ) ) ; } $ arr = array ( 1 , 0 , 1 , 0 , 1 , 1 ) ; $ n = sizeof ( $ arr ) ; $ pre = array_fill ( 0 , $ n , NULL ) ; precompute ( $ arr , $ n , $ pre ) ; echo decimalOfSubarr ( $ arr , 2 , 4 , $ n , $ pre ) . " " ; echo decimalOfSubarr ( $ arr , 4 , 5 , $ n , $ pre ) . " " ; ? >
Count elements which divide all numbers in range L | function to count element Time complexity O ( n ^ 2 ) worst case ; answer for query ; 0 based index ; iterate for all elements ; check if the element divides all numbers in range ; no of elements ; if all elements are divisible by a [ i ] ; answer for every query ; Driver Code
< ? php function answerQuery ( $ a , $ n , $ l , $ r ) { $ count = 0 ; $ l = $ l - 1 ; for ( $ i = $ l ; $ i < $ r ; $ i ++ ) { $ element = $ a [ $ i ] ; $ divisors = 0 ; for ( $ j = $ l ; $ j < $ r ; $ j ++ ) { if ( $ a [ $ j ] % $ a [ $ i ] == 0 ) $ divisors ++ ; else break ; } if ( $ divisors == ( $ r - $ l ) ) $ count ++ ; } return $ count ; } $ a = array ( 1 , 2 , 3 , 5 ) ; $ n = sizeof ( $ a ) ; $ l = 1 ; $ r = 4 ; echo answerQuery ( $ a , $ n , $ l , $ r ) . " STRNEWLINE " ; $ l = 2 ; $ r = 4 ; echo answerQuery ( $ a , $ n , $ l , $ r ) . " STRNEWLINE " ;
Number whose sum of XOR with given array range is maximum | PHP program to find smallest integer X such that sum of its XOR with range is maximum . ; Function to make prefix array which counts 1 's of each bit up to that number ; Making a prefix array which sums number of 1 's up to  that position ; If j - th bit of a number is set then add one to previously counted 1 's ; Function to find X ; Initially taking maximum value all bits 1 ; Iterating over each bit ; get 1 ' s ▁ at ▁ ith ▁ bit ▁ between ▁ the ▁ ▁ range ▁ L - R ▁ by ▁ subtracting ▁ 1' s till Rth number - 1 's till L-1th number ; If 1 ' s ▁ are ▁ more ▁ than ▁ or ▁ equal ▁ ▁ to ▁ 0' s then unset the ith bit from answer ; Set ith bit to 0 by doing Xor with 1 ; Driver Code
< ? php $ one = array ( ) ; $ MAX = 2147483647 ; function make_prefix ( $ A , $ n ) { global $ one , $ MAX ; for ( $ j = 0 ; $ j < 32 ; $ j ++ ) $ one [ 0 ] [ $ j ] = 0 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ a = $ A [ $ i - 1 ] ; for ( $ j = 0 ; $ j < 32 ; $ j ++ ) { $ x = pow ( 2 , $ j ) ; if ( $ a & $ x ) $ one [ $ i ] [ $ j ] = 1 + $ one [ $ i - 1 ] [ $ j ] ; else $ one [ $ i ] [ $ j ] = $ one [ $ i - 1 ] [ $ j ] ; } } } function Solve ( $ L , $ R ) { global $ one , $ MAX ; $ l = $ L ; $ r = $ R ; $ tot_bits = $ r - $ l + 1 ; $ X = $ MAX ; for ( $ i = 0 ; $ i < 31 ; $ i ++ ) { $ x = $ one [ $ r ] [ $ i ] - $ one [ $ l - 1 ] [ $ i ] ; if ( $ x >= ( $ tot_bits - $ x ) ) { $ ith_bit = pow ( 2 , $ i ) ; $ X = $ X ^ $ ith_bit ; } } return $ X ; } $ n = 5 ; $ q = 3 ; $ A = [ 210 , 11 , 48 , 22 , 133 ] ; $ L = [ 1 , 4 , 2 ] ; $ R = [ 3 , 14 , 4 ] ; make_prefix ( $ A , $ n ) ; for ( $ j = 0 ; $ j < $ q ; $ j ++ ) echo ( Solve ( $ L [ $ j ] , $ R [ $ j ] ) . " STRNEWLINE " ) ; ? >
Count of pairs from 1 to a and 1 to b whose sum is divisible by N | Function to find the distinct pairs from 1 - a & 1 - b such that their sum is divisible by n . ; Iterate over 1 to a to find distinct pairs ; For each integer from 1 to a b / n integers exists such that pair sum is divisible by n ; If ( i % n + b % n ) >= n one more pair is possible ; Return answer ; Driver code
< ? php function findCountOfPairs ( $ a , $ b , $ n ) { $ ans = 0 ; for ( $ i = 1 ; $ i <= $ a ; $ i ++ ) { $ ans += ( int ) ( $ b / $ n ) ; $ ans += ( ( $ i % $ n ) + ( $ b % $ n ) ) >= $ n ? 1 : 0 ; } return $ ans ; } $ a = 5 ; $ b = 13 ; $ n = 3 ; echo findCountOfPairs ( $ a , $ b , $ n ) ; ? >
Buy minimum items without change and given coins | See if we can buy less than 10 items Using 10 Rs coins and one r Rs coin ; We can always buy 10 items ; Driver Code
< ? php function minItems ( $ k , $ r ) { for ( $ i = 1 ; $ i < 10 ; $ i ++ ) if ( ( $ i * $ k - $ r ) % 10 == 0 || ( $ i * $ k ) % 10 == 0 ) return $ i ; return 10 ; } $ k = 15 ; $ r = 2 ; echo minItems ( $ k , $ r ) ; ? >
Number of indexes with equal elements in given range | function that answers every query in O ( r - l ) ; traverse from l to r and count the required indexes ; Driver Code ; 1 - st query ; 2 nd query
< ? php function answer_query ( $ a , $ n , $ l , $ r ) { $ count = 0 ; for ( $ i = $ l ; $ i < $ r ; $ i ++ ) if ( $ a [ $ i ] == $ a [ $ i + 1 ] ) $ count += 1 ; return $ count ; } $ a = array ( 1 , 2 , 2 , 2 , 3 , 3 , 4 , 4 , 4 ) ; $ n = count ( $ a ) ; $ L = 1 ; $ R = 8 ; echo ( answer_query ( $ a , $ n , $ L , $ R ) . " " ) ; $ L = 0 ; $ R = 4 ; echo ( answer_query ( $ a , $ n , $ L , $ R ) . " " ) ; ? >
Number of indexes with equal elements in given range | PHP program to count the number of indexes in range L R such that Ai = Ai + 1 ; array to store count of index from 0 to i that obey condition ; precomputing prefixans [ ] array ; traverse to compute the prefixans [ ] array ; function that answers every query in O ( 1 ) ; Driver Code ; pre - computation ; 1 - st query ; 2 nd query
< ? php $ N = 1000 ; $ prefixans = array_fill ( 0 , $ N , 0 ) ; function countIndex ( $ a , $ n ) { global $ N , $ prefixans ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { if ( $ a [ $ i ] == $ a [ $ i + 1 ] ) $ prefixans [ $ i ] = 1 ; if ( $ i != 0 ) $ prefixans [ $ i ] += $ prefixans [ $ i - 1 ] ; } } function answer_query ( $ l , $ r ) { global $ N , $ prefixans ; if ( $ l == 0 ) return $ prefixans [ $ r - 1 ] ; else return ( $ prefixans [ $ r - 1 ] - $ prefixans [ $ l - 1 ] ) ; } $ a = array ( 1 , 2 , 2 , 2 , 3 , 3 , 4 , 4 , 4 ) ; $ n = count ( $ a ) ; countIndex ( $ a , $ n ) ; $ L = 1 ; $ R = 8 ; echo ( answer_query ( $ L , $ R ) . " " ) ; $ L = 0 ; $ R = 4 ; echo ( answer_query ( $ L , $ R ) . " " ) ; ? >
Count subarrays with Prime sum | Function to count subarrays with Prime sum ; USE SIEVE TO FIND ALL PRIME NUMBERS LESS THAN OR EQUAL TO max_val Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Remaining part of SIEVE ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Initialize result ; Traverse through the array ; return answer ; Driver program
< ? php function primeSubarrays ( $ A , $ n ) { $ max_val = pow ( 10 , 5 ) ; $ prime = array_fill ( 0 , $ max_val + 1 , true ) ; $ prime [ 0 ] = false ; $ prime [ 1 ] = false ; for ( $ p = 2 ; $ p * $ p <= $ max_val ; $ p ++ ) { if ( $ prime [ $ p ] == true ) { for ( $ i = $ p * 2 ; $ i <= $ max_val ; $ i += $ p ) $ prime [ $ i ] = false ; } } $ cnt = 0 ; for ( $ i = 0 ; $ i < $ n - 1 ; ++ $ i ) { $ val = $ A [ $ i ] ; for ( $ j = $ i + 1 ; $ j < $ n ; ++ $ j ) { $ val += $ A [ $ j ] ; if ( $ prime [ $ val ] ) ++ $ cnt ; } } return $ cnt ; } $ A = array ( 1 , 2 , 3 , 4 , 5 ) ; $ n = count ( $ A ) ; echo primeSubarrays ( $ A , $ n ) ; ? >
Total numbers with no repeated digits in a range | Function to check if the given number has repeated digit or not ; Traversing through each digit ; if the digit is present more than once in the number ; return 0 if the number has repeated digit ; return 1 if the number has no repeated digit ; Function to find total number in the given range which has no repeated digit ; Traversing through the range ; Add 1 to the answer if i has no repeated digit else 0 ; Driver Code ; Calling the calculate
< ? php function repeated_digit ( $ n ) { $ c = 10 ; $ a = array_fill ( 0 , $ c , 0 ) ; while ( $ n > 0 ) { $ d = $ n % 10 ; if ( $ a [ $ d ] > 0 ) { return 0 ; } $ a [ $ d ] ++ ; $ n = ( int ) ( $ n / 10 ) ; } return 1 ; } function calculate ( $ L , $ R ) { $ answer = 0 ; for ( $ i = $ L ; $ i <= $ R ; $ i ++ ) { $ answer += repeated_digit ( $ i ) ; } return $ answer ; } $ L = 1 ; $ R = 100 ; echo calculate ( $ L , $ R ) ; ? >
Minimum swaps required to make a binary string alternating | returns the minimum number of swaps of a binary string passed as the argument to make it alternating ; counts number of zeroes at odd and even positions ; counts number of ones at odd and even positions ; alternating string starts with 0 ; alternating string starts with 1 ; calculates the minimum number of swaps ; Driver code
< ? php function countMinSwaps ( $ st ) { $ min_swaps = 0 ; $ odd_0 = 0 ; $ even_0 = 0 ; $ odd_1 = 0 ; $ even_1 = 0 ; $ n = strlen ( $ st ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ i % 2 == 0 ) { if ( $ st [ $ i ] == '1' ) { $ even_1 ++ ; } else { $ even_0 ++ ; } } else { if ( $ st [ $ i ] == '1' ) { $ odd_1 ++ ; } else { $ odd_0 ++ ; } } } $ cnt_swaps_1 = min ( $ even_0 , $ odd_1 ) ; $ cnt_swaps_2 = min ( $ even_1 , $ odd_0 ) ; return min ( $ cnt_swaps_1 , $ cnt_swaps_2 ) ; } $ st = "000111" ; echo ( countMinSwaps ( $ st ) ) ; ? >
Find the value of the function Y = ( X ^ 6 + X ^ 2 + 9894845 ) % 971 | computing ( a ^ b ) % c ; Driver code
< ? php function modpow ( $ base , $ exp , $ modulus ) { $ base %= $ modulus ; $ result = 1 ; while ( $ exp > 0 ) { if ( $ exp & 1 ) $ result = ( $ result * $ base ) % $ modulus ; $ base = ( $ base * $ base ) % $ modulus ; $ exp >>= 1 ; } return $ result ; } $ n = 654654 ; $ mod = 971 ; echo ( ( ( modpow ( $ n , 6 , $ mod ) + modpow ( $ n , 2 , $ mod ) ) % $ mod + 355 ) % $ mod ) ; ? >
Largest Sum Contiguous Subarray | PHP program to print largest contiguous array sum ; Driver code
< ? php function maxSubArraySum ( $ a , $ size ) { $ max_so_far = PHP_INT_MIN ; $ max_ending_here = 0 ; for ( $ i = 0 ; $ i < $ size ; $ i ++ ) { $ max_ending_here = $ max_ending_here + $ a [ $ i ] ; if ( $ max_so_far < $ max_ending_here ) $ max_so_far = $ max_ending_here ; if ( $ max_ending_here < 0 ) $ max_ending_here = 0 ; } return $ max_so_far ; } $ a = array ( -2 , -3 , 4 , -1 , -2 , 1 , 5 , -3 ) ; $ n = count ( $ a ) ; $ max_sum = maxSubArraySum ( $ a , $ n ) ; echo " Maximum ▁ contiguous ▁ sum ▁ is ▁ " , $ max_sum ; ? >
Largest Sum Contiguous Subarray |  ; Do not compare for all elements . Compare only when max_ending_here > 0
< ? php < ? php function maxSubArraySum ( & $ a , $ size ) { $ max_so_far = $ a [ 0 ] ; $ max_ending_here = 0 ; for ( $ i = 0 ; $ i < $ size ; $ i ++ ) { $ max_ending_here = $ max_ending_here + $ a [ $ i ] ; if ( $ max_ending_here < 0 ) $ max_ending_here = 0 ; else if ( $ max_so_far < $ max_ending_here ) $ max_so_far = $ max_ending_here ; } return $ max_so_far ; ? >
Minimum rooms for m events of n batches with given schedule | Returns minimum number of rooms required to perform classes of n groups in m slots with given schedule . ; Store count of classes happening in every slot . ; initialize all values to zero ; Number of rooms required is equal to maximum classes happening in a particular slot . ; Driver Code
< ? php function findMinRooms ( $ slots , $ n , $ m ) { $ counts = array_fill ( 0 , $ m , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ m ; $ j ++ ) if ( $ slots [ $ i ] [ $ j ] == '1' ) $ counts [ $ j ] ++ ; return max ( $ counts ) ; } $ n = 3 ; $ m = 7 ; $ slots = array ( "0101011" , "0011001" , "0110111" ) ; echo findMinRooms ( $ slots , $ n , $ m ) ; ? >
Largest Sum Contiguous Subarray |  ; Driver Code
< ? php < ? php function maxSubArraySum ( $ a , $ size ) { $ max_so_far = $ a [ 0 ] ; $ curr_max = $ a [ 0 ] ; for ( $ i = 1 ; $ i < $ size ; $ i ++ ) { $ curr_max = max ( $ a [ $ i ] , $ curr_max + $ a [ $ i ] ) ; $ max_so_far = max ( $ max_so_far , $ curr_max ) ; } return $ max_so_far ; } $ a = array ( -2 , -3 , 4 , -1 , -2 , 1 , 5 , -3 ) ; $ n = sizeof ( $ a ) ; $ max_sum = maxSubArraySum ( $ a , $ n ) ; echo " Maximum ▁ contiguous ▁ sum ▁ is ▁ " . $ max_sum ; ? >