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
Explanation 1 :
Fix/remove of the four stops as fixed points and calculate in how many ways the other stations can be inserted between them, if you must have at least one station between stops.
A x x x x x x x x B
Between these 8 non-halting stations we have 9 places and we select these 9 places as halt between these 8 stations.
Therefore, answer should be = 126
Explanation 2 :
If you know about combinations about indistinguishable balls into distinguishable boxes, then you can simply use, . In this question, $n$ is number of stations and $p$ is number of stations on which you want to stop. Here stopping stations are as indistinguishable balls and non-stopping stations are as distinguishable boxes.
Therefore, = = 126
Below is the code implementation of the solution:
C++
#include <stdio.h>
int stopping_station( int , int );
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)
printf ( "%d" , num / dem);
else
printf ( "not possible" );
}
int main()
{
int n, s;
stopping_station(4, 12);
}
|
Java
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 );
}
}
|
Python3
def stopping_station( p, n):
num = 1
dem = 1
s = p
while p ! = 1 :
dem * = p
p - = 1
t = n - s + 1
while t ! = (n - 2 * s + 1 ):
num * = t
t - = 1
if (n - s + 1 ) > = s:
return int (num / dem)
else :
return - 1
num = stopping_station( 4 , 12 )
if num ! = - 1 :
print (num)
else :
print ( "Not Possible" )
|
C#
using System;
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)
Console.WriteLine(num / dem);
else
Console.WriteLine( "Not possible" );
return 0;
}
public static void Main(String []args)
{
stopping_station(4, 12);
}
}
|
PHP
<?php
function stopping_station(int $p , int $n )
{
$num = 1;
$dem = 1;
$s = $p ;
while ( $p != 1)
{
$dem *= $p ;
$p --;
}
$t = $n - $s + 1;
while ( $t != ( $n - 2 * $s + 1))
{
$num *= $t ;
$t --;
}
if (( $n - $s + 1) >= $s )
echo $num / $dem ;
else
echo "not possible" ;
}
$n ; $s ;
stopping_station(4, 12);
?>
|
Javascript
<script>
function stopping_station( p, n)
{
var num = 1, dem = 1, s = p;
while (p != 1)
{
dem *= p;
p--;
}
var t = n - s + 1;
while (t != (n - 2 * s + 1))
{
num *= t;
t--;
}
if ((n - s + 1) >= s){
document.write(num / dem);
}
else {
document.write( "Not possible" );
}
return 0;
}
stopping_station(4, 12);
</script>
|
Output :
126
Time Complexity: O(s)
Auxiliary Space: O(1)
Last Updated :
10 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...