The first line contains a single integer ‘T’ denoting the number of test cases. The test cases follow.
The first line of each test case contains a single integer ‘X’.
The second line of the input contains the elements of the doubly linked list separated by a single space and terminated by -1. Hence, -1 would never be a list element.
For each test case, print in a new line a single integer denoting the number of triplets that have the sum equal to ‘X’.
You don’t need to print anything; it has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= N <= 10 ^ 3
- 3 * 10 ^ 5 <= X <= 3 * 10 ^ 5
- 10 ^ 5 <= data <= 10 ^ 5 and data != - 1
Where ‘N’ is the number of nodes in the given linked list, and ‘X’ is the required triplet sum value.
Time limit: 1 sec
The idea is to explore all the triplets and count those triplets which have a sum equal to ‘X’. We will use a variable 'COUNT' which will be initialized to 0, to count the number of triplets with sum ‘X’. The steps are as follows:
The idea is to store nodes that have been explored in the HashSet so that we can effectively check if any particular node exists in the doubly linked list or not and then explore all possible pairs of nodes and then find the third node using the HashSet. We will use a variable 'COUNT' which will be initialized to 0, to count the number of triplets with sum ‘X’.
The steps are as follows:
Since the given doubly linked list is sorted, we can pivot one element, and then we can use the two-pointer technique. We will use a variable 'COUNT' which will be initialised to 0, to count the number of triplets with sum ‘X’.
The algorithm looks like:
Deletion In Doubly Linked List
Deletion In Doubly Linked List
Insertion In Doubly Linked List
LRU Cache
Delete Nodes On Regular Intervals
Add One To Linked List