Given a binary search tree containing N distinct nodes and a value K. The task is to count pairs in the given binary search tree whose sum is greater than the given value K.
Input: 5 / \ 3 7 / \ / \ 2 4 6 8 k = 11 Output: 6 Explanation: There are 6 pairs which are (4, 8), (5, 7), (5, 8), (6, 7), (6, 8) and (7, 8). Input: 8 / \ 3 9 \ / \ 5 6 18 k = 23 Output: 3 Explanation: There are 3 pairs which are (6, 18), (8, 18) and (9, 18).
To solve the problem mentioned above we have to store inorder traversal of BST in an array then run two loops to generate all pairs and one by one check if the current pair’s sum is greater than k or not.
The above method can be optimized if we store the inorder traversal of BST in an array and take the initial and last index of the array in l and r variable to find the total pair in the inorder array. Initially assign l as 0 and r as n-1. Consider a variable and initialize it to zero. This variable result will be our final answer. Now iterate until l < r and if the current left and current right have a sum greater than K, all elements from l+1 to r form a pair with it otherwise it doesn't, therefore, increment current left. Finally, return the result.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Smallest number in BST which is greater than or equal to N
- Smallest number in BST which is greater than or equal to N ( Iterative Approach)
- Print all pairs from two BSTs whose sum is greater than the given value
- K'th Largest Element in BST when modification to BST is not allowed
- Convert a normal BST to Balanced BST
- Convert a BST to a Binary Tree such that sum of all greater keys is added to every key
- Count all Grandparent-Parent-Child Triplets in a binary tree whose sum is greater than X
- Find all the pairs with given sum in a BST | Set 2
- Add all greater values to every node in a given BST
- Largest number in BST which is less than or equal to N
- Count of nodes that are greater than Ancestors
- Queries to count array elements greater than or equal to a given number with updates
- Sum of previous numbers that are greater than current number for given array
- Number of nodes greater than a given value in n-ary tree
- Number of unique paths in tree such that every path has a value greater than K
- Longest subsequence with first and last element greater than all other elements
- Print all paths of the Binary Tree with maximum element in each path greater than or equal to K
- Count BST nodes that lie in a given range
- Count permutations of given array that generates the same Binary Search Tree (BST)
- BST to a Tree with sum of all smaller keys
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.