codeparrot/xlcost-text-to-code huggingface dataset:
Count the number of operations required to reduce the given number | PHP implementation of the approach ; To store the normalized value of all the operations ; Minimum possible value for a series of operations ; If k can be reduced with first ( i + 1 ) operations ; Impossible to reduce k ; Number of times all the operations can be performed on k without reducing it to <= 0 ; Perform operations ; Final check ; Driver code
< ? php function operations ( $ op , $ n , $ k ) { $ count = 0 ; $ nVal = 0 ; $ minimum = PHP_INT_MAX ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ nVal += $ op [ $ i ] ; $ minimum = min ( $ minimum , $ nVal ) ; if ( ( $ k + $ nVal ) <= 0 ) return ( $ i + 1 ) ; } if ( $ nVal >= 0 ) return -1 ; $ times = round ( ( $ k - abs ( $ minimum ) ) / abs ( $ nVal ) ) ; $ k = ( $ k - ( $ times * abs ( $ nVal ) ) ) ; $ count = ( $ times * $ n ) ; while ( $ k > 0 ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ k = $ k + $ op [ $ i ] ; $ count ++ ; if ( $ k <= 0 ) break ; } } return $ count ; } $ op = array ( -60 , 65 , -1 , 14 , -25 ) ; $ n = sizeof ( $ op ) ; $ k = 100000 ; echo operations ( $ op , $ n , $ k ) ; ? >
Find the smallest number X such that X ! contains at least Y trailing zeros . | Function to count the number of factors P in X ! ; Function to find the smallest X such that X ! contains Y trailing zeros ; Driver code
< ? php function countFactor ( $ P , $ X ) { if ( $ X < $ P ) return 0 ; return ( ( int ) ( $ X / $ P ) + countFactor ( $ P , ( $ X / $ P ) ) ) ; } function findSmallestX ( $ Y ) { $ low = 0 ; $ high = 5 * $ Y ; $ N = 0 ; while ( $ low <= $ high ) { $ mid = ( int ) ( ( $ high + $ low ) / 2 ) ; if ( countFactor ( 5 , $ mid ) < $ Y ) { $ low = $ mid + 1 ; } else { $ N = $ mid ; $ high = $ mid - 1 ; } } return $ N ; } $ Y = 10 ; echo ( findSmallestX ( $ Y ) ) ; ? >
Find maximum N such that the sum of square of first N natural numbers is not more than X | Function to return the sum of the squares of first N natural numbers ; Function to return the maximum N such that the sum of the squares of first N natural numbers is not more than X ; Driver code
< ? php function squareSum ( $ N ) { $ sum = ( $ N * ( int ) ( $ N + 1 ) * ( 2 * $ N + 1 ) ) / 6 ; return $ sum ; } function findMaxN ( $ X ) { $ low = 1 ; $ high = 100000 ; $ N = 0 ; while ( $ low <= $ high ) { $ mid = ( int ) ( $ high + $ low ) / 2 ; if ( squareSum ( $ mid ) <= $ X ) { $ N = $ mid ; $ low = $ mid + 1 ; } else $ high = $ mid - 1 ; } return $ N ; } $ X = 25 ; echo findMaxN ( $ X ) ; ? >
Number of subarrays have bitwise OR >= K | Function to return the count of required sub - arrays ; Traverse sub - array [ i . . j ] ; Driver code
< ? php function countSubArrays ( $ arr , $ n , $ K ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { $ bitwise_or = 0 ; for ( $ k = $ i ; $ k < $ j + 1 ; $ k ++ ) $ bitwise_or = $ bitwise_or | $ arr [ $ k ] ; if ( $ bitwise_or >= $ K ) $ count += 1 ; } } return $ count ; } $ arr = array ( 3 , 4 , 5 ) ; $ n = count ( $ arr ) ; $ k = 6 ; print ( countSubArrays ( $ arr , $ n , $ k ) ) ; ? >
Occurrences of a pattern in binary representation of a number | Function to return the count of occurrence of pat in binary representation of n ; To store decimal value of the pattern ; To store a number that has all ones in its binary representation and length of ones equal to length of the pattern ; Find values of $pattern_int and $all_ones ; If the pattern occurs in the last digits of $n ; Right shift $n by 1 bit ; Driver code
< ? php function countPattern ( $ n , $ pat ) { $ pattern_int = 0 ; $ power_two = 1 ; $ all_ones = 0 ; for ( $ i = strlen ( $ pat ) - 1 ; $ i >= 0 ; $ i -- ) { $ current_bit = $ pat [ $ i ] - '0' ; $ pattern_int += ( $ power_two * $ current_bit ) ; $ all_ones = $ all_ones + $ power_two ; $ power_two = $ power_two * 2 ; } $ count = 0 ; while ( $ n && $ n >= $ pattern_int ) { if ( ( $ n & $ all_ones ) == $ pattern_int ) { $ count ++ ; } $ n = $ n >> 1 ; } return $ count ; } $ n = 500 ; $ pat = "10" ; echo countPattern ( $ n , $ pat ) ; ? >
Remove exactly one element from the array such that max | function to calculate max - min ; Driver code
< ? php function max_min ( & $ a , $ n ) { sort ( $ a ) ; return min ( $ a [ $ n - 2 ] - $ a [ 0 ] , $ a [ $ n - 1 ] - $ a [ 1 ] ) ; } $ a = array ( 1 , 3 , 3 , 7 ) ; $ n = sizeof ( $ a ) ; echo ( max_min ( $ a , $ n ) ) ; ? >
Count numbers < = N whose difference with the count of primes upto them is > = K | PHP implementation of the above approach ; primeUpto [ i ] denotes count of prime numbers upto i ; Function to compute all prime numbers and update primeUpto array ; 0 and 1 are not primes ; If i is prime ; Set all multiples of i as non - prime ; Compute primeUpto array ; Function to return the count of valid numbers ; Compute primeUpto array ; Check if the number is valid , try to reduce it ; ans is the minimum valid number ; Driver Code
< ? php $ MAX = 100001 ; $ primeUpto = array_fill ( 0 , $ MAX , 0 ) ; function SieveOfEratosthenes ( ) { global $ MAX , $ primeUpto ; $ isPrime = array_fill ( 0 , $ MAX , true ) ; $ isPrime [ 0 ] = $ isPrime [ 1 ] = false ; for ( $ i = 2 ; $ i * $ i < $ MAX ; $ i ++ ) { if ( $ isPrime [ $ i ] ) { for ( $ j = $ i * 2 ; $ j < $ MAX ; $ j += $ i ) $ isPrime [ $ j ] = false ; } } for ( $ i = 1 ; $ i < $ MAX ; $ i ++ ) { $ primeUpto [ $ i ] = $ primeUpto [ $ i - 1 ] ; if ( $ isPrime [ $ i ] ) $ primeUpto [ $ i ] ++ ; } } function countOfNumbers ( $ N , $ K ) { SieveOfEratosthenes ( ) ; global $ primeUpto ; $ low = 1 ; $ high = $ N ; $ ans = 0 ; while ( $ low <= $ high ) { $ mid = ( $ low + $ high ) >> 1 ; if ( $ mid - $ primeUpto [ $ mid ] >= $ K ) { $ ans = $ mid ; $ high = $ mid - 1 ; } else $ low = $ mid + 1 ; } return ( $ ans ? $ N - $ ans + 1 : 0 ) ; } $ N = 10 ; $ K = 3 ; echo countOfNumbers ( $ N , $ K ) ; ? >
Find the element whose multiplication with | Function to find minimum index such that sum becomes 0 when the element is multiplied by - 1 ; Find array sum ; Find element with value equal to sum / 2 ; when sum is equal to 2 * element then this is our required element ; Driver code
< ? php function minIndex ( & $ arr , $ n ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += $ arr [ $ i ] ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( 2 * $ arr [ $ i ] == $ sum ) return ( $ i + 1 ) ; } return -1 ; } $ arr = array ( 1 , 3 , -5 , 3 , 4 ) ; $ n = sizeof ( $ arr ) ; echo ( minIndex ( $ arr , $ n ) ) ; ? >
Find the intersection of two Matrices | Function to print the resultant matrix ; print element value for equal elements else * ; Driver Code
< ? php function printIntersection ( $ A , $ B ) { $ N = 4 ; $ M = 4 ; for ( $ i = 0 ; $ i < $ M ; $ i ++ ) { for ( $ j = 0 ; $ j < $ N ; $ j ++ ) { if ( $ A [ $ i ] [ $ j ] == $ B [ $ i ] [ $ j ] ) echo $ A [ $ i ] [ $ j ] . " ▁ " ; else echo " * ▁ " ; } echo " STRNEWLINE " ; } } $ A = array ( array ( 2 , 4 , 6 , 8 ) , array ( 1 , 3 , 5 , 7 ) , array ( 8 , 6 , 4 , 2 ) , array ( 7 , 5 , 3 , 1 ) ) ; $ B = array ( array ( 2 , 3 , 6 , 8 ) , array ( 1 , 3 , 5 , 2 ) , array ( 8 , 1 , 4 , 2 ) , array ( 3 , 5 , 4 , 1 ) ) ; printIntersection ( $ A , $ B ) ; ? >
Count Triplets such that one of the numbers can be written as sum of the other two | Function to count the number of ways to choose the triples ; compute the max value in the array and create frequency array of size max_val + 1. We can also use HashMap to store frequencies . We have used an array to keep remaining code simple . ; Case 1 : 0 , 0 , 0 ; Case 2 : 0 , x , x ; Case 3 : x , x , 2 * x ; Case 4 : x , y , x + y iterate through all pairs ( x , y ) ; Driver code
< ? php function countWays ( $ arr , $ n ) { $ max_val = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ max_val = max ( $ max_val , $ arr [ $ i ] ) ; $ freq = array_fill ( 0 , $ max_val + 1 , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ freq [ $ arr [ $ i ] ] ++ ; $ ans += ( int ) ( $ freq [ 0 ] * ( $ freq [ 0 ] - 1 ) * ( $ freq [ 0 ] - 2 ) / 6 ) ; for ( $ i = 1 ; $ i <= $ max_val ; $ i ++ ) $ ans += ( int ) ( $ freq [ 0 ] * $ freq [ $ i ] * ( $ freq [ $ i ] - 1 ) / 2 ) ; for ( $ i = 1 ; 2 * $ i <= $ max_val ; $ i ++ ) $ ans += ( int ) ( $ freq [ $ i ] * ( $ freq [ $ i ] - 1 ) / 2 * $ freq [ 2 * $ i ] ) ; for ( $ i = 1 ; $ i <= $ max_val ; $ i ++ ) { for ( $ j = $ i + 1 ; $ i + $ j <= $ max_val ; $ j ++ ) $ ans += $ freq [ $ i ] * $ freq [ $ j ] * $ freq [ $ i + $ j ] ; } return $ ans ; } $ arr = array ( 1 , 2 , 3 , 4 , 5 ) ; $ n = count ( $ arr ) ; echo countWays ( $ arr , $ n ) ; ? >
Longest subarray with elements having equal modulo K | function to find longest sub - array whose elements gives same remainder when divided with K ; Iterate in the array ; check if array element greater then X or not ; Driver code
< ? php function LongestSubarray ( $ arr , $ n , $ k ) { $ cnt = 1 ; $ max_length = 1 ; $ prev_mod = $ arr [ 0 ] % $ k ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ curr_mod = $ arr [ $ i ] % $ k ; if ( $ curr_mod == $ prev_mod ) { $ cnt ++ ; } else { $ max_length = max ( $ max_length , $ cnt ) ; $ cnt = 1 ; $ prev_mod = $ curr_mod ; } } return max ( $ max_length , $ cnt ) ; } $ arr = array ( 4 , 9 , 7 , 18 , 29 , 11 ) ; $ n = count ( $ arr ) ; $ k = 11 ; echo LongestSubarray ( $ arr , $ n , $ k ) ; ? >
Search in a sorted 2D matrix ( Stored in row major order ) | Php program to find whether a given element is present in the given 2 - D matrix ; Basic binary search to find an element in a 1 - D array ; if element found return true ; if middle less than K then skip the left part of the array else skip the right part ; if not found return false ; Function to search an element in a matrix based on Divide and conquer approach ; if the element lies in the range of this row then call 1 - D binary search on this row ; if the element is less then the starting element of that row then search in upper rows else search in the lower rows ; if not found ; Driver code
< ? php $ M = 3 ; $ N = 4 ; function binarySearch1D ( $ arr , $ K ) { $ low = 0 ; $ high = $ GLOBALS [ ' N ' ] - 1 ; while ( $ low <= $ high ) { $ mid = $ low + ( int ) ( $ high - $ low ) / 2 ; if ( $ arr [ $ mid ] == $ K ) return True ; if ( $ arr [ $ mid ] < $ K ) $ low = $ mid + 1 ; else $ high = $ mid - 1 ; } return False ; } function searchMatrix ( $ matrix , $ K ) { $ low = 0 ; $ high = $ GLOBALS [ ' M ' ] - 1 ; while ( $ low <= $ high ) { $ mid = $ low + ( int ) ( $ high - $ low ) / 2 ; if ( $ K >= $ matrix [ $ mid ] [ 0 ] && $ K <= $ matrix [ $ mid ] [ $ GLOBALS [ ' N ' ] - 1 ] ) return binarySearch1D ( $ matrix [ $ mid ] , $ K ) ; if ( $ K < $ matrix [ $ mid ] [ 0 ] ) $ high = $ mid - 1 ; else $ low = $ mid + 1 ; } return False ; } $ matrix = array ( [ 1 , 3 , 5 , 7 ] , [ 10 , 11 , 16 , 20 ] , [ 23 , 30 , 34 , 50 ] ) ; $ K = 3 ; $ M = 3 ; $ N = 4 ; if ( searchMatrix ( $ matrix , $ K ) ) echo " Found " ; else echo " Not ▁ found " ; ? >
Number of elements that can be seen from right side | PHP program to find number of elements that can be seen from right side . ; Driver code
< ? php function numberOfElements ( $ height , $ n ) { $ max_so_far = 0 ; $ coun = 0 ; for ( $ i = $ n - 1 ; $ i >= 0 ; $ i -- ) { if ( $ height [ $ i ] > $ max_so_far ) { $ max_so_far = $ height [ $ i ] ; $ coun ++ ; } } return $ coun ; } $ n = 6 ; $ height = array ( 4 , 8 , 2 , 0 , 0 , 5 ) ; echo numberOfElements ( $ height , $ n ) ;
Largest element in the array that is repeated exactly k times | Function that finds the largest element which is repeated ' k ' times ; sort the array ; if the value of ' k ' is 1 and the largest appears only once in the array ; counter to count the repeated elements ; check if the element at index ' i ' is equal to the element at index ' i + 1' then increase the count ; else set the count to 1 to start counting the frequency of the new number ; if the count is equal to k and the previous element is not equal to this element ; if there is no such element ; Driver code ; find the largest element that is repeated K times
< ? php function solve ( & $ arr , $ n , $ k ) { sort ( $ arr ) ; if ( $ k == 1 && $ arr [ $ n - 2 ] != $ arr [ $ n - 1 ] ) { echo $ arr [ $ n - 1 ] ; echo ( " STRNEWLINE " ) ; return ; } $ count = 1 ; for ( $ i = $ n - 2 ; $ i >= 0 ; $ i -- ) { if ( $ arr [ $ i ] == $ arr [ $ i + 1 ] ) $ count ++ ; else $ count = 1 ; if ( $ count == $ k && ( $ i == 0 || ( $ arr [ $ i - 1 ] != $ arr [ $ i ] ) ) ) { echo ( $ arr [ $ i ] ) ; echo ( " STRNEWLINE " ) ; return ; } } echo ( " No ▁ such ▁ element " ) ; echo ( " STRNEWLINE " ) ; } $ arr = array ( 1 , 1 , 2 , 3 , 3 , 4 , 5 , 5 , 6 , 6 , 6 ) ; $ k = 2 ; $ n = sizeof ( $ arr ) ; solve ( $ arr , $ n , $ k ) ; ? >
Sum and Product of minimum and maximum element of an Array | Function to find minimum element ; Function to find maximum element ; Function to get Sum ; Function to get product ; Driver Code ; Sum of min and max element ; Product of min and max element
< ? php function getMin ( $ arr , $ n ) { $ res = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ res = min ( $ res , $ arr [ $ i ] ) ; return $ res ; } function getMax ( $ arr , $ n ) { $ res = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ res = max ( $ res , $ arr [ $ i ] ) ; return $ res ; } function findSum ( $ arr , $ n ) { $ min = getMin ( $ arr , $ n ) ; $ max = getMax ( $ arr , $ n ) ; return $ min + $ max ; } function findProduct ( $ arr , $ n ) { $ min = getMin ( $ arr , $ n ) ; $ max = getMax ( $ arr , $ n ) ; return $ min * $ max ; } $ arr = array ( 12 , 1234 , 45 , 67 , 1 ) ; $ n = sizeof ( $ arr ) ; echo " Sum = " ▁ . ▁ findSum ( $ arr , ▁ $ n ) ▁ . ▁ " " ; STRNEWLINE echo ▁ " Product = "
Count characters with same neighbors | Function to count the characters with same adjacent characters ; if length is less than 3 then return length as there will be only two characters ; Traverse the string ; Increment the count if the previous and next character is same ; Return count ; Driver code
< ? php function countChar ( $ str ) { $ n = strlen ( $ str ) ; if ( $ n <= 2 ) return $ n ; $ count = 2 ; for ( $ i = 1 ; $ i < $ n - 1 ; $ i ++ ) if ( $ str [ $ i - 1 ] == $ str [ $ i + 1 ] ) $ count ++ ; return $ count ; } $ str = " egeeksk " ; echo countChar ( $ str ) ; ? >
First strictly smaller element in a sorted array in Java | PHP program to find first element that is strictly smaller than given target . ; Minimum size of the array should be 1 ; If target lies beyond the max element , than the index of strictly smaller value than target should be ( end - 1 ) ; Move to the left side if the target is smaller ; Move right side ; Driver code
< ? php function next0 ( $ arr , $ target ) { $ start = 0 ; $ end = sizeof ( $ arr ) - 1 ; if ( $ end == 0 ) return -1 ; if ( $ target > $ arr [ $ end ] ) return $ end ; $ ans = -1 ; while ( $ start <= $ end ) { $ mid = ( int ) ( ( $ start + $ end ) / 2 ) ; if ( $ arr [ $ mid ] >= $ target ) { $ end = $ mid - 1 ; } else { $ ans = $ mid ; $ start = $ mid + 1 ; } } return $ ans ; } { $ arr = array ( 1 , 2 , 3 , 5 , 8 , 12 ) ; echo ( next0 ( $ arr , 5 ) ) ; }
First strictly greater element in a sorted array in Java | PHP program to find first element that is strictly greater than given target . ; Move to right side if target is greater . ; Move left side . ; Driver code
< ? php function next0 ( $ arr , $ target ) { $ start = 0 ; $ end = sizeof ( $ arr ) - 1 ; $ ans = -1 ; while ( $ start <= $ end ) { $ mid = ( int ) ( ( $ start + $ end ) / 2 ) ; if ( $ arr [ $ mid ] <= $ target ) { $ start = $ mid + 1 ; } else { $ ans = $ mid ; $ end = $ mid - 1 ; } } return $ ans ; } { $ arr = array ( 1 , 2 , 3 , 5 , 8 , 12 ) ; echo ( next0 ( $ arr , 8 ) ) ; } ? >
Reallocation of elements based on Locality of Reference | A function to perform sequential search . ; Linearly search the element ; If not found ; Shift elements before one position ; Driver Code
< ? php function search ( $ arr , $ n , $ x ) { $ res = -1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ x == $ arr [ $ i ] ) $ res = $ i ; if ( $ res == -1 ) return false ; $ temp = $ arr [ $ res ] ; for ( $ i = $ res ; $ i > 0 ; $ i -- ) $ arr [ $ i ] = $ arr [ $ i - 1 ] ; $ arr [ 0 ] = $ temp ; return true ; } $ arr = array ( 12 , 25 , 36 , 85 , 98 , 75 , 89 , 15 , 63 , 66 , 64 , 74 , 27 , 83 , 97 ) ; $ q = array ( 63 , 63 , 86 , 63 , 78 ) ; $ n = sizeof ( $ arr ) ; $ m = sizeof ( $ q ) ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) if ( search ( $ arr , $ n , $ q [ $ i ] ) ) echo " Yes ▁ " ; else echo " No ▁ " ;
Probability of a key K present in array | Function to find the probability ; find probability ; Driver Code
< ? php function kPresentProbability ( & $ a , $ n , $ k ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( $ a [ $ i ] == $ k ) $ count ++ ; return $ count / $ n ; } $ A = array ( 4 , 7 , 2 , 0 , 8 , 7 , 5 ) ; $ K = 2 ; $ N = sizeof ( $ A ) ; echo round ( kPresentProbability ( $ A , $ N , $ K ) , 2 ) ; ? >
Find the slope of the given number | function to find slope of a number ; to store slope of the given number ' num ' ; loop from the 2 nd digit up to the 2 nd last digit of the given number ' num ' ; if the digit is a maxima ; if the digit is a minima ; required slope ; Driver Code
< ? php function slopeOfNum ( $ num , $ n ) { $ slope = 0 ; for ( $ i = 1 ; $ i < $ n - 1 ; $ i ++ ) { if ( $ num [ $ i ] > $ num [ $ i - 1 ] && $ num [ $ i ] > $ num [ $ i + 1 ] ) $ slope ++ ; else if ( $ num [ $ i ] < $ num [ $ i - 1 ] && $ num [ $ i ] < $ num [ $ i + 1 ] ) $ slope ++ ; } return $ slope ; } $ num = "1213321" ; $ n = strlen ( $ num ) ; echo " Slope = " ? >
Previous greater element | php program previous greater element A naive solution to print previous greater element for every element in an array . ; Previous greater for first element never exists , so we print - 1. ; Let us process remaining elements . ; Find first element on left side that is greater than arr [ i ] . ; If all elements on left are smaller . ; Driver code
< ? php function prevGreater ( & $ arr , $ n ) { echo ( " - 1 , ▁ " ) ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { for ( $ j = $ i - 1 ; $ j >= 0 ; $ j -- ) { if ( $ arr [ $ i ] < $ arr [ $ j ] ) { echo ( $ arr [ $ j ] ) ; echo ( " , ▁ " ) ; break ; } } if ( $ j == -1 ) echo ( " - 1 , ▁ " ) ; } } $ arr = array ( 10 , 4 , 2 , 20 , 40 , 12 , 30 ) ; $ n = sizeof ( $ arr ) ; prevGreater ( $ arr , $ n ) ; ? >
Duplicates in an array in O ( n ) time and by using O ( 1 ) extra space | Set | Function to find repeating elements ; Flag variable used to represent whether repeating element is found or not . ; Check if current element is repeating or not . If it is repeating then value will be greater than or equal to n . ; Check if it is first repetition or not . If it is first repetition then value at index arr [ i ] is less than 2 * n . Print arr [ i ] if it is first repetition . ; Add n to index arr [ i ] to mark presence of arr [ i ] or to mark repetition of arr [ i ] . ; If flag variable is not set then no repeating element is found . So print - 1. ; Driver Function
< ? php function printDuplicates ( $ arr , $ n ) { $ i ; $ fl = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ arr [ $ i ] % $ n ] >= $ n ) { if ( $ arr [ $ arr [ $ i ] % $ n ] < 2 * $ n ) { echo $ arr [ $ i ] % $ n . " " ; $ fl = 1 ; } } $ arr [ $ arr [ $ i ] % $ n ] += $ n ; } if ( ! $ fl ) echo " - 1" ; } $ arr = array ( 1 , 6 , 3 , 1 , 3 , 6 , 6 ) ; $ arr_size = sizeof ( $ arr ) ; printDuplicates ( $ arr , $ arr_size ) ;
Find the smallest positive number missing from an unsorted array | Set 2 | Function to find smallest positive missing number . ; to store current array element ; to store next array element in current traversal ; if value is negative or greater than array size , then it cannot be marked in array . So move to next element . ; traverse the array until we reach at an element which is already marked or which could not be marked . ; find first array index which is not marked which is also the smallest positive missing number . ; if all indices are marked , then smallest missing positive number is array_size + 1. ; Driver code
< ? php function findMissingNo ( $ arr , $ n ) { $ val ; $ nextval ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] <= 0 $ arr [ $ i ] > $ n ) continue ; $ val = $ arr [ $ i ] ; while ( $ arr [ $ val - 1 ] != $ val ) { $ nextval = $ arr [ $ val - 1 ] ; $ arr [ $ val - 1 ] = $ val ; $ val = $ nextval ; if ( $ val <= 0 $ val > $ n ) break ; } } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] != $ i + 1 ) { return $ i + 1 ; } } return $ n + 1 ; } $ arr = array ( 2 , 3 , 7 , 6 , 8 , -1 , -10 , 15 ) ; $ arr_size = sizeof ( $ arr ) / sizeof ( $ arr [ 0 ] ) ; $ missing = findMissingNo ( $ arr , $ arr_size ) ; echo " The ▁ smallest ▁ positive ▁ " . " missing ▁ number ▁ is ▁ " , $ missing ; ? >
Print all triplets with given sum | Prints all triplets in arr [ ] with given sum ; Driver code
< ? php function findTriplets ( $ arr , $ n , $ sum ) { 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 ] == $ sum ) { echo $ arr [ $ i ] , " " , $ arr [ $ j ] , " " , $ arr [ $ k ] , " " ; } } } } } $ arr = array ( 0 , -1 , 2 , -3 , 1 ) ; $ n = sizeof ( $ arr ) ; findTriplets ( $ arr , $ n , -2 ) ; ? >
Maximum product quadruple ( sub | Function to find a maximum product of a quadruple in array of integers of size n ; if size is less than 4 , no quadruple exists ; will contain max product ; Driver Code
< ? php function maxProduct ( $ arr , $ n ) { if ( $ n < 4 ) return -1 ; $ max_product = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ n - 3 ; $ i ++ ) for ( $ j = $ i + 1 ; $ j < $ n - 2 ; $ j ++ ) for ( $ k = $ j + 1 ; $ k < $ n - 1 ; $ k ++ ) for ( $ l = $ k + 1 ; $ l < $ n ; $ l ++ ) $ max_product = max ( $ max_product , $ arr [ $ i ] * $ arr [ $ j ] * $ arr [ $ k ] * $ arr [ $ l ] ) ; return $ max_product ; } $ arr = array ( 10 , 3 , 5 , 6 , 20 ) ; $ n = count ( $ arr ) ; $ max = maxProduct ( $ arr , $ n ) ; if ( $ max == -1 ) echo " No ▁ Quadruple ▁ Exists " ; else echo " Maximum ▁ product ▁ is ▁ " , $ max ; ? >
Maximum product quadruple ( sub | Function to find a maximum product of a quadruple in array of integers of size n ; if size is less than 4 , no quadruple exists ; Sort the array in ascending order ; Return the maximum of x , y and z ; Driver Code
< ? php function maxProduct ( $ arr , $ n ) { if ( $ n < 4 ) return -1 ; sort ( $ arr ) ; $ x = $ arr [ $ n - 1 ] * $ arr [ $ n - 2 ] * $ arr [ $ n - 3 ] * $ arr [ $ n - 4 ] ; $ y = $ arr [ 0 ] * $ arr [ 1 ] * $ arr [ 2 ] * $ arr [ 3 ] ; $ z = $ arr [ 0 ] * $ arr [ 1 ] * $ arr [ $ n - 1 ] * $ arr [ $ n - 2 ] ; return max ( $ x , max ( $ y , $ z ) ) ; } $ arr = array ( -10 , -3 , 5 , 6 , -20 ) ; $ n = sizeof ( $ arr ) ; $ max = maxProduct ( $ arr , $ n ) ; if ( $ max == -1 ) echo " No ▁ Quadruple ▁ Exists " ; else echo " Maximum ▁ product ▁ is ▁ " . $ max ;
Minimum value of " max ▁ + ▁ min " in a subarray | PHP program to find sum of maximum and minimum in any subarray of an array of positive numbers . ; Driver code
< ? php function maxSum ( $ arr , $ n ) { if ( $ n < 2 ) return -1 ; $ ans = $ arr [ 0 ] + $ arr [ 1 ] ; for ( $ i = 1 ; $ i + 1 < $ n ; $ i ++ ) $ ans = min ( $ ans , ( $ arr [ $ i ] + $ arr [ $ i + 1 ] ) ) ; return $ ans ; } $ arr = array ( 1 , 12 , 2 , 2 ) ; $ n = count ( $ arr ) ; echo maxSum ( $ arr , $ n ) ; ? >