# Find time taken for signal to reach all positions in a string

Given an string of ‘x’ and ‘o’. From each ‘x’ a signal originates which travels in both direction. It takes one unit of time for the signal to travel to next cell. If a cell contains ‘o’, the signal changes it to ‘x’ . Task is to compute the time taken to convert the array into string of signals.
An string of signal contains only ‘x’ in it.

Examples:

Input : s = “oooxooooxooo”
Output : 3

Input : s = “oooxoooo”
Output : 4

Source :OYO Rooms Interview Set 2

The idea is to find the length of longest contiguous ‘o’. Also check, if ‘x’ is present at right side and left side of contiguous ‘o’ then time period will be reduce to half the maximum length else time period will be equal to the maximum length.

Below is the implementation of above approach.

## C++

 `// C++ program to Find time taken for signal ` `// to reach all positions in a string ` `#include ` `using` `namespace` `std; ` ` `  `// Returns time needed for signal to traverse ` `// through complete string. ` `int` `maxLength(string s, ``int` `n) ` `{ ` `    ``int` `right = 0, left = 0; ` `    ``int` `coun = 0, max_length = INT_MIN; ` ` `  `    ``s = s + ``'1'``; ``// for the calculation of last index ` `    ``// for strings like oxoooo, xoxxoooo .. ` ` `  `    ``for` `(``int` `i = 0; i <= n; i++) { ` `        ``if` `(s[i] == ``'o'``) ` `            ``coun++; ` ` `  `        ``else` `{ ` ` `  `            ``// if coun is greater than max_length ` `            ``if` `(coun > max_length) { ` ` `  `                ``right = 0; ` `                ``left = 0; ` ` `  `                ``// if 'x' is present at the right side ` `                ``// of max_length ` `                ``if` `(s[i] == ``'x'``) ` `                    ``right = 1; ` ` `  `                ``// if 'x' is present at left side of ` `                ``// max_length ` `                ``if` `(((i - coun) > 0) && (s[i - coun - 1] == ``'x'``)) ` `                    ``left = 1; ` ` `  `                ``// We use ceiling function to handle odd ` `                ``// number 'o's ` `                ``coun = ``ceil``((``double``)coun / (right + left)); ` ` `  `                ``max_length = max(max_length, coun); ` `            ``} ` `            ``coun = 0; ` `        ``} ` `    ``} ` ` `  `    ``return` `max_length; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``string s = ``"oooxoooooooooxooo"``; ` `    ``int` `n = s.size(); ` `    ``cout << maxLength(s, n); ` `    ``return` `0; ` `} `

## Java

 `// Java program to Find time taken for signal ` `// to reach all positions in a string ` `public` `class` `GFG { ` ` `  `    ``// Returns time needed for signal to traverse ` `    ``// through complete string. ` `    ``static` `int` `maxLength(String s, ``int` `n) ` `    ``{ ` `        ``int` `right = ``0``, left = ``0``; ` `        ``int` `coun = ``0``, max_length = Integer.MIN_VALUE; ` ` `  `        ``s = s + ``'1'``; ``// for the calculation of last index ` `        ``// for strings like oxoooo, xoxxoooo .. ` ` `  `        ``for` `(``int` `i = ``0``; i <= n; i++) { ` `            ``if` `(s.charAt(i) == ``'o'``) ` `                ``coun++; ` ` `  `            ``else` `{ ` ` `  `                ``// if coun is greater than max_length ` `                ``if` `(coun > max_length) { ` ` `  `                    ``right = ``0``; ` `                    ``left = ``0``; ` ` `  `                    ``// if 'x' is present at the right side ` `                    ``// of max_length ` `                    ``if` `(s.charAt(i) == ``'x'``) ` `                        ``right = ``1``; ` ` `  `                    ``// if 'x' is present at left side of ` `                    ``// max_length ` `                    ``if` `(((i - coun) > ``0``) && (s.charAt(i - coun - ``1``) == ``'x'``)) ` `                        ``left = ``1``; ` ` `  `                    ``// We use ceiling function to handle odd ` `                    ``// number 'o's ` `                    ``coun = (``int``)Math.ceil((``double``)coun / (right + left)); ` ` `  `                    ``max_length = Math.max(max_length, coun); ` `                ``} ` `                ``coun = ``0``; ` `            ``} ` `        ``} ` ` `  `        ``return` `max_length; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``String s = ``"oooxoooooooooxooo"``; ` `        ``int` `n = s.length(); ` `        ``System.out.println(maxLength(s, n)); ` `    ``} ` `    ``// This code is contributed by ANKITRAI1 ` `} `

