Count triplets in a sorted doubly linked list whose product is equal to a given value x | Python3 implementation to count triplets in a sorted doubly linked list whose product is equal to a given value 'x ; Structure of node of doubly linked list ; Function to count pairs whose product equal to given 'value ; The loop terminates when either of two pointers become None , or they cross each other ( second . next == first ) , or they become same ( first == second ) ; Pair found ; Increment count ; Move first in forward direction ; Move second in backward direction ; If product is greater than ' value ' move second in backward direction ; Else move first in forward direction ; Required count of pairs ; Function to count triplets in a sorted doubly linked list whose product is equal to a given value 'x ; If list is empty ; Get pointer to the last node of the doubly linked list ; Traversing the doubly linked list ; For each current node ; Count pairs with product ( x / current . data ) in the range first to last and add it to the ' count ' of triplets ; Required count of triplets ; A utility function to insert a new node at the beginning of doubly linked list ; Allocate node ; Put in the data ; Driver code ; Start with an empty doubly linked list ; Insert values in sorted order
' NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE self . prev = None NEW_LINE DEDENT DEDENT ' NEW_LINE def countPairs ( first , second , value ) : NEW_LINE INDENT count = 0 NEW_LINE while ( first != None and second != None and first != second and second . next != first ) : NEW_LINE INDENT if ( ( first . data * second . data ) == value ) : NEW_LINE INDENT count += 1 NEW_LINE first = first . next NEW_LINE second = second . prev NEW_LINE DEDENT elif ( ( first . data * second . data ) > value ) : NEW_LINE INDENT second = second . prev NEW_LINE DEDENT else : NEW_LINE INDENT first = first . next NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT ' NEW_LINE def countTriplets ( head , x ) : NEW_LINE INDENT if ( head == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT count = 0 NEW_LINE last = head NEW_LINE while ( last . next != None ) : NEW_LINE INDENT last = last . next NEW_LINE DEDENT current = head NEW_LINE while current != None : NEW_LINE INDENT first = current . next NEW_LINE count += countPairs ( first , last , x // current . data ) NEW_LINE current = current . next NEW_LINE DEDENT return count NEW_LINE DEDENT def insert ( head , data ) : NEW_LINE INDENT temp = Node ( data ) NEW_LINE temp . data = data NEW_LINE temp . next = temp . prev = None NEW_LINE if ( ( head ) == None ) : NEW_LINE INDENT ( head ) = temp NEW_LINE DEDENT else : NEW_LINE INDENT temp . next = head NEW_LINE ( head ) . prev = temp NEW_LINE ( head ) = temp NEW_LINE DEDENT return head NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = None NEW_LINE head = insert ( head , 9 ) NEW_LINE head = insert ( head , 8 ) NEW_LINE head = insert ( head , 6 ) NEW_LINE head = insert ( head , 5 ) NEW_LINE head = insert ( head , 4 ) NEW_LINE head = insert ( head , 2 ) NEW_LINE head = insert ( head , 1 ) NEW_LINE x = 8 NEW_LINE print ( " Count ▁ = ▁ " + str ( countTriplets ( head , x ) ) ) NEW_LINE DEDENT