Home

2A2B

2A2B is a shorthand notation used in the Bulls and Cows puzzle and in many programming challenges to describe feedback after a guess. The notation xAyB means that x digits are correct and in the correct position (A), and y digits are correct but in the wrong position (B).

In the common 4-digit version, both the secret and the guess have the same length. A is

Variants vary in length and rules about repeated digits. Some problems allow repeated digits; others require

Example: secret 1234 and guess 1432 yield 2A2B, because digits 1 and 3 are in the correct

Origin and usage: Bulls and Cows is a historical code-breaking game with roots in European puzzles; the

Implementation notes: a typical approach uses two passes—first to count A by direct comparison, then to count

counted
by
directly
comparing
corresponding
positions;
B
is
counted
for
digits
that
appear
in
both
numbers
but
are
not
already
counted
as
A,
with
duplicates
handled
so
that
a
digit
is
not
counted
more
times
than
it
occurs
in
the
secret.
all
digits
to
be
unique.
Some
versions
use
letters
instead
of
digits,
or
cast
the
puzzle
as
a
string-based
game.
positions,
while
digits
2
and
4
are
present
but
in
each
other’s
positions.
A/B
notation
was
adopted
in
modern
programming
challenges
in
the
early
2000s
as
a
concise
feedback
format.
2A2B
has
become
a
common
target
outcome
in
algorithmic
challenges
and
interview
questions
for
counting
matches
efficiently.
B
using
frequency
counts
or
maps
for
digits
not
matched
in
A,
ensuring
each
digit
is
counted
only
as
many
times
as
it
appears
in
the
secret.