Given a BST and a sum, find if there is a pair with given sum.
We have discussed different approaches to find a pair with given sum in below post.Find a pair with given sum in a Balanced BST
In this post, hashing based solution is discussed. We traverse binary search tree by inorder way and insert node’s value into a set. Also check for any node, difference between given sum and node’s value in set, if it is found then pair exists otherwise it doesn’t exist.
Pair is found (8, 25)
Time Complexity is O(n).
- Pair with given product | Set 1 (Find if any pair exists)
- Find a pair with given sum in a Balanced BST
- Find pairs with given sum such that elements of pair are in different rows
- Find pair with greatest product in array
- Find pairs with given sum such that pair elements lie in different BSTs
- Find a pair of elements swapping which makes sum of two arrays same
- Find if there is a pair in root to a leaf path with sum equals to root's data
- Pair with given sum in matrix
- Subarray with no pair sum divisible by K
- Linked List Pair Sum
- Pair with given sum and maximum shortest distance from end
- Given an array A and a number x, check for pair in A with sum as x
- Check if a pair with given product exists in a Matrix
- Check if a pair with given product exists in Linked list
- Check if a pair with given absolute difference exists in a Matrix
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.
Improved By : Vikash Kumar 37