The GATE Grind

GATE 2014 CS (CS2) – Question 21

Programming and Data Structures · Programming in C · 1 mark · Multiple choice

n and p are unsigned int in C. To set p to n choose 3 for large n, which is most likely correct?

  1. p = n * (n-1) * (n-2) / 6;
  2. p = n * (n-1) / 2 * (n-2) / 3;
  3. p = n * (n-1) / 3 * (n-2) / 2;
  4. p = n * (n-1) * (n-2) / 6.0;

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: (B) p = n * (n-1) / 2 * (n-2) / 3;

Explanation

OptionB divides the always-even product n(n−1) by2 before the next multiplication, reducing intermediate magnitude without losing information. The remaining product with(n−2) is divisible by3. Thus **B** is most likely correct for large representable results. It still cannot guarantee correctness if an intermediate or final value exceeds the unsigned range.