GATE Computer ScienceHardGATE CS
Binary search (lo = 0, hi = n − 1, mid = ⌊(lo + hi)/2⌋; each loop iteration compares a[mid] once and ends the search on equality) is run once for each of the 20 elements of a sorted array of n = 20 distinct elements, always with the element present. What is the total number of loop iterations over all 20 searches?
- A. 71
- B. 74
- C. 80
- D. 65
Correct answer: B. 74
The iteration count for an element equals its depth plus one in the implicit binary search tree. The tree has levels with 1, 2, 4, 8 and 5 nodes, so the total is 1·1 + 2·2 + 4·3 + 8·4 + 5·5 = 74.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- Evaluate the postfix expression 8 2 3 ^ / 2 3 + 5 3 - - where ^ is exponentiation (x ^ y = xʸ), / is integer division and the operators…
- How many structurally distinct binary search trees can be built from 7 distinct keys?
- An AVL tree of height 6 (a single node has height 0) must contain at least how many nodes?
- Keys 12, 7, 19, 3, 25, 9, 14 are inserted one by one into an initially empty max-heap stored in an array (index 0 is the root), each…
- An 11-slot hash table with h(k) = k mod 11 and linear probing receives keys 23, 34, 45, 12, 56, 67, 78 in that order. In which slot (0–10)…
- int a[6] = {2, 4, 6, 8, 10, 12}; int p = a + 1, q = a + 5; What does printf("%ld %d", (long)(q - p), (p + (q - p) / 2)); print?