Count Pairs whose sum is equal to X | Practice | GeeksforGeeks

Given two linked list of size N1 and N2 respectively of distinct elements, your task is to complete the function countPairs(), which returns the count of all pairs from both lists whose sum is equal to the given value X.
Note: The 2 numbers of a pair should be parts of different lists.
Example 1:
Input:
L1 = 1->2->3->4->5->6
L2 = 11->12->13
X = 15
Output: 3
Explanation: There are 3 pairs that
add up to 15 : (4,11) , (3,12) and (2,13)

Example 2:
Input:
L1 = 7->5->1->3
L2 = 3->5->2->8
X = 10
Output: 2
Explanation: There are 2 pairs that add up
to 10 : (7,3) and (5,5)
Your Task:
You only need to implement the given function countPairs() and return the count.
Expected Time Complexity: O(N+M)
Expected Auxiliary Space: O(N+M)
Constraints:
1<=size of linked list<=10000
1<=X<=10000
Note : All elements in a linked list are unique.
+1
shahabuddinbravo405 days ago
class Solution{ public:      int countPairs(struct Node* head1, struct Node* head2, int x) {             Node *ptr1=head1,*ptr2=head2;            unordered_set<int>s;            while(ptr2)      {          s.insert(ptr2->data);          ptr2=ptr2->next;      }            int pair=0;      while(ptr1)      {        auto it=s.find(x-ptr1->data);          if(it!=s.end()){              pair++;          }          ptr1=ptr1->next;      }            return pair;   }};
0
arjunaryagupta2031 week ago
#Python Code
#Using Hashing 
        dic={}       temp=h1       while temp!=None:           if temp.data in dic:               dic[temp.data]+=1           elif temp.data not in dic:               dic[temp.data]=1                      temp=temp.next                     count=0       while h2!=None:           if x-h2.data in dic:               count=count+dic.get(x-h2.data)           h2=h2.next                  return count               
 
+1
sammy101 week ago
 void solve(struct Node* head,vector<int>&v){       if(head == NULL){           return;       }       struct Node* temp = head;       while(temp != NULL){           v.push_back(temp->data);           temp = temp->next;       }   }   int countPairs(struct Node* head1, struct Node* head2, int x) {       // Code here       vector<int>v;       vector<int>r;       solve(head1,v);       solve(head2,r);       sort(v.begin(),v.end());       sort(r.begin(),r.end());       int n = v.size();       int m = r.size();       int i=0;       int j=m-1;       int count = 0;       while(i!=n && j!=-1){           int sum = v[i] + r[j];           if(sum == x){               count++;           }           if(sum > x){               j--;           }else{               i++;           }       }              return count;   }
+1
velspace012 weeks ago
java sol
 HashMap<Integer,Integer> hm=new HashMap<>();
        for(int i=0;i<head1.size();i++){
            hm.put(head1.get(i),i);
        }
        int count=0;
        for(int i=0;i<head2.size();i++){
            int num=x-head2.get(i);
            if(hm.containsKey(num)){
                count++;
            }
        }
        return count;
0
tthakare732 weeks ago
//java Solution 
public static int countPairs(LinkedList<Integer> head1, LinkedList<Integer> head2, int x) {
        // add your code here
        HashSet<Integer> map = new HashSet<>();
        int count = 0;
        
        for (int i = 0; i < head1.size(); i++) {
            map.add(x - head1.get(i));
        }
        
        for (int i = 0; i < head2.size(); i++) {
             if(map.contains(head2.get(i))) count++;
        }
        return count;
    }
+1
vikulol1 month ago
class Solution{
  public:
    int countPairs(struct Node* head1, struct Node* head2, int x) {
        
        unordered_map<int,int> umap;
        Node* end = head1;
        while(end != NULL)
        {
            umap[end->data] = 0;
            end = end->next;
        }
        Node* start = head2;
        int count = 0;
        while(start != NULL)
        {
            if(umap.find(x - (start->data)) != umap.end())
            {
                count++;
            }
            
            start = start->next;
        }
        
        return count;
    }
};
0
as0042301 month ago
In C++;
    int countPairs(struct Node* head1, struct Node* head2, int x) {       int count = 0;       unordered_set<int> s;              while(head1){           s.insert(head1->data);           head1 = head1->next;       }              while(head2){           if(s.find(x - head2->data) != s.end())               count++;           head2 = head2->next;       }       return count;   }
0
tarundhiman851 month ago
java 8 code:
public static int countPairs(LinkedList<Integer> head1, LinkedList<Integer> head2,int x) {       Set<Integer> set = new HashSet<>(head1);       long count = head2.stream().filter(ele -> set.contains(x - ele)).count();       return (int)count;   }
0
thiruvazhidhinesh1 month ago
JAVA SOLUTION: 
class Solution {
   public static int countPairs(LinkedList<Integer> head1, LinkedList<Integer> head2, int x) {        
        int count=0;        Map<Integer ,Integer> map=new HashMap<>();        Iterator<Integer> i=head1.iterator();        while(i.hasNext()){            int n=i.next();            map.put(x-n,n);        }        Iterator<Integer> i2=head2.iterator();        while(i2.hasNext()){            if(map.containsKey(i2.next())){                count++;            }        }        return count;           }}
0
mashhadihossain2 months ago
SIMPLE JAVA SOLUTION
class Solution {
   public static int countPairs(LinkedList<Integer> head1, LinkedList<Integer> head2,                         int x) {       HashSet<Integer> set=new HashSet<Integer>();       int count=0;       for(int i=0;i<head1.size();i++)       {           set.add(x-head1.get(i));       }       for(int val : head2)       {           if(set.contains(val))           {               count++;           }       }       return count;   }} 
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.