Find Shortest Binary String In Given Interval
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
If a binary string represents an integer in the usual way with no leading zeroes, then the shortest binary string in an interval is simply the representation of a number in that interval with the smallest bit length. That turns the problem from "searching strings" into a much simpler question about binary length boundaries.
Restating the Problem Carefully
Suppose the interval is [a, b] with 0 <= a <= b, and binary strings are written in standard form:
- '
0is written as0' - positive numbers have no leading zeroes
Then the length of the binary representation of a positive integer x is:
floor(log2(x)) + 1
That means every integer in the range:
- '
1to1has length1' - '
2to3has length2' - '
4to7has length3' - '
8to15has length4'
So the shortest binary string in [a, b] is any number from the interval that lies in the smallest bit-length bucket touched by that interval.
The Key Observation
If a and b have the same bit length, then every integer in the interval has the same binary string length. In that case, there may be many valid answers, but none is shorter than the others.
If the interval spans a power-of-two boundary, the answer comes from the smaller-length side.
For example:
- interval
[10, 15]gives length4for every value - interval
[7, 20]includes7, whose binary form111has length3, while most later values have length4or5 - interval
[0, 9]includes0, so the shortest string is simply0
This means you do not need to enumerate every binary string in the interval.
A Direct Algorithm
The simplest algorithm is:
- if
0lies in the interval, return0 - otherwise compute the bit length of
a - find the smallest number in the interval having that bit length
- return its binary representation
In standard non-negative intervals, that number is just a itself, because all integers from a upward have length at least the length of a.
So under the usual interpretation, the shortest binary string in [a, b] is the binary representation of a, unless 0 is in the interval and you want the one-character answer 0.
Python Implementation
Here is a straightforward implementation:
This works because a is already the smallest number in the interval, and the smallest number in a non-negative interval also has the smallest binary length.
If You Want Only the Length
Sometimes the real task is to find the minimum length rather than the actual string. That can be computed without building the string at all:
bit_length() is the cleanest tool for this in Python.
Why the Problem Sometimes Looks Harder Than It Is
This problem can appear more complicated because people start thinking in terms of string search, lexicographic order, or generating all binaries between two endpoints. That is usually unnecessary.
The numerical meaning of the binary string is what matters. Once you frame the problem numerically, the answer follows from the monotonic growth of bit length:
- larger non-negative integers never have shorter standard binary representations than smaller ones
That property collapses the search space immediately.
A Variation: Allowing Leading Zeroes
If leading zeroes were allowed and string length were still being minimized, the problem becomes ambiguous or trivial depending on the exact rules. For example, 00101 and 101 represent the same number but have different lengths.
That is why the standard interpretation forbids leading zeroes for positive integers. Without that assumption, “shortest binary string” is not a well-posed question.
Common Pitfalls
The biggest mistake is overcomplicating the problem by enumerating every value in the interval. For standard binary representations of non-negative integers, the answer is determined by the smallest value.
Another issue is forgetting to define whether leading zeroes are allowed. Most algorithm problems assume they are not.
People also sometimes confuse the shortest numeric representation with lexicographically smallest binary string. Those are different objectives.
Finally, if negative numbers are allowed, the problem changes completely because binary representation then depends on conventions such as sign bits or two's complement width.
Summary
- For standard non-negative binary representations, shorter strings correspond to smaller bit lengths.
- In an interval
[a, b]with no leading zeroes, the shortest binary string is the representation of the smallest value, namelya. - If
ais0, the answer is0with length1. - You do not need to enumerate the interval to solve the standard problem.
- Always clarify whether leading zeroes or negative numbers are part of the representation model.
Related reading
- Find shortest subarray containing all elements
- Find simplest regular expression matching all given strings
- find smallest area that contains all the rectangles
- Find substring in text which has the highest similarity to a given keyword
- Find the best combination from a given set of multiple sets
- Find the biggest interval that has all its members in list in On
- Find the 2nd largest element in an array with minimum number of comparisons
- Find the centroid of a polygon with weighted vertices

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.