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 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);
}
}
Try this easy and small length of code . 🙂 in JAVA (code = DigitalMachine)
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
sc.nextLine();
String binary = sc.nextLine();
int count = 0;
for(int i =1 ; i<N-1 ; i++){
if(binary.charAt(i)=='1'){
count++;
}
else{
continue;
}
}
System.out.print(count);
sc.close();
}
}
Can you provide the solution of above ques in python language?
Yes can you give the solution in c++ language
n=6
s=”101111110″
l=[]
count=0
for i in s:
if(i==”1″):
count=count+1
else:
l.append(count)
count=0
print(max(l))