What is the minimum number of comparisons required to find the minimum and the maximum of hundred numbers?

(A) 147.1 to 148.1 (B) 145.1 to 146.1 (C) 140 to 146 (D) 140 to 148

If n is odd: 3*(n-1)/2 If n is even: 3n/2 -2

Here n = 100,

3n/2 - 2 = 3*100/2 - 2 = 148 how is it 147.1 to 148.1 ?

7

Improve Article

Save Article

Like Article

The minimum number of comparisons required to find the minimum and the maximum of 100 numbers is ______________.
(A) 148
(B) 147
(C) 146
(D) 140

Answer: (A)

Explanation: Steps to find minimum and maximum element out of n numbers:

1. Pick 2 elements(a, b), compare them. (say a > b) 2. Update min by comparing (min, b) 3. Update max by comparing (max, a)

Therefore, we need 3 comparisons for each 2 elements, so total number of required comparisons will be (3n)/2 – 2, because we do not need to update min or max in the very first step.

Recurrence relation will be:

T(n) = T(⌈n/2⌉)+T(⌊n/2⌋)+2 = 2T(n/2)+2 = ⌈3n/2⌉-2

By putting the value n=100, (3*100/2)-2 = 148 which is answer.

 

Quiz of this Question


Page 2

Improve Article

Save Article

Like Article

Consider the following pseudo code. What is the total number of multiplications to be performed?

D = 2 for i = 1 to n do for j = i to n do for k = j + 1 to n do D = D * 3

(A) Half of the product of the 3 consecutive integers.
(B) One-third of the product of the 3 consecutive integers.
(C) One-sixth of the product of the 3 consecutive integers.
(D) None of the above.

Answer: (C)

Explanation: The statement “D = D * 3” is executed n*(n+1)*(n-1)/6 times. Let us see how. 

For i = 1, the multiplication statement is executed (n-1) + (n-2) + .. 2 + 1 times. For i = 2, the statement is executed (n-2) + (n-3) + .. 2 + 1 times ……………………….. ………………………. For i = n-1, the statement is executed once. 

For i = n, the statement is not executed at all 

So overall the statement is executed following times 
[(n-1) + (n-2) + .. 2 + 1] + [(n-2) + (n-3) + .. 2 + 1] + … + 1 + 0 

The above series can be written as 
S = [n*(n-1)/2 + (n-1)*(n-2)/2 + ….. + 1] 

The sum of above series can be obtained by trick of subtraction the series from standard Series S1 = n2 + (n-1)2 + .. 12. The sum of this standard series is n*(n+1)*(2n+1)/6 

S1 – 2S = n + (n-1) + … 1 = n*(n+1)/2 2S = n*(n+1)*(2n+1)/6 – n*(n+1)/2 

S = n*(n+1)*(n-1)/6

The above solution is relies on a trick to obtain the sum of the series, but there is a much more straightforward way of obtaining the same result :

For i = 1, the multiplication statement is executed (n-1) + (n-2) + .. 2 + 1 times.

For i = 2, the statement is executed (n-2) + (n-3) + .. 2 + 1 times 

Therefore, for each i, the number of times the statement is executed is: 

What is the minimum number of comparisons required to find the minimum and the maximum of hundred numbers?

This is the sum of n natural numbers from 1 to n – i. Therefore, it can be simplified to:

What is the minimum number of comparisons required to find the minimum and the maximum of hundred numbers?

Multiplying, we will get the following:

What is the minimum number of comparisons required to find the minimum and the maximum of hundred numbers?

Now, this is the number of times the statement will be executed for each i. Therefore, in total, the number of times the statement will be executed is:

What is the minimum number of comparisons required to find the minimum and the maximum of hundred numbers?

Ignore the half for the moment. Expanding the summation is simple. You can distribute it across the expression to get this:

What is the minimum number of comparisons required to find the minimum and the maximum of hundred numbers?

Using the formulae for sum of n natural numbers and the sum of squares of n natural numbers, we get:

What is the minimum number of comparisons required to find the minimum and the maximum of hundred numbers?

Simplifying further and taking n/6 as a common term will give you:

What is the minimum number of comparisons required to find the minimum and the maximum of hundred numbers?

Now, take 2 as the common term and use it to cancel out the half that we ignored before. This gives you:

What is the minimum number of comparisons required to find the minimum and the maximum of hundred numbers?

Finally, using a2-b2, you get the required answer:

What is the minimum number of comparisons required to find the minimum and the maximum of hundred numbers?

This should make the solution clear to you now!


Quiz of this Question


Page 3

Improve Article

Save Article

