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

PythonServer Side ProgrammingProgramming

<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 &minus;</p><ul class="list"><li><p>if a + b &lt; n, then</p><ul class="list"><li><p>return b + 1</p></li></ul></li><li><p>if a + b &gt;= 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 &lt; n: return b + 1 if a + b &gt;= 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>
Updated on 11-Oct-2021 08:08:03