Count pairs from two sorted arrays whose sum is equal to a given value x | function to search ' value ' in the given array ' arr [ ] ' it uses binary search technique as ' arr [ ] ' is sorted ; value found ; value not found ; function to count all pairs from both the sorted arrays whose sum is equal to a given value ; for each arr1 [ i ] ; check if the ' value ' is present in ' arr2 [ ] ' ; required count of pairs ; Driver Code
< ? 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 countPairs ( $ arr1 , $ arr2 , $ m , $ n , $ x ) { $ count = 0 ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) { $ value = $ x - $ arr1 [ $ i ] ; if ( isPresent ( $ arr2 , 0 , $ n - 1 , $ value ) ) $ count ++ ; } return $ count ; } $ arr1 = array ( 1 , 3 , 5 , 7 ) ; $ arr2 = array ( 2 , 3 , 5 , 8 ) ; $ m = count ( $ arr1 ) ; $ n = count ( $ arr2 ) ; $ x = 10 ; echo " Count ▁ = ▁ " , countPairs ( $ arr1 , $ arr2 , $ m , $ n , $ x ) ; ? >