Like Article

You are given the postorder traversal, P, of a binary search tree on the n elements 1, 2, …, n. You have to determine the unique binary search tree that has P as its postorder traversal. What is the time complexity of the most efficient algorithm for doing this?
(A) O(Logn)
(B) O(n)
(C) O(nLogn)
(D) none of the above, as the tree cannot be uniquely determined.

Answer: (B)

Explanation: One important thing to note is, it is Binary Search Tree, not Binary Tree. In BST, inorder traversal can always be obtained by sorting all keys.

See method 2 of https://www.geeksforgeeks.org/construct-bst-from-given-preorder-traversa/ for details.

Same technique can be used for postorder traversal.

Quiz of this Question


Page 4

Improve Article

Save Article

Like Article

Suppose we have a balanced binary search tree T holding n numbers. We are given two numbers L and H and wish to sum up all the numbers in T that lie between L and H. Suppose there are m such numbers in T. If the tightest upper bound on the time to
compute the sum is O(nalogb n + mc logd n), the value of a + 10b + 100c + 1000d is ____.
(A) 60
(B) 110
(C) 210
(D) 50

Answer: (B)

Explanation:

int getSum(node *root, int L, int H) { // Base Case if (root == NULL) return 0; if (root->key < L) return getSum(root->right, L, H); if (root->key > H) return getSum(root->left, L, H) if (root->key >= L && root->key <=H) return getSum(root->left, L, H) + root->key + getSum(root->right, L, H); }

The above always takes O(m + Logn) time. Note that the code first traverses across height to find the node which lies in range.  Once such a node is found, it recurs for left and right children. Two recursive calls are made only if the node is in range. So for every node that is in range, we make at most one extra call (here extra call means calling for a node that is not in range).

Quiz of this Question


Page 5

Improve Article

Save Article

Like Article

Let T(n) be the number of different binary search trees on n distinct elements.
Then

What is the minimum number of comparisons required to find the minimum and the maximum of hundred numbers?
, where x is
(A) n-k+1
(B) n-k
(C) n-k-1
(D) n-k-2

Answer: (B)

Explanation: The idea is to make a key root, put (k-1) keys in one subtree and remaining n-k keys in other subtree.

A Binary Search Tree (BST) is a tree in which all the nodes follow the below-mentioned properties −

  • The left sub-tree of a node has a key less than or equal to its parent node’s key.
  • The right sub-tree of a node has a key greater than to its parent node’s key.

Now construction binary search trees from n distinct number-Lets for simplicity consider n distinct numbers as first n natural numbers (starting from 1)

If n=1 We have only one possibility, therefore only 1 BST.


If n=2 We have 2 possibilities , when smaller number is root and bigger number is the right child or second when the bigger number is root and smaller number as left child.

What is the minimum number of comparisons required to find the minimum and the maximum of hundred numbers?

If n=3 We have 5 possibilities. Keeping each number first as root and then arranging the remaining 2 numbers as in case of n=2.

What is the minimum number of comparisons required to find the minimum and the maximum of hundred numbers?

If n=4 We have 14 possibilities. Taking each number as root and arranging smaal numbers as left subtree and larger numbers as right subtree.

What is the minimum number of comparisons required to find the minimum and the maximum of hundred numbers?

Thus we can conclude that with n distinct numbers, if we take ‘k’ as root then all the numbers smaller than k will left subtree and numbers larger than k will be right subtree where the the right subtree and left subtree will again be constructed recursively like the root.Therefore,

What is the minimum number of comparisons required to find the minimum and the maximum of hundred numbers?

This solution is contributed by Parul Sharma.

Quiz of this Question


Page 6

Improve Article

Save Article

Like Article

The number of ways in which the numbers 1, 2, 3, 4, 5, 6, 7 can be inserted in an empty binary search tree, such that the resulting tree has height 6, is _____________

Note: The height of a tree with a single node is 0.

[This question was originally a Fill-in-the-Blanks question]
(A) 2
(B) 4
(C) 64
(D) 32

Answer: (C)

Explanation: To get height 6, we need to put either 1 or 7 at root.

So count can be written as T(n) = 2*T(n-1) with T(1) = 1

7 / [1..6] 1 \ [2..7]

Therefore count is 26 = 64

Another Explanation:

Consider these cases,1 2 3 4 5 6 71 2 3 4 5 7 61 7 6 5 4 3 21 7 6 5 4 2 37 6 5 4 3 2 17 6 5 4 3 1 27 1 2 3 4 5 6

7 1 2 3 4 6 5

For height 6, we have 2 choices. Either we select the root as 1 or 7.Suppose we select 7.Now, we have 6 nodes and remaining height = 5.So, now we have 2 ways to select root for this sub-tree also.Now, we keep on repeating the same procedure till remaining height = 1

For this last case also, we have 2 ways.

Therefore, total number of ways = 26= 64

Quiz of this Question


Page 7

Improve Article

Save Article

Like Article

Suppose that we have numbers between 1 and 100 in a binary search tree and want to search for the number 55. Which of the following sequences CANNOT be the sequence of nodes examined?
(A) {10, 75, 64, 43, 60, 57, 55}
(B) {90, 12, 68, 34, 62, 45, 55}
(C) {9, 85, 47, 68, 43, 57, 55}
(D) {79, 14, 72, 56, 16, 53, 55}

Answer: (C)

Explanation: In BST, on right child of parent should be greater than parent and left child should be smaller than the parent, but in C after 47, 68 goes on the right side because it greater then parent, now everything below this point should be greater then 47 but 43 appears that does not satisfy the BST property.

What is the minimum number of comparisons required to find the minimum and the maximum of hundred numbers?

Quiz of this Question


Page 8

Improve Article

Save Article

Like Article

When searching for the key value 60 in a binary search tree, nodes containing the key values 10, 20, 40, 50, 70 80, 90 are traversed, not necessarily in the order given. How many different orders are possible in which these key values can occur on the search path from the root to the node containing the value 60?
(A) 35
(B) 64
(C) 128
(D) 5040

Answer: (A)

Explanation: There are two set of values, smaller than 60 and greater than 60. Smaller values 10, 20, 40 and 50 are visited, means they are visited in order. Similarly, 90, 80 and 70 are visited in order.

= 7!/(4!3!)= 35

Quiz of this Question


Page 9

Improve Article

Save Article

Like Article

A Binary Search Tree (BST) stores values in the range 37 to 573. Consider the following sequence of keys.

I. 81, 537, 102, 439, 285, 376, 305 II. 52, 97, 121, 195, 242, 381, 472 III. 142, 248, 520, 386, 345, 270, 307

IV. 550, 149, 507, 395, 463, 402, 270

Suppose the BST has been unsuccessfully searched for key 273. Which all of the above sequences list nodes in the order in which we could have encountered them in the search?
(A) II and III only
(B) I and III only
(C) III and IV only
(D) III only

Answer: (D)

Explanation: Key to be searched 273:

  • I) 81, 537, 102, 439, 285, 376, 305 is not correct
    We cannot go to 376 from 285 as 273 is smaller than 285.
  • II) 52, 97, 121, 195, 242, 381, 472 is not correct.
    We cannot go to 472 from 381 as 273 is smaller than 381.
  • III) 142, 248, 520, 386, 345, 270, 307 is correct
    What is the minimum number of comparisons required to find the minimum and the maximum of hundred numbers?
  • 550, 149, 507, 395, 463, 402, 270 is not correct.
    We cannot go to 463 from 395 in search of 273

