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 value1 ; for each arr1 [ i ] ; check if the ' value ' is present in ' arr2 [ ] ' ; required count of pairs ; Driver Code
function isPresent ( arr , low , high , value ) { while ( low <= high ) { let mid = Math . floor ( ( 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 ) { let count = 0 ; for ( let i = 0 ; i < m ; i ++ ) { let value = x - arr1 [ i ] ; if ( isPresent ( arr2 , 0 , n - 1 , value ) ) count ++ ; } return count ; } let arr1 = [ 1 , 3 , 5 , 7 ] ; let arr2 = [ 2 , 3 , 5 , 8 ] ; let m = arr1 . length ; let n = arr2 . length ; let x = 10 ; document . write ( " " + countPairs ( arr1 , arr2 , m , n , x ) ) ;