We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I agree with you about the problem of duplicates and I think that makes using arrays with binary search a poor approach. Of course I coded the whole array based solution before realizing this.
I think the better solution is to create a binary search tree as you read the costs in. Each node in the tree has the original index and the cost. Every time you read a new cost from stdin, first search the tree to see if it's matching pair is already there. If it's not, then insert it to the tree.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Ice Cream Parlor
You are viewing a single comment's thread. Return to all comments →
I agree with you about the problem of duplicates and I think that makes using arrays with binary search a poor approach. Of course I coded the whole array based solution before realizing this.
I think the better solution is to create a binary search tree as you read the costs in. Each node in the tree has the original index and the cost. Every time you read a new cost from stdin, first search the tree to see if it's matching pair is already there. If it's not, then insert it to the tree.