Skip to content
Related Articles

Related Articles

GATE | GATE-CS-2014-(Set-2) | Question 65
  • Difficulty Level : Hard
  • Last Updated : 10 Dec, 2018

Suppose n and p are unsigned int variables in a C program. We wish to set p to nC3. If n is large, which of the following statements is most likely to set p correctly?

(A) p = n * (n-1) * (n-2) / 6;
(B) p = n * (n-1) / 2 * (n-2) / 3;
(C) p = n * (n-1) / 3 * (n-2) / 2;
(D) p = n * (n-1) * (n-2) / 6.0;

Answer: (B)

Explanation: As n is large, the product n*(n-1)*(n-2) will go out of the range(overflow) and it will return a value different from what is expected. Therefore, option (A) and (D) are eliminated.
So we consider a shorter product n*(n-1).
n*(n-1) is always an even number. So the subexpression ” n * (n-1) / 2 ” in option B would always produce an integer, which means no precision loss in this subexpression. And when we consider ” n*(n-1)/2*(n-2) “, it will always give a number which is a multiple of 3. So dividing it with 3 won’t have any loss.

Quiz of this Question


My Personal Notes arrow_drop_up
Recommended Articles
Page :