## Python3

 `# Python3 program to Find time taken for signal  ` `# to reach all positions in a string ` `import` `sys ` `import` `math ` ` `  `# Returns time needed for signal  ` `# to traverse through complete string.  ` `def` `maxLength(s, n): ` `    ``right ``=` `0` `    ``left ``=` `0` `    ``coun ``=` `0` `    ``max_length ``=` `-``(sys.maxsize``-``1``) ` `     `  `    ``# for the calculation of last index  ` `    ``s ``=` `s``+``'1'`  `     `  `    ``# for strings like oxoooo, xoxxoooo   ` `    ``for` `i ``in` `range``(``0``, n ``+` `1``): ` `        ``if` `s[i]``=``=``'o'``: ` `            ``coun``+``=` `1` `        ``else``: ` `             `  `            ``# if coun is greater than  ` `            ``# max_length  ` `            ``if` `coun>max_length: ` `                ``right ``=` `0` `                ``left ``=` `0` `                 `  `                ``# if 'x' is present at the right side  ` `                ``# of max_length  ` `                ``if` `s[i]``=``=``'x'``: ` `                    ``right ``=` `1` `                     `  `                ``# if 'x' is present at left side of  ` `                ``# max_length  ` `                ``if` `i``-``coun>``0` `and` `s[i``-``coun``-``1``] ``=``=` `'x'``: ` `                    ``left ``=` `1` `                     `  `                ``# We use ceiling function to handle odd  ` `                ``# number 'o's  ` `                ``coun ``=` `math.ceil(``float``(coun``/``(right ``+` `left))) ` `                ``max_length ``=` `max``(max_length, coun) ` `            ``coun ``=` `0` ` `  `    ``return` `max_length ` ` `  `# Driver code ` `if` `__name__``=``=``'__main__'``: ` `    ``s ``=` `"oooxoooooooooxooo"` `    ``n ``=` `len``(s) ` `    ``print``(maxLength(s, n)) ` ` `  `# This code is contributed by ` `# Shrikant13 `

## C#

 `// C# program to Find time taken ` `// for signal to reach all ` `// positions in a string ` `using` `System; ` ` `  `class` `GFG { ` ` `  `    ``// Returns time needed for signal ` `    ``// to traverse through complete string. ` `    ``static` `int` `maxLength(``string` `s, ``int` `n) ` `    ``{ ` `        ``int` `right = 0, left = 0; ` `        ``int` `coun = 0, max_length = ``int``.MinValue; ` ` `  `        ``s = s + ``'1'``; ``// for the calculation of last index ` ` `  `        ``// for strings like oxoooo, xoxxoooo .. ` `        ``for` `(``int` `i = 0; i <= n; i++) { ` `            ``if` `(s[i] == ``'o'``) ` `                ``coun++; ` ` `  `            ``else` `{ ` ` `  `                ``// if coun is greater than max_length ` `                ``if` `(coun > max_length) { ` `                    ``right = 0; ` `                    ``left = 0; ` ` `  `                    ``// if 'x' is present at the right ` `                    ``// side of max_length ` `                    ``if` `(s[i] == ``'x'``) ` `                        ``right = 1; ` ` `  `                    ``// if 'x' is present at left side ` `                    ``// of max_length ` `                    ``if` `(((i - coun) > 0) && (s[i - coun - 1] == ``'x'``)) ` `                        ``left = 1; ` ` `  `                    ``// We use ceiling function to ` `                    ``// handle odd number 'o's ` `                    ``coun = (``int``)Math.Ceiling((``double``)coun / (right + left)); ` ` `  `                    ``max_length = Math.Max(max_length, coun); ` `                ``} ` `                ``coun = 0; ` `            ``} ` `        ``} ` ` `  `        ``return` `max_length; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``string` `s = ``"oooxoooooooooxooo"``; ` `        ``int` `n = s.Length; ` `        ``Console.Write(maxLength(s, n)); ` `    ``} ` `} ` ` `  `// This code is contributed ` `// by ChitraNayal `

## PHP

 ` ``\$max_length``)  ` `            ``{ ` ` `  `                ``\$right` `= 0; ` `                ``\$left` `= 0; ` ` `  `                ``// if 'x' is present at the right side  ` `                ``// of max_length ` `                ``if` `(``\$s``[``\$i``] == ``'x'``)  ` `                    ``\$right` `= 1; ` ` `  `                ``// if 'x' is present at left side of  ` `                ``// max_length ` `                ``if` `(((``\$i` `- ``\$coun``) > 0) &&  ` `                    ``(``\$s``[``\$i` `- ``\$coun` `- 1] == ``'x'``))  ` `                    ``\$left` `= 1; ` ` `  `                ``// We use ceiling function to handle odd ` `                ``// number 'o's ` `                ``\$coun` `= (int)``ceil``((double)``\$coun` `/ (``\$right` `+  ` `                                                   ``\$left``)); ` ` `  `                ``\$max_length` `= max(``\$max_length``, ``\$coun``); ` `            ``} ` `            ``\$coun` `= 0; ` `        ``} ` `    ``} ` ` `  `    ``return` `\$max_length``; ` `} ` ` `  `// Driver code ` `\$s` `= ``"oooxoooooooooxooo"``; ` `\$n` `= ``strlen``(``\$s``); ` `echo``(maxLength(``\$s``, ``\$n``)); ` ` `  `// This code is contributed by Code_Mech `

Output:

```5
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

Article Tags :
Practice Tags :

12

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.