Answer to Question #230241 in C for Vikas21r

Question #230241

given a number of mangoes and number of persons. Find the number of ways to distribute identical mangoes among identical persons


1
Expert's answer
2021-08-27T14:04:45-0400
// Online C compiler to run C program online
#include <stdio.h>

int main() {

  // function used to generate binomial coefficient
// time complexity O(m)
int binomial_coefficient(int n, int m)
{
  int res = 1;
 
  if (m > n - m)
    m = n - m;
 
  for (int i = 0; i < m; ++i) {
    res *= (n - i);
    res /= (i + 1);
  }
 
  return res;
}
// helper function for generating no of ways
// to distribute m mangoes amongst n people
int calculate_ways(int m, int n)
{
  // not enough mangoes to be distributed
  if (m < n)
    return 0;
   
  // ways -> (n+m-1)C(n-1)
  int ways = binomial_coefficient(n + m - 1, n - 1);
  return ways;
}
 int m = 8, n = 5;
 
  int result = calculate_ways(m, n);
  printf("%d\n", result);
  return 0;
}
//for example in the above case the answer is 495

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS