codeparrot/xlcost-text-to-code huggingface dataset:
Maximize the profit by selling at | Function to find profit ; Calculating profit for each gadget ; sort the profit array in descending order ; variable to calculate total profit ; check for best M profits ; Driver Code
< ? php function solve ( $ N , $ M , & $ cp , & $ sp ) { $ profit = array_fill ( 0 , $ N , NULL ) ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) $ profit [ $ i ] = $ sp [ $ i ] - $ cp [ $ i ] ; rsort ( $ profit ) ; $ sum = 0 ; for ( $ i = 0 ; $ i < $ M ; $ i ++ ) { if ( $ profit [ $ i ] > 0 ) $ sum += $ profit [ $ i ] ; else break ; } return $ sum ; } $ N = 5 ; $ M = 3 ; $ CP = array ( 5 , 10 , 35 , 7 , 23 ) ; $ SP = array ( 11 , 10 , 0 , 9 , 19 ) ; echo solve ( $ N , $ M , $ CP , $ SP ) ; ? >
Find the largest number that can be formed with the given digits | Function to generate largest possible number with given digits ; sort the given array in descending order ; generate the number ; Driver code
< ? php function findMaxNum ( & $ arr , $ n ) { rsort ( $ arr ) ; $ num = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ num = $ num * 10 + $ arr [ $ i ] ; } return $ num ; } $ arr = array ( 1 , 2 , 3 , 4 , 5 , 0 ) ; $ n = sizeof ( $ arr ) ; echo findMaxNum ( $ arr , $ n ) ; ? >
Minimum partitions of maximum size 2 and sum limited by given value | PHP program to count minimum number of partitions of size 2 and sum smaller than or equal to given key . ; sort the array ; if sum of ith smaller and jth larger element is less than key , then pack both numbers in a set otherwise pack the jth larger number alone in the set ; After ending of loop i will contain minimum number of sets ; Driver Code
< ? php function minimumSets ( $ arr , $ n , $ key ) { $ i ; $ j ; sort ( $ arr ) ; for ( $ i = 0 , $ j = $ n - 1 ; $ i <= $ j ; ++ $ i ) if ( $ arr [ $ i ] + $ arr [ $ j ] <= $ key ) $ j -- ; return $ i ; } $ arr = array ( 3 , 5 , 3 , 4 ) ; $ n = count ( $ arr ) ; $ key = 5 ; echo minimumSets ( $ arr , $ n , $ key ) ; ? >
Number of visible boxes after putting one inside another | return the minimum number of visible boxes ; New Queue of integers . ; sorting the array ; traversing the array ; checking if current element is greater than or equal to twice of front element ; Pushing each element of array ; Driver Code
< ? php function minimumBox ( $ arr , $ n ) { $ q = array ( ) ; sort ( $ arr ) ; array_push ( $ q , $ arr [ 0 ] ) ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ now = $ q [ 0 ] ; if ( $ arr [ $ i ] >= 2 * $ now ) array_pop ( $ q ) ; array_push ( $ q , $ arr [ $ i ] ) ; } return count ( $ q ) ; } $ arr = array ( 4 , 1 , 2 , 8 ) ; $ n = count ( $ arr ) ; echo minimumBox ( $ arr , $ n ) ; ? >
Sort a binary array using one traversal | PHP Code for Sort a binary array using one traversal ; if number is smaller than 1 then swap it with j - th number ; Driver Code
< ? php function sortBinaryArray ( $ a , $ n ) { $ j = -1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] < 1 ) { $ j ++ ; $ temp = $ a [ $ j ] ; $ a [ $ j ] = $ a [ $ i ] ; $ a [ $ i ] = $ temp ; } } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ a [ $ i ] . " ▁ " ; } $ a = array ( 1 , 0 , 0 , 1 , 0 , 1 , 0 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , 1 , 0 , 0 ) ; $ n = count ( $ a ) ; sortBinaryArray ( $ a , $ n ) ; ? >
Smallest element in an array that is repeated exactly ' k ' times . | finds the smallest number in arr [ ] that is repeated k times ; Since arr [ ] has numbers in range from 1 to MAX ; set count to 1 as number is present once ; If frequency of number is equal to ' k ' ; Driver code
< ? php function findDuplicate ( $ arr , $ n , $ k ) { $ MAX = 1000 ; $ res = $ MAX + 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] > 0 ) { $ count = 1 ; for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) if ( $ arr [ $ i ] == $ arr [ $ j ] ) $ count += 1 ; if ( $ count == $ k ) $ res = min ( $ res , $ arr [ $ i ] ) ; } } return $ res ; } $ arr = array ( 2 , 2 , 1 , 3 , 1 ) ; $ k = 2 ; $ n = count ( $ arr ) ; echo findDuplicate ( $ arr , $ n , $ k ) ; ? >
Smallest element in an array that is repeated exactly ' k ' times . | finds the smallest number in arr [ ] that is repeated k times ; Sort the array ; Find the first element with exactly k occurrences . ; Driver code
< ? php function findDuplicate ( $ arr , $ n , $ k ) { sort ( $ arr ) ; $ i = 0 ; while ( $ i < $ n ) { $ j ; $ count = 1 ; for ( $ j = $ i + 1 ; $ j < $ n && $ arr [ $ j ] == $ arr [ $ i ] ; $ j ++ ) $ count ++ ; if ( $ count == $ k ) return $ arr [ $ i ] ; $ i = $ j ; } return -1 ; } $ arr = array ( 2 , 2 , 1 , 3 , 1 ) ; $ k = 2 ; $ n = sizeof ( $ arr ) ; echo ( findDuplicate ( $ arr , $ n , $ k ) ) ; ? >
Check if it is possible to sort an array with conditional swapping of adjacent allowed | Returns true if it is possible to sort else false ; We need to do something only if previousl element is greater ; If difference is more than one , then not possible ; Driver Code
< ? php function checkForSorting ( $ arr , $ n ) { $ temp = 0 ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { if ( $ arr [ $ i ] > $ arr [ $ i + 1 ] ) { if ( $ arr [ $ i ] - $ arr [ $ i + 1 ] == 1 ) { $ temp = $ arr [ $ i ] ; $ arr [ $ i ] = $ arr [ $ i + 1 ] ; $ arr [ $ i + 1 ] = $ temp ; } else return false ; } } return true ; } $ arr = array ( 1 , 0 , 3 , 2 ) ; $ n = sizeof ( $ arr ) ; if ( checkForSorting ( $ arr , $ n ) ) echo " Yes " ; else echo " No " ; ? >
K | Return the K - th smallest element . ; sort ( arr , arr + n ) ; ; Checking if k lies before 1 st element ; If k is the first element of array arr [ ] . ; If k is more than last element ; If first element of array is 1. ; Reducing k by numbers before arr [ 0 ] . ; Finding k 'th smallest element  after removing array elements. ; Finding count of element between i - th and ( i - 1 ) - th element . ; Driver Code
< ? php function ksmallest ( $ arr , $ n , $ k ) { sort ( $ arr ) ; if ( $ k < $ arr [ 0 ] ) return $ k ; if ( $ k == $ arr [ 0 ] ) return $ arr [ 0 ] + 1 ; if ( $ k > $ arr [ $ n - 1 ] ) return $ k + $ n ; if ( $ arr [ 0 ] == 1 ) $ k -- ; else $ k -= ( $ arr [ 0 ] - 1 ) ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ c = $ arr [ $ i ] - $ arr [ $ i - 1 ] - 1 ; if ( $ k <= $ c ) return $ arr [ $ i - 1 ] + $ k ; else $ k -= $ c ; } return $ arr [ $ n - 1 ] + $ k ; } $ k = 1 ; $ arr = array ( 1 ) ; $ n = sizeof ( $ arr ) ; echo ksmallest ( $ arr , $ n , $ k ) ; ? >
Check whether a given number is even or odd | Returns true if n is even , else odd ; n & 1 is 1 , then odd , else even ; Driver code
< ? php function isEven ( $ n ) { return ( ! ( n & 1 ) ) ; } $ n = 101 ; if ( isEven ( $ n ) == true ) echo " Even " ; else echo " Odd " ; ? >
Count distinct occurrences as a subsequence | PHP program to count number of times S appears as a subsequence in T ; T can 't appear as a subsequence in S ; mat [ i ] [ j ] stores the count of occurrences of T ( 1. . i ) in S ( 1. . j ) . ; Initializing first column with all 0 s . An empty string can 't have another  string as suhsequence ; Initializing first row with all 1 s . An empty string is subsequence of all . ; Fill mat [ ] [ ] in bottom up manner ; If last characters don 't match,  then value is same as the value  without last character in S. ; Else value is obtained considering two cases . a ) All substrings without last character in S b ) All substrings without last characters in both . ; uncomment this to print matrix mat for ( int i = 1 ; i <= m ; i ++ , cout << endl ) for ( int j = 1 ; j <= n ; j ++ ) cout << mat [ i ] [ j ] << " ▁ " ; ; Driver Code
< ? php function findSubsequenceCount ( $ S , $ T ) { $ m = strlen ( $ T ) ; $ n = strlen ( $ S ) ; if ( $ m > $ n ) return 0 ; $ mat = array ( array ( ) ) ; for ( $ i = 1 ; $ i <= $ m ; $ i ++ ) $ mat [ $ i ] [ 0 ] = 0 ; for ( $ j = 0 ; $ j <= $ n ; $ j ++ ) $ mat [ 0 ] [ $ j ] = 1 ; for ( $ i = 1 ; $ i <= $ m ; $ i ++ ) { for ( $ j = 1 ; $ j <= $ n ; $ j ++ ) { if ( $ T [ $ i - 1 ] != $ S [ $ j - 1 ] ) $ mat [ $ i ] [ $ j ] = $ mat [ $ i ] [ $ j - 1 ] ; else $ mat [ $ i ] [ $ j ] = $ mat [ $ i ] [ $ j - 1 ] + $ mat [ $ i - 1 ] [ $ j - 1 ] ; } } return $ mat [ $ m ] [ $ n ] ; } $ T = " ge " ; $ S = " geeksforgeeks " ; echo findSubsequenceCount ( $ S , $ T ) . " " ;
Check if it is possible to make two matrices strictly increasing by swapping corresponding values only | Function to check whether the matrices can be made strictly increasing with the given operation ; Swap only when a [ i ] [ j ] > b [ i ] [ j ] ; Check if rows are strictly increasing ; Check if columns are strictly increasing ; Driver code
< ? php function Check ( $ a , $ b , $ n , $ m ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( $ a [ $ i ] [ $ j ] > $ b [ $ i ] [ $ j ] ) { $ temp = $ a [ $ i ] [ $ j ] ; $ a [ $ i ] [ $ j ] = $ b [ $ i ] [ $ j ] ; $ b [ $ i ] [ $ j ] = $ temp ; } } } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ m - 1 ; $ j ++ ) { if ( $ a [ $ i ] [ $ j ] >= $ a [ $ i ] [ $ j + 1 ] or $ b [ $ i ] [ $ j ] >= $ b [ $ i ] [ $ j + 1 ] ) return " No " ; } } for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { for ( $ j = 0 ; $ j < $ m ; $ j ++ ) { if ( $ a [ $ i ] [ $ j ] >= $ a [ $ i + 1 ] [ $ j ] or $ b [ $ i ] [ $ j ] >= $ b [ $ i + 1 ] [ $ j ] ) return " No " ; } } return " Yes " ; } $ n = 2 ; $ m = 2 ; $ a = array ( array ( 2 , 10 ) , array ( 11 , 5 ) ) ; $ b = array ( array ( 9 , 4 ) , array ( 3 , 12 ) ) ; print ( Check ( $ a , $ b , $ n , $ m ) ) ; ? >
Find the lexicographically smallest string which satisfies the given condition | Function to return the required string ; First character will always be ' a ' ; To store the resultant string ; Since length of the string should be greater than 0 and first element of array should be 1 ; Check one by one all element of given prefix array ; If the difference between any two consecutive elements of the prefix array is greater than 1 then there will be no such string possible that satisfies the given array Also , string cannot have more than 26 distinct characters ; If difference is 0 then the ( i + 1 ) th character will be same as the ith character ; If difference is 1 then the ( i + 1 ) th character will be different from the ith character ; Return the resultant string ; Driver code
< ? php function smallestString ( $ N , $ A ) { $ ch = ' a ' ; $ S = " " ; if ( $ N < 1 $ A [ 0 ] != 1 ) { $ S = " - 1" ; return $ S ; } $ S . = $ ch ; $ ch ++ ; for ( $ i = 1 ; $ i < $ N ; $ i ++ ) { $ diff = $ A [ $ i ] - $ A [ $ i - 1 ] ; if ( $ diff > 1 $ diff < 0 $ A [ $ i ] > 26 ) { $ S = " - 1" ; return $ S ; } else if ( $ diff == 0 ) $ S . = ' a ' ; else { $ S . = $ ch ; $ ch ++ ; } } return $ S ; } $ arr = array ( 1 , 1 , 2 , 3 , 3 ) ; $ n = sizeof ( $ arr ) ; echo ( smallestString ( $ n , $ arr ) ) ; ? >
Count of alphabets whose ASCII values can be formed with the digits of N | Function that returns true if num can be formed with the digits in digits [ ] array ; Copy of the digits array ; Get last digit ; If digit array doesn 't contain  current digit ; One occurrence is used ; Remove the last digit ; Function to return the count of required alphabets ; To store the occurrences of digits ( 0 - 9 ) ; Get last digit ; Update the occurrence of the digit ; Remove the last digit ; If any lowercase character can be picked from the current digits ; If any uppercase character can be picked from the current digits ; Return the required count of alphabets ; Driver code
< ? php function canBePicked ( $ digits , $ num ) { $ copyDigits = array ( ) ; for ( $ i = 0 ; $ i < sizeof ( $ digits ) ; $ i ++ ) $ copyDigits [ $ i ] = $ digits [ $ i ] ; while ( $ num > 0 ) { $ digit = $ num % 10 ; if ( $ copyDigits [ $ digit ] == 0 ) return false ; else $ copyDigits [ $ digit ] -- ; $ num = floor ( $ num / 10 ) ; } return true ; } function countAlphabets ( $ n ) { $ count = 0 ; $ digits = array_fill ( 0 , 10 , 0 ) ; while ( $ n > 0 ) { $ digit = $ n % 10 ; $ digits [ $ digit ] ++ ; $ n = floor ( $ n / 10 ) ; } for ( $ i = ord ( ' a ' ) ; $ i <= ord ( ' z ' ) ; $ i ++ ) if ( canBePicked ( $ digits , $ i ) ) $ count ++ ; for ( $ i = ord ( ' A ' ) ; $ i <= ord ( ' Z ' ) ; $ i ++ ) if ( canBePicked ( $ digits , $ i ) ) $ count ++ ; return $ count ; } $ n = 1623455078 ; echo countAlphabets ( $ n ) ; ? >
Find two numbers whose sum and GCD are given | Function to find gcd of two numbers ; Function to find two numbers whose sum and gcd is given ; sum != gcd checks that both the numbers are positive or not ; Driver code
< ? php function __gcd ( $ a , $ b ) { if ( $ b == 0 ) return $ a ; return __gcd ( $ b , $ a % $ b ) ; } function findTwoNumbers ( $ sum , $ gcd ) { if ( __gcd ( $ gcd , $ sum - $ gcd ) == $ gcd && $ sum != $ gcd ) echo " a = " ▁ , ▁ min ( $ gcd , ▁ $ sum ▁ - ▁ $ gcd ) , STRNEWLINE TABSYMBOL TABSYMBOL TABSYMBOL " b = " else echo ( -1 ) ; } $ sum = 8 ; $ gcd = 2 ; findTwoNumbers ( $ sum , $ gcd ) ; ? >
Find maximum distance between any city and station | Function to calculate the maximum distance between any city and its nearest station ; Initialize boolean list ; Assign True to cities containing station ; Driver code
< ? php function findMaxDistance ( $ numOfCities , $ station , $ n ) { $ hasStation = array_fill ( 0 , $ numOfCities + 1 , false ) ; for ( $ city = 0 ; $ city < $ n ; $ city ++ ) { $ hasStation [ $ station [ $ city ] ] = true ; } $ dist = 0 ; $ maxDist = PHP_INT_MAX ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ maxDist = min ( $ station [ $ i ] , $ maxDist ) ; } for ( $ city = 0 ; $ city < $ numOfCities ; $ city ++ ) { if ( $ hasStation [ $ city ] == true ) { $ maxDist = max ( ( int ) ( ( $ dist + 1 ) / 2 ) , $ maxDist ) ; $ dist = 0 ; } else $ dist += 1 ; } return max ( $ maxDist , $ dist ) ; } $ numOfCities = 6 ; $ station = array ( 3 , 1 ) ; $ n = count ( $ station ) ; echo " Max ▁ Distance : ▁ " . findMaxDistance ( $ numOfCities , $ station , $ 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
< ? php function split ( $ x , $ n ) { if ( $ x < $ n ) echo ( -1 ) ; else if ( $ x % $ n == 0 ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { echo ( $ x / $ n ) ; echo ( " ▁ " ) ; } } else { $ zp = $ n - ( $ x % $ n ) ; $ pp = $ x / $ n ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ i >= $ zp ) { echo ( int ) $ pp + 1 ; echo ( " ▁ " ) ; } else { echo ( int ) $ pp ; echo ( " ▁ " ) ; } } } } $ x = 5 ; $ n = 3 ; split ( $ x , $ n ) ; ? >
Minimum time to reach a point with + t and | returns the minimum time required to reach ' X ' ; Stores the minimum time ; increment ' t ' by 1 ; update the sum ; Driver code
< ? php function cal_minimum_time ( $ X ) { $ t = 0 ; $ sum = 0 ; while ( $ sum < $ X ) { $ t ++ ; $ sum = $ sum + $ t ; } return $ t ; } $ n = 6 ; $ ans = cal_minimum_time ( $ n ) ; echo " The ▁ minimum ▁ time ▁ required ▁ is ▁ : ▁ " . $ ans ; ? >
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
< ? 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 ) ) ; ? >
Find the Largest Cube formed by Deleting minimum Digits from a number | Returns vector of Pre Processed perfect cubes ; convert the cube to string and push into preProcessedCubes vector ; Utility function for findLargestCube ( ) . Returns the Largest cube number that can be formed ; reverse the preProcessed cubes so that we have the largest cube in the beginning of the vector ; iterate over all cubes ; check if the current digit of the cube matches with that of the number num ; if control reaches here , the its not possible to form a perfect cube ; wrapper for findLargestCubeUtil ( ) ; pre process perfect cubes ; convert number n to String ; Driver Code
< ? php function preProcess ( $ n ) { $ preProcessedCubes = array ( ) ; for ( $ i = 1 ; $ i * $ i * $ i < $ n ; $ i ++ ) { $ iThCube = $ i * $ i * $ i ; $ cubeString = strval ( $ iThCube ) ; array_push ( $ preProcessedCubes , $ cubeString ) ; } return $ preProcessedCubes ; } function findLargestCubeUtil ( $ num , $ preProcessedCubes ) { $ preProcessedCubes = array_reverse ( $ preProcessedCubes ) ; $ totalCubes = count ( $ preProcessedCubes ) ; for ( $ i = 0 ; $ i < $ totalCubes ; $ i ++ ) { $ currCube = $ preProcessedCubes [ $ i ] ; $ digitsInCube = strlen ( $ currCube ) ; $ index = 0 ; $ digitsInNumber = strlen ( $ num ) ; for ( $ j = 0 ; $ j < $ digitsInNumber ; $ j ++ ) { if ( $ num [ $ j ] == $ currCube [ $ index ] ) $ index += 1 ; if ( $ digitsInCube == $ index ) return $ currCube ; } } return " Not ▁ Possible " ; } function findLargestCube ( $ n ) { $ preProcessedCubes = preProcess ( $ n ) ; $ num = strval ( $ n ) ; $ ans = findLargestCubeUtil ( $ num , $ preProcessedCubes ) ; print ( " Largest ▁ Cube ▁ that ▁ can ▁ be ▁ formed ▁ from ▁ " . $ n . " ▁ is ▁ " . $ ans . " STRNEWLINE " ) ; } $ n = 4125 ; findLargestCube ( $ n ) ; $ n = 876 ; findLargestCube ( $ n ) ? >
Print a closest string that does not contain adjacent duplicates | Function to print simple string ; If any two adjacent characters are equal ; Initialize it to ' a ' ; Traverse the loop until it is different from the left and right letter . ; Driver Code
< ? php function noAdjacentDup ( $ s ) { $ n = strlen ( $ s ) ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { if ( $ s [ $ i ] == $ s [ $ i - 1 ] ) { $ s [ $ i ] = ' a ' ; while ( $ s [ $ i ] == $ s [ $ i - 1 ] || ( $ i + 1 < $ n && $ s [ $ i ] == $ s [ $ i + 1 ] ) ) $ s [ $ i ] ++ ; $ i ++ ; } } return $ s ; } $ s = " geeksforgeeks " ; echo ( noAdjacentDup ( $ s ) ) ; ? >
Array element moved by k using single moves | PHP program to find winner of game ; if the number of steps is more then n - 1 , ; initially the best is 0 and no . of wins is 0. ; traverse through all the numbers ; if the value of array is more then that of previous best ; best is replaced by a [ i ] ; if not the first index ; no of wins is 1 now ; if it wins ; if any position has more then k wins then return ; Maximum element will be winner because we move smaller element at end and repeat the process . ; Driver Code
< ? php function winner ( $ a , $ n , $ k ) { if ( $ k >= $ n - 1 ) return $ n ; $ best = 0 ; $ times = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] > $ best ) { $ best = $ a [ $ i ] ; if ( $ i ) $ times = 1 ; } else $ times += 1 ; if ( $ times >= $ k ) return $ best ; } return $ best ; } $ a = array ( 2 , 1 , 3 , 4 , 5 ) ; $ n = sizeof ( $ a ) ; $ k = 2 ; echo ( winner ( $ a , $ n , $ k ) ) ; ? >
Queries for bitwise OR in the index range [ L , R ] of the given array | PHP implementation of the approach ; Array to store bit - wise prefix count ; Function to find the prefix sum ; Loop for each bit ; Loop to find prefix count ; Function to answer query ; To store the answer ; Loop for each bit ; To store the number of variables with ith bit set ; Condition for ith bit of answer to be set ; Driver code
< ? php $ MAX = 100000 ; $ bitscount = 32 ; $ prefix_count = array_fill ( 0 , $ bitscount , array_fill ( 0 , $ MAX , NULL ) ) ; function findPrefixCount ( & $ arr , $ n ) { global $ MAX , $ bitscount , $ prefix_count ; for ( $ i = 0 ; $ i < $ bitscount ; $ i ++ ) { $ prefix_count [ $ i ] [ 0 ] = ( ( $ arr [ 0 ] >> $ i ) & 1 ) ; for ( $ j = 1 ; $ j < $ n ; $ j ++ ) { $ prefix_count [ $ i ] [ $ j ] = ( ( $ arr [ $ j ] >> $ i ) & 1 ) ; $ prefix_count [ $ i ] [ $ j ] += $ prefix_count [ $ i ] [ $ j - 1 ] ; } } } function rangeOr ( $ l , $ r ) { global $ MAX , $ bitscount , $ prefix_count ; $ ans = 0 ; for ( $ i = 0 ; $ i < $ bitscount ; $ i ++ ) { if ( $ l == 0 ) $ x = $ prefix_count [ $ i ] [ $ r ] ; else $ x = $ prefix_count [ $ i ] [ $ r ] - $ prefix_count [ $ i ] [ l - 1 ] ; if ( $ x != 0 ) $ ans = ( $ ans | ( 1 << $ i ) ) ; } return $ ans ; } $ arr = array ( 7 , 5 , 3 , 5 , 2 , 3 ) ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; findPrefixCount ( $ arr , $ n ) ; $ queries = array ( array ( 1 , 3 ) , array ( 4 , 5 ) ) ; $ q = sizeof ( $ queries ) / sizeof ( $ queries [ 0 ] ) ; for ( $ i = 0 ; $ i < $ q ; $ i ++ ) echo rangeOr ( $ queries [ $ i ] [ 0 ] , $ queries [ $ i ] [ 1 ] ) . " STRNEWLINE " ; return 0 ; ? >
Bellman Ford Algorithm ( Simple Implementation ) | The main function that finds shortest distances from src to all other vertices using Bellman - Ford algorithm . The function also detects negative weight cycle The row graph [ i ] represents i - th edge with three values u , v and w . ; Initialize distance of all vertices as infinite . ; initialize distance of source as 0 ; Relax all edges | V | - 1 times . A simple shortest path from src to any other vertex can have at - most | V | - 1 edges ; check for negative - weight cycles . The above step guarantees shortest distances if graph doesn 't contain  negative weight cycle. If we get a  shorter path, then there is a cycle. ; Every edge has three values ( u , v , w ) where the edge is from vertex u to v . And weight of the edge is w .
< ? php function BellmanFord ( $ graph , $ V , $ E , $ src ) { $ dis = array ( ) ; for ( $ i = 0 ; $ i < $ V ; $ i ++ ) $ dis [ $ i ] = PHP_INT_MAX ; $ dis [ $ src ] = 0 ; for ( $ i = 0 ; $ i < $ V - 1 ; $ i ++ ) { for ( $ j = 0 ; $ j < $ E ; $ j ++ ) { if ( $ dis [ $ graph [ $ j ] [ 0 ] ] != PHP_INT_MAX && $ dis [ $ graph [ $ j ] [ 0 ] ] + $ graph [ $ j ] [ 2 ] < $ dis [ $ graph [ $ j ] [ 1 ] ] ) $ dis [ $ graph [ $ j ] [ 1 ] ] = $ dis [ $ graph [ $ j ] [ 0 ] ] + $ graph [ $ j ] [ 2 ] ; } } for ( $ i = 0 ; $ i < $ E ; $ i ++ ) { $ x = $ graph [ $ i ] [ 0 ] ; $ y = $ graph [ $ i ] [ 1 ] ; $ weight = $ graph [ $ i ] [ 2 ] ; if ( $ dis [ $ x ] != PHP_INT_MAX && $ dis [ $ x ] + $ weight < $ dis [ $ y ] ) echo " Graph ▁ contains ▁ negative ▁ weight ▁ cycle ▁ STRNEWLINE " ; } echo " Vertex ▁ Distance ▁ from ▁ Source ▁ STRNEWLINE " ; for ( $ i = 0 ; $ i < $ V ; $ i ++ ) echo $ i , " TABSYMBOL TABSYMBOL " , $ dis [ $ i ] , " STRNEWLINE " ; } $ graph = array ( array ( 0 , 1 , -1 ) , array ( 0 , 2 , 4 ) , array ( 1 , 2 , 3 ) , array ( 1 , 3 , 2 ) , array ( 1 , 4 , 2 ) , array ( 3 , 2 , 5 ) , array ( 3 , 1 , 1 ) , array ( 4 , 3 , -3 ) ) ; BellmanFord ( $ graph , $ V , $ E , 0 ) ; ? >