• recategorized by
3,341 views

2 Answers

1 1 vote

Suppose number is given x we need to find it appears maximum number of times or not i.e more than n/2 times or not.

Array is not give in sorted order So use Inoredr Traversal and sort it O(n). Logic over here is find the index of x into the array by using Binary search O(logn) time suppose index is i. Now check element at (i + n/2) =O(1) also x if yes than it is maximal element else not. 

If Array has given in sorted order than Time complexity will be O(logn) but Here  O(n). 

Assuming BST available. If not available than O(nlogn) should be the ans 

--------------------------------------------------------------------------------------------------------------------------------------------------------------

Same procedure apply for when x is not given .

Step1: Build BST= O(nlogn)  = Sort the array + Than build BST  = O( nlogn + n )

Step2. Each Element search into BST and check it's index i and i+ (n/2)th element = O(nlogn)

So Time complexity = O(nlogn)

----------------------------------------------------------------------------------------------------------------------------------------------------------------

Improvement Welcomes :

• edited by
0 0 votes

Using augmentation in binary search tree , we can simplify this problem..

So what we do is we modify in insertion method..First prior to insertion if we find the key already is present in BST then we increase the count field of the concerned node..

Else we insert the node and make count of it = 1 in the augmented BST..

So to find maximum frequency character we do inorder traversal of this BST and check the count value and update the max value and character as and when required..

Hence the complexity is O(n)..Hence B) should be correct answer..

Position:
Show:

Related questions

0 0 votes
1 1 answer
631
631 views
rupamsardar asked Sep 17, 2023
631 views
when searching for the key 60 in a binary search tree containing nodes: 10,20,40,50,70,80,90 are traversed, not nessesarily in this same order.How many different orders a...
1 1 vote
1 1 answer
19.6k
19.6k views
pradeepchaudhary asked Aug 19, 2018
19,624 views
8. What are the worst case and average case complexities of a binary search tree?a) O(n), O(n)b) O(logn), O(logn)c) O(logn), O(n)d) O(n), O(logn)
5 5 votes
1 1 answer
1.9k
1.9k views
VS asked Jan 14, 2018
1,904 views
A balanced binary search tree of n nodes,the number of steps needed to find and remove the 9th largest element in the worst case?(Please mention the algorithm followed)
2 2 votes
0 0 answers
760
760 views
set2018 asked Nov 7, 2017
760 views
In a binary search tree ,the key with value 5 was searched after traversing nodes with values 1,3,4,6,7,8,9 not necessarily in that order.Let P is the probability that 3...