Java Program for Number of stopping station problem
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
import java.io.*;
import java.util.*;
class GFG {
public static int stopping_station( int p, int n)
{
int num = 1 , dem = 1 , s = p;
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)
System.out.print(num / dem);
else
System.out.print( "not possible" );
return 0 ;
}
public static void main(String[] args)
{
int n, s;
stopping_station( 4 , 12 );
}
}
|
Please refer complete article on Number of stopping station problem for more details!
Last Updated :
05 Dec, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...