recategorized by
481 views

2 Answers

2 votes
2 votes

Binary search tree is applied when array is sorted . otherwise binary search tree not work.

Binary search will take O(nlogn) time only since take any element and find another element using logn comparision.

Another method :

1) Initialize Binary Hash Map M[] = {0, 0, ...}
2) Do following for each element A[i] in A[]
   (a)	If M[x - A[i]] is set then print the pair (A[i], x - A[i])
   (b)	Set M[A[i]]

Take O(n) time only.

edited by
2 votes
2 votes
We already have a binary search tree. Instead of storing in array. Just store each number in hash table while traversing the tree and again traverse the tree and check if x-A[i] exists in hash table. Saves array space and used just 2 iterations. This takes O(N) time.
edited by

Related questions

3 votes
3 votes
2 answers
1
Nithish asked Jan 11, 2017
2,130 views
Consider an array with ‘n’ numbers, let “T” be time complexity for finding a number appeared maximum number of times in an array. Using Binary Search Tree data st...
0 votes
0 votes
1 answer
2
0 votes
0 votes
1 answer
3