Problem Details
Count triplets in a sorted doubly linked list whose sum is equal to a given value x


1. If no such triplets exist, return zero.
2. At least three elements will always be present in the linked list.
The first line of input contains an integer ‘T’ denoting the number of test cases.
The next ‘2*T’ lines represent the ‘T’ test cases.
The first line of each test case contains space-separated integers denoting the nodes of the linked list. Each line is guaranteed to have -1 at the end to signify the end of the linked list.
The second line of each test case contains a single integer 'X' denoting the value of triplet sum.
For each test case, print an integer denoting the count of the triplets.
The output of every test case will be printed in a separate line.
You don’t have to print anything, it has already been taken care of. Just implement the given function.
1 <= 'T' <= 10
3 <= 'N' <= 100
1 <= 'X' <= 10^8
1 <= 'V' <= 10^8
Where 'V' denotes any linked list element.
Time Limit: 1 sec
The idea is to traverse the linked list using three nested loops,
Such each possible combination is tried and checked whether it sums up to the value X and, if it does, increment the counter for the result.
The idea is to create a hashmap with (key, value) pair in which the key will be the DLLNODE.DATA and value will be a pointer to that node, further traverse the doubly linked list and store each node’s data and its pointer pair in the hash map and check whether (X-PAIRSUM) exists in the hash map or not and If it exists, then also verify that the two nodes in the pair are not same to the node associated with (X-PAIRSUM) in the hash table. Finally, return COUNT/3 as the answer.
The idea is to have two pointers in which we iterate through the linked list, checking each value in the COUNTPAIRS function. A COUNTPAIRS function counts the number of pairs whose sum is equal to the given value and then traverse the doubly linked list from left to right.