- 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 out the position of a ball after n reversals in Python

Suppose, there are n number of balls. The balls are ordered in a fashion 1,2,3,4,...,n. Now the balls are reversed in order, or ordered in a way n, n-1, n-2, ......, 2, 1. The balls are again reversed in order, this time they are reversed from position 1 to n, or now the order becomes n, 1, 2,....., n-1. This reversing process is repeated n times and each time the starting position is moved 1 place to the right. We now have to find out the position of a ball initially in position 'index' after the reversals.

So, if the input is like balls = 5, index = 2, then the output will be 4 The balls initially are: 1, 2, 3, 4, 5

Then,

5,4,3,2,1 5,1,2,3,4 5,1,4,3,2 5,1,4,2,3

The ball in position 2 is currently at position 4.

To solve this, we will follow these steps −

- if index < floor value of (balls / 2), then
- return 2 * index + 1

- otherwise,
- return 2 *(balls - index - 1)

## Example

Let us see the following implementation to get better understanding −

def solve(balls, index): if index < balls // 2: return 2 * index + 1 else: return 2 * (balls - index - 1) print(solve(5, 2))

## Input

5, 2

## Output

4

- Related Questions & Answers
- Find the position of box which occupies the given ball in Python
- Python Program to find out the price of a product after a number of days
- Program to find out the sum of the maximum subarray after a operation in Python
- Program to find where the ball lands in a grid box in Python
- Program to Find Out the Probability of Having n or Fewer Points in Python
- Program to find maximize score after n operations in Python
- Program to find next board position after sliding the given direction once in Python
- Program to find out the minimum number of moves for a chess piece to reach every position in Python
- Check if Matrix remains unchanged after row reversals in Python
- Program to find number of items left after selling n items in python
- Program to find out if the strings supplied differ by a character in the same position in Python
- C++ program to find winner of ball removal game
- Program to find out the value of a given equation in Python
- Program to find out the value of a power of 2 in Python
- Program to find remainder after dividing n number of 1s by m in Python