# 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. ## 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 count[] = new int;        int max = 0;        int countOne = 0;        int flag = 0;        char ch = str.charAt (0);        int same = 0;                for (int i = 1; i < n; i++){                        if (same == 1 && ch == str.charAt (i))            continue;                        else            same = 0;                        if (str.charAt (i) == '1'){                                if ((i == 1 || i == n - 1)&& str.charAt (i - 1) == str.charAt (i)){                                        countOne = 0;                    flag = 0;                    same = 1;                    ch = str.charAt (i);                    continue;                                    }                countOne++;                flag = 1;                            }                        else if (str.charAt (i) == '0' && flag == 1){                                countOne = 0;                flag = 0;                            }                        max = Math.max (max, countOne);                    }                int countZero = 0;        flag = 0;        same = 0;                for (int i = 1; i < n; i++){                        if (same == 1 && ch == str.charAt (i))            continue;                        else            same = 0;                        if (str.charAt (i) == '0'){                                if ((i == 1 || i == n - 1)&& str.charAt (i - 1) == str.charAt (i)){                                        countOne = 0;                    flag = 0;                    same = 1;                    ch = str.charAt (i);                    continue;                                    }                countZero++;                flag = 1;                            }                        else if (str.charAt (i) == '1' && flag == 1){                countZero = 0;                flag = 0;                            }            max = Math.max (max, countZero);                    }        System.out.println (max);            }}`
`#include<bits/stdc++.h>using namespace std;int main(){        int n;    String str;        int count;        int maxi = 0, countOne=0, flag=0, same=0;        char ch = str;        for (int i = 1; i < n; i++){                if (same == 1 and ch == str[i])        continue;                else        same = 0;                if (str[i] == '1'){                        if ((i == 1 or i == n - 1) and str[i - 1] == str[i]){                                countOne = 0;                flag = 0;                same = 1;                ch = str[i];                continue;                            }                        countOne++;            flag = 1;                    }                else if (str[i] == '0' and flag == 1){            countOne = 0;            flag = 0;                    }                maxi = max (maxi, countOne);            }        int countZero = 0;        flag = 0;    same = 0;        for (int i = 1; i < n; i++){                if (same == 1 and ch == str[i])          continue;                  else        same = 0;                if (str[i] == '0'){                        if ((i == 1 or i == n - 1) and str[i - 1] == str[i]){                countOne = 0;                flag = 0;                same = 1;                ch = str[i];                continue;                            }                        countZero++;            flag = 1;                    }                else if (str[i] == '1' and flag == 1){            countZero = 0;            flag = 0;                    }        maxi = max (maxi, countZero);            }        cout<<maxi;        return 0;  }`