- 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 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
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
Advertisements