A hillside vineyard co-operative logs each terrace's grape yield into a running appraisal ledger the moment it is harvested. The ledger is kept as a binary search tree: the first logged yield becomes the root, and every yield after that is walked down from the root — moving left whenever it is smaller than the node it is being compared to, and right whenever it is larger — until it reaches an empty spot where it is planted as a new leaf. All logged yields are distinct.
At the end of the season the co-op wants to know whether two different terraces exist whose yields add up to exactly a target quota; such a pair would jointly qualify for a matching regional subsidy.
Given the yields in the order they were logged (which determines the ledger's shape) and the target quota, determine whether two distinct terraces' yields sum to exactly that quota.
Line 1: two integers n target (1 <= n <= 10000, -1000000 <= target <= 1000000) — the number of terraces logged and the subsidy quota. Line 2: n distinct integers v_1 v_2 ... v_n (-100000 <= v_i <= 100000) — the yields in the order they were logged. v_1 becomes the ledger's root; each subsequent v_i is inserted following standard binary-search-tree insertion starting from the root.
Print "true" if two distinct terraces' yields sum to exactly target, otherwise print "false".
1 <= n <= 10000 -1000000 <= target <= 1000000 -100000 <= v_i <= 100000, and all v_i are distinct The BST is built by inserting v_1, ..., v_n in that order using standard BST insertion, with no rebalancing.
Example 1
Input
5 9 5 3 8 2 4
Expected
true
Explanation
Inserting in order builds a BST with root 5, left child 3 (whose children are 2 and 4), and right child 8. The logged yields are {5, 3, 8, 2, 4}. Since 5 + 4 = 9, two distinct terraces sum to the target quota, so the answer is true.
Example 2
Input
3 100 2 1 3
Expected
false
Explanation
The logged yields are {2, 1, 3}. The only pairwise sums are 2+1=3, 2+3=5, and 1+3=4 — none equal 100 — so the answer is false.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →