

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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
<p>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.</p><p>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.</p><p>To solve this, we will follow these steps −</p><ul class="list"><li><p>if a + b < n, then</p><ul class="list"><li><p>return b + 1</p></li></ul></li><li><p>if a + b >= n, then</p><ul class="list"><li><p>return n - a</p></li></ul></li></ul><h2>Example</h2><p>Let us see the following implementation to get better understanding</p><pre class="demo-code notranslate language-python" data-lang="python">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))</pre><h2>Input</h2><pre class="prettyprint notranslate">10, 3, 4 </pre><h2>Output</h2><pre class="result notranslate">5</pre>
- Related Questions & Answers
- Program to find the number of possible position in a line in Python
- Program to find number of minimum steps required to meet all person at any cell in Python
- C++ Program to find number of calling person pairs are calling
- Determine the position of the third person on regular N sided polygon in C++ Program
- Determine the position of the third person on regular N sided polygon in C++?
- Why a living person sinks and a dead person floats in water?
- Reversing the alphabet from back to front and front to back in JavaScript
- Position of a person diametrically opposite on a circle in C++
- What makes a person a champion at anything?
- C++ Program to find out if a person has won lottery
- Program to find minimum distance that needs to be covered to meet all person in Python
- Program to convert linked list by alternating nodes from front and back in Python
- Counting pairs when a person can form pair with at most one in C++
- Program to find position of first event number of line l of a triangle of numbers in Python
- queue::front() and queue::back() in C++ STL
Advertisements