Binary Pattern (3)

Computer Science Level pending

In binary puzzles , a grid must be filled with ones and zeroes. Each column and row must be a sequence following these rules:

  • The numbers of zeroes and ones are equal.
  • There may be no subsequence of three or more of the same number (zero or one).

For instance, 110010 110010 is a valid sequence; 100011 100011 is invalid because there are three zeroes in a row; 110101 110101 is invalid because there are more ones than zeroes.

How many valid sequences of length fourteen are possible?


The answer is 518.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

In Java:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
int k = 7;
int N = 0, maxpat = 1 << (2*k);
for (int pat = 0; pat < maxpat; pat++) {
    int b = 0, nb = 0, sb = 0, snb = 0, x = pat;
    for (int i = 0; i < 2*k; i++, x >>= 1) {
        if ((x & 1) == 1) {
            b++; if (b > k) break;
            snb = 0; sb++;
            if (sb >= 3) break;
        } else {
            nb++; if (nb > k) break;
            sb = 0; snb++;
            if (snb >= 3) break;
        }
    }

    if (sb < 3 && snb < 3 && b == nb) N++;
}

System.out.println(N);

Output:

1
518

Clarification of this somewhat dense code:

  • Each pattern may be represented as a 2 k 2k -bit number, i.e. positive integer less than maxpat = 2 2 k =\ 2^{2k} . The loop variable pat loops through all such patterns.

  • In the inner loop, i indexes the bits in the pattern, from right to left. In each step, the pattern (in variable x ) is shifted one to the right. We study the bits one by one by looking at x&1 .

  • The variables b and nb ("bit" and "no bit") count the numbers of ones and zeroes. After the loop, we check that they are equal. If either of them exceeds half of the pattern length ( b > k ) the pattern is invalid, and we break out of the inner loop.

  • The variables sb and snb ("sequence of bits") count how many ones or zeroes were counted in a row. If the opposite value is read, they are reset to zero. If any of them becomes three or more (too much!), the break statement gets us out.


An alternative solution, "by hand", is doable using the generalization I presented in this problem . We have

2 [ ( 7 0 ) ( 7 0 ) + ( 7 0 ) ( 6 1 ) + ( 6 1 ) ( 6 1 ) + ( 6 1 ) ( 5 2 ) + ( 5 2 ) ( 5 2 ) + ( 5 2 ) ( 4 3 ) + ( 4 3 ) ( 4 3 ) ] = 2 [ 1 2 + 1 6 + 6 2 + 6 10 + 1 0 2 + 10 4 + 4 2 ] = 2 [ 1 + 6 + 36 + 60 + 100 + 40 + 16 ] = 2 259 = 518 . 2\cdot\left[\left(\begin{array}{c} 7 \\ 0 \end{array}\right)\left(\begin{array}{c} 7 \\ 0 \end{array}\right) + \left(\begin{array}{c} 7 \\ 0 \end{array}\right)\left(\begin{array}{c} 6 \\ 1 \end{array}\right) + \left(\begin{array}{c} 6 \\ 1 \end{array}\right)\left(\begin{array}{c} 6 \\ 1 \end{array}\right) + \left(\begin{array}{c} 6 \\ 1 \end{array}\right)\left(\begin{array}{c} 5 \\ 2 \end{array}\right) + \left(\begin{array}{c} 5 \\ 2 \end{array}\right)\left(\begin{array}{c} 5 \\ 2 \end{array}\right) + \left(\begin{array}{c} 5 \\ 2 \end{array}\right)\left(\begin{array}{c} 4 \\ 3 \end{array}\right) + \left(\begin{array}{c} 4 \\ 3 \end{array}\right)\left(\begin{array}{c} 4 \\ 3 \end{array}\right) \right] \\ = 2\cdot\left[1^2 + 1\cdot 6 + 6^2 + 6\cdot 10 + 10^2 + 10\cdot 4 + 4^2\right] \\ = 2\cdot\left[1 + 6 + 36 + 60 + 100 + 40 + 16\right] = 2\cdot 259 = \boxed{518}.

It's correct, but in good programming style you must add comments otherwise you can't understand your program after some time. And give less senseless variable names. And call subprograms. You use OOP language but it could be Fortran, you don't use its possiblities.

Hasmik Garyaka - 3 years, 8 months ago

Log in to reply

There is very little need to use OOP capabilities for a simple computational task like this. Even subprograms are not needed. You are right that the code could use some clarifying comments. I will document under my code.

Arjen Vreugdenhil - 3 years, 8 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...