codeparrot/xlcost-text-to-code huggingface dataset:
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
< ? 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 STRNEWLINE " ) ; 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 ) ; ? >
Find three element from different three arrays such that a + b + c = sum | Function to check if there is an element from each array such that sum of the three elements is equal to given sum . ; Driver Code
< ? php function findTriplet ( $ a1 , $ a2 , $ a3 , $ n1 , $ n2 , $ n3 , $ sum ) { for ( $ i = 0 ; $ i < $ n1 ; $ i ++ ) for ( $ j = 0 ; $ j < $ n2 ; $ j ++ ) for ( $ k = 0 ; $ k < $ n3 ; $ k ++ ) if ( $ a1 [ $ i ] + $ a2 [ $ j ] + $ a3 [ $ k ] == $ sum ) return true ; return false ; } $ a1 = array ( 1 , 2 , 3 , 4 , 5 ) ; $ a2 = array ( 2 , 3 , 6 , 1 , 2 ) ; $ a3 = array ( 3 , 2 , 4 , 5 , 6 ) ; $ sum = 9 ; $ n1 = count ( $ a1 ) ; $ n2 = count ( $ a2 ) ; $ n3 = count ( $ a3 ) ; if ( findTriplet ( $ a1 , $ a2 , $ a3 , $ n1 , $ n2 , $ n3 , $ sum ) == true ) echo " Yes " ; else echo " No " ; ? >
Minimum insertions to form a palindrome with permutations allowed | Function will return number of characters to be added ; To store string length ; To store number of characters occurring odd number of times ; To store count of each character ; To store occurrence of each character ; To count characters with odd occurrence ; As one character can be odd return res - 1 but if string is already palindrome return 0 ; Driver program
< ? php function minInsertion ( $ str ) { $ n = strlen ( $ str ) ; $ res = 0 ; $ count = array ( 26 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ count [ ord ( $ str [ $ i ] ) - ord ( ' a ' ) ] ++ ; for ( $ i = 0 ; $ i < 26 ; $ i ++ ) { if ( $ count [ $ i ] % 2 == 1 ) $ res ++ ; } return ( $ res == 0 ) ? 0 : $ res - 1 ; } $ str = " geeksforgeeks " ; echo ( minInsertion ( $ str ) ) ; ? >
Difference between highest and least frequencies in an array | PHP code to find the difference between highest and least frequencies function that returns difference ; sort the array ; checking consecutive elements ; Driver Code
< ? php function findDiff ( $ arr , $ n ) { sort ( $ arr ) ; $ count = 0 ; $ max_count = 0 ; $ min_count = $ n ; for ( $ i = 0 ; $ i < ( $ n - 1 ) ; $ i ++ ) { if ( $ arr [ $ i ] == $ arr [ $ i + 1 ] ) { $ count += 1 ; continue ; } else { $ max_count = max ( $ max_count , $ count ) ; $ min_count = min ( $ min_count , $ count ) ; $ count = 0 ; } } return ( $ max_count - $ min_count ) ; } $ arr = array ( 7 , 8 , 4 , 5 , 4 , 1 , 1 , 7 , 7 , 2 , 5 ) ; $ n = sizeof ( $ arr ) ; echo ( findDiff ( $ arr , $ n ) . " " ) ; ? >
Maximum possible difference of two subsets of an array | function for maximum subset diff ; if frequency of any element is two make both equal to zero ; Driver Code
< ? php function maxDiff ( $ arr , $ n ) { $ SubsetSum_1 = 0 ; $ SubsetSum_2 = 0 ; for ( $ i = 0 ; $ i <= $ n - 1 ; $ i ++ ) { $ isSingleOccurance = true ; for ( $ j = $ i + 1 ; $ j <= $ n - 1 ; $ j ++ ) { if ( $ arr [ $ i ] == $ arr [ $ j ] ) { $ isSingleOccurance = false ; $ arr [ $ i ] = $ arr [ $ j ] = 0 ; break ; } } if ( $ isSingleOccurance ) { if ( $ arr [ $ i ] > 0 ) $ SubsetSum_1 += $ arr [ $ i ] ; else $ SubsetSum_2 += $ arr [ $ i ] ; } } return abs ( $ SubsetSum_1 - $ SubsetSum_2 ) ; } $ arr = array ( 4 , 2 , -3 , 3 , -2 , -2 , 8 ) ; $ n = sizeof ( $ arr ) ; echo " Maximum ▁ Difference ▁ = ▁ " , maxDiff ( $ arr , $ n ) ; ? >
Maximum possible difference of two subsets of an array | function for maximum subset diff ; sort the array ; calculate the result ; check for last element ; return result ; Driver Code
< ? php function maxDiff ( $ arr , $ n ) { $ result = 0 ; sort ( $ arr ) ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { if ( $ arr [ $ i ] != $ arr [ $ i + 1 ] ) $ result += abs ( $ arr [ $ i ] ) ; else $ i ++ ; } if ( $ arr [ $ n - 2 ] != $ arr [ $ n - 1 ] ) $ result += abs ( $ arr [ $ n - 1 ] ) ; return $ result ; } $ arr = array ( 4 , 2 , -3 , 3 , -2 , -2 , 8 ) ; $ n = count ( $ arr ) ; echo " Maximum ▁ Difference ▁ = ▁ " , maxDiff ( $ arr , $ n ) ; ? >
Print all triplets in sorted array that form AP | Function to print all triplets in given sorted array that forms AP ; Use hash to find if there is a previous element with difference equal to arr [ j ] - arr [ i ] ; Driver code
< ? php function printAllAPTriplets ( $ arr , $ n ) { $ s = array ( ) ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) { for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) { $ diff = $ arr [ $ j ] - $ arr [ $ i ] ; if ( in_array ( $ arr [ $ i ] - $ diff , $ arr ) ) echo ( ( $ arr [ $ i ] - $ diff ) . " " ▁ . ▁ $ arr [ $ i ] ▁ . ▁ " " ▁ . ▁ $ arr [ $ j ] ▁ . ▁ " " } array_push ( $ s , $ arr [ $ i ] ) ; } } $ arr = array ( 2 , 6 , 9 , 12 , 17 , 22 , 31 , 32 , 35 , 42 ) ; $ n = count ( $ arr ) ; printAllAPTriplets ( $ arr , $ n ) ; ? >
Print all triplets in sorted array that form AP | Function to print all triplets in given sorted array that forms AP ; Search other two elements of AP with arr [ i ] as middle . ; if a triplet is found ; Since elements are distinct , arr [ k ] and arr [ j ] cannot form any more triplets with arr [ i ] ; If middle element is more move to higher side , else move lower side . ; Driver code
< ? php function findAllTriplets ( $ arr , $ n ) { for ( $ i = 1 ; $ i < $ n - 1 ; $ i ++ ) { for ( $ j = $ i - 1 , $ k = $ i + 1 ; $ j >= 0 && $ k < $ n { if ( $ arr [ $ j ] + $ arr [ $ k ] == 2 * $ arr [ $ i ] ) { echo $ arr [ $ j ] . " ▁ " . $ arr [ $ i ] . " ▁ " . $ arr [ $ k ] . " STRNEWLINE " ; $ k ++ ; $ j -- ; } else if ( $ arr [ $ j ] + $ arr [ $ k ] < 2 * $ arr [ $ i ] ) $ k ++ ; else $ j -- ; } } } $ arr = array ( 2 , 6 , 9 , 12 , 17 , 22 , 31 , 32 , 35 , 42 ) ; $ n = count ( $ arr ) ; findAllTriplets ( $ arr , $ n ) ; ? >
Count number of triplets with product equal to given number | Function to count such triplets ; Consider all triplets and count if their product is equal to m ; Driver code
< ? php function countTriplets ( $ arr , $ n , $ m ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ n - 2 ; $ i ++ ) for ( $ j = $ i + 1 ; $ j < $ n - 1 ; $ j ++ ) for ( $ k = $ j + 1 ; $ k < $ n ; $ k ++ ) if ( $ arr [ $ i ] * $ arr [ $ j ] * $ arr [ $ k ] == $ m ) $ count ++ ; return $ count ; } $ arr = array ( 1 , 4 , 6 , 2 , 3 , 8 ) ; $ n = sizeof ( $ arr ) ; $ m = 24 ; echo countTriplets ( $ arr , $ n , $ m ) ; ? >
Count of index pairs with equal elements in an array | Return the number of pairs with equal values . ; for each index i and j ; finding the index with same value but different index . ; Driven Code
< ? php function countPairs ( $ arr , $ n ) { $ ans = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) if ( $ arr [ $ i ] == $ arr [ $ j ] ) $ ans ++ ; return $ ans ; } $ arr = array ( 1 , 1 , 2 ) ; $ n = count ( $ arr ) ; echo countPairs ( $ arr , $ n ) ; ? >
Queries to answer the number of ones and zero to the left of given index | Function to pre - calculate the left [ ] array ; Iterate in the binary array ; Initialize the number of 1 and 0 ; Increase the count ; Driver code ; Queries ; Solve queries
< ? php function preCalculate ( $ binary , $ n ) { $ left = array ( ) ; $ count1 = 0 ; $ count0 = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ left [ $ i ] = array ( $ count1 , $ count0 ) ; if ( $ binary [ $ i ] ) $ count1 ++ ; else $ count0 ++ ; } return $ left ; } $ binary = array ( 1 , 1 , 1 , 0 , 0 , 1 , 0 , 1 , 1 ) ; $ n = count ( $ binary ) ; $ left = preCalculate ( $ binary , $ n ) ; $ queries = array ( 0 , 1 , 2 , 4 ) ; $ q = count ( $ queries ) ; for ( $ i = 0 ; $ i < $ q ; $ i ++ ) echo $ left [ $ queries [ $ i ] ] [ 0 ] , " ▁ ones ▁ " , $ left [ $ queries [ $ i ] ] [ 1 ] , " ▁ zeros STRNEWLINE " ; ? >
Partition the array into three equal sum segments | First segment 's end index ; Third segment 's start index ; This function returns true if the array can be divided into three equal sum segments ; Prefix Sum Array ; Suffix Sum Array ; Stores the total sum of the array ; We can also take pre [ pos2 - 1 ] - pre [ pos1 ] == total_sum / 3 here . ; Driver Code
< ? php $ pos1 = -1 ; $ pos2 = -1 ; function equiSumUtil ( $ arr ) { global $ pos2 , $ pos1 ; $ n = count ( $ arr ) ; $ pre = array_fill ( 0 , $ n , 0 ) ; $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ sum += $ arr [ $ i ] ; $ pre [ $ i ] = $ sum ; } $ suf = array_fill ( 0 , $ n , 0 ) ; $ sum = 0 ; for ( $ i = $ n - 1 ; $ i >= 0 ; $ i -- ) { $ sum += $ arr [ $ i ] ; $ suf [ $ i ] = $ sum ; } $ total_sum = $ sum ; $ i = 0 ; $ j = $ n - 1 ; while ( $ i < $ j - 1 ) { if ( $ pre [ $ i ] == $ total_sum / 3 ) { $ pos1 = $ i ; } if ( $ suf [ $ j ] == $ total_sum / 3 ) { $ pos2 = $ j ; } if ( $ pos1 != -1 && $ pos2 != -1 ) { if ( $ suf [ $ pos1 + 1 ] - $ suf [ $ pos2 ] == $ total_sum / 3 ) { return true ; } else { return false ; } } if ( $ pre [ $ i ] < $ suf [ $ j ] ) { $ i ++ ; } else { $ j -- ; } } return false ; } function equiSum ( $ arr ) { global $ pos2 , $ pos1 ; $ ans = equiSumUtil ( $ arr ) ; if ( $ ans ) { print ( " First ▁ Segment ▁ : ▁ " ) ; for ( $ i = 0 ; $ i <= $ pos1 ; $ i ++ ) { print ( $ arr [ $ i ] . " " ) ; } print ( " STRNEWLINE " ) ; print ( " Second ▁ Segment ▁ : ▁ " ) ; for ( $ i = $ pos1 + 1 ; $ i < $ pos2 ; $ i ++ ) { print ( $ arr [ $ i ] . " " ) ; } print ( " STRNEWLINE " ) ; print ( " Third ▁ Segment ▁ : ▁ " ) ; for ( $ i = $ pos2 ; $ i < count ( $ arr ) ; $ i ++ ) { print ( $ arr [ $ i ] . " " ) ; } print ( " STRNEWLINE " ) ; } else { println ( " Array ▁ cannot ▁ be ▁ divided ▁ into ▁ " , " three ▁ equal ▁ sum ▁ segments " ) ; } } $ arr = array ( 1 , 3 , 6 , 2 , 7 , 1 , 2 , 8 ) ; equiSum ( $ arr ) ; ? >
Leftmost and rightmost indices of the maximum and the minimum element of an array | Function to return the index of the rightmost minimum element from the array ; First element is the minimum in a sorted array ; While the elements are equal to the minimum update rightMin ; Final check whether there are any elements which are equal to the minimum ; Function to return the index of the leftmost maximum element from the array ; Last element is the maximum in a sorted array ; While the elements are equal to the maximum update leftMax ; Final check whether there are any elements which are equal to the maximum ; Driver code ; First element is the leftmost minimum in a sorted array ; Last element is the rightmost maximum in a sorted array
< ? php function getRightMin ( $ arr , $ n ) { $ min = $ arr [ 0 ] ; $ rightMin = 0 ; $ i = 1 ; while ( $ i < $ n ) { if ( $ arr [ $ i ] == $ min ) $ rightMin = $ i ; $ i *= 2 ; } $ i = $ rightMin + 1 ; while ( $ i < $ n && $ arr [ $ i ] == $ min ) { $ rightMin = $ i ; $ i ++ ; } return $ rightMin ; } function getLeftMax ( $ arr , $ n ) { $ max = $ arr [ $ n - 1 ] ; $ leftMax = $ n - 1 ; $ i = $ n - 2 ; while ( $ i > 0 ) { if ( $ arr [ $ i ] == $ max ) $ leftMax = $ i ; $ i /= 2 ; } $ i = $ leftMax - 1 ; while ( $ i >= 0 && $ arr [ $ i ] == $ max ) { $ leftMax = $ i ; $ i -- ; } return $ leftMax ; } $ arr = array ( 0 , 0 , 1 , 2 , 5 , 5 , 6 , 8 , 8 ) ; $ n = sizeof ( $ arr ) ; echo " Minimum ▁ left ▁ : ▁ " , 0 , " STRNEWLINE " ; echo " Minimum ▁ right ▁ : ▁ " , getRightMin ( $ arr , $ n ) , " STRNEWLINE " ; echo " Maximum ▁ left ▁ : ▁ " , getLeftMax ( $ arr , $ n ) , " STRNEWLINE " ; echo " Maximum ▁ right ▁ : ▁ " , ( $ n - 1 ) , " STRNEWLINE " ; ? >
Elements to be added so that all elements of a range are present in array | Function to count numbers to be added ; Sort the array ; Check if elements are consecutive or not . If not , update count ; Driver code
< ? php function countNum ( $ arr , $ n ) { $ count = 0 ; sort ( $ arr ) ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) if ( $ arr [ $ i ] != $ arr [ $ i + 1 ] && $ arr [ $ i ] != $ arr [ $ i + 1 ] - 1 ) $ count += $ arr [ $ i + 1 ] - $ arr [ $ i ] - 1 ; return $ count ; } $ arr = array ( 3 , 5 , 8 , 6 ) ; $ n = count ( $ arr ) ; echo countNum ( $ arr , $ n ) ; ? >
Count subarrays with same even and odd elements | function that returns the count of subarrays that contain equal number of odd as well as even numbers ; initialize difference and answer with 0 ; initialize these auxiliary arrays with 0 ; since the difference is initially 0 , we have to initialize hash_positive [ 0 ] with 1 ; for loop to iterate through whole array ( zero - based indexing is used ) ; incrementing or decrementing difference based on arr [ i ] being even or odd , check if arr [ i ] is odd ; adding hash value of ' difference ' to our answer as all the previous occurrences of the same difference value will make even - odd subarray ending at index ' i ' . After that , we will increment hash array for that ' difference ' value for its occurrence at index ' i ' . if difference is negative then use hash_negative ; else use hash_positive ; return total number of even - odd subarrays ; Driver code ; Printing total number of even - odd subarrays
< ? php function countSubarrays ( & $ arr , $ n ) { $ difference = 0 ; $ ans = 0 ; $ hash_positive = array_fill ( 0 , $ n + 1 , NULL ) ; $ hash_negative = array_fill ( 0 , $ n + 1 , NULL ) ; $ hash_positive [ 0 ] = 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] & 1 == 1 ) $ difference ++ ; else $ difference -- ; if ( $ difference < 0 ) { $ ans += $ hash_negative [ - $ difference ] ; $ hash_negative [ - $ difference ] ++ ; } else { $ ans += $ hash_positive [ $ difference ] ; $ hash_positive [ $ difference ] ++ ; } } return $ ans ; } $ arr = array ( 3 , 4 , 6 , 8 , 1 , 10 , 5 , 7 ) ; $ n = sizeof ( $ arr ) ; echo " Total ▁ Number ▁ of ▁ Even - Odd ▁ subarrays " . " ▁ are ▁ " . countSubarrays ( $ arr , $ n ) ; ? >
Saddleback Search Algorithm in a 2D array | PHP program to search an element in row - wise and column - wise sorted matrix ; Searches the element x in mat [ m ] [ n ] . If the element is found , then prints its position and returns true , otherwise prints " not ▁ found " and returns false ; set indexes for bottom left element ; if mat [ i ] [ j ] < x ; Driver Code
< ? php $ MAX = 100 ; function search ( $ mat , $ m , $ n , $ x ) { $ i = $ m - 1 ; $ j = 0 ; while ( $ i >= 0 && $ j < $ n ) { if ( $ mat [ $ i ] [ $ j ] == $ x ) return true ; if ( $ mat [ $ i ] [ $ j ] > $ x ) $ i -- ; else $ j ++ ; } return false ; } $ mat = array ( array ( 10 , 20 , 30 , 40 ) , array ( 15 , 25 , 35 , 45 ) , array ( 27 , 29 , 37 , 48 ) , array ( 32 , 33 , 39 , 50 ) , array ( 50 , 60 , 70 , 80 ) ) ; if ( search ( $ mat , 5 , 4 , 29 ) ) echo " Yes " ; else echo " No " ; ? >
Number of Larger Elements on right side in a string | PHP program to find counts of right greater characters for every character . ; Driver code
< ? php function printGreaterCount ( $ str ) { $ len = strlen ( $ str ) ; $ right = array_fill ( 0 , $ len , 0 ) ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { for ( $ j = $ i + 1 ; $ j < $ len ; $ j ++ ) if ( $ str [ $ i ] < $ str [ $ j ] ) $ right [ $ i ] ++ ; } for ( $ i = 0 ; $ i < $ len ; $ i ++ ) echo $ right [ $ i ] . " ▁ " ; } $ str = ' bcd ' printGreaterCount ( $ str ) ; ? >
N / 3 repeated number in an array with O ( 1 ) space | PHP program to find if any element appears more than n / 3. ; take the integers as the maximum value of integer hoping the integer would not be present in the array ; if this element is previously seen , increment count1 . ; if this element is previously seen , increment count2 . ; if current element is different from both the previously seen variables , decrement both the counts . ; Again traverse the array and find the actual counts . ; Driver code
< ? php function appearsNBy3 ( $ arr , $ n ) { $ count1 = 0 ; $ count2 = 0 ; $ first = PHP_INT_MAX ; $ second = PHP_INT_MAX ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ first == $ arr [ $ i ] ) $ count1 ++ ; else if ( $ second == $ arr [ $ i ] ) $ count2 ++ ; else if ( $ count1 == 0 ) { $ count1 ++ ; $ first = $ arr [ $ i ] ; } else if ( $ count2 == 0 ) { $ count2 ++ ; $ second = $ arr [ $ i ] ; } else { $ count1 -- ; $ count2 -- ; } } $ count1 = 0 ; $ count2 = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] == $ first ) $ count1 ++ ; else if ( $ arr [ $ i ] == $ second ) $ count2 ++ ; } if ( $ count1 > $ n / 3 ) return $ first ; if ( $ count2 > $ n / 3 ) return $ second ; return -1 ; } $ arr = array ( 1 , 2 , 3 , 1 , 1 ) ; $ n = count ( $ arr ) ; echo appearsNBy3 ( $ arr , $ n ) ; ? >
Count pairs in array whose sum is divisible by 4 | Program to count pairs whose sum divisible by '4' ; Create a frequency array to count occurrences of all remainders when divided by 4 ; Count occurrences of all remainders ; If both pairs are divisible by '4' ; If both pairs are 2 modulo 4 ; If one of them is equal to 1 modulo 4 and the other is equal to 3 modulo 4 ; Driver code
< ? php function count4Divisibiles ( $ arr , $ n ) { $ freq = array ( 0 , 0 , 0 , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) ++ $ freq [ $ arr [ $ i ] % 4 ] ; $ ans = $ freq [ 0 ] * ( $ freq [ 0 ] - 1 ) / 2 ; $ ans += $ freq [ 2 ] * ( $ freq [ 2 ] - 1 ) / 2 ; $ ans += $ freq [ 1 ] * $ freq [ 3 ] ; return $ ans ; } $ arr = array ( 2 , 2 , 1 , 7 , 5 ) ; $ n = sizeof ( $ arr ) ; echo count4Divisibiles ( $ arr , $ n ) ; ? >
Smallest number whose set bits are maximum in a given range | Returns smallest number whose set bits are maximum in given range . ; Initialize the maximum count and final answer as ' num ' ; Traverse for every bit of ' i ' number ; If count is greater than previous calculated max_count , update it ; Driver code
< ? php function countMaxSetBits ( $ left , $ right ) { $ max_count = -1 ; $ num ; for ( $ i = $ left ; $ i <= $ right ; ++ $ i ) { $ temp = $ i ; $ cnt = 0 ; while ( $ temp ) { if ( $ temp & 1 ) ++ $ cnt ; $ temp >>= 1 ; } if ( $ cnt > $ max_count ) { $ max_count = $ cnt ; $ num = $ i ; } } return $ num ; } $ l = 1 ; $ r = 5 ; echo countMaxSetBits ( $ l , $ r ) , " STRNEWLINE " ; $ l = 1 ; $ r = 10 ; echo countMaxSetBits ( $ l , $ r ) ; ? >
Recaman 's sequence | Prints first n terms of Recaman sequence ; First term of the sequence is always 0 ; Fill remaining terms using recursive formula . ; If arr [ i - 1 ] - i is negative or already exists . ; Driver Code
< ? php function recaman ( $ n ) { $ arr [ 0 ] = 0 ; echo $ arr [ 0 ] , " , ▁ " ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ curr = $ arr [ $ i - 1 ] - $ i ; $ j ; for ( $ j = 0 ; $ j < $ i ; $ j ++ ) { if ( ( $ arr [ $ j ] == $ curr ) $ curr < 0 ) { $ curr = $ arr [ $ i - 1 ] + $ i ; break ; } } $ arr [ $ i ] = $ curr ; echo $ arr [ $ i ] , " , ▁ " ; } } $ n = 17 ; recaman ( $ n ) ; ? >
Recaman 's sequence | Prints first n terms of Recaman sequence ; Print first term and store it in a hash ; Print remaining terms using recursive formula . ; If arr [ i - 1 ] - i is negative or already exists . ; Driver code
< ? php function recaman ( $ n ) { if ( $ n <= 0 ) return ; print ( "0 , ▁ " ) ; $ s = array ( ) ; array_push ( $ s , 0 ) ; $ prev = 0 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ curr = $ prev - $ i ; if ( $ curr < 0 or in_array ( $ curr , $ s ) ) $ curr = $ prev + $ i ; array_push ( $ s , $ curr ) ; print ( $ curr . " , " ) ; $ prev = $ curr ; } } $ n = 17 ; recaman ( $ n ) ; ? >
Maximum area rectangle by picking four sides from array | function for finding max area ; sort array in non - increasing order ; Initialize two sides of rectangle ; traverse through array ; if any element occurs twice store that as dimension ; return the product of dimensions ; Driver Code
< ? php function findArea ( $ arr , $ n ) { rsort ( $ arr ) ; $ dimension = array ( 0 , 0 ) ; for ( $ i = 0 , $ j = 0 ; $ i < $ n - 1 && $ j < 2 ; $ i ++ ) if ( $ arr [ $ i ] == $ arr [ $ i + 1 ] ) $ dimension [ $ j ++ ] = $ arr [ $ i ++ ] ; return ( $ dimension [ 0 ] * $ dimension [ 1 ] ) ; } $ arr = array ( 4 , 2 , 1 , 4 , 6 , 6 , 2 , 5 ) ; $ n = count ( $ arr ) ; echo findArea ( $ arr , $ n ) ; ? >
Given a sorted and rotated array , find if there is a pair with a given sum | This function returns true if arr [ 0. . n - 1 ] has a pair with sum equals to x . ; Find the pivot element ; l is now index of smallest element ; r is now index of largest element ; Keep moving either l or r till they meet ; If we find a pair with sum x , we return true ; If current pair sum is less , move to the higher sum ; Move to the lower sum side ; Driver Code
< ? php function pairInSortedRotated ( $ arr , $ n , $ x ) { $ i ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) if ( $ arr [ $ i ] > $ arr [ $ i + 1 ] ) break ; $ l = ( $ i + 1 ) % $ n ; $ r = $ i ; while ( $ l != $ r ) { if ( $ arr [ $ l ] + $ arr [ $ r ] == $ x ) return true ; if ( $ arr [ $ l ] + $ arr [ $ r ] < $ x ) $ l = ( $ l + 1 ) % $ n ; else $ r = ( $ n + $ r - 1 ) % $ n ; } return false ; } $ arr = array ( 11 , 15 , 6 , 8 , 9 , 10 ) ; $ sum = 16 ; $ n = sizeof ( $ arr ) ; if ( pairInSortedRotated ( $ arr , $ n , $ sum ) ) echo " Array ▁ has ▁ two ▁ elements ▁ " . " with ▁ sum ▁ 16" ; else echo " Array ▁ doesn ' t ▁ have ▁ two ▁ " . " elements ▁ with ▁ sum ▁ 16 ▁ " ; ? >
Given a sorted and rotated array , find if there is a pair with a given sum | This function returns count of number of pairs with sum equals to x . ; Find the pivot element . Pivot element is largest element of array . ; l is index of smallest element . ; r is index of largest element . ; Variable to store count of number of pairs . ; Find sum of pair formed by arr [ l ] and arr [ r ] and update l , r and cnt accordingly . ; If we find a pair with sum x , then increment cnt , move l and r to next element . ; This condition is required to be checked , otherwise l and r will cross each other and loop will never terminate . ; If current pair sum is less , move to the higher sum side . ; If current pair sum is greater , move to the lower sum side . ; Driver Code
< ? php function pairsInSortedRotated ( $ arr , $ n , $ x ) { $ i ; for ( $ i = 0 ; $ i < $ n - 1 ; $ i ++ ) if ( $ arr [ $ i ] > $ arr [ $ i + 1 ] ) break ; $ l = ( $ i + 1 ) % $ n ; $ r = $ i ; $ cnt = 0 ; while ( $ l != $ r ) { if ( $ arr [ $ l ] + $ arr [ $ r ] == $ x ) { $ cnt ++ ; if ( $ l == ( $ r - 1 + $ n ) % $ n ) { return $ cnt ; } $ l = ( $ l + 1 ) % $ n ; $ r = ( $ r - 1 + $ n ) % $ n ; } else if ( $ arr [ $ l ] + $ arr [ $ r ] < $ x ) $ l = ( $ l + 1 ) % $ n ; else $ r = ( $ n + $ r - 1 ) % $ n ; } return $ cnt ; } $ arr = array ( 11 , 15 , 6 , 7 , 9 , 10 ) ; $ sum = 16 ; $ n = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; echo pairsInSortedRotated ( $ arr , $ n , $ sum ) ; ? >