C Program for Number of stopping station problem

• Last Updated : 05 Dec, 2018

There are 12 intermediate stations between two places A and B. Find the number of ways in which a train can be made to stop at 4 of these intermediate stations so that no two stopping stations are consecutive?

Examples –

Input  : n = 12, s = 4
Output : 126

Input  : n = 16, s = 5
Output : 792

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

 #include int stopping_station(int, int);  // function to calculate number// of ways of selecting 'p' non consecutive// stations out of 'n' stationsint stopping_station(int p, int n){      int num = 1, dem = 1, s = p;      // selecting 's' positions out of 'n-s+1'    while (p != 1) {          dem *= p;        p--;    }      int t = n - s + 1;    while (t != (n - 2 * s + 1)) {          num *= t;        t--;    }      if ((n - s + 1) >= s)        printf("%d", num / dem);      else          // if conditions does not satisfy of combinatorics        printf("not possible");}  // driver codeint main(){      // n is total number of stations    // s is no. of stopping stations    int n, s;      // arguments of function are    // number of stopping station    // and total number of stations    stopping_station(4, 12);}
Output:
126

Please refer complete article on Number of stopping station problem for more details!

My Personal Notes arrow_drop_up