Replace each node with its Surpasser Count in Linked List
Given LinkedList, replace each node’s value with its surpasser count. That is the count of elements which are greater towards its right.
Input : 10->12->5->40->21->70->1->49->37->NULL
Output : 6->5->5->2->3->0->2->0->0->NULL
Element in the first node is 10 and the number of elements to the right of the node that are greater than 10 is 6. Therefore replace the node with 6.
Element in the first node is 12 and the number of elements to the right of the node that are greater than 12 is 5. Therefore replace the node with 5.
Similarly, replace for all the elements in the list.
Input : 5->4->6->3->2->NULL
Output : 1->1->0->0->0->NULL
- Take two pointers p and x. The pointer p is used to traverse the list and x is used to traverse the right half of the list for every node.
- Initialize a variable count to count the nodes greater than the current nodes.
- Traverse through all the nodes in the list using the pointer p.
- Initialize the count to 0.
- Initialize the pointer x to point the current node p.
- Count the number of nodes that are greater than the current node.
- Replace the current node with the count.
- Repeat step 4 until the list is traversed completely.
Below is the implementation of the above approach:
6 5 5 2 3 0 2 0 0
- Time Complexity: O(N2) where N is the number of nodes in the linked list.
- Auxiliary Space: O(1)
Another Approach with PBDS and Hash map
We can list down our requirements and approach to observe and find a solution. Our approach:
- Traverse the linked list from backwards (so we do not concern about elements to the left of current)
- Store the current element in a sorted data structure (any point of time, elements are sorted in our data structure)
- Find total number of elements greater than the current element in our sorted data structure and replace the current element with it
To traverse the linked list backwards, we will use recursion.
We can use PBDS. With PBDS we can find elements strictly smaller than a particular key. With strictly smaller, we can add current element and subtract it from all the elements. So basically:
Greater elements than current = Total Elements in PBDS – (Query in PBDS to find elements strictly smaller) – (Current element + element equal to current)
PBDS does not allow duplicate elements. But for the counting part, we need duplicate elements. So we will insert a pair in PBDS (first = element, second = index) to make each entry unique. After that, to find total elements equal to the current element, we will use a hash map. In hash map we will store total occurrences of an element (a basic integer to integer map).
NOTE: We can also construct our own data structure that fulfills the current requirements as we stated. Insert for duplicates in sorted order and query for elements just greater than the key. But we will use C++ STL DS to do it in this post.
- Time complexity: O(n*log(n)) as insert and order_of_key of PBDS uses log(n) and hash map takes O(1).
- Space complexity: O(n) as we store the elements in PBDS and hash to count.