If the value at that index is negative then we need to find the index of the value just greater than - D [ i ]
else { let k = upper_bound ( D , 0 , D . length , - D [ i ] ) total += n - k ; } } return total ; } function upper_bound ( a , low , high , element ) { while ( low < high ) { let middle = low + Math . floor ( ( high - low ) / 2 ) ; if ( a [ middle ] > element ) high = middle ; else low = middle + 1 ; } return low ; }