Wipro Coding Question 4

Digital Machine

Digital Machine is one of the coding question asked in previous wipro Elite NTH Exam. In this article, we will discuss about the Maximum signal problem with their solution in C++ and Java. 

Wipro Coding Questions

Question 4

A digital machine generates binary data which consists of a string of 0s and 1s. A maximum signal M, in the data, consists of the maximum number of either 1s or 0s appearing consecutively in the data but M can’t be at the beginning or end of the string. Design a way to find the length of the maximum signal.

Input
The first line of the input consists of an integer N, representing the length of the binary string. The second line consists of a string of length N consisting of 0s and 1s only.

Output
Print an integer representing the length of the maximum signal.

Example
Example 1:

Input
6
101000

Output
1

Explanation
For 101000, M can be 0 at the second index or at the third index so in both cases max length = 1.

Example2:

Input
9
101111110

Output
6

Explanation
For 101111110, M = 111111 so maxlength = 6.

import java.util.*;
class Solution
{
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
int n = sc.nextInt ();
String str = sc.next ();
int max = 0, count = 0;
char ch = str.charAt (n - 1);
int i = n - 1;

while (str.charAt (i) == ch)
i--;
str = str.substring (0, i + 1);

ch = str.charAt (0);
i = 0;
while (str.charAt (i) == ch)
i++;
str = str.substring (i);

int countZero = 0, countOne = 0;

for (i = 0; i < str.length (); i++)
{
if (str.charAt (i) == '0')
countZero++;
else
countZero = 0;
if (str.charAt (i) == '1')
countOne++;
else
countOne = 0;
max = Math.max (max, Math.max (countZero, countOne));
}
System.out.println (max);
}
}

2 comments on “Wipro Coding Question 4”