Find the consecutive number 1

I am trying to find the number of consecutive 1s in binary format.

Example: Converting a decimal to binary and looking for consecutive 1

static int count = 0;
static int max = 0;
static int index = 1;

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int n = scan.nextInt();
    scan.close();
    String b = Integer.toBinaryString(n);
    char[] arr = b.toCharArray();
    System.out.println(arr);

    for (int i = 0; i < b.length(); i++) {
        if (arr[i] == index) {
            count++;
        } else {
            count = 0;
        }

        if (count > max) {
            max = count;
        }
    }

    System.out.println(max);
}

I always get 0. It seems that the condition is not working in my code. Could you offer your suggestion about where I am wrong?

+4
source share
5 answers

qusetion , AFAIU , 1. , if (arr[i] == index), char integer, arr char. ? , char array integer index char. , .

if (arr[i] == index + '0')

. , , , . - ,

private static int maxConsecutiveOnes(int x) {
        // Initialize result
        int count = 0;

        // Count the number of iterations to
        // reach x = 0.
        while (x!=0) {
            // This operation reduces length
            // of every sequence of 1s by one.
            x = (x & (x << 1));

            count++;
        }

        return count;
}

:

      11101111   (x)
    & 11011110   (x << 1)
    ----------
      11001110   (x & (x << 1)) 
        ^    ^
        |    |
   trailing 1 removed
+2

, 1 int. . 7917=0b1111011101101 4 ( 1: 1, 2, 3, 4).

( ). ( 1 ) max . , , 1, , 0, getMaxConsecutiveSetBit1.

- 1, getMaxConsecutiveSetBit2. + . , char Java - int JVM. , char int 1, . , 1, - '1'.

public static void main(String[] args) {
    try (Scanner scan = new Scanner(System.in)) {
        int val = scan.nextInt();
        System.out.println(Integer.toBinaryString(val));
        System.out.println(getMaxConsecutiveSetBit1(val));
        System.out.println(getMaxConsecutiveSetBit2(val));
    }
}

public static int getMaxConsecutiveSetBit1(int val) {
    int max = 0;
    int cur = 0;

    while (val != 0) {
        if ((val & 0x1) != 0)
            cur++;
        else {
            max = Math.max(max, cur);
            cur = 0;
        }

        val >>>= 1;
    }

    return Math.max(max, cur);
}

public static int getMaxConsecutiveSetBit2(int val) {
    int max = 0;
    int cur = 0;

    for (char ch : Integer.toBinaryString(val).toCharArray()) {
        if (ch == '1')
            cur++;
        else {
            max = Math.max(max, cur);
            cur = 0;
        }
    }

    return Math.max(max, cur);
}
+1

index int char:

static char index = 1;

:

if (arr[i] == index)

. int 1 ( , index), char '1' ( arr[]) , ASCII char int 1. , char '1' ASCII 49, , 1 (49 1).

, ASCII , , . , char int ==.

index char , .

+1
source

Using your loop structure and changing a few things, as well as adding some other characteristics to the report that may be useful. I count the total number 1 in number, the number of consecutive 1 (groups of 1) and the largest number of consecutive 1. In addition, the for loop was cyclic based on the length of the string, and not on the length of the array, which is just a nit sort. Here is the code

int count = 0;
int max = 0;
char index = '1';
int consecutiveOnePairs = 0;
int numberOfOnes = 0;

Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
String b = Integer.toBinaryString(n);
char[] arr = b.toCharArray();
System.out.println(arr);

for (int i = 0; i < arr.length; i++) {
    if (arr[i] == index)
    {
        count++;
        numberOfOnes++;
    }

    if((i + 1 == arr.length && count > 1) || arr[i] != index)
    {
        if(count > 1)
            consecutiveOnePairs++;
        if (count > max)
            max = count;
        count = 0;
    }
}

System.out.println("Total Number of 1 in " + n + " is " + numberOfOnes);
System.out.println("Total Number of Consecutive 1 in " + n + " is " + consecutiveOnePairs);
System.out.println("Greatest Number of Consecutive 1 in " + n + " is " + max);
scan.close();

Output

13247
11001110111111
Total Number of 1 in 13247 is 11
Total Number of Consecutive 1 in 13247 is 3
Greatest Number of Consecutive 1 in 13247 is 6

511
111111111
Total Number of 1 in 511 is 9
Total Number of Consecutive 1 in 511 is 1
Greatest Number of Consecutive 1 in 511 is 9

887
1101110111
Total Number of 1 in 887 is 8
Total Number of Consecutive 1 in 887 is 3
Greatest Number of Consecutive 1 in 887 is 3
0
source

If you are using Java 8, you can try this snippet:

 public int maxOneConsecutive(int x)
  {
    String intAsBinaryStr = Integer.toBinaryString(x);
    String[] split = intAsBinaryStr.split("0");
    return Arrays.stream(split)
        .filter(str -> !str.isEmpty())
        .map(String::length)
        .max(Comparator.comparingInt(a -> a)).orElse(0);
  }
0
source

Source: https://habr.com/ru/post/1691713/


All Articles