Given N balls in a line. The initial direction of each ball is represented by the string consists of only ‘L’ and ‘R’ for the left and right direction respectively. It is given that both the balls reverse their direction after the collision and speed will remain the same before and after the collision. Calculate the total number of the collision that takes place.
Input: str = "RRLL" Output: 4 Explanation After first collision: RLRL After second collision: LRRL After third collision: LRLR After fourth collision: LLRR Input: str = "RRLR" Output: 2 Explanation After first collision: RLRR After second collision: LRRR
At each stage we have to find the no of substrings “RL”, change the substring “RL” to “LR”, and again count the no of substring “RL” in the resulting string and repeat in the following until no further substring “RL” is available. We are not going to count the no of substring “RL” on each stage as it will consume lots of time. Another way of doing this is, observe that following resulting strings we have seen on each stage –> “RRLL” -> “RLRL”-> “LRLR” ->”LLRR”.
The initial string is “RRLL”. let’s have a 3rd ball whose direction is L. Now observe this L is shifting to the left side of the string. As we are exchanging “RL” to “LR”, so we are shifting that L towards the left side of the full string. Now similarly, if we continue this exchange, this L will face every R that is present on the left side of that string, so again forming “RL”. Hence, for this L, the total no of “RL” will be the total no of R which is present on the left side of that particular L. We will repeat this for each L. Hence in general, in order to count every possible substring “RL” at every stage, we will count the total no of R present of the left side of each L as these R and L will form “RL” in every stage which can be done in linear time complexity.
Below is the implementation of the above approach:
- Find ways to arrange K green balls among N balls such that exactly i moves is needed to collect all K green balls
- Ways to choose balls such that at least one ball is chosen
- Ways to arrange Balls such that adjacent balls are of different types
- Find the position of box which occupies the given ball
- Count of collisions at a point (X, Y)
- Number of shuffles required for each element to return to its initial position
- Number of ways in which an item returns back to its initial position in N swaps in array of size K
- Total number of ways to place X and Y at n places such that no two X are together
- Find out the correct position of the ball after shuffling
- Find the Initial Array from given array after range sum queries
- Find initial sequence that produces a given Array by cyclic increments upto index P
- Number of comparisons in each direction for m queries in linear search
- Find initial integral solution of Linear Diophantine equation if finite solution exists
- Puzzle | Weight of Heavy Ball
- In how many ways the ball will come back to the first boy after N turns
- Minimum initial vertices to traverse whole matrix with given conditions
- Nth angle of a Polygon whose initial angle and per angle increment is given
- Total ways of choosing X men and Y women from a total of M men and W women
- Find the direction from given string
- Distributing all balls without repetition
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.