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

C++Server Side ProgrammingProgramming

<p>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&rsquo;s say, k. A linked list is a sequence of data structures, which are connected together via links.</p><p><strong>Input</strong>&nbsp;</p><pre class="result notranslate">vector v_1 = {5, 7, 8, 10, 11},. vector v_2 = {6, 4, 3, 2, 0} , int k = 11</pre><p><strong>Output</strong>&nbsp;</p><pre class="result notranslate">Count of pairs from two linked lists whose sum is equal to a given value k are: 4</pre><p><strong>Explanation</strong>&nbsp;</p><pre class="result notranslate">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.</pre><p><strong>Input</strong>&nbsp;</p><pre class="result notranslate">vector v_1 = {2, 3, 5, 6},. vector v_2 = {6, 4, 3} , int k = 6</pre><p><strong>Output</strong>&nbsp;</p><pre class="result notranslate">Count of pairs from two linked lists whose sum is equal to a given value k are: 2</pre><p><strong>Explanation</strong>&nbsp;</p><pre class="result notranslate">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.</pre><h2>Approach used in the below program is as follows</h2><ul class="list"><li><p>Input the value of k and integer type values into two vectors such that we can pass the vectors to form a linked list</p></li><li><p>Create a function that will create a linked list using the vector passed as an argument to the function.</p></li><li><p>Traverse the loop till the size of a vector and create a pointer object of class</p><p>ListNode</p><ul class="list"><li><p>Traverse the loop while ptr-&gt; next not equals to NULL and set ptr to ptr-&gt;next</p></li><li><p>Inside the ptr-&gt;next set vector[i]</p></li><li><p>Return start</p></li></ul></li><li><p>Create a function that will return the count of pairs matching with the given sum.</p><ul class="list"><li><p>Take a temporary variable count and set it to 0</p></li><li><p>Create two pointer object i.e. *first_list for first linked list and *second_list for second linked list.</p></li><li><p>Start loop from start pointer of first list till the list is not empty</p></li><li><p>Inside the loop, start another loop from start pointer of second list till the list is not empty</p></li><li><p>Inside the loop, check IF (first_list-&gt;data + second_list-&gt;data) == k then increment the count by 1</p></li><li><p>Return the count</p></li></ul></li><li><p>Print the result.</p></li></ul><h2>Example</h2><p><a class="demo" href="http://tpcg.io/9iz6E543" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">#include&lt;bits/stdc++.h&gt; using namespace std; class ListNode{ public: &nbsp; &nbsp;int data; &nbsp; &nbsp;ListNode *next; &nbsp; &nbsp;ListNode(int data){ &nbsp; &nbsp; &nbsp; this-&gt;data = data; &nbsp; &nbsp; &nbsp; next = NULL; &nbsp; &nbsp;} }; ListNode *CreateList(vector v){ &nbsp; &nbsp;ListNode *start = new ListNode(v[0]); &nbsp; &nbsp;for (int i = 1; i &lt; v.size(); i++){ &nbsp; &nbsp; &nbsp; ListNode *ptr = start; &nbsp; &nbsp; &nbsp; while (ptr-&gt;next != NULL){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ptr = ptr-&gt;next; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; ptr-&gt;next = new ListNode(v[i]); &nbsp; &nbsp;} &nbsp; &nbsp;return start; } int sum_pair(ListNode *start_1, ListNode *start_2, int k){ &nbsp; &nbsp;int count = 0; &nbsp; &nbsp;ListNode *first_list , *second_list; &nbsp; &nbsp;for (first_list = start_1; first_list != NULL; first_list = first_list-&gt;next){ &nbsp; &nbsp; &nbsp; for (second_list = start_2; second_list != NULL; second_list = second_list-&gt;next){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ((first_list-&gt;data + second_list-&gt;data) == k){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp;} &nbsp; &nbsp;return count; } int main(){ &nbsp; &nbsp;vector&lt;int&gt; v_1 = {5, 7, 8, 10, 11}; &nbsp; &nbsp;ListNode* start_1 = CreateList(v_1); &nbsp; &nbsp;vector v_2 = {6, 4, 3, 2, 0}; &nbsp; &nbsp;ListNode* start_2 = CreateList(v_2); &nbsp; &nbsp;int k = 11; &nbsp; &nbsp;cout&lt;&lt;&quot;Count of pairs from two linked lists whose sum is equal to a given value k are: &quot;&lt;&lt;sum_pair(start_1, start_2, k); }</pre><h2>Output</h2><p>If we run the above code it will generate the following output &minus;</p><pre class="result notranslate">Count of pairs from two linked lists whose sum is equal to a given value k are: 4</pre>
raja
Updated on 02-Nov-2020 06:06:40

Advertisements