codeparrot/xlcost-text-to-code huggingface dataset:
Given an n x n square matrix , find sum of all sub | Size of given matrix ; A O ( n ^ 2 ) function to find sum of all sub - squares of size k x k in a given square matrix of size n x n ; k must be smaller than or equal to n ; 1 : PREPROCESSING To store sums of all strips of size k x 1 ; Go column by column ; Calculate sum of first k x 1 rectangle in this column ; Calculate sum of remaining rectangles ; 2 : CALCULATE SUM of Sub - Squares using stripSum [ ] [ ] ; Calculate and print sum of first subsquare in this row ; Calculate sum of remaining squares in current row by removing the leftmost strip of previous sub - square and adding a new strip ; Driver Code
< ? php $ n = 5 ; function printSumTricky ( $ mat , $ k ) { global $ n ; if ( $ k > $ n ) return ; $ stripSum = array ( array ( ) ) ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ k ; $ i ++ ) $ sum += $ mat [ $ i ] [ $ j ] ; $ stripSum [ 0 ] [ $ j ] = $ sum ; for ( $ i = 1 ; $ i < $ n - $ k + 1 ; $ i ++ ) { $ sum += ( $ mat [ $ i + $ k - 1 ] [ $ j ] - $ mat [ $ i - 1 ] [ $ j ] ) ; $ stripSum [ $ i ] [ $ j ] = $ sum ; } } for ( $ i = 0 ; $ i < $ n - $ k + 1 ; $ i ++ ) { $ sum = 0 ; for ( $ j = 0 ; $ j < $ k ; $ j ++ ) $ sum += $ stripSum [ $ i ] [ $ j ] ; echo $ sum , " " ; for ( $ j = 1 ; $ j < $ n - $ k + 1 ; $ j ++ ) { $ sum += ( $ stripSum [ $ i ] [ $ j + $ k - 1 ] - $ stripSum [ $ i ] [ $ j - 1 ] ) ; echo $ sum , " " ; } echo " STRNEWLINE " ; } } $ mat = array ( array ( 1 , 1 , 1 , 1 , 1 ) , array ( 2 , 2 , 2 , 2 , 2 ) , array ( 3 , 3 , 3 , 3 , 3 ) , array ( 4 , 4 , 4 , 4 , 4 ) , array ( 5 , 5 , 5 , 5 , 5 ) ) ; $ k = 3 ; printSumTricky ( $ mat , $ k ) ; ? >
Program to find transpose of a matrix | PHP Program to find transpose of a matrix ; This function stores transpose of A [ ] [ ] in B [ ] [ ] ; Driver code
< ? php $ N = 4 ; $ M = 3 ; function transpose ( & $ A , & $ B ) { for ( $ i = 0 ; $ i < $ N ; $ i ++ ) for ( $ j = 0 ; $ j < $ M ; $ j ++ ) $ B [ $ i ] [ $ j ] = $ A [ $ j ] [ $ i ] ; } $ A = array ( array ( 1 , 1 , 1 , 1 ) , array ( 2 , 2 , 2 , 2 ) , array ( 3 , 3 , 3 , 3 ) ) ; $ N = 4 ; $ M = 3 ; transpose ( $ A , $ B ) ; echo " Result ▁ matrix ▁ is ▁ STRNEWLINE " ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { for ( $ j = 0 ; $ j < $ M ; $ j ++ ) { echo $ B [ $ i ] [ $ j ] ; echo " ▁ " ; } echo " STRNEWLINE " ; } ? >
Program to find transpose of a matrix | PHP Program to find transpose of a matrix ; Converts A [ ] [ ] to its transpose ; Driver Code
< ? php $ N = 4 ; function transpose ( & $ A ) { for ( $ i = 0 ; $ i < $ N ; $ i ++ ) for ( $ j = $ i + 1 ; $ j < $ N ; $ j ++ ) { $ temp = $ A [ $ i ] [ $ j ] ; $ A [ $ i ] [ $ j ] = $ A [ $ j ] [ $ i ] ; $ A [ $ j ] [ $ i ] = $ temp ; } } $ N = 4 ; $ A = array ( array ( 1 , 1 , 1 , 1 ) , array ( 2 , 2 , 2 , 2 ) , array ( 3 , 3 , 3 , 3 ) , array ( 4 , 4 , 4 , 4 ) ) ; transpose ( $ A ) ; echo " Modified ▁ matrix ▁ is ▁ " . " STRNEWLINE " ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { for ( $ j = 0 ; $ j < $ N ; $ j ++ ) echo $ A [ $ i ] [ $ j ] . " ▁ " ; echo " STRNEWLINE " ; } ? >
Number of paths with exactly k coins | A Naive Recursive PHP program to count paths with exactly ' k ' coins ; Recursive function to count paths with sum k from ( 0 , 0 ) to ( m , n ) ; Base cases ; ( m , n ) can be reached either through ( m - 1 , n ) or through ( m , n - 1 ) ; A wrapper over pathCountRec ( ) ; Driver program
< ? php $ R = 3 ; $ C = 3 ; function pathCountRec ( $ mat , $ m , $ n , $ k ) { if ( $ m < 0 or $ n < 0 ) return 0 ; if ( $ m == 0 and $ n == 0 ) return ( $ k == $ mat [ $ m ] [ $ n ] ) ; return pathCountRec ( $ mat , $ m - 1 , $ n , $ k - $ mat [ $ m ] [ $ n ] ) + pathCountRec ( $ mat , $ m , $ n - 1 , $ k - $ mat [ $ m ] [ $ n ] ) ; } function pathCount ( $ mat , $ k ) { global $ R , $ C ; return pathCountRec ( $ mat , $ R - 1 , $ C - 1 , $ k ) ; } $ k = 12 ; $ mat = array ( array ( 1 , 2 , 3 ) , array ( 4 , 6 , 5 ) , array ( 3 , 2 , 1 ) ) ; echo pathCount ( $ mat , $ k ) ; ? >
Selection Sort | sort function ; One by one move boundary of unsorted subarray ; Find the minimum element in unsorted array ; swap the minimum value to $ith node ; Driver Code
< ? php function selection_sort ( & $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ low = $ i ; for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) { if ( $ arr [ $ j ] < $ arr [ $ low ] ) { $ low = $ j ; } } if ( $ arr [ $ i ] > $ arr [ $ low ] ) { $ tmp = $ arr [ $ i ] ; $ arr [ $ i ] = $ arr [ $ low ] ; $ arr [ $ low ] = $ tmp ; } } } $ arr = array ( 64 , 25 , 12 , 22 , 11 ) ; $ len = count ( $ arr ) ; selection_sort ( $ arr , $ len ) ; echo " Sorted ▁ array ▁ : ▁ STRNEWLINE " ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; ? >
Bubble Sort | An optimized version of Bubble Sort ; traverse the array from 0 to n - i - 1. Swap if the element found is greater than the next element ; IF no two elements were swapped by inner loop , then break ; Driver code to test above
< ? php function bubbleSort ( & $ arr ) { $ n = sizeof ( $ arr ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ swapped = False ; for ( $ j = 0 ; $ j < $ n - $ i - 1 ; $ j ++ ) { if ( $ arr [ $ j ] > $ arr [ $ j + 1 ] ) { $ t = $ arr [ $ j ] ; $ arr [ $ j ] = $ arr [ $ j + 1 ] ; $ arr [ $ j + 1 ] = $ t ; $ swapped = True ; } } if ( $ swapped == False ) break ; } } $ arr = array ( 64 , 34 , 25 , 12 , 22 , 11 , 90 ) ; $ len = sizeof ( $ arr ) ; bubbleSort ( $ arr ) ; echo " Sorted ▁ array ▁ : ▁ STRNEWLINE " ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) echo $ arr [ $ i ] . " ▁ " ; ? >
Find k closest elements to a given value | Function to find the cross over point ( the point before which elements are smaller than or equal to x and after which greater than x ) ; Base cases x is greater than all ; x is smaller than all ; Find the middle point ; If x is same as middle element , then return mid ; If x is greater than arr [ mid ] , then either arr [ mid + 1 ] is ceiling of x or ceiling lies in arr [ mid + 1. . . high ] ; This function prints k closest elements to x in arr [ ] . n is the number of elements in arr [ ] ; Find the crossover point ; Right index to search ; To keep track of count of elements already printed ; If x is present in arr [ ] , then reduce left index Assumption : all elements in arr [ ] are distinct ; Compare elements on left and right of crossover point to find the k closest elements ; If there are no more elements on right side , then print left elements ; If there are no more elements on left side , then print right elements ; Driver Code
< ? php function findCrossOver ( $ arr , $ low , $ high , $ x ) { if ( $ arr [ $ high ] <= $ x ) return $ high ; if ( $ arr [ $ low ] > $ x ) return $ low ; $ mid = ( $ low + $ high ) / 2 ; if ( $ arr [ $ mid ] <= $ x and $ arr [ $ mid + 1 ] > $ x ) return $ mid ; if ( $ arr [ $ mid ] < $ x ) return findCrossOver ( $ arr , $ mid + 1 , $ high , $ x ) ; return findCrossOver ( $ arr , $ low , $ mid - 1 , $ x ) ; } function printKclosest ( $ arr , $ x , $ k , $ n ) { $ l = findCrossOver ( $ arr , 0 , $ n - 1 , $ x ) ; $ r = $ l + 1 ; $ count = 0 ; if ( $ arr [ $ l ] == $ x ) $ l -- ; while ( $ l >= 0 and $ r < $ n and $ count < $ k ) { if ( $ x - $ arr [ $ l ] < $ arr [ $ r ] - $ x ) echo $ arr [ $ l -- ] , " ▁ " ; else echo $ arr [ $ r ++ ] , " ▁ " ; $ count ++ ; } while ( $ count < $ k and $ l >= 0 ) echo $ arr [ $ l -- ] , " ▁ " ; $ count ++ ; while ( $ count < $ k and $ r < $ n ) echo $ arr [ $ r ++ ] ; $ count ++ ; } $ arr = array ( 12 , 16 , 22 , 30 , 35 , 39 , 42 , 45 , 48 , 50 , 53 , 55 , 56 ) ; $ n = count ( $ arr ) ; $ x = 35 ; $ k = 4 ; printKclosest ( $ arr , $ x , 4 , $ n ) ; ? >
Coin Change | DP | Returns the count of ways we can sum S [ 0. . . m - 1 ] coins to get sum n ; If n is 0 then there is 1 solution ( do not include any coin ) ; If n is less than 0 then no solution exists ; If there are no coins and n is greater than 0 , then no solution exist ; count is sum of solutions ( i ) including S [ m - 1 ] ( ii ) excluding S [ m - 1 ] ; Driver Code
< ? php function coun ( $ S , $ m , $ n ) { if ( $ n == 0 ) return 1 ; if ( $ n < 0 ) return 0 ; if ( $ m <= 0 && $ n >= 1 ) return 0 ; return coun ( $ S , $ m - 1 , $ n ) + coun ( $ S , $ m , $ n - $ S [ $ m - 1 ] ) ; } $ arr = array ( 1 , 2 , 3 ) ; $ m = count ( $ arr ) ; echo coun ( $ arr , $ m , 4 ) ; ? >
Coin Change | DP | Dynamic Programming PHP implementation of Coin Change problem ; table [ i ] will be storing the number of solutions for value i . We need n + 1 rows as the table is constructed in bottom up manner using the base case ( n = 0 ) ; Base case ( If given value is 0 ) ; Pick all coins one by one and update the table [ ] values after the index greater than or equal to the value of the picked coin ; Driver Code
< ? php function count_1 ( & $ S , $ m , $ n ) { $ table = array_fill ( 0 , $ n + 1 , NULl ) ; $ table [ 0 ] = 1 ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) for ( $ j = $ S [ $ i ] ; $ j <= $ n ; $ j ++ ) $ table [ $ j ] += $ table [ $ j - $ S [ $ i ] ] ; return $ table [ $ n ] ; } $ arr = array ( 1 , 2 , 3 ) ; $ m = sizeof ( $ arr ) ; $ n = 4 ; $ x = count_1 ( $ arr , $ m , $ n ) ; echo $ x ; ? >
Matrix Chain Multiplication | DP | Matrix Ai has dimension p [ i - 1 ] x p [ i ] for i = 1. . n ; For simplicity of the program , one extra row and one extra column are allocated in m [ ] [ ] . 0 th row and 0 th column of m [ ] [ ] are not used ; cost is zero when multiplying one matrix . ; L is chain length . ; q = cost / scalar multiplications ; Driver Code
< ? php function MatrixChainOrder ( $ p , $ n ) { $ m [ ] [ ] = array ( $ n , $ n ) ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ m [ $ i ] [ $ i ] = 0 ; for ( $ L = 2 ; $ L < $ n ; $ L ++ ) { for ( $ i = 1 ; $ i < $ n - $ L + 1 ; $ i ++ ) { $ j = $ i + $ L - 1 ; if ( $ j == $ n ) continue ; $ m [ $ i ] [ $ j ] = PHP_INT_MAX ; for ( $ k = $ i ; $ k <= $ j - 1 ; $ k ++ ) { $ q = $ m [ $ i ] [ $ k ] + $ m [ $ k + 1 ] [ $ j ] + $ p [ $ i - 1 ] * $ p [ $ k ] * $ p [ $ j ] ; if ( $ q < $ m [ $ i ] [ $ j ] ) $ m [ $ i ] [ $ j ] = $ q ; } } } return $ m [ 1 ] [ $ n - 1 ] ; } $ arr = array ( 1 , 2 , 3 , 4 ) ; $ size = sizeof ( $ arr ) ; echo " Minimum ▁ number ▁ of ▁ multiplications ▁ is ▁ " . MatrixChainOrder ( $ arr , $ size ) ; ? >
Cutting a Rod | DP | Returns the best obtainable price for a rod of length n and price [ ] as prices of different pieces ; Recursively cut the rod in different pieces and compare different configurations ; Driver Code
< ? php function cutRod ( $ price , $ n ) { if ( $ n <= 0 ) return 0 ; $ max_val = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ max_val = max ( $ max_val , $ price [ $ i ] + cutRod ( $ price , $ n - $ i - 1 ) ) ; return $ max_val ; } $ arr = array ( 1 , 5 , 8 , 9 , 10 , 17 , 17 , 20 ) ; $ size = count ( $ arr ) ; echo " Maximum ▁ Obtainable ▁ Value ▁ is ▁ " , cutRod ( $ arr , $ size ) ; ? >
Multiply two integers without using multiplication , division and bitwise operators , and no loops | function to multiply two numbers x and y ; 0 multiplied with anything gives 0 ; Add x one by one ; the case where y is negative ; Driver Code
< ? php function multiply ( $ x , $ y ) { if ( $ y == 0 ) return 0 ; if ( $ y > 0 ) return ( $ x + multiply ( $ x , $ y - 1 ) ) ; if ( $ y < 0 ) return - multiply ( $ x , - $ y ) ; } echo multiply ( 5 , -11 ) ; ? >
Sieve of Eratosthenes | php program to print all primes smaller than or equal to n using Sieve of Eratosthenes ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Print all prime numbers ; Driver Code
< ? php function SieveOfEratosthenes ( $ n ) { $ prime = array_fill ( 0 , $ n + 1 , true ) ; for ( $ p = 2 ; $ p * $ p <= $ n ; $ p ++ ) { if ( $ prime [ $ p ] == true ) { for ( $ i = $ p * $ p ; $ i <= $ n ; $ i += $ p ) $ prime [ $ i ] = false ; } } for ( $ p = 2 ; $ p <= $ n ; $ p ++ ) if ( $ prime [ $ p ] ) echo $ p . " " ; } $ n = 30 ; echo " Following ▁ are ▁ the ▁ prime ▁ numbers ▁ " . " smaller ▁ than ▁ or ▁ equal ▁ to ▁ " . $ n . " STRNEWLINE " ; SieveOfEratosthenes ( $ n ) ; ? >
Pascal 's Triangle | binomialCoeff ; Function to print first n lines of Pascal 's Triangle ; Iterate through every line and print entries in it ; Every line has number of integers equal to line number ; Driver Code
< ? php function binomialCoeff ( $ n , $ k ) { $ res = 1 ; if ( $ k > $ n - $ k ) $ k = $ n - $ k ; for ( $ i = 0 ; $ i < $ k ; ++ $ i ) { $ res *= ( $ n - $ i ) ; $ res /= ( $ i + 1 ) ; } return $ res ; } function printPascal ( $ n ) { for ( $ line = 0 ; $ line < $ n ; $ line ++ ) { for ( $ i = 0 ; $ i <= $ line ; $ i ++ ) echo " " . binomialCoeff ( $ line , $ i ) . " ▁ " ; echo " STRNEWLINE " ; } } $ n = 7 ; printPascal ( $ n ) ; ? >
Pascal 's Triangle | A O ( n ^ 2 ) time and O ( n ^ 2 ) extra space method for Pascal 's Triangle ; An auxiliary array to store generated pscal triangle values ; Iterate through every line and print integer ( s ) in it ; Every line has number of integers equal to line number ; First and last values in every row are 1 ; Other values are sum of values just above and left of above ; Driver code
< ? php function printPascal ( $ n ) { $ arr = array ( array ( ) ) ; for ( $ line = 0 ; $ line < $ n ; $ line ++ ) { for ( $ i = 0 ; $ i <= $ line ; $ i ++ ) { if ( $ line == $ i $ i == 0 ) $ arr [ $ line ] [ $ i ] = 1 ; else $ arr [ $ line ] [ $ i ] = $ arr [ $ line - 1 ] [ $ i - 1 ] + $ arr [ $ line - 1 ] [ $ i ] ; echo $ arr [ $ line ] [ $ i ] . " " ; } echo " STRNEWLINE " ; } } $ n = 5 ; printPascal ( $ n ) ; ? >
Pascal 's Triangle | PHP program for Pascal ' s ▁ Triangle ▁ A ▁ O ( n ^ 2 ) ▁ time ▁ and ▁ O ( 1 ) ▁ extra ▁ space ▁ method ▁ for ▁ Pascal ' s Triangle Pascal function ; used to represent C ( line , i ) ; The first value in a line is always 1 ; Driver code
< ? php function printPascal ( $ n ) { for ( $ line = 1 ; $ line <= $ n ; $ line ++ ) { $ C = 1 ; for ( $ i = 1 ; $ i <= $ line ; $ i ++ ) { print ( $ C . " " ) ; $ C = $ C * ( $ line - $ i ) / $ i ; } print ( " STRNEWLINE " ) ; } } $ n = 5 ; printPascal ( $ n ) ; ? >
Add two numbers without using arithmetic operators | PHP Program to add two numbers without using arithmetic operator ; Iterate till there is no carry ; carry now contains common set bits of x and y ; Sum of bits of x and y where at least one of the bits is not set ; Carry is shifted by one so that adding it to x gives the required sum ; Driver Code
< ? php function Add ( $ x , $ y ) { while ( $ y != 0 ) { $ carry = $ x & $ y ; $ x = $ x ^ $ y ; $ y = $ carry << 1 ; } return $ x ; } echo Add ( 15 , 32 ) ; ? >
Compute modulus division by a power | This function will return n % d . d must be one of : 1 , 2 , 4 , 8 , 16 , 32 , ... ; Driver Code ; d must be a power of 2
< ? php function getModulo ( $ n , $ d ) { return ( $ n & ( $ d - 1 ) ) ; } $ n = 6 ; $ d = 4 ; echo $ n , " ▁ moduo " , " ▁ " , $ d , " ▁ is ▁ " , " ▁ " , getModulo ( $ n , $ d ) ; ? >
Count set bits in an integer | Function to get no of set bits in binary representation of positive integer n ; Driver Code
< ? php function countSetBits ( $ n ) { $ count = 0 ; while ( $ n ) { $ count += $ n & 1 ; $ n >>= 1 ; } return $ count ; } $ i = 9 ; echo countSetBits ( $ i ) ; ? >
Count set bits in an integer | recursive function to count set bits ; base case ; get value from user ; function calling
< ? php function countSetBits ( $ n ) { if ( $ n == 0 ) return 0 ; else return 1 + countSetBits ( $ n & ( $ n - 1 ) ) ; } $ n = 9 ; echo countSetBits ( $ n ) ; ? >
Count set bits in an integer | Driver code
< ? php $ t = log10 ( 4 ) ; $ x = log ( 15 , 2 ) ; $ tt = ceil ( $ t ) ; $ xx = ceil ( $ x ) ; echo ( $ tt ) , " STRNEWLINE " ; echo ( $ xx ) , " STRNEWLINE " ; ? >
Count set bits in an integer | PHP program to count set bits by pre - storing count set bits in nibbles . ; Recursively get nibble of a given number and map them in the array ; Find last nibble ; Use pre - stored values to find count in last nibble plus recursively add remaining nibbles . ; Driver code
< ? php $ num_to_bits = array ( 0 , 1 , 1 , 2 , 1 , 2 , 2 , 3 , 1 , 2 , 2 , 3 , 2 , 3 , 3 , 4 ) ; function countSetBitsRec ( $ num ) { global $ num_to_bits ; $ nibble = 0 ; if ( 0 == $ num ) return $ num_to_bits [ 0 ] ; $ nibble = $ num & 0xf ; return $ num_to_bits [ $ nibble ] + countSetBitsRec ( $ num >> 4 ) ; } $ num = 31 ; echo ( countSetBitsRec ( $ num ) ) ; ? >
Program to find parity | Function to get parity of number n . It returns 1 if n has odd parity , and returns 0 if n has even parity ; Driver Code
< ? php function getParity ( $ n ) { $ parity = 0 ; while ( $ n ) { $ parity = ! $ parity ; $ n = $ n & ( $ n - 1 ) ; } return $ parity ; } $ n = 7 ; echo " Parity ▁ of ▁ no ▁ " , $ n , " ▁ = ▁ " , getParity ( $ n ) ? " odd " : " even " ; ? >
Program to find whether a no is power of two | PHP Program to find whether a no is power of two Function to check Log base 2 ; Function to check if x is power of 2 ; Driver Code
< ? php function Log2 ( $ x ) { return ( log10 ( $ x ) / log10 ( 2 ) ) ; } function isPowerOfTwo ( $ n ) { return ( ceil ( Log2 ( $ n ) ) == floor ( Log2 ( $ n ) ) ) ; } if ( isPowerOfTwo ( 31 ) ) echo " Yes STRNEWLINE " ; else echo " No STRNEWLINE " ; if ( isPowerOfTwo ( 64 ) ) echo " Yes STRNEWLINE " ; else echo " No STRNEWLINE " ; ? >
Program to find whether a no is power of two | Function to check if x is power of 2 ; Driver Code
< ? php function isPowerOfTwo ( $ n ) { if ( $ n == 0 ) return 0 ; while ( $ n != 1 ) { if ( $ n % 2 != 0 ) return 0 ; $ n = $ n / 2 ; } return 1 ; } if ( isPowerOfTwo ( 31 ) ) echo " Yes STRNEWLINE " ; else echo " No STRNEWLINE " ; if ( isPowerOfTwo ( 64 ) ) echo " Yes STRNEWLINE " ; else echo " No STRNEWLINE " ; ? >
Program to find whether a no is power of two | Function to check if x is power of 2 ; First x in the below expression is for the case when x is 0 ; Driver Code
< ? php function isPowerOfTwo ( $ x ) { return $ x && ( ! ( $ x & ( $ x - 1 ) ) ) ; } if ( isPowerOfTwo ( 31 ) ) echo " Yes STRNEWLINE " ; else echo " No STRNEWLINE " ; if ( isPowerOfTwo ( 64 ) ) echo " Yes STRNEWLINE " ; else echo " No STRNEWLINE " ; ? >
Find the maximum repeating number in O ( n ) time and O ( 1 ) extra space | Returns maximum repeating element in arr [ 0. . n - 1 ] . The array elements are in range from 0 to k - 1 ; Iterate though input array , for every element arr [ i ] , increment arr [ arr [ i ] % k ] by k ; Find index of the maximum repeating element ; Return index of the maximum element ; Driver Code
< ? php function maxRepeating ( $ arr , $ n , $ k ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ arr [ $ arr [ $ i ] % $ k ] += $ k ; $ max = $ arr [ 0 ] ; $ result = 0 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] > $ max ) { $ max = $ arr [ $ i ] ; $ result = $ i ; } } return $ result ; } $ arr = array ( 2 , 3 , 3 , 5 , 3 , 4 , 1 , 7 ) ; $ n = sizeof ( $ arr ) ; $ k = 8 ; echo " The ▁ maximum ▁ repeating ▁ number ▁ is ▁ " , maxRepeating ( $ arr , $ n , $ k ) ; ? >
Range Query on array whose each element is XOR of index value and previous element | function return derived formula value . ; finding xor value of range [ y ... x ] ; function to solve query for l and r . ; if l or r is 0. ; finding x is divisible by 2 or not . ; Driver Code
< ? php function fun ( $ x ) { $ y = ( ( int ) ( $ x / 4 ) * 4 ) ; $ ans = 0 ; for ( $ i = $ y ; $ i <= $ x ; $ i ++ ) $ ans ^= $ i ; return $ ans ; } function query ( $ x ) { if ( $ x == 0 ) return 0 ; $ k = ( int ) ( ( $ x + 1 ) / 2 ) ; return ( $ x %= 2 ) ? 2 * fun ( $ k ) : ( ( fun ( $ k - 1 ) * 2 ) ^ ( $ k & 1 ) ) ; } function allQueries ( $ q , $ l , $ r ) { for ( $ i = 0 ; $ i < $ q ; $ i ++ ) echo ( query ( $ r [ $ i ] ) ^ query ( $ l [ $ i ] - 1 ) ) , " STRNEWLINE " ; } $ q = 3 ; $ l = array ( 2 , 2 , 5 ) ; $ r = array ( 4 , 8 , 9 ) ; allQueries ( $ q , $ l , $ r ) ; ? >
Minimum adjacent swaps required to Sort Binary array | Function to find minimum swaps to sort an array of 0 s and 1 s . ; Array to store count of zeroes ; Count number of zeroes on right side of every one . ; Count total number of swaps by adding number of zeroes on right side of every one . ; Driver code
< ? php function findMinSwaps ( $ arr , $ n ) { $ noOfZeroes [ $ n ] = array ( ) ; $ noOfZeroes = array_fill ( 0 , $ n , true ) ; $ count = 0 ; $ noOfZeroes [ $ n - 1 ] = 1 - $ arr [ $ n - 1 ] ; for ( $ i = $ n - 2 ; $ i >= 0 ; $ i -- ) { $ noOfZeroes [ $ i ] = $ noOfZeroes [ $ i + 1 ] ; if ( $ arr [ $ i ] == 0 ) $ noOfZeroes [ $ i ] ++ ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] == 1 ) $ count += $ noOfZeroes [ $ i ] ; } return $ count ; } $ arr = array ( 0 , 0 , 1 , 0 , 1 , 0 , 1 , 1 ) ; $ n = sizeof ( $ arr ) ; echo findMinSwaps ( $ arr , $ n ) ; ? >
Find the two numbers with odd occurrences in an unsorted array | Prints two numbers that occur odd number of times . The function assumes that the array size is at least 2 and there are exactly two numbers occurring odd number of times . ; Will hold XOR of two odd occurring elements ; Will have only single set bit of xor2 ; Get the xor of all elements in arr [ ] . The xor will basically be xor of two odd occurring elements ; Get one set bit in the xor2 . We get rightmost set bit in the following line as it is easy to get ; Now divide elements in two sets : 1 ) The elements having the corresponding bit as 1. 2 ) The elements having the corresponding bit as 0. ; XOR of first set is finally going to hold one odd occurring number x ; XOR of second set is finally going to hold the other odd occurring number y ; Driver Code
< ? php function printTwoOdd ( $ arr , $ size ) { $ xor2 = $ arr [ 0 ] ; $ set_bit_no ; $ i ; $ n = $ size - 2 ; $ x = 0 ; $ y = 0 ; for ( $ i = 1 ; $ i < $ size ; $ i ++ ) $ xor2 = $ xor2 ^ $ arr [ $ i ] ; $ set_bit_no = $ xor2 & ~ ( $ xor2 - 1 ) ; for ( $ i = 0 ; $ i < $ size ; $ i ++ ) { if ( $ arr [ $ i ] & $ set_bit_no ) $ x = $ x ^ $ arr [ $ i ] ; else $ y = $ y ^ $ arr [ $ i ] ; } echo " The ▁ two ▁ ODD ▁ elements ▁ are ▁ " , $ x , " ▁ & ▁ " , $ y ; } $ arr = array ( 4 , 2 , 4 , 5 , 2 , 3 , 3 , 1 ) ; $ arr_size = sizeof ( $ arr ) ; printTwoOdd ( $ arr , $ arr_size ) ; ? >
Find a pair with the given difference | The function assumes that the array is sorted ; Initialize positions of two elements ; Search for a pair ; Driver Code
< ? php function findPair ( & $ arr , $ size , $ n ) { $ i = 0 ; $ j = 1 ; while ( $ i < $ size && $ j < $ size ) { if ( $ i != $ j && $ arr [ $ j ] - $ arr [ $ i ] == $ n ) { echo " Pair ▁ Found : ▁ " . " ( " . $ arr [ $ i ] . " , ▁ " . $ arr [ $ j ] . " ) " ; return true ; } else if ( $ arr [ $ j ] - $ arr [ $ i ] < $ n ) $ j ++ ; else $ i ++ ; } echo " No ▁ such ▁ pair " ; return false ; } $ arr = array ( 1 , 8 , 30 , 40 , 100 ) ; $ size = sizeof ( $ arr ) ; $ n = 60 ; findPair ( $ arr , $ size , $ n ) ; ? >
Check whether Arithmetic Progression can be formed from the given array | Returns true if a permutation of arr [ 0. . n - 1 ] can form arithmetic progression ; Sort array ; After sorting , difference between consecutive elements must be same . ; Driver Code
< ? php function checkIsAP ( $ arr , $ n ) { if ( $ n == 1 ) return true ; sort ( $ arr ) ; $ d = $ arr [ 1 ] - $ arr [ 0 ] ; for ( $ i = 2 ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] - $ arr [ $ i - 1 ] != $ d ) return false ; return true ; } $ arr = array ( 20 , 15 , 5 , 0 , 10 ) ; $ n = count ( $ arr ) ; if ( checkIsAP ( $ arr , $ n ) ) echo " Yes " ; else echo " No " ; ? >
Count ways of choosing a pair with maximum difference | PHP Code to find no . of Ways of choosing a pair with maximum difference ; To find minimum and maximum of the array ; to find the count of minimum and maximum elements ; Count variables ; condition for all elements equal ; Driver code
< ? php function countPairs ( $ a , $ n ) { $ mn = PHP_INT_MAX ; $ mx = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ mn = min ( $ mn , $ a [ $ i ] ) ; $ mx = max ( $ mx , $ a [ $ i ] ) ; } $ c1 = 0 ; $ c2 = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] == $ mn ) $ c1 ++ ; if ( $ a [ $ i ] == $ mx ) $ c2 ++ ; } if ( $ mn == $ mx ) return $ n * ( $ n - 1 ) / 2 ; else return $ c1 * $ c2 ; } $ a = array ( 3 , 2 , 1 , 1 , 3 ) ; $ n = count ( $ a ) ; echo countPairs ( $ a , $ n ) ; ? >