You have been given an array/list(ARR) of positive and negative integers. Print the number of pairs where:
arr[i] = -arr[j] and i != j
Note:
Given array/list can contain duplicate elements and will not contain '0'.
(arr[i],arr[j]) and (arr[j],arr[i]) are considered same.
The first line of each test case contains an integers 'N' where 'N' denotes the size of array/list(ARR).
The next line contains 'N' space-separated integers representing array elements.
Output format :
Print the total number of positive-negative pairs present in the array/list.
Note:
You are not required to print the output explicitly, it has already been taken care of. Just implement the function.
1 <= N <= 10^5
-10^9 <= arr[i] <= 10^9
Time Limit: 1 sec
9
-1 3 6 2 5 -4 3 2 -4
0
Since there doesn't exist any positive-negative pair, the Output is '0'.
6
-2 8 2 5 -2 -5
3
Try to explore every possible pair and check for the required condition.
O(N^2), where N is the size of the array.
As we are running two nested loops of size N.
O(1)
As constant extra space, is used.