Count pairs ( i , j ) from an array such that | arr [ i ] | and | arr [ j ] | both lies between | arr [ i ] | Function to find pairs ( i , j ) such that | arr [ i ] | and | arr [ j ] | lies in between | arr [ i ] - arr [ j ] | and | arr [ i ] + arr [ j ] | ; Calculate absolute value of all array elements ; Sort the array ; Stores the count of pairs ; Traverse the array ; Increment left ; Add to the current count of pairs ; Print the answer ; Driver Code
def findPairs ( arr , N ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT arr [ i ] = abs ( arr [ i ] ) NEW_LINE DEDENT arr . sort ( ) NEW_LINE left = 0 NEW_LINE ans = 0 NEW_LINE for right in range ( N ) : NEW_LINE INDENT while ( 2 * arr [ left ] < arr [ right ] ) : NEW_LINE INDENT left += 1 NEW_LINE DEDENT ans += ( right - left ) NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 3 , 5 , 7 ] NEW_LINE N = len ( arr ) NEW_LINE findPairs ( arr , N ) NEW_LINE DEDENT