# Number of different positions where a person can stand

A person stands in line of n people, but he doesn’t know exactly which position he occupies. He can say that there are no less than ‘f’ people standing in front of him and no more than ‘b’ people standing behind him. The task is to find the number of different positions he can occupy.

**Examples:**

Input: n = 3, f = 1, b = 1 Output: 2 3 is the number of people in the line and there can be no less than 1 people standing

in front of him and no more than 1 people standing behind him.So the positions could be 2 and 3

(if we number the positions starting with 1). Input: n = 5, f = 2, b = 3 Output: 3 In this example the positions are 3, 4, 5.

**Approach:** Let’s iterate through the each item and check whether it is appropriate to the conditions a<=i-1 and n-i<=b (for i from 1 to n). The first condition can be converted into **a+1<=i**, and the condition **n-i<=b** in **n-b<=i**, then the general condition can be written **max(a+1, n-b)<=i** and then our answer can be calculated by the formula **n-max(a+1, n-b)+1**.

Below is the implementation of above approach:

## C++

`// C++ implementation of above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the position ` `int` `findPosition(` `int` `n, ` `int` `f, ` `int` `b) ` `{ ` ` ` ` ` `return` `n - max(f + 1, n - b) + 1; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `int` `n = 5, f = 2, b = 3; ` ` ` `cout << findPosition(n, f, b); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of above approach ` `import` `java.util.*; ` `import` `java.lang.*; ` `import` `java.io.*; ` ` ` `class` `GFG{ ` ` ` ` ` `// Function to find the position ` `static` `int` `findPosition(` `int` `n, ` `int` `f, ` `int` `b) ` `{ ` ` ` ` ` `return` `n - Math.max(f + ` `1` `, n - b) + ` `1` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` ` ` `int` `n = ` `5` `, f = ` `2` `, b = ` `3` `; ` ` ` `System.out.print(findPosition(n, f, b)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of ` `# above approach ` ` ` `# Function to find the position ` `def` `findPosition(n, f, b): ` ` ` ` ` `return` `n ` `-` `max` `(f ` `+` `1` `, n ` `-` `b) ` `+` `1` `; ` ` ` `# Driver code ` `n, f, b ` `=` `5` `, ` `2` `, ` `3` `print` `(findPosition(n, f, b)) ` ` ` `# This code is contributed by ` `# Sanjit_Prasad ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of above approach ` `using` `System; ` `class` `GFG ` `{ ` ` ` `// Function to find the position ` `static` `int` `findPosition(` `int` `n, ` ` ` `int` `f, ` `int` `b) ` `{ ` ` ` ` ` `return` `n - Math.Max(f + 1, n - b) + 1; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `n = 5, f = 2, b = 3; ` ` ` `Console.WriteLine(findPosition(n, f, b)); ` `} ` `} ` ` ` `// This code is contributed ` `// by inder_verma ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of above approach ` ` ` `// Function to find the position ` `function` `findPosition(` `$n` `, ` `$f` `, ` `$b` `) ` `{ ` ` ` `return` `$n` `- max(` `$f` `+ 1, ` ` ` `$n` `- ` `$b` `) + 1; ` `} ` ` ` `// Driver code ` `$n` `= 5; ` `$f` `= 2; ` `$b` `= 3; ` `echo` `findPosition(` `$n` `, ` `$f` `, ` `$b` `); ` ` ` `// This code is contributed ` `// by anuj_67 ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

3

## Recommended Posts:

- Number of hours after which the second person moves ahead of the first person if they travel at a given speed
- Find a number containing N - 1 set bits at even positions from the right
- Number of positions such that adding K to the element is greater than sum of all other elements
- Total number of days taken to complete the task if after certain days one person leaves
- Number of sequences which has HEAD at alternate positions to the right of the first HEAD
- Position of a person diametrically opposite on a circle
- Determine the position of the third person on regular N sided polygon
- Find a way to fill matrix with 1's and 0's in blank positions
- Ways to place 4 items in n^2 positions such that no row/column contains more than one
- Minimum cost to cover the given positions in a N*M grid
- Count of Numbers in a Range divisible by m and having digit d in even positions
- Find the sum of the ascii values of characters which are present at prime positions
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Count number of triplets with product equal to given number with duplicates allowed
- Count number of trailing zeros in Binary representation of a number using Bitset

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.