Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C Program for Number of stopping station problem
The number of stopping stations problem calculates the ways a train can stop at r stations out of n total stations such that no two stopping stations are consecutive. This is a classic combinatorial problem solved using the formula for selecting non-adjacent elements.
Syntax
C(n-r+1, r) = (n-r+1)! / (r! * (n-2*r+1)!)
Problem Explanation
When a train travels from point X to Y with n intermediate stations, we need to find the number of ways to select r stopping stations such that no two selected stations are adjacent. This constraint transforms the problem into selecting r items from n-r+1 possible positions.
The mathematical formula becomes −
C(n-r+1, r) = (n-r+1)! / (r! × (n-2×r+1)!)
Example
Let's implement the solution to calculate the number of valid stopping patterns −
#include <stdio.h>
int factorial(int n) {
if (n <= 1) return 1;
int result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
}
int main() {
int n = 16, r = 6;
printf("Total stations: %d<br>", n);
printf("Stopping stations: %d<br>", r);
/* Check if solution exists */
if (n - r + 1 < r) {
printf("No possible ways - not enough stations<br>");
return 0;
}
/* Calculate C(n-r+1, r) */
int numerator = factorial(n - r + 1);
int denominator = factorial(r) * factorial(n - 2 * r + 1);
int ways = numerator / denominator;
printf("Number of ways: %d<br>", ways);
return 0;
}
Total stations: 16 Stopping stations: 6 Number of ways: 462
Optimized Approach
To avoid large factorial calculations, we can use an optimized method −
#include <stdio.h>
int main() {
int n = 16, r = 6;
printf("Total stations: %d<br>", n);
printf("Stopping stations: %d<br>", r);
/* Check validity */
if (n - r + 1 < r) {
printf("No possible ways<br>");
return 0;
}
/* Calculate using optimized method */
int numerator = 1, denominator = 1;
/* Calculate r! in denominator */
for (int i = 1; i <= r; i++) {
denominator *= i;
}
/* Calculate (n-r+1) to (n-2*r+2) in numerator */
for (int i = n - r + 1; i > n - 2 * r + 1; i--) {
numerator *= i;
}
int ways = numerator / denominator;
printf("Number of ways: %d<br>", ways);
return 0;
}
Total stations: 16 Stopping stations: 6 Number of ways: 462
Key Points
- The formula is based on the principle that if we select r non-adjacent positions from n positions, it's equivalent to selecting r positions from n-r+1 positions without restriction.
- The condition
n - r + 1 >= rensures that a valid solution exists. - The optimized approach prevents integer overflow for large factorial values.
Conclusion
The stopping station problem demonstrates the application of combinatorics in constraint-based selection problems. The solution efficiently calculates valid arrangements using the combination formula C(n-r+1, r).
