Question

Suppose we have a balanced binary search tree TT holding nn numbers. We are given two numbers LL and HH and wish to sum up all the numbers in TT that lie between LL and HH. Suppose there are mm such numbers in TT. If the tightest upper bound on the time to compute the sum is O(nalog⁡bn+mclog⁡dn)O(n^a \log^b n + m^c \log^d n), the value of a+10b+100c+1000da + 10b + 100c + 1000d is _____.

Official answer and solution
Official answer
110
From
Official GATE 2014 answer key

Solution

Analyze the time complexity of a range query on a balanced binary search tree and match powers to find the required combination.

  1. Range Query on a Balanced BST:
    To sum all mm elements within the range [L,H][L, H] in a balanced BST containing nn elements:

    • Step 1: Search for the smallest key ≥L\ge L in the tree. Because the tree is balanced (height Θ(log⁡n)\Theta(\log n)), this search takes O(log⁡n)O(\log n) time.
    • Step 2: From this starting node, perform an in-order traversal to retrieve each of the mm keys in range, stopping when a key >H> H is encountered.
    • The in-order traversal visits each of the mm reported nodes plus at most O(height)=O(log⁡n)O(\text{height}) = O(\log n) boundary nodes.
    • Thus, the total time required is:
      T(n,m)=O(log⁡n+m).T(n, m) = O(\log n + m).
  2. Matching Coefficients:
    We are given the tightest upper bound form:

    O(nalog⁡bn+mclog⁡dn).O(n^a \log^b n + m^c \log^d n).

    Matching this with O(n0log⁡1n+m1log⁡0n)O(n^0 \log^1 n + m^1 \log^0 n):

    • a=0a = 0
    • b=1b = 1
    • c=1c = 1
    • d=0d = 0
  3. Compute the Required Expression:

    Value=a+10b+100c+1000d\text{Value} = a + 10b + 100c + 1000d
    Value=0+10(1)+100(1)+1000(0)=10+100=110.\text{Value} = 0 + 10(1) + 100(1) + 1000(0) = 10 + 100 = 110.
Final answer

110

Common trap

Assuming the range search takes O(m log n) time by searching for each element individually from the root instead of continuing via tree traversal.

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2014 Computer Science and Information Technology (CS) Session 3
Question
49 of 65, printed as CS Q.39
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.