Positive Negative Pair | Practice | GeeksforGeeks

Given an array of distinct integers, find all the pairs having both negative and positive values of a number in the array.

Example 1:
Input:
n = 8
arr[] = {1,3,6,-2,-1,-3,2,7}
Output: -1 1 -3 3 -2 2
Explanation: 1, 3 and 2 are present 
pairwise positive and negative. 6 and 
7 have no pair.

Example 2:
Input:
n = 3
arr[] = {3,2,1}
Output: 0
Explanation: No such pair exists so the 
output is 0.

Your Task:
You do not need to read input or print anything. Complete the function findPairs() which takes the array A[] and the size of the array, n, as input parameters and returns a list of integers denoting the pairs. The pair that appears first(i.e. second element of the pair appears first) in A[] should appear first in the returning list and within the pair, the negative integer should appear before the positive integer. Return an empty integer list if no such pair exists. 

Expected Time Complexity: O(n)
Expected Auxiliary Space: O(n)

Constraints:
1 <= n <= 106
-106 <= arr[i] <= 106
0
shubham211019971 week ago
public static ArrayList<Integer>findPairs(int a[], int n) 
   { 
       ArrayList<Integer> arr=new  ArrayList<Integer>();
       HashSet<Integer> h=new HashSet<>();
      
       for(int i:a){
           h.add(i);
           if(i!=0 && h.contains(-i) ){
               if(i>0){
                   arr.add(-i);
                   arr.add(i);
               }else{
                   arr.add(i);
               arr.add(-i);
               }
           }
           
       }
       return arr;
   }
0
smilingstar4421 week ago
vector <int> findPairs(int arr[], int n)    {       // code here       unordered_map<int,int>m;       vector<int>v;       for(int i=0;i<n;i++)       {           if(arr[i]<0)           {               arr[i]*=-1;           }           m[arr[i]]++;           if(m[arr[i]]==2)           {               v.push_back(arr[i]*-1);               v.push_back(arr[i]);           }       }       return v;   }
0
anvisinha20111 month ago
  public static ArrayList<Integer>findPairs(int arr[], int n)    {     // code here.    ArrayList<Integer> res=new ArrayList<>();    HashSet<Integer> hs=new HashSet<>();                for(int i=0;i<n;i++)     {          hs.add(arr[i]);         if(arr[i]!=0 && hs.contains(arr[i]) && hs.contains(-arr[i]))         {             if(arr[i]>0)            {                res.add(-arr[i]);               res.add(arr[i]);            }            else            {               res.add(arr[i]);               res.add(-arr[i]);              }         }            }     return res;      }}
0
tarun19151411 month ago
//best and easy to understand solution using map
 vector <int> findPairs(int arr[], int n)    {       // code here       //start traversing array from begin to end       //for each value of array check in map its opposite value if present then push pair  in vector        //and decrese the count of map by 1       vector <int> ans;      unordered_map<int,int> m1;//for +ve value      unordered_map<int,int> m2;//for -ve value      for (int i=0 ; i<n ;i++){                    if(arr[i]>0){              if(m2.find(-arr[i])!=m2.end()){                  if(m2[-arr[i]]>0){                      ans.push_back(-arr[i]);                      ans.push_back(arr[i]);                  }                  m2[-arr[i]]--;              }else m1[arr[i]]++;          }          else{              if(m1.find(-arr[i])!=m1.end()){                  if(m1[-arr[i]]>0){                      ans.push_back(arr[i]);                      ans.push_back(-arr[i]);                  }                  m1[-arr[i]]--;              }else m2[arr[i]]++;          }                 }      return ans;          }
0
amiransarimy1 month ago
Simple Few Lines Python Solutions
 
 def findPairs(self,arr,n):
       resList =[]
       mdict ={}
       
       for num in arr:
           if num in mdict or num*-1 in mdict:
               n1 = abs(num)
               resList.append(n1*-1)
               resList.append(n1)
           
           mdict[num] = num 
       
       return resLis
0
demon49032 months ago
class Solution{ public:   //Function to return list containing all the pairs having both   //negative and positive values of a number in the array.   vector <int> findPairs(int arr[], int n)    {       // code here       unordered_set<int>x;       vector<int>v;       for(int i=0 ; i<n ; i++){           int t=(-1)*arr[i];           if(x.find(t)!=x.end()){               if(arr[i]>0){                   v.push_back(t);                   v.push_back(arr[i]);               }               else{                   v.push_back(arr[i]);                   v.push_back(t);               }           }           else{               x.insert(arr[i]);           }       }       return v;   }};
0
yashwantcm2 months ago

class Solution{
  public:
    //Function to return list containing all the pairs having both
    //negative and positive values of a number in the array.
    vector <int> findPairs(int arr[], int n) 
    {
        // code here
        unordered_map<int,int> mp;
        vector<int> ans;
        for(int i=0;i<n;i++){
            mp[abs(arr[i])]++;
            if(mp[abs(arr[i])]>1){
                ans.push_back(-1*abs(arr[i]));
                ans.push_back(abs(arr[i]));
            }
        }
        
        return ans;
        
    }
};
0
mangalam33312 months ago
// Simple C++ solution.
vector <int> findPairs(int arr[], int n) 
    {
        unordered_set<int> s;
         vector <int> res;
        for(int i=0;i<n;i++)
        {
            if(s.count(-arr[i])==1)
            {
                res.push_back(-abs(arr[i]));
                res.push_back(abs(arr[i]));
            }
            s.insert(arr[i]);
        }
        return res;
    }
0
tarunkanade3 months ago
public static ArrayList<Integer>findPairs(int arr[], int n) 
    { 
    	// code here.
    	HashMap<Integer,Integer> map = new HashMap<>();
    	ArrayList<Integer> res = new ArrayList<>();
    	
    	for(int i : arr){
    	    map.put(Math.abs(i), map.getOrDefault(Math.abs(i),0)+1);
    	    
    	    if(map.get(Math.abs(i)) == 2){
    	        if(i < 0){
    	            res.add(i);
    	            res.add(-i);
    	        }else{
    	            res.add(-i);
    	            res.add(i);
    	        }
    	    }
    	}
    	
    	return res;
    }
0
nimox3 months ago
class Solution
{
    //Function to return list containing all the pairs having both
    //negative and positive values of a number in the array.
    public static ArrayList<Integer>findPairs(int arr[], int n) 
    { 
    	Set<Integer> set = new HashSet<>();
    	
    	ArrayList<Integer> res = new ArrayList<Integer>();
    	
    	for(int i = 0 ; i < n ; i++){
    	    
    	    if(set.contains(-arr[i])){
    	        res.add(-Math.abs(arr[i]));
    	        res.add(Math.abs(arr[i]));
    	    }
    	    
    	    else
    	    set.add(arr[i]);
    	    
    	}
    	return res;
    }
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.