Program to find the number of possible position in a line in Python


Suppose we have a number n, and p and q. Now suppose we are standing in a line of n people. We do not know which position we are in, but we know there are at least p people in front of us and at most q people behind us. We have to find the number of possible positions we could be in.

So, if the input is like n = 10, p = 3, q = 4, then the output will be 5, as there are 10 people and at least 3 are in front and at most 4 are at behind. So we can stand at indexes [0, 1, 2, 3, 4]. For example, at index 0, 9 people are in front, 0 are behind.

The solution is simple, we will return the minimum of q+1 and n-p

Let us see the following implementation to get better understanding −

Example

 Live Demo

class Solution:
   def solve(self, n, p, q):
      return min(q+1, n-p)
ob = Solution()
print(ob.solve(n = 10, p = 2, q = 5))

Input

10, 2, 5

Output

6

Updated on: 05-Oct-2020

172 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements