Count triplets in a sorted doubly linked list whose product is equal to a given value x in C++

C++Server Side ProgrammingProgramming

<p>Given a sorted doubly linked list containing integer values. The goal is to find triplets whose product is equal to the given value x. If input linked list is 3&minus;4&minus;1&minus;2 and x is 6 then count will be 1 (triplet (3,1,2))</p><p style=""><img src="https://www.tutorialspoint.com/assets/questions/media/46990/gates.jpg" class="fr-fic fr-dib" width="457" height="98"></p><p><strong>For Example</strong></p><h2 style="">Input</h2><pre class="result notranslate">linked list: [ 200&minus;4&minus;16&minus;5&minus;10&minus;10&minus;2 ] x=200</pre><h2>Output</h2><pre class="result notranslate">Count of triplets in a sorted doubly linked list whose product is equal to a given value x are: 3</pre><h2>Explanation</h2><pre class="result notranslate">Triplets will be: (4,5,10), (4,5,10) and (10,10,2)</pre><h2>Input</h2><pre class="result notranslate">linked list: [ 4&minus;3&minus;1&minus;5&minus;2&minus;4&minus;2] x=12</pre><h2>Output</h2><pre class="result notranslate">Count of triplets in a sorted doubly linked list whose product is equal to a given value x are: 3</pre><h2>Explanation</h2><pre class="result notranslate">Triplets will be: (4,3,1), (3,1,4) and (3,2,2)</pre><p><strong>Approach used in the below program is as follows</strong> &minus;</p><p>In this approach.</p><ul class="list"><li><p>We are taking a linked list node as struct containing int data part and self&minus;referential next and previous pointers</p></li><li><p>Function insert_node(struct block** head, int data) adds the node at the head of linked list with data.</p></li><li><p>Function Product_x(struct block* head, int x) takes the pointer to the head of doubly linked list and integer x and returns the count of triplets of nodes having product of data part as x.</p></li><li><p>Take the initial count as 0.</p></li><li><p>Take three pointers temp_1, temp_2 and temp_3 of type struct block.</p></li><li><p>Starting from temp_1 pointing to the head of the linked list, temp_2 pointing to next to temp_1 and temp_3 pointing to the next to temp_3, we have three pointers pointing to the first three nodes.</p></li><li><p>Traverse using these pointers until the last node.</p></li><li><p>If current data parts of all above pointers have product as x. ((temp_1&minus;&gt;data * temp_2&minus; &gt;data * temp_3&minus;&gt;data) == x) then increment count.</p></li><li><p>At the end we counted the number of triplets and stored in count.</p></li><li><p>Return count as result.</p></li></ul><h2>Example</h2><p><a class="demo" href="http://tpcg.io/8TqnmmDC" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">#include &lt;iostream&gt; using namespace std; struct block{ &nbsp; &nbsp;int data; &nbsp; &nbsp;struct block *next, *prev; }; void insert_node(struct block** head, int data){ &nbsp; &nbsp;struct block* ptr = new block(); &nbsp; &nbsp;ptr&minus;&gt;data = data; &nbsp; &nbsp;ptr&minus;&gt;next = NULL; &nbsp; &nbsp;ptr&minus;&gt;prev = NULL; &nbsp; &nbsp;if ((*head) == NULL){ &nbsp; &nbsp; &nbsp; (*head) = ptr; &nbsp; &nbsp;} else { &nbsp; &nbsp; &nbsp; ptr&minus;&gt;next = *head; &nbsp; &nbsp; &nbsp; (*head)&minus;&gt;prev = ptr; &nbsp; &nbsp; &nbsp; (*head) = ptr; &nbsp; &nbsp;} } int Product_x(struct block* head, int x){ &nbsp; &nbsp;int count = 0; &nbsp; &nbsp;struct block *temp_1, *temp_2, *temp_3; &nbsp; &nbsp;for (temp_1 = head; temp_1!= NULL; temp_1 = temp_1&minus;&gt;next){ &nbsp; &nbsp; &nbsp; for (temp_2 = temp_1&minus;&gt;next; temp_2 != NULL; temp_2 = temp_2&minus;&gt;next){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for (temp_3 = temp_2&minus;&gt;next; temp_3!= NULL; temp_3 = temp_3&minus;&gt;next){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((temp_1&minus;&gt;data * temp_2&minus;&gt;data * temp_3&minus;&gt;data) == x){ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;count++; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp;} &nbsp; &nbsp;return count; } int main(){ &nbsp; &nbsp;struct block* head = NULL; &nbsp; &nbsp;insert_node(&amp;head, 200); &nbsp; &nbsp;insert_node(&amp;head, 100); &nbsp; &nbsp;insert_node(&amp;head, 16); &nbsp; &nbsp;insert_node(&amp;head, 14); &nbsp; &nbsp;insert_node(&amp;head, 10); &nbsp; &nbsp;insert_node(&amp;head, 10); &nbsp; &nbsp;insert_node(&amp;head, 2); &nbsp; &nbsp;int x = 200; &nbsp; &nbsp;cout&lt;&lt;&quot;Count of triplets in a sorted doubly linked list whose product is equal to a given value &nbsp; &nbsp;x are: &quot;&lt;&lt;Product_x(head, x); &nbsp; &nbsp;return 0; }</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 triplets in a sorted doubly linked list whose product is equal to a given value x are : 1</pre>
raja
Updated on 05-Jan-2021 05:09:32

Advertisements