- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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