TRENDING NEWS

POPULAR NEWS

How To Find The Sum Of The Largest Lgn Elements In Heap

How can I find the median of two sorted arrays?

I will lay out the general idea of the solution:1. Let's assume that median of the first array is larger than the median of the second array, and both arrays are of same size N (if either of these assumptions does not hold it is easy to make it do, think how).2. Now you know that the real median can NOT appear in A1[N/2+1...N] as at least N values are smaller than any of the numbers in this subarray (think why this holds). A symmetric argument applies for A2[1..N/2].3. So you are left with a search space of size N, in subarrays A1[1..N/2] and A2[N/2+1...N]. It holds that the median these arrays concatenated is the median of A1+A2 (think why).4. You guess right, recursion of steps 1-3.5. Once you are left with two single-element arrays, the result depends on the definition of the median you use: it either is the smaller element, or their average.Running time is O(lgN) with O(1) extra space needed (if implemented effectively).

How would you make an algorithm's time complexity O(logn)?

Algorithms that run in O(log(n)) usually work by discarding a significant portion of the problem space with each step.For example, in a binary search, each step reduces the number of places to look by half. You can cut an array of length [math]n[/math] a maximum of [math]\log_2(n)[/math] times before you're left with only 1 element, hence binary search has a running time in O(log(n)).Searching through a binary tree is another example of an O(log(n)) algorithm: with each step, you decide to go to the left or the right child of a node, thereby completely discarding half of the values with each step.If you're asking how, in general, you might convert any algorithm into one that runs in O(log(n)), then I'm sorry to tell you that that's not usually possible. There are often tricks you might do in an algorithm that normally runs in O(n) in a naive implementation which could reduce it to O(log(n)), but in no way does this apply to all algorithms overall.Sorting, for example, will never be faster than O(n*log(n)) for any sorting algorithm which relies on making a comparison between two elements. There are special cases, like insertion sort on an already-sorted list taking only O(n), but its complexity on any other input is O(n^2). Radix sort is a good example of a sorting algorithm which runs in O(n), and it accomplishes this by not requiring comparisons between elements, but instead working only on integers and only when the minimum and maximum values are known.There is otherwise no way whatsoever to reduce a sorting algorithm below O(n), or below O(n*log(n)) if it works by comparing pairs of elements. It's impossible.In fact, there's a whole class of problems in a group that computer scientists call "NP-complete" for which no one has ever found an algorithm to solve in polynomial time. None of these problems will ever have an O(1), O(log(n)), O(n), or O(n*log(n)) solution for all cases.

The height of any binary tree of n nodes is between log(n) and n-1. What is the proof for this observation?

Suppose a binary tree has n nodes. There's at most 1 node (the root) at height 0, at most 2 nodes (2 children of the root) at height 1, at most 4 nodes (2 children each for the 2 children of the root) at height 2, and so on.So, for a tree with a given height [math]H[/math], the maximum number of nodes on all levels is [math]1 + 2 + 4 + 8 + ... + 2^{H} = 2^{H+1} - 1[/math]. Therefore, if we know that there are [math]N[/math] nodes, we have [math]2^{H+1} - 1 \geq N[/math], so [math]H \geq \log_2(N+1) - 1[/math]. This is the lower bound on height.To get the upper bound, we consider that there cannot be a node at height  [math]H[/math] without there being a node at height [math]H - 1[/math] (except in the case of  [math]H = 0[/math]). Therefore, if a tree has height  [math]H[/math], it must have at least one node at height  [math]H[/math], then a node at height  [math]H - 1[/math], then a node at  [math]H - 2[/math], all the way to [math]0[/math]. The number of nodes  [math]N[/math] therefore satisfies [math]N \geq H + 1[/math] and therefore [math]H \leq N - 1[/math].So, the overall result is:[math]\log_2(N+1) - 1 \leq H \leq N - 1[/math]

How many levels will there be in a completely binary tree if it has n number of nodes?