Following representation of the binary search trees is given below.

What is the minimum number of comparisons required to find the minimum and the maximum of hundred numbers?

Quiz of this Question


Page 10

Improve Article

Save Article

Like Article

A Binary Search Tree (BST) stores values in the range 37 to 573. Consider the following sequence of keys.I. 81, 537, 102, 439, 285, 376, 305II. 52, 97, 121, 195, 242, 381, 472III. 142, 248, 520, 386, 345, 270, 307IV. 550, 149, 507, 395, 463, 402, 270Which of the following statements is TRUE?

(A) I, II and IV are inorder sequences of three different BSTs


(B) I is a preorder sequence of some BST with 439 as the root
(C) II is an inorder sequence of some BST where 121 is the root and 52 is a leaf
(D) IV is a postorder sequence of some BST with 149 as the root

Answer: (C)

Explanation: A: I and IV are not in ascending order

B: If 439 is root, It should be 1st element in preorderD: If 149 is root, It should be last element in postorder

Quiz of this Question


Page 11

Improve Article

Save Article

Like Article

How many distinct BSTs can be constructed with 3 distinct keys?
(A) 4
(B) 5
(C) 6
(D) 9

Answer: (B)

Explanation: 2nCn/(n+1) = 6C3/4 = 5

Quiz of this Question


Page 12

Improve Article

Save Article

Like Article

A binary search tree is generated by inserting in order the following integers:

50, 15, 62, 5, 20, 58, 91, 3, 8, 37, 60, 24

The number of nodes in the left subtree and right subtree of the root respectively is
(A) (4, 7)
(B) (7, 4)
(C) (8, 3)
(D) (3, 8)

