Program to find number of possible position in n-person line with few person at front and back in Python


Suppose we have three numbers n, a and b. Consider we are in a line of n people. And we are unaware about our position in the line. But we know there are at least a number of people in front of us and at most b number of people behind us. We have to find the number of possible positions possible for us.

So, if the input is like n = 10 a = 3 b = 4, then the output will be 5, because there are 10 people in the line and at least 3 are in front and at most 4 are at back. So we are at any places [0, 1, 2, 3, 4]. When we are at position 0 then 9 people are in front, 0 are behind and so on.

To solve this, we will follow these steps −

  • if a + b < n, then

    • return b + 1

  • if a + b >= n, then

    • return n - a

Example

Let us see the following implementation to get better understanding

def solve(n, a, b):
   if a + b < n:
      return b + 1
   if a + b >= n:
      return n - a

n = 10
a = 3
b = 4
print(solve(n, a, b))

Input

10, 3, 4

Output

5

Updated on: 11-Oct-2021

86 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements