Find time taken for signal to reach all positions in a string
Given a 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 the 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 a string of signals.
A string of signals 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 the longest contiguous ‘o’. Also check, if ‘x’ is present on the right side and left side of contiguous ‘o’ then the time period will be reduced to half the maximum length else the time period will be equal to the maximum length.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int maxLength(string s, int n)
{
int right = 0, left = 0;
int coun = 0, max_length = INT_MIN;
s = s + '1' ;
for ( int i = 0; i <= n; i++) {
if (s[i] == 'o' )
coun++;
else {
if (coun > max_length) {
right = 0;
left = 0;
if (s[i] == 'x' )
right = 1;
if (((i - coun) > 0) && (s[i - coun - 1] == 'x' ))
left = 1;
coun = ceil (( double )coun / (right + left));
max_length = max(max_length, coun);
}
coun = 0;
}
}
return max_length;
}
int main()
{
string s = "oooxoooooooooxooo" ;
int n = s.size();
cout << maxLength(s, n);
return 0;
}
|
Java
public class GFG {
static int maxLength(String s, int n)
{
int right = 0 , left = 0 ;
int coun = 0 , max_length = Integer.MIN_VALUE;
s = s + '1' ;
for ( int i = 0 ; i <= n; i++) {
if (s.charAt(i) == 'o' )
coun++;
else {
if (coun > max_length) {
right = 0 ;
left = 0 ;
if (s.charAt(i) == 'x' )
right = 1 ;
if (((i - coun) > 0 ) && (s.charAt(i - coun - 1 ) == 'x' ))
left = 1 ;
coun = ( int )Math.ceil(( double )coun / (right + left));
max_length = Math.max(max_length, coun);
}
coun = 0 ;
}
}
return max_length;
}
public static void main(String args[])
{
String s = "oooxoooooooooxooo" ;
int n = s.length();
System.out.println(maxLength(s, n));
}
}
|
Python3
import sys
import math
def maxLength(s, n):
right = 0
left = 0
coun = 0
max_length = - (sys.maxsize - 1 )
s = s + '1'
for i in range ( 0 , n + 1 ):
if s[i] = = 'o' :
coun + = 1
else :
if coun>max_length:
right = 0
left = 0
if s[i] = = 'x' :
right = 1
if i - coun> 0 and s[i - coun - 1 ] = = 'x' :
left = 1
coun = math.ceil( float (coun / (right + left)))
max_length = max (max_length, coun)
coun = 0
return max_length
if __name__ = = '__main__' :
s = "oooxoooooooooxooo"
n = len (s)
print (maxLength(s, n))
|
C#
using System;
class GFG {
static int maxLength( string s, int n)
{
int right = 0, left = 0;
int coun = 0, max_length = int .MinValue;
s = s + '1' ;
for ( int i = 0; i <= n; i++) {
if (s[i] == 'o' )
coun++;
else {
if (coun > max_length) {
right = 0;
left = 0;
if (s[i] == 'x' )
right = 1;
if (((i - coun) > 0) && (s[i - coun - 1] == 'x' ))
left = 1;
coun = ( int )Math.Ceiling(( double )coun / (right + left));
max_length = Math.Max(max_length, coun);
}
coun = 0;
}
}
return max_length;
}
public static void Main()
{
string s = "oooxoooooooooxooo" ;
int n = s.Length;
Console.Write(maxLength(s, n));
}
}
|
PHP
<?php
function maxLength( $s , $n )
{
$right = 0; $left = 0;
$coun = 0;
$max_length = PHP_INT_MIN;
$s = $s . '1' ;
for ( $i = 0; $i <= $n ; $i ++)
{
if ( $s [ $i ] == 'o' )
$coun ++;
else
{
if ( $coun > $max_length )
{
$right = 0;
$left = 0;
if ( $s [ $i ] == 'x' )
$right = 1;
if ((( $i - $coun ) > 0) &&
( $s [ $i - $coun - 1] == 'x' ))
$left = 1;
$coun = (int) ceil ((double) $coun / ( $right +
$left ));
$max_length = max( $max_length , $coun );
}
$coun = 0;
}
}
return $max_length ;
}
$s = "oooxoooooooooxooo" ;
$n = strlen ( $s );
echo (maxLength( $s , $n ));
|
Javascript
<script>
function maxLength( s, n)
{
var right = 0, left = 0;
var coun = 0, max_length = Number.MIN_VALUE;
s = s + '1' ;
for ( var i = 0; i <= n; i++) {
if (s[i] == 'o' )
coun++;
else {
if (coun > max_length) {
right = 0;
left = 0;
if (s[i] == 'x' )
right = 1;
if (((i - coun) > 0) && (s[i - coun - 1] == 'x' ))
left = 1;
coun = Math.ceil(coun / (right + left));
max_length = Math.max(max_length, coun);
}
coun = 0;
}
}
return max_length;
}
var s = "oooxoooooooooxooo" ;
var n = s.length;
document.write( maxLength(s, n));
</script>
|
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Last Updated :
06 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...