Home > Enterprise >  counting the sequences of 0s in binary number
counting the sequences of 0s in binary number

Time:10-25

package com.company;

public
class ToBinary
    {
        public String convertToBinary( int n)
            {
                return Integer.toBinaryString( n );
            }
        public int howMany(int number)
        {
            String x = convertToBinary(number);
            char[] y = x.toCharArray();
            int counter=0; //how many seq in binary number
            /*
                sequence counts if 0s are surrounded by 1 f.ex 101 1001 is 1 sequence
                                                               100100101 is 3 seq
                                                               1010 is 1 seq
             */
            int temp=0;
            int maxGlobal=0;
            int maxLocal =0;
            for(int i=1;i<y.length-1;i  )
                {
                    if(y[i]!=y[i-1] && y[i]!=y[i 1])
                        counter  ;
                    if(y[i]==0 && y[i-1]==0)
                        {
                            maxLocal  =1;
                        }
                    if(y[i]==0 && y[i 1]==1)
                    {
                        maxLocal  =1;
                        maxGlobal= maxLocal;
                        maxLocal =0;
                    }
                    else
                        continue;
                }
            System.out.println(maxGlobal);
            return counter;
        }

    }

I wrote some code but i don't know what I am doing here wrong.

CodePudding user response:

Instead of debugging parsing logic it may be more convenient to process the input, get rid of irrelevant parts (leading and or trailing zeroes), find valid groups of 0s, define their length and then count the occurrences or find the longest sequence:

  • using String::split String::replaceAll
private static IntStream toBinary(int n) {
    return Arrays.stream(Integer.toBinaryString(n)
            .replaceAll("(^0 1|10 $)", "1") // remove leading/trailing 0s
            .split("1 ")
        ) // Stream<String>
        .mapToInt(String::length)
        .filter(i -> i > 1);
}

public static int howManyZeroSeqs(int n) {
    return (int) toBinary(n).count();
}

public static int maxZeroSeqLength(int n) {
    return toBinary(n).max().orElse(0);
}

Tests

for (int i : new int[]{0b01001001000100, 0b010010010000100100010000}) {
    System.out.println("#seqs ("   Integer.toBinaryString(i)   ") = "   howManyZeroSeqs(i));
    System.out.println("max length ("   Integer.toBinaryString(i)   ") = "   maxZeroSeqLength(i));
}

Output:

#seqs (1001001000100) = 3
max length (1001001000100) = 3
#seqs (10010010000100100010000) = 5
max length (10010010000100100010000) = 4

  • use matching pattern to detect sequences of at least 2 zeroes not at the end of the string

online demo

static Pattern ZEROSEQ = Pattern.compile("(?=1(0{2,} )(?!$))1");
private static IntStream toBinary(int n) {
    return ZEROSEQ.matcher(Integer.toBinaryString(n))
        .results()
        .map(mr -> mr.group(1))
        .mapToInt(String::length);
}

(?=1(0{2,} )(?!$))1 - ?=1(0{2,} ) positive lookahead: 1 followed by a capturing group for the sequence of at least 2 zeroes

(?!$) - negative lookahead - group of 0s not followed by the end of the string 1 - closing 1

The test results are the same.

CodePudding user response:

how about:

String trimmed = convertToBinary(number).replaceAll("0 $","");
return (trimmed.length() - trimmed.replaceAll("10","").length())/2;

It doesn't matter how long the sequence is, just that it has a zero in it and isn't at the end of the string.

  • Related