- 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 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 find maximum number of balls in a box using Python
- Python Program to Determine How Many Times a Given Letter Occurs in a String Recursively
- Program to find circular greater element to the right in Python
- How many times finalize method will be invoked? who invokes finalize() method in Java?
- Program to Find Out the Minimal Submatrices in Python
- Program to find how many ways we can climb stairs (maximum steps at most k times) in Python
- Program to count how many times we can find "pizza" with given string characters in Python
- Program to find the maximum sum of circular sublist in Python
- Program to Find Out the Special Nodes in a Tree in Python
- Program to Find Out the Points Achievable in a Contest in Python
- Program to Find Out Currency Arbitrage in Python

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

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]))

[0, 10]

400000

Advertisements