Exams › GATE › Technical
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
- Half of the product of the 3 consecutive integers.
- One-third of the product of the 3 consecutive integers.
- One-sixth of the product of the 3 consecutive integers.
- None of the above.
Correct answer: One-sixth of the product of the 3 consecutive integers.
Solution
The correct option is right because the nested loops iterate in such a way that the total number of multiplications corresponds to the combination of choosing 3 distinct integers from a set of n, which mathematically is represented as n(n-1)(n-2)/6, equating to one-sixth of the product of three consecutive integers.
Related GATE Technical questions
⚔️ Practice GATE Technical free + battle 1v1 →