GATE / CS ARCHIVE

GATE CSE 2014 SET 1 / QUESTION 42

GATE CSE 2014 Set 1 Q42

Algorithms · Asymptotic Analysis & Recurrences · Full GATE CSE 2014 Set 1 paper

GATE 2014 Set 1, Q42 2 marks · MCQ
Report issue

Asymptotic Analysis & Recurrences

Question 42, GATE 2014: Pseudocode sets D=2, then loops i=1..n, j=i..n, k=j+1..n, performing D=D*3 once per innermost iteration. How many multiplications occur? A: Half the product of three consecutive integers. B: One-third the product of three consecutive integers. C: One-sixth the product of three consecutive integers. D: None of these.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

1. Method and setup. Count valid index triples rather than estimating every loop as n iterations. For a fixed j, index i can take j values and k can take n−j values. Multiplying those independent choices and summing over j gives an exact count; the assignment D=2 performs no multiplication.

2. Work through the result and its assumptions. For each middle index j there are j choices of i and n−j choices of k. The number of multiplications is sum(j=1..n)j(n−j)=n·n(n+1)/2−n(n+1)(2n+1)/6=(n−1)n(n+1)/6, one-sixth the product of three consecutive integers, C.

Original paper, page 18 · Answer key