-3

I need help with a program that'll count the maximum number of 0's between two 1's in a given binary number in c#. For example 1100101(binary for 101) the maximum number of 0's between two 1's are 2. Any help?

This the code for counting the 0's in the string but not the 0's between 1's

string bin = "";
int max = 0;
for (int i = rem.Length - 1; i >= 0; i--)
{
     if (rem[i] == '0') 
     {
        bin = bin + rem[i];
        c++;
     }    
     else
     {
        bin = bin + rem[i];
     }
} 
  • 2
    show some code that you tried. We don't write full code for you. For that, you need to hire a freelancer. – Sourav Ghosh Feb 19 '17 at 07:39
  • You can do this in a one liner, it's rather simple. What have you tried? – InBetween Feb 19 '17 at 07:39
  • I can't come up with a logic to solve the question. I can count the number or 0's in the string but can't count between two 1's – Haris Mashood Feb 19 '17 at 07:40
  • @SouravGhosh this is the code I'm using to count the 0's "string bin = ""; int max = 0; for (int i = rem.Length - 1; i >= 0; i--) { if (rem[i] == '0') { bin = bin + rem[i]; c++; } else { bin = bin + rem[i]; } " – Haris Mashood Feb 19 '17 at 07:47
  • @HarisMashood Welcome to Stackoverflow. Please edit your question and add the code you commented, into the question. Other users will be more likely to help you. Many users don't read comments before answering. Please take some time to visit the [help center](http://stackoverflow.com/help) and also read [How to Ask](http://stackoverflow.com/help/how-to-ask), [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve), so you can learn what types of questions are accepted here, how to write questions, and how to use this site effectively. – Sourav Ghosh Feb 19 '17 at 07:53
  • Please see [Why is “Can someone help me?” not an actual question?](http://meta.stackoverflow.com/q/284236) – EJoshuaS - Stand with Ukraine Feb 19 '17 at 08:02

3 Answers3

2

Try this (updated code):

        string num = "1011100";

        char[] myChar = num.ToCharArray();

        bool blFirst = false;  //This will check if there is "1" on first element of the input
        bool blLast = false;  //This will check if there is "1" on last element of the input

        if (myChar[0] == '0') //If the condition is true we will remove this on the result
            blFirst = true;

        if (myChar[myChar.Length - 1] == '0')
            blLast = true;

        string[] intArr = num.Split('1').ToArray();

        List<string> intResult = new List<string>();

        //We will make sure that all results only contains '0' and not empty.
        intResult = intArr.Where(x => x.All(y => y == '0') && x != string.Empty).Select(x => x).ToList();

        if (blFirst == true)
            intResult.RemoveAt(0);

        if (blLast == true)
            intResult.RemoveAt(intResult.Count - 1);

        //After all conditions are met (Get only '0' numbers between 1), that's the time we get the maximum count
        intOutput = intResult.Select(x => x).Max(x => x.Length);
Willy David Jr
  • 8,604
  • 6
  • 46
  • 57
  • Thanks mate. You're a lifesaver. – Haris Mashood Feb 19 '17 at 07:59
  • I'm using this to check, but in sometimes it's incorrect string bin = ""; for (int i = rem.Length - 1; i >= 0; i--) { bin = bin + rem[i]; } Console.WriteLine(bin); string m = bin; string[] intArr = m.Split('1').ToArray(); //This will get the highest count of zero, in this example, is 3 int intResult = intArr.Select(x => x).Max(x => x.Length); Console.Write("Max numnber of zeros are: "); Console.WriteLine(intResult); – Haris Mashood Feb 19 '17 at 08:08
  • Suppose for 110100(52) it says that the max. number of zeros between 1's are two but the correct answer is 1. – Haris Mashood Feb 19 '17 at 08:10
  • I've given it an input of 110100 and it still gives output of 2. string num = "110100"; string[] intArr = num.Split('1').ToArray(); //This will get the highest count of zero, in this example, is 3 string intResult = intArr.Where(x => x.All(y => y == '0')).Select(x => x).Max(x => x.Length.ToString()); Console.WriteLine(intResult); – Haris Mashood Feb 19 '17 at 14:07
  • I updated everything to make sure all your conditions are met. Kindly check my updated code and let me know if it works now in your part @HarisMashood – Willy David Jr Feb 19 '17 at 14:47
  • Works flawlessly. Thanks mate. – Haris Mashood Feb 19 '17 at 15:25
2

A simpler version:

string num = "1011100";

//trim the 0's at the start and the end
num=num.Trim(new Char[] { '0' });

string[] intArr = num.Split(new string[] {"1"}, StringSplitOptions.RemoveEmptyEntries);

int Output = intArr.Max(x => x.Length);
0

This should work:

public static int CountZerosBetweenOnes(string binary)
{
    var indicesOfOnes = 
        binary.Select((c, i) => new {c, i})
        .Where(x => x.c == '1')
        .Select(x => x.i);

    return 
        indicesOfOnes
        .Zip(indicesOfOnes.Skip(1), (a, b) => b - a - 1)
        .DefaultIfEmpty(0)
        .Max();
}

It does assume that binary does not contain any characters other than 1 and 0.

If you want a version that accepts an int:

public static int CountZerosBetweenOnes(int binary)
{
    var indicesOfOnes = 
        Convert.ToString(binary, 2)
        .Select((c, i) => new {c, i})
        .Where(x => x.c == '1')
        .Select(x => x.i);

    return 
        indicesOfOnes
        .Zip(indicesOfOnes.Skip(1), (a, b) => b - a - 1)
        .DefaultIfEmpty(0)
        .Max();
}

I tested this with the following test code, which outputs the expected result:

public static void Main(string[] args)
{
    test("0000000000000"); // Outputs 0
    test("0000100010000"); // Outputs 3
    test("1111111111111"); // Outputs 0
    test("1000000000001"); // Outputs 11
    test("1000000000000"); // Outputs 0
    test("0000000000001"); // Outputs 0
    test("1010101010101"); // Outputs 1
    test("1001000100001"); // Outputs 4
}

static void test(string s)
{
    Console.WriteLine(CountZerosBetweenOnes(s));
}

Or run it on .Net Fiddle here: https://dotnetfiddle.net/H2Mt8w

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • Thanks mate but it only counts the total zeros in the string not between the 1s – Haris Mashood Feb 19 '17 at 12:00
  • @HarisMashood No, it calculates the maximum number of consecutive zeros in the string that are bracketed by 1s. I verified that `CountZerosBetweenOnes("0000100010000")` returns `3` correctly. How were you testing it? – Matthew Watson Feb 19 '17 at 16:59
  • I was taking the input from the user and converting it into binary and then I was passing it to the function. – Haris Mashood Feb 20 '17 at 09:50
  • @HarisMashood Strange that you though this didn't work, because it does work (as per the test program). – Matthew Watson Feb 20 '17 at 10:21