Answer: (B)

Explanation:

Quiz of this Question


Please comment below if you find anything wrong in the above post


Page 13

Improve Article

Save Article

Like Article

A binary search tree contains the values 1, 2, 3, 4, 5, 6, 7, 8. The tree is traversed in pre-order and the values are printed out. Which of the following sequences is a valid output?
(A) 53124786
(B) 53126487
(C) 53241678
(D) 53124768

Answer: (D)

Explanation:

Quiz of this Question


Please comment below if you find anything wrong in the above post


Page 14

Improve Article

Save Article

Like Article

A binary search tree contains the numbers 1, 2, 3, 4, 5, 6, 7, 8. When the tree is traversed in pre-order and the values in each node printed out, the sequence of values obtained is 5, 3, 1, 2, 4, 6, 8, 7. If the tree is traversed in post-order, the sequence obtained would be

 
(A) 8, 7, 6, 5, 4, 3, 2, 1
(B) 1, 2, 3, 4, 8, 7, 6, 5
(C) 2, 1, 4, 3, 6, 7, 8, 5
(D) 2, 1, 4, 3, 7, 8, 6, 5

Answer: (D)

Explanation: Please see this link for more details

https://www.geeksforgeeks.org/construct-tree-from-given-inorder-and-preorder-traversal/

Quiz of this Question


Page 15

Improve Article

Save Article

Like Article

Consider the following segment of C-code:

int j, n; j = 1; while (j <=>

The number of comparisons made in the execution of the loop for any n > 0 is:

Base of Log is 2 in all options.
(A) CEIL(logn) + 2
(B) n
(C) CEIL(logn)
(D) FLOOR(logn) + 2

Answer: (D)

Explanation:

We can see it by taking few examples like n = 1, n = 3, etc. For example, for n=5 we have the following (4) comparisons: ------------------------ 1 <=>


Quiz of this Question


Page 16

Improve Article

Save Article

Like Article

Consider the following C-program fragment in which i, j and n are integer variables.

for (i = n, j = 0; i >0; i /= 2, j += i);

Let val(j) denote the value stored in the variable j after termination of the for loop. Which one of the following is true?
(A) val(j) =

What is the minimum number of comparisons required to find the minimum and the maximum of hundred numbers?
(logn)
(B) vaI(j) =
What is the minimum number of comparisons required to find the minimum and the maximum of hundred numbers?
(sqrt(n))
(C) val(j) =
What is the minimum number of comparisons required to find the minimum and the maximum of hundred numbers?
(n)
(D) val(j) =
What is the minimum number of comparisons required to find the minimum and the maximum of hundred numbers?
(nlogn)

(A) A
(B) B
(C) C
(D) D

Answer: (C)

Explanation: The variable j is initially 0 and value of j is sum of values of i. i is initialized as n and is reduced to half in each iteration.

j = n/2 + n/4 + n/8 + .. + 1 = Θ(n)

Note the semicolon after the for loop, so there is nothing in the body.

Same as question 1 of https://www.geeksforgeeks.org/c-language-set-6/

Quiz of this Question


Page 17

Improve Article

Save Article

Like Article

An unordered list contains n distinct elements. The number of comparisons to find an element in this list that is neither maximum nor minimum is
(A) Θ(nlogn)
(B) Θ(n)
(C) Θ(logn)
(D) Θ(1)

Answer: (D)

Explanation: We only need to consider any 3 elements and compare them. So the number of comparisons is constants, that makes time complexity as Θ(1)

The catch here is, we need to return any element that is neither maximum not minimum.

Let us take an array {10, 20, 15, 7, 90}. Output can be 10 or 15 or 20

Pick any three elements from given liar. Let the three elements be 10, 20 and 7.

Using 3 comparisons, we can find that the middle element is 10.

Quiz of this Question


Page 18

Improve Article

Save Article

Like Article

Two matrices M1 and M2 are to be stored in arrays A and B respectively. Each array can be stored either in row-major or column-major order in contiguous memory locations. The time complexity of an algorithm to compute M1 × M2 will be
(A) best if A is in row-major, and B is in column- major order
(B) best if both are in row-major order
(C) best if both are in column-major order
(D) independent of the storage scheme

Answer: (D)

Explanation: This is a trick question. Note that the questions asks about time complexity, not time taken by the program. for time complexity, it doesn’t matter how we store array elements, we always need to access same number of elements of M1 and M2 to multiply the matrices. It is always constant or O(1) time to do element access in arrays, the constants may differ for different schemes, but not the time complexity.

Quiz of this Question