Complete Binary Tree: A Binary Tree is complete Binary Tree if all levels are completely filled except possibly the last level and the last level has all keys as left as possibleFollowing are examples of Complete Binary Trees 18
/ \
15 30
/ \ / \
40 50 100 40


18
/ \
15 30
/ \ / \
40 50 100 40
/ \ /
8 7 9
Suppose height of tree = h. then #levels (L) = h+1 .then maximum possible nodes in complete binary tree is when all levels are completely filled meansat rrot level i.e. level 1 #nodes = 1 = 2^0at level 2 #nodes = 2=2^1at level 3 #nodes = 4=2^2at level 4 #nodes = 8=2^3at level 5 #nodes =16= 2^4.....at level h #nodes = 2^(h-1)at level h+1 #nodes = 2^htotal = 2^0 + 2^1 + 2^2 + 2^3 + ………+2^(h-1) + 2^h====> 1+2+4+8+…………………………………….+2^hso given number of nodes in complete binary tree must be at most = totali.e. n<= totaln<= 1+2+4+8+…………………………………….+2^hn<= 2^(h+1)n<= 2^L (becoz, L= h+1)log2(n) <=LL >= log2(n)L = ceil ( log2(n) )

What is the maximum number of comparisons that a binary search function will make when searching for a value in a 1,000 - element array?

Suppose you are searching for a number which is located at index 498 in an array of 1000 element, let’s do Binary Search “halving” using Math.Floor till we find the element.> Math.Floor((0 + 999) / 2) = 499 > Not matched index> Math.Floor((0 + 498) / 2) = 249 > Not matched index> Math.Floor((250 + 498) / 2) = 374 > Not matched index> Math.Floor((375 + 498) / 2) = 436 > Not matched index> Math.Floor((437 + 498) / 2) = 467 > Not matched index> Math.Floor((468 + 498) / 2) = 483 > Not matched index> Math.Floor((484 + 498) / 2) = 491 > Not matched index> Math.Floor((492 + 498) / 2) = 495 > Not matched index> Math.Floor((496 + 498) / 2) = 497 > Not matched index> Math.Floor((498 + 498) / 2) = 498 > Index Found> Number was found after 10 Guesses—————————————————————let’s now use Math.Round instead and see the result> Math.Round((0 + 999) / 2) = 500 > Not matched index> Math.Round((0 + 499) / 2) = 250 > Not matched index> Math.Round((251 + 499) / 2) = 375 > Not matched index> Math.Round((376 + 499) / 2) = 438 > Not matched index> Math.Round((439 + 499) / 2) = 469 > Not matched index> Math.Round((470 + 499) / 2) = 484 > Not matched index> Math.Round((485 + 499) / 2) = 492 > Not matched index> Math.Round((493 + 499) / 2) = 496 > Not matched index> Math.Round((497 + 499) / 2) = 498 > Index Found> Number was found after 9 Guesses——————————————————Conclusion: Depending on the implementation of the algorithm the result will be different, it will be approximately lg n or lg n (±1)

Why must a heap of size n have height floor(log(n))?

A heap is a Tree which satisfies the following two properties:1. All the child nodes must be less (or greater) than its parent node for a min (or max) heap.2. The leaves must either fill the h or h-1 node where h is the height of the tree; it means it must be a complete binary tree.Complete binary tree:A complete binary tree is a binary tree in which every level, except possibly the deepest, is completely filled. At depth h, height of the tree, all nodes are as far left as possible.Google about Complete binary tree for better understanding.Height of a tree is the height of the root i.e it is the no of edges from root to the deepest leaf.As you can see the number of nodes in i th level is 2^iAnd using geometric progressionTherefore total number of nodes will be[math]1 + 2 + 2^2 + 2^3 + ......... + 2^h = 2^(h+1) - 1[/math]Now your answer lies in this.PS: I have recently started answering to question on Quora and I dont know how to write in superscript form, 2^i as superscript format. comment please.Thank You! :)

TRENDING NEWS