Count pairs from two linked lists whose product 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 product 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 = 20</pre><p><strong>Output</strong>&nbsp;</p><pre class="result notranslate">Count of pairs from two linked lists whose product 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: (5, 6) = 30(not equals to k), (5, 4) = 20(equals to k), (5, 3) = 15(not equals to k), (5, 2) = 10(not equals to k), (5, 0) = 0(not equals to k), (7, 6) = 42(not equals to k), (7, 4) = 28(not equals to k), (7, 3) = 21(not equals to k), (7, 2) = 14(not equals to k), (7, 0) = 0(not equals to k), (8, 6) = 48(not equals to k), (8, 4) = 32(not equals to k), (8, 3) = 24(not equals to k), (8, 2) = 16(not equals to k), (8, 0) = 0(not equals to k), (10, 6) = 60(not equals to k), (10, 4) = 40(not equals to k), (10, 3) = 30(not equals to k), (10, 2) = 20(not equals to k), (10, 0) = 0(not equals to k), (11, 6) = 66(not equals to k), (11, 4) = 44(not equals to k), (11, 3) = 3(not equals to k), (11, 2) = 22(not equals to k), (11, 0) = 0(not equals to k). So, clearly there are 2 pairs which are equal to the given product.</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 = 9</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: 1</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) = 12(not equals to k), (2, 4) = 8(not equals to k), (2, 3) = 6(not equals to k), (3, 6) = 18(not equals to k), (3, 4) = 12(not equals to k), (3, 3) = 9(equals to k), (5, 6) = 30(not equals to k), (5, 4) = 20(not equals to k), (5, 3) = 15(not equals to k), (6, 6) = 36(not equals to k), (6, 4) = 24(not equals to k), (6, 3) = 18(not equals to k),. So, clearly there is 1 pair which is 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 product.</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/G3vJf4kn" 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 product_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 = 30; &nbsp; &nbsp;cout&lt;&lt;&quot;Count of pairs from two linked lists whose product is equal to a given value k are: &quot;&lt;&lt;product_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 product is equal to a given value k are: 2</pre>
raja
Updated on 02-Nov-2020 06:08:56

Advertisements