This is the "Pairs with Positive Negative values" problem from geeksforgeeks.org.
Given an array of distinct integers, print all the pairs having positive value and negative value of a number that exists in the array.
NOTE: If there is no such pair in the array , print "0".
Input:
The first line consists of an integer T i.e number of test cases. The first line of each test case consists of an integer n. The next line of each test case consists of n spaced integers.Output:
Print all the required pairs in the increasing order of their absolute numbers.
Here is my code:
def findPairs(l):
listLen=len(l)
nums=set()
for i in range(listLen):
if (-1*l[i]) in l[i+1:]:
nums.add(abs(l[i]))
continue
nums=sorted(nums)
#print (nums)
pairs=[]
for num in nums:
pairs.extend([-1*num,num])
return pairs
T=int(input())
pair=[]
for i in range(T):
input()
pair=findPairs(list(map(int,input().split())))
if len(pair):
print(" ".join(str(x) for x in pair))
else:
print("0")