Take the sequence of all natural numbers in binary, (1, 10, 11, ..) then write them vertically beside each-other like this (least significant bit on top; 0s have been replaced with spaces):

1 1 1 1 1 1 1 1 1
 11  11  11  11
   1111    1111

Notice there are areas of connected 1s in the binary representation of the number.

Your task is, for a given n, calculate the biggest continuous area in the binary representation of all numbers less than n. If you start with n=1 and consider all numbers less than or equal to n, the sequence starts like this:

(1, 1, 3, 3, 3, 4, 7, 7, 7, 7, 7, 7, 8, 11, 15, 15, 15...)

Areas are joined if they connect horizontally or vertically, diagonals don't count.

rules apply: you can choose to consider either all numbers strictly less than N or those less than or equal to N, and either 0 or 1. This is , shortest answer wins.

  • \$\begingroup\$ Sandbox link \$\endgroup\$
    – mousetail
    Commented Mar 2, 2023 at 13:46
  • 1
    \$\begingroup\$ @LuisMendo In accordance to sequence rules you can start with either 1 or 0, so you can pick either option for your answer \$\endgroup\$
    – mousetail
    Commented Mar 2, 2023 at 14:04

7 Answers 7


JavaScript (V8), 67 bytes

Prints the sequence infinitely.


Try it online!


This is based on the observation that the biggest local area is always those of the pattern formed by the leading 1's (in red below).

If the area of the leading pattern is \$N\$, any non-leading pattern may at best reach an area of \$N-1\$ (like the green one in figure 2).


(Figure 1 is for \$n=8\$ to \$n=15\$. Figure 2 is for \$n=16\$ to \$n=23\$.)


for(              // infinite loop:
  m =             //   m = current maximum
  n = 0;          //   n = counter
  g = k =>        //   g is a helper recursive function taking k
                  //   and returning the sum of s and the number
                  //   of leading 1's in k
    k ?           //   if k is not 0:
      !(          //     increment the final result if ...
        k & k + 1 //     ... k and k+1 have no bits in common
      ) +         //     (i.e. k+1 is a power of 2)
      g(k >> 1)   //     do a recursive call with floor(k / 2)
    :             //   else:
      s;          //     stop and return s
  print(          //   after each iteration, print ...
    m = m > s ? m //   ... the maximum of m and s
              : s //   (and update m accordingly)
  )               //
)                 //
  s =             //   s is the area of the pattern of leading 1's
    n & ++n ?     //   increment n; if n is not a power of 2:
      g(n)        //     update s to g(n)
    :             //   else:
      1           //     reset s to 1
  • \$\begingroup\$ Sad, I was hoping it would take a bit longer before someone found a efficient solution, nice job \$\endgroup\$
    – mousetail
    Commented Mar 2, 2023 at 17:46
  • 1
    \$\begingroup\$ @mousetail Well, it's probably not optimal. A more direct formula may exist, or at least an approach with a single recursion giving the answer for any \$n\$ without having to keep track of the maximum from the beginning. \$\endgroup\$
    – Arnauld
    Commented Mar 2, 2023 at 17:54

MATL, 14 13 bytes

1 byte saved thanks to @Suever


Inputs n and outputs the n-th term of the sequence, using the "less or equal" definition.

Try it MATL online!


:       % Implicit input: n. Range [1 2 ... n]
B       % Convert to binary. Gives an n-row matrix
4&1ZI   % Label connected components, using 4-neighbourhood. This labels each
        % connected component of cells equal to 1 with an integer, starting at 1
Xz      % Nonzeros. Removes zeros and reshapes into a column vector
5#XM    % Second output of 'mode' function: gives the number of times that the
        % mode occurs. Implicit display
  • 1
    \$\begingroup\$ You may be able to use the second output of mode rather than 8#uX> like this. That may be a good candidate for & for XM \$\endgroup\$
    – Suever
    Commented Mar 2, 2023 at 19:51
  • \$\begingroup\$ @Suever Great ideas both! Thank you! \$\endgroup\$
    – Luis Mendo
    Commented Mar 2, 2023 at 21:29

Python3, 333 bytes:

def f(n):
 k=[i[2:][::-1]+'0'*(max(map(len,t))-len(i))for i in t]
 q,l=[(x,y)for x,r in E(k)for y,u in E(r)if'1'==u],[]
 while q:
  while Q:
   for X,Y in[(0,1),(0,-1),(1,0),(-1,0)]:
    if C in q:Q+=[C];q.remove(C);s+=1
 return max(l)

Try it online!

  • \$\begingroup\$ -2 with C=x+X,y+Y \$\endgroup\$
    – Vélimir
    Commented Mar 3, 2023 at 23:53
  • \$\begingroup\$ And 2 others if you put t, k and q on the same line by using semicolons \$\endgroup\$
    – Vélimir
    Commented Mar 3, 2023 at 23:56
  • \$\begingroup\$ -1 with *t,=map(bin,range(1,n)) \$\endgroup\$
    – Vélimir
    Commented Mar 4, 2023 at 0:05

Python, 95 93 92 88 bytes

while g:=lambda k:k and(k&k+1<1)+g(k>>1):print(m:=max(m,s:=n&(n:=n+1)<1or g(n)+s))

Attempt This Online!

Outputs True instead of 1. +2 bytes to fix this

Port of Arnauld's JS answer.

-2 thanks to @Arnauld
-4 thanks to @


Charcoal, 28 bytes


Try it online! Link is to verbose version of code. Outputs the first n terms. Explanation:


Repeat n times.


If the effective 1-based index is a power of 2, then start the new area from 1, otherwise add the number of leading 1s to it.


Save the size of the latest area.


Output the size of the largest area.

I did find some relevant sequences in the OEIS:

  • A000225 is the list of indices where the size of the largest contiguous area equals the index.
  • A090996 is the number of leading 1s.
  • A267604 appears to be list of indices where the new area overtakes the old area in size.

Unfortunately I was unable to use any of these sequences to reduce my byte count.


Jelly,  12  11 bytes


A monadic Link that accepts \$n\$ and yields the largest binary area when arranging the numbers \$1\$ to \$n\$ inclusive.

Try it online!


B«\)LÐṀFS)Ṁ - Link: positive integer, n
         )  - for each (i in [1..n]):
   )        -   for each (j in [1..i]):
B           -     convert (j) to binary
  \         -     cumulative reduce by:
 «          -       minimum (i.e. zero any bits after the first 0)
     ÐṀ     -   keep those maximal under:
    L       -     length (e.g. j = 27 -> [zeroed(binary(16))..zeroed(binary(27))])
       F    -   flatten
        S   -   sum
          Ṁ - maximum

Python 3.8, 85 bytes

lambda n:max((j:=2**len(bin(n))>>3)-1,sum(bin(i*2)[2:].find('0')for i in range(j,n)))

An anonymous function that accepts a positive integer, n, and returns the largest binary area formed using the numbers less than n.

Try it online!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.