- 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

# Python Program to find out how many times the balls will collide in a circular tube

Suppose, there are n balls in a circular tube. The tube is 100 meters long and initially, each ball in the tube is i meters away from a point that we call the starting point. Now the balls start to travel within the tube in a circular order in different directions. The balls travel 0.1 meters per second within the tube. When two balls meet at a point, a collision occurs and the balls change their direction of travel. If this process goes for a long time, let's say 10^9 + 6 seconds; we have to find out the number of times the balls engage in a collision. The initial distance of the balls from the starting point is given as input.

So, if the input is like input_array = [0, 10], then the output will be 400000

There are two balls, and their initial distance from the starting line is given to us as input. If their directions are the same, they will never engage in a collision. But, if their directions are different, they will collide in times. One ball will collide with another exactly 400000 times.

To solve this, we will follow these steps −

- sort the list input_array
- size := size of input_array
- lap_count := (10^5)*2
- output := 2 * lap_count * floor value of (size / 2) * (size - floor value of (size / 2))
- stop := 0
- for i in range 0 to size - 1, do
- if stop is not same as 1, then
- if input_array[i] + 1 is same as input_array[i+1], then
- output := output + 2
- stop := 1

- otherwise,
- stop := 0

- if input_array[i] + 1 is same as input_array[i+1], then
- otherwise,
- stop := 0

- return output

## Example

Let us see the following implementation to get better understanding −

def solve(input_array): input_array.sort() size = len(input_array) lap_count = (10**5)*2 output = 2*lap_count*(size//2)*(size - size//2) stop = 0 for i in range(size - 1): if stop != 1: if input_array[i] + 1 == input_array[i+1]: output+=2 stop = 1 else: stop = 0 else: stop = 0 return output print(solve([0, 10]))

## Input

[0, 10]

## Output

400000

- Related Questions & Answers
- Python Program to find out how many cubes are cut
- Program to find how many times a character appears in a string in PHP
- Program to find out how many transfer requests can be satisfied in Python
- Program to find out how many boxes can be put into the godown in Python
- Program to find how many years it will take to reach t amount in Python
- Program to count how many swimmers will win the final match in Python
- Python Program to Determine How Many Times a Given Letter Occurs in a String Recursively
- Program to find minimum limit of balls in a bag in Python
- Program to find maximum number of balls in a box using Python
- Program to find hoe many children will get candies while distributing them maintaining the rules in Python
- How many times finalize method will be invoked? who invokes finalize() method in Java?
- Program to find how many lines intersect in Python
- Program to count how many blocks are covered k times by walking in Python
- Program to find circular greater element to the right in Python
- Program to find how many ways we can climb stairs (maximum steps at most k times) in Python