Hackerrank: Pairs
- Hackerrank problem link: link
- Two pointer approach: Github gist
- Binary search approach: Github girst
I solved this problem by two pointer approach at first. However, when I read the editorial, it mentioned using binary search tree. I found that is really clever, and decided to try that binary search tree to compare how fast it can run.
My code is on Github gist. Look them up if you are interested in how I wrote them. If you are interested in performance difference, keep reading.
This is the result with two pointer implementation:
And here is the binary search implementation result:
Look at how fast binary search tree runs! This is amazing!! It runs by 0s for some test cases.
It is awesome that if you pick a right solution, your program solve the problem in incredible speed!