# Find total no of collisions taking place between the balls in which initial direction of each ball is given

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.**Examples:**

Input:str = "RRLL"Output:4ExplanationAfter first collision: RLRL After second collision: LRRL After third collision: LRLR After fourth collision: LLRRInput:str = "RRLR"Output:2ExplanationAfter first collision: RLRR After second collision: LRRR

**Approach:**

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 represent 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:

## C++

`// C++ implementation to Find total` `// no of collisions taking place between` `// the balls in which initial direction` `// of each ball is given` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to count no of collision` `int` `count(string s)` `{` ` ` `int` `N, i, cnt = 0, ans = 0;` ` ` `// length of the string` ` ` `N = s.length();` ` ` `for` `(i = 0; i < N; i++) {` ` ` `if` `(s[i] == ` `'R'` `)` ` ` `cnt++;` ` ` `if` `(s[i] == ` `'L'` `)` ` ` `ans += cnt;` ` ` `}` ` ` `return` `ans;` `}` `// Driver code` `int` `main()` `{` ` ` `string s = ` `"RRLL"` `;` ` ` `cout << count(s) << endl;` ` ` `return` `0;` `}` |

## Java

`// Java implementation to Find total` `// no of collisions taking place between` `// the balls in which initial direction` `// of each ball is given` `import` `java.io.*;` `class` `GFG {` ` ` `// Function to count` `// no of collision` `static` `int` `count(String s)` `{` ` ` `int` `N, i, cnt = ` `0` `, ans = ` `0` `;` ` ` `// length of the string` ` ` `N = s.length();` ` ` `for` `(i = ` `0` `; i < N; i++)` ` ` `{` ` ` `if` `(s.charAt(i) == ` `'R'` `)` ` ` `cnt++;` ` ` `if` `(s.charAt(i) == ` `'L'` `)` ` ` `ans += cnt;` ` ` `}` ` ` `return` `ans;` `}` ` ` `// Driver code` `static` `public` `void` `main(String[] args)` `{` ` ` `String s = ` `"RRLL"` `;` ` ` `System.out.println(count(s));` `}` `}` `// This code is contributed by Rutvik_56` |

## Python3

`# Python3 implementation to find total` `# no of collisions taking place between` `# the balls in which initial direction` `# of each ball is given` `# Function to count no of collision` `def` `count(s):` ` ` `cnt, ans ` `=` `0` `, ` `0` ` ` `# Length of the string` ` ` `N ` `=` `len` `(s)` ` ` `for` `i ` `in` `range` `(N):` ` ` `if` `(s[i] ` `=` `=` `'R'` `):` ` ` `cnt ` `+` `=` `1` ` ` `if` `(s[i] ` `=` `=` `'L'` `):` ` ` `ans ` `+` `=` `cnt` ` ` ` ` `return` `ans` `# Driver code` `s ` `=` `"RRLL"` `print` `( count(s))` `# This code is contributed by chitranayal` |

## C#

`// C# implementation to Find total` `// no of collisions taking place between` `// the balls in which initial direction` `// of each ball is given` `using` `System;` `class` `GFG{` `// Function to count no of collision` `static` `int` `count(String s)` `{` ` ` `int` `N, i, cnt = 0, ans = 0;` ` ` `// length of the string` ` ` `N = s.Length;` ` ` `for` `(i = 0; i < N; i++)` ` ` `{` ` ` `if` `(s[i] == ` `'R'` `)` ` ` `cnt++;` ` ` `if` `(s[i] == ` `'L'` `)` ` ` `ans += cnt;` ` ` `}` ` ` `return` `ans;` `}` `// Driver code` `public` `static` `void` `Main(String[] args)` `{` ` ` `String s = ` `"RRLL"` `;` ` ` `Console.Write(count(s));` `}` `}` `// This code is contributed by shivanisinghss2110` |

## Javascript

`<script>` ` ` `// Javascript implementation to Find total` ` ` `// no of collisions taking place between` ` ` `// the balls in which initial direction` ` ` `// of each ball is given` ` ` ` ` `// Function to count no of collision` ` ` `function` `count(s)` ` ` `{` ` ` `let N, i, cnt = 0, ans = 0;` ` ` `// length of the string` ` ` `N = s.length;` ` ` `for` `(i = 0; i < N; i++) {` ` ` `if` `(s[i] == ` `'R'` `)` ` ` `cnt++;` ` ` `if` `(s[i] == ` `'L'` `)` ` ` `ans += cnt;` ` ` `}` ` ` `return` `ans;` ` ` `}` ` ` ` ` `let s = ` `"RRLL"` `;` ` ` ` ` `document.write(count(s));` `// This code is contributed by divyesh072019.` `</script>` |

**Output:**

4

Time Complexity: O(N)

Auxiliary Space: O(1)