- 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

# Find the distance covered to collect items at equal distances in Python

Suppose one race is going to be organized. Where different stones are placed on a road. One bucket is present at the starting point of the race, this is 6 units away from the first stone. The other stones are 4 units apart from each other and lie straight in a line one after another. Now, the participants start from the bucket, then collects the nearest stone, comes back and puts that stone into the bucket, after that runs again to collect the next nearest stone, runs back, and puts it in the bucket. This process will be continued until all the stones have been put into the bucket. If there are n stones, then we have to find total distance that need to be covered for the participants.

So, if the input is like n = 5, then the output will be 140 as 2*6 + 2(6 + 4) + 2(6 + 4 + 4) + 2(6 + 4 + 4 + 4) + 2(6 + 4 + 4 + 4 + 4) = 140

To solve this, we have to solve this equation −

For stone 1, we have to cover (6+6) = 2*6 distance

For stone 2, we have to cover ((6+4)+(6+4)) = 2*(6+4) distance

For stone 3, we have to cover ((6+4+4)+(6+4+4)) = 2*(6+4+4) distance

For stone n, we have to cover ((6+4*(n-1))+(6+4*(n-1))) = 2*(6+4*(n-1)) distance

For all stones we have to cover −

D = 2*6 + 2*(6+4) + 2*(6+4+4) + … + 2*(6+4*(n-1))

D = 2*[6 + (6+4) + (6+2*4) + … + (6+(n-1)*4)]

D = 2*[6n + 4(1 + 2 + … + (n-1))]

D = 2*[6n + 4(n*(n-1)/2)]

D = 2*[6n + 2(n*(n-1))]

## Example

Let us see the following implementation to get better understanding −

def find_distance(n): return 2*(6*n + 2*((n-1)*n)) n = 5 print(find_distance(n))

## Input

5

## Output

140

- Related Questions & Answers
- Find the maximum distance covered using n bikes in Python
- Program to find minimum distance that needs to be covered to meet all person in Python
- JavaScript - find distance between items on array
- How to find distance between items on array JavaScript
- Count paths with distance equal to Manhattan distance in C++
- Program to find maximum number of coins we can collect in Python
- Program to find total area covered by two rectangles in Python
- Program to find minimum distance to the target element using Python
- Find the City With the Smallest Number of Neighbors at a Threshold Distance in C++
- How to Find The Largest Or Smallest Items in Python?
- Program to find out number of blocks that can be covered in Python
- Program to find maximum equal frequency in Python
- Insert the string at the beginning of all items in a list in Python
- Program to find number of items left after selling n items in python
- Align gathered items at the end in Bootstrap 4