in Algorithms edited by
8,127 views
25 votes
25 votes

The following function computes the value of  $\binom{m}{n}$ correctly for all legal values $m$ and $n$ ($m ≥1, n ≥ 0$ and $m > n$)

int func(int m, int n)
{
    if (E) return 1;
    else return(func(m -1, n) + func(m - 1, n - 1));
}


In the above function, which of the following is the correct expression for E?

  1. $(n = = 0) || (m = = 1)$
  2. $(n = = 0)$ && $(m = = 1)$
  3. $(n = = 0) || (m = = n)$
  4. $(n = = 0)$ && $(m = = n)$
in Algorithms edited by
8.1k views

4 Comments

Why not option (d)  is the answer?????
0
0
edited by

$ \textbf{m > n}$

Why we are not considering this condition. Due to it we should not take $m == n$

0
0

@KARAN

because those are the conditions to the input of the functions and they can change inside the function like in this case due to recursion.

 

0
0

3 Answers

34 votes
34 votes
Best answer

Answer: C

Because $\binom{m}{0}$ = $1$ and $\binom{n}{n}$ = $1$.

edited by

4 Comments

C(m,n) = C(m-1,n) + C(m-1,n-1)

This is the recursive definition of Combinations of m objects chosen n at a time.

We all know, when does this function returns 1, it is only when nCn or nC0
0
0

vijaycs

In the third line it must be

[1/r  + 1/(n-r)]

you mistyped – in case of +.

0
0

@Ahwan sir!

This code is somewhat similar to the 01KS problem so i think the time complexity given by you must be correct. 

2
2
1 vote
1 vote
$\binom{m}{0} or   (m==n) \binom{m}{n}$

return 1;

(n==0) || (m==n)

Amswer : C

4 Comments

That's what is my doubt. N==0 is true and m==n is true , so whole statement is true right. So opt d also serves the solution.
0
0
Ok, value of $\binom{6}{6}$ is 1.

so here n is 6, according to d option (n==0) && (m==n), (n==0) is also should be true then whole statement return 1, but here it will not return 1.

while taking to option c. (n==0)||(m==n) the whole statment return 1.

same you also check with $\binom{6}{0}$
0
0
Missed a fine logic,now I get that. Thanks a lot.
0
0
0 votes
0 votes
If we put m=1, then we can put only one value of n i.e. 0 and putting n=0, we will get the answer as 1. Then why option (a) is not correct?
Answer:

Related questions