Count quadruples from four sorted arrays whose sum is equal to a given value x | find the ' value ' in the given array ' arr [ ] ' binary search technique is applied ; ' value ' found ; ' value ' not found ; function to count all quadruples from four sorted arrays whose sum is equal to a given value x ; generate all triplets from the 1 st three arrays ; calculate the sum of elements in the triplet so generated ; check if ' x - T ' is present in 4 th array or not ; increment count ; required count of quadruples ; four sorted arrays each of size ' n '
< ? php function isPresent ( $ arr , $ low , $ high , $ value ) { while ( $ low <= $ high ) { $ mid = ( $ low + $ high ) / 2 ; if ( $ arr [ $ mid ] == $ value ) return true ; else if ( $ arr [ $ mid ] > $ value ) $ high = $ mid - 1 ; else $ low = $ mid + 1 ; } return false ; } function countQuadruples ( $ arr1 , $ arr2 , $ arr3 , $ arr4 , $ n , $ x ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = 0 ; $ j < $ n ; $ j ++ ) for ( $ k = 0 ; $ k < $ n ; $ k ++ ) { $ T = $ arr1 [ $ i ] + $ arr2 [ $ j ] + $ arr3 [ $ k ] ; if ( isPresent ( $ arr4 , 0 , $ n , $ x - $ T ) ) $ count ++ ; } return $ count ; } $ arr1 = array ( 1 , 4 , 5 , 6 ) ; $ arr2 = array ( 2 , 3 , 7 , 8 ) ; $ arr3 = array ( 1 , 4 , 6 , 10 ) ; $ arr4 = array ( 2 , 4 , 7 , 8 ) ; $ n = sizeof ( $ arr1 ) ; $ x = 30 ; echo " Count = " ? >