- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

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−4−1−2 and x is 6 then count will be 1 (triplet (3,1,2))

**For Example**

## Input

linked list: [ 200−4−16−5−10−10−2 ] x=200

## Output

Count of triplets in a sorted doubly linked list whose product is equal to a given value x are: 3

## Explanation

Triplets will be: (4,5,10), (4,5,10) and (10,10,2)

## Input

linked list: [ 4−3−1−5−2−4−2] x=12

## Output

Count of triplets in a sorted doubly linked list whose product is equal to a given value x are: 3

## Explanation

Triplets will be: (4,3,1), (3,1,4) and (3,2,2)

**Approach used in the below program is as follows** −

In this approach.

We are taking a linked list node as struct containing int data part and self−referential next and previous pointers

Function insert_node(struct block** head, int data) adds the node at the head of linked list with data.

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.

Take the initial count as 0.

Take three pointers temp_1, temp_2 and temp_3 of type struct block.

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.

Traverse using these pointers until the last node.

If current data parts of all above pointers have product as x. ((temp_1−>data * temp_2− >data * temp_3−>data) == x) then increment count.

At the end we counted the number of triplets and stored in count.

Return count as result.

## Example

#include <iostream> using namespace std; struct block{ int data; struct block *next, *prev; }; void insert_node(struct block** head, int data){ struct block* ptr = new block(); ptr−>data = data; ptr−>next = NULL; ptr−>prev = NULL; if ((*head) == NULL){ (*head) = ptr; } else { ptr−>next = *head; (*head)−>prev = ptr; (*head) = ptr; } } int Product_x(struct block* head, int x){ int count = 0; struct block *temp_1, *temp_2, *temp_3; for (temp_1 = head; temp_1!= NULL; temp_1 = temp_1−>next){ for (temp_2 = temp_1−>next; temp_2 != NULL; temp_2 = temp_2−>next){ for (temp_3 = temp_2−>next; temp_3!= NULL; temp_3 = temp_3−>next){ if ((temp_1−>data * temp_2−>data * temp_3−>data) == x){ count++; } } } } return count; } int main(){ struct block* head = NULL; insert_node(&head, 200); insert_node(&head, 100); insert_node(&head, 16); insert_node(&head, 14); insert_node(&head, 10); insert_node(&head, 10); insert_node(&head, 2); int x = 200; cout<<"Count of triplets in a sorted doubly linked list whose product is equal to a given value x are: "<<Product_x(head, x); return 0; }

## Output

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

Count of triplets in a sorted doubly linked list whose product is equal to a given value x are : 1

- Related Articles
- Count triplets in a sorted doubly linked list whose sum is equal to a given value x in C++
- Find pairs with given product in a sorted Doubly Linked List in C++
- Count pairs from two linked lists whose product is equal to a given value in C++
- Find pairs with given product in a sorted Doubly Linked List in Python
- Count quadruples from four sorted arrays whose sum is equal to a given value x in C++
- Count pairs from two sorted arrays whose sum is equal to a given value x in C++
- Count pairs from two linked lists whose sum is equal to a given value in C++
- Count pairs in a binary tree whose sum is equal to a given value x in C++
- Count number of triplets with product equal to given number in C++
- C++ Program to Implement Sorted Doubly Linked List
- Count pairs from two BSTs whose sum is equal to a given value x in C++
- Count all triplets whose sum is equal to a perfect cube in C++
- C++ Program to Implement Sorted Circularly Doubly Linked List
- Count number of triplets with product equal to given number with duplicates allowed in C++
- Delete a Doubly Linked List node at a given position in C++