# Count pairs from two linked lists whose sum is equal to a given value in C++

C++Server Side ProgrammingProgramming

We are given with two linked lists and the task is to form the pairs using the integer elements of linked lists such that their sum is equal to a given value which is let’s say, k. A linked list is a sequence of data structures, which are connected together via links.

Input

vector v_1 = {5, 7, 8, 10, 11},.
vector v_2 = {6, 4, 3, 2, 0} , int k = 11

Output

Count of pairs from two linked lists whose sum is equal to a given value k are: 4

Explanation

The pairs which can be formed using the given linked lists are: (5, 6) = 11(equals to k), (5, 4) = 9(not equals to k), (5, 3) = 8(not equals to k), (5, 2) = 7(not equals to k), (5, 0) = 5(not equals to k), (7, 6) = 13(not equals to k), (7, 4) = 11(equals to k), (7, 3) = 10(not equals to k), (7, 2) = 9(not equals to k), (7, 0) = 7(not equals to k), (8, 6) = 14(not equals to k), (8, 4) = 12(not equals to k), (8, 3) = 11(equals to k), (8, 2) = 10(not equals to k), (8, 0) = 8(not equals to k), (10, 6) = 16(not equals to k), (10, 4) = 14(not equals to k), (10, 3) = 13(not equals to k), (10, 2) = 12(not equals to k), (10, 0) = 10(not equals to k), (11, 6) = 17(not equals to k), (11, 4) = 15(not equals to k), (11, 3) = 14(not equals to k), (11, 2) = 13(not equals to k), (11, 0) = 11(not equals to k). So, clearly there are 3 pairs which are equal to the given sum.

Input

vector v_1 = {2, 3, 5, 6},.
vector v_2 = {6, 4, 3} , int k = 6

Output

Count of pairs from two linked lists whose sum is equal to a given value k are: 2

Explanation

The pairs which can be formed using the given linked lists are: (2, 6) = 8(not equals to k), (2, 4) = 6(equals to k), (2, 3) = 5(not equals to k), (3, 6) = 9(not equals to k), (3, 4) = 7(not equals to k), (3, 3) = 6(equals to k), (5, 6) = 11(not equals to k), (5, 4) = 9(not equals to k), (5, 3) = 8(not equals to k), (6, 6) = 12(not equals to k), (6, 4) = 10(not equals to k), (6, 3) = 9(not equals to k),. So, clearly there are 2 pairs which are equal to the given sum.

## Approach used in the below program is as follows

• Input the value of k and integer type values into two vectors such that we can pass the vectors to form a linked list

• Create a function that will create a linked list using the vector passed as an argument to the function.

• Traverse the loop till the size of a vector and create a pointer object of class

ListNode

• Traverse the loop while ptr-> next not equals to NULL and set ptr to ptr->next

• Inside the ptr->next set vector[i]

• Return start

• Create a function that will return the count of pairs matching with the given sum.

• Take a temporary variable count and set it to 0

• Create two pointer object i.e. *first_list for first linked list and *second_list for second linked list.

• Start loop from start pointer of first list till the list is not empty

• Inside the loop, start another loop from start pointer of second list till the list is not empty

• Inside the loop, check IF (first_list->data + second_list->data) == k then increment the count by 1

• Return the count

• Print the result.

## Example

Live Demo

#include<bits/stdc++.h>
using namespace std;
class ListNode{
public:
int data;
ListNode *next;
ListNode(int data){
this->data = data;
next = NULL;
}
};
ListNode *CreateList(vector v){
ListNode *start = new ListNode(v[0]);
for (int i = 1; i < v.size(); i++){
ListNode *ptr = start;
while (ptr->next != NULL){
ptr = ptr->next;
}
ptr->next = new ListNode(v[i]);
}
return start;
}
int sum_pair(ListNode *start_1, ListNode *start_2, int k){
int count = 0;
ListNode *first_list , *second_list;
for (first_list = start_1; first_list != NULL; first_list = first_list->next){
for (second_list = start_2; second_list != NULL; second_list = second_list->next){
if ((first_list->data + second_list->data) == k){
count++;
}
}
}
return count;
}
int main(){
vector<int> v_1 = {5, 7, 8, 10, 11};
ListNode* start_1 = CreateList(v_1);
vector v_2 = {6, 4, 3, 2, 0};
ListNode* start_2 = CreateList(v_2);
int k = 11;
cout<<"Count of pairs from two linked lists whose sum is equal to a given value k are: "<<sum_pair(start_1, start_2, k);
}

## Output

If we run the above code it will generate the following output −

Count of pairs from two linked lists whose sum is equal to a given value k are: 4
Published on 02-Nov-2020 06